Files
stock/stock-html/run.md
T
freedakgmail 9c7d7abdd4 Initial commit
2026-07-17 18:49:35 +08:00

301 lines
11 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 股票投资分析系统 - 运行指南
## 快速启动
### 本地开发
```bash
cd /Users/freedak/Documents/go-new/stock/stock-html
# 激活虚拟环境
source venv/bin/activate
# 启动服务
python app.py
```
访问地址:http://localhost:3333
### 服务器部署
#### 主服务器(原有)
- **服务器地址**43.135.128.39(腾讯云)
- **登录用户**ubuntu(需sudo
- **部署路径**/opt/stock-app
- **外部访问**http://43.135.128.39:3333
#### 新服务器(stock.allbyai.cn
- **服务器地址**152.136.182.184(腾讯云)
- **登录用户**ubuntu(需sudo
- **部署路径**/opt/stock-app
- **域名访问**http://stock.allbyai.cn
- **IP直连**http://152.136.182.184:3333
#### 同步代码到服务器
**注意**:本地修改代码后请执行下方同步并重启,使服务器生效。
```bash
# 同步全部代码(推荐)
cd /Users/freedak/Documents/go-new/stock/stock-html
rsync -avz ./ ubuntu@43.135.128.39:/opt/stock-app/ \
--exclude='.git' \
--exclude='venv' \
--exclude='__pycache__' \
--exclude='stock_data_cache' \
--exclude='*.pyc' \
--exclude='.DS_Store' \
--exclude='stock_names.json' \
--exclude='alerts_cache.json' \
--exclude='trades.json' \
--exclude='watchlist.json' \
--exclude='*.log' \
--exclude='.playwright-mcp' \
--exclude='/app.js' \
--exclude='/index.html' \
--exclude='/main.css' \
--exclude='/css' \
--exclude='/js' \
--exclude='/.windsurfrules'
# 仅同步前端
rsync -avz templates/ ubuntu@43.135.128.39:/opt/stock-app/templates/
rsync -avz static/ ubuntu@43.135.128.39:/opt/stock-app/static/
# 仅同步后端
rsync -avz app.py routes/ services/ ubuntu@43.135.128.39:/opt/stock-app/
# 重启服务
ssh ubuntu@43.135.128.39 "systemctl restart stock-app"
# ========== 新服务器 stock.allbyai.cn ==========
# 同步全部代码到新服务器
cd /Users/freedak/Documents/go-new/stock/stock-html
rsync -avz ./ ubuntu@152.136.182.184:/opt/stock-app/ \
--exclude='.git' \
--exclude='venv' \
--exclude='__pycache__' \
--exclude='stock_data_cache' \
--exclude='*.pyc' \
--exclude='.DS_Store' \
--exclude='stock_names.json' \
--exclude='alerts_cache.json' \
--exclude='trades.json' \
--exclude='watchlist.json' \
--exclude='*.log' \
--exclude='.playwright-mcp' \
--exclude='/app.js' \
--exclude='/index.html' \
--exclude='/main.css' \
--exclude='/css' \
--exclude='/js' \
--exclude='/.windsurfrules'
# 重启新服务器服务
ssh ubuntu@152.136.182.184 "systemctl restart stock-app"
# 或使用一键脚本
./deploy/sync-to-new-server.sh
```
#### 服务管理
```bash
# Web应用服务
ssh ubuntu@43.135.128.39 "systemctl start stock-app"
ssh ubuntu@43.135.128.39 "systemctl stop stock-app"
ssh ubuntu@43.135.128.39 "systemctl restart stock-app"
ssh ubuntu@43.135.128.39 "systemctl status stock-app"
ssh ubuntu@43.135.128.39 "journalctl -u stock-app -f"
# 数据采集服务
ssh ubuntu@43.135.128.39 "systemctl start stock-data-service"
ssh ubuntu@43.135.128.39 "systemctl stop stock-data-service"
ssh ubuntu@43.135.128.39 "systemctl status stock-data-service"
ssh ubuntu@43.135.128.39 "journalctl -u stock-data-service -f"
# ========== 新服务器 stock.allbyai.cn ==========
# Web应用服务
ssh ubuntu@152.136.182.184 "systemctl start stock-app"
ssh ubuntu@152.136.182.184 "systemctl stop stock-app"
ssh ubuntu@152.136.182.184 "systemctl restart stock-app"
ssh ubuntu@152.136.182.184 "systemctl status stock-app"
ssh ubuntu@152.136.182.184 "journalctl -u stock-app -f"
# 数据采集服务
ssh ubuntu@152.136.182.184 "systemctl start stock-data-service"
ssh ubuntu@152.136.182.184 "systemctl stop stock-data-service"
ssh ubuntu@152.136.182.184 "systemctl status stock-data-service"
ssh ubuntu@152.136.182.184 "journalctl -u stock-data-service -f"
```
## 项目结构
```
stock-html/
├── app.py # Flask后端主程序
├── config.py # 配置文件(支持环境变量)
├── db.py # 数据库操作层
├── requirements.txt # Python依赖
├── start.sh # 启动脚本
├── stock_data_service.py # 数据采集服务(定时任务)
├── full_signal_scan.py # 全市场信号扫描脚本
├── sync_fund_flow.py # 资金流向全市场采集脚本
├── auto_sync_fund_flow.sh # 资金流向采集定时任务
├── routes/ # API路由
│ ├── analysis.py # 分析/扫描/信号相关API
│ ├── auth.py # 认证API
│ ├── market.py # 市场数据API
│ ├── sim_trade.py # 模拟交易API
│ ├── trades.py # 交易记录API
│ └── watchlist.py # 关注列表API
├── services/ # 业务服务
│ ├── signal_detector.py # 7种技术信号检测器
│ ├── technical_indicators.py # 技术指标计算
│ ├── scheduler.py # 自动交易调度器
│ ├── stock_service.py # 股票数据服务
│ ├── mairui_api.py # 迈瑞API
│ └── doubao_api.py # 豆包AI分析
├── templates/
│ └── index.html # 前端页面(Vue.js
├── static/
│ ├── css/main.css # 样式
│ └── js/app.js # Vue.js应用
└── stock-data-service.service # systemd服务配置
```
## systemd 服务
### stock-app.service (Web应用)
路径:`/etc/systemd/system/stock-app.service`
端口:3333
### stock-data-service.service (数据采集)
路径:`/etc/systemd/system/stock-data-service.service`
功能:定时采集实时价格数据
### 全景扫描定时任务 (crontab)
应用内**没有**自动配置全景扫描的定时任务,需在服务器上添加 crontab。当前配置**每天两次扫描**:
| 时间 | 说明 |
|------|------|
| **11:50** | 午休扫描 — 上午收盘20分钟后,确保数据API更新完毕,供下午交易参考 |
| **16:30** | 收盘扫描 — 收盘1.5小时后,确保全天K线数据完整更新 |
> **为什么留缓冲时间?** A股上午 11:30 收盘、下午 15:00 收盘。数据API更新需要一定时间,11:50 和 16:30 给予充分缓冲确保数据完整性。
**方式一:一键配置(推荐)**
在本地 `stock-html` 目录下执行(会同步 `auto_scan.sh`、在服务器上添加 crontab):
```bash
cd /path/to/stock-html
./setup_cron_scan.sh
```
如需指定服务器或目录:`STOCK_SERVER=root@你的IP STOCK_APP_DIR=/opt/stock-app ./setup_cron_scan.sh`
**方式二:在服务器上手动配置**
```bash
ssh ubuntu@43.135.128.39
chmod +x /opt/stock-app/auto_scan.sh
crontab -e
# 添加两行:
50 11 * * 1-5 /opt/stock-app/auto_scan.sh >> /opt/stock-app/auto_scan.log 2>&1
30 16 * * 1-5 /opt/stock-app/auto_scan.sh >> /opt/stock-app/auto_scan.log 2>&1
# 5分钟K线采集(17:30,收盘后数据完整)
30 17 * * 1-5 /opt/stock-app/auto_sync_kline_5min.sh >> /opt/stock-app/sync_kline_5min.log 2>&1
# 资金流向采集(18:00,在K线采集之后)
0 18 * * 1-5 /opt/stock-app/auto_sync_fund_flow.sh >> /opt/stock-app/sync_fund_flow.log 2>&1
```
**验证与排查**
```bash
# 查看当前 crontab
ssh ubuntu@43.135.128.39 "crontab -l"
# 手动执行一次测试
ssh ubuntu@43.135.128.39 "/opt/stock-app/auto_scan.sh"
# 查看扫描日志
ssh ubuntu@43.135.128.39 "tail -50 /opt/stock-app/auto_scan.log"
```
- 未配置 crontab 时:前端「全景扫描」按钮仅查看已有缓存结果,不会触发新扫描。需手动在服务器执行 `FORCE_RESCAN=1 python full_signal_scan.py` 或配置 crontab。
## API接口
| 接口 | 方法 | 说明 |
|------|------|------|
| `/api/health` | GET | 健康检查 |
| `/api/signal_alerts` | POST | 信号分析提醒(新模型) |
| `/api/tech_signals/<code>` | GET | 单只股票7种信号检测 |
| `/api/batch_tech_signals` | POST | 批量信号检测 |
| `/api/start_full_scan` | POST | 启动全市场扫描 |
| `/api/scan_status` | GET | 扫描进度查询 |
| `/api/scan_results` | GET | 扫描结果查询 |
| `/api/scan_strategy` | GET | 策略建议 |
| `/api/realtime_price/<code>` | GET | 获取实时价格 |
| `/api/trades` | GET/POST | 交易记录 |
| `/api/watchlist` | GET/POST/DELETE | 关注列表 |
| `/api/alerts_cache` | GET/POST | 分析缓存 |
| `/api/fundamental/<code>` | GET | 基本面数据 |
| `/api/kline/<code>` | GET | K线数据 |
## 核心算法(交易信号实战体系)
### 7种技术信号
1. ★主升浪 - 最稳最猛(利润核心阶段)
2. 日线底背离 - 最安全抄底(反转前置信号)
3. 龙抬头 - 最佳入场点(实操核心买点)
4. 真龙 - 趋势确认(中期行情定局信号)
5. 短底背离 - 辅助参考
6. 老鼠仓 - 辅助参考
7. 反弹 - 辅助参考
### 最强战法
1. 日线底背离出现 → 纳入关注
2. 龙抬头出现 → 执行买入
3. 真龙/主升浪出现 → 持有加仓
4. MACD死叉+主升浪消失 → 卖出
## 常用命令
```bash
# 一键同步全部代码并重启(已排除根目录重复项,仅同步 templates/ static/ 等正确路径)
cd /Users/freedak/Documents/go-new/stock/stock-html && \
rsync -avz ./ ubuntu@43.135.128.39:/opt/stock-app/ \
--exclude='.git' --exclude='venv' --exclude='__pycache__' --exclude='stock_data_cache' \
--exclude='*.pyc' --exclude='.DS_Store' --exclude='*.log' \
--exclude='stock_names.json' --exclude='alerts_cache.json' \
--exclude='.playwright-mcp' \
--exclude='/app.js' --exclude='/index.html' --exclude='/main.css' \
--exclude='/css' --exclude='/js' --exclude='/.windsurfrules' && \
ssh ubuntu@43.135.128.39 "systemctl restart stock-app && echo '服务已重启'"
# 仅同步前端并重启
rsync -avz templates/ static/ ubuntu@43.135.128.39:/opt/stock-app/ && \
ssh ubuntu@43.135.128.39 "systemctl restart stock-app"
# 查看两个服务状态
ssh ubuntu@43.135.128.39 "systemctl status stock-app stock-data-service"
# ========== 新服务器 stock.allbyai.cn 快捷命令 ==========
# 一键同步并重启(推荐)
cd /Users/freedak/Documents/go-new/stock/stock-html && \
rsync -avz ./ ubuntu@152.136.182.184:/opt/stock-app/ \
--exclude='.git' --exclude='venv' --exclude='__pycache__' --exclude='stock_data_cache' \
--exclude='*.pyc' --exclude='.DS_Store' --exclude='*.log' \
--exclude='stock_names.json' --exclude='alerts_cache.json' \
--exclude='.playwright-mcp' \
--exclude='/app.js' --exclude='/index.html' --exclude='/main.css' \
--exclude='/css' --exclude='/js' --exclude='/.windsurfrules' && \
ssh ubuntu@152.136.182.184 "systemctl restart stock-app && echo '服务已重启'"
# 或使用脚本
./deploy/sync-to-new-server.sh
# 查看服务状态
ssh ubuntu@152.136.182.184 "systemctl status stock-app stock-data-service"
```
---
最后更新:2026-03-02