# 部署指南 ## 项目结构 ``` auto_trade_sys/ ├── backend/ # 后端服务(FastAPI) ├── frontend/ # 前端应用(React) └── [交易系统文件] # 原有交易系统 ``` ## 1. 数据库初始化 ```bash # 创建数据库 mysql -u root -p < backend/database/init.sql # 或手动执行 mysql -u root -p CREATE DATABASE auto_trade_sys; USE auto_trade_sys; source backend/database/init.sql; ``` ## 2. 后端部署 ```bash cd backend # 安装依赖 pip install -r requirements.txt # 设置环境变量 export DB_HOST=localhost export DB_PORT=3306 export DB_USER=root export DB_PASSWORD=your_password export DB_NAME=auto_trade_sys export CORS_ORIGINS=http://localhost:3000,http://your-domain.com # 初始化配置(从config.py迁移到数据库) python init_config.py # 启动服务 uvicorn api.main:app --host 0.0.0.0 --port 8000 # 或使用supervisor(生产环境) # 创建 /etc/supervisor/conf.d/auto_trade_api.conf ``` ### Supervisor配置(后端) ```ini [program:auto_trade_api] command=/www/wwwroot/auto_trade_sys/backend/.venv/bin/uvicorn api.main:app --host 0.0.0.0 --port 8000 directory=/www/wwwroot/auto_trade_sys/backend user=www autostart=true autorestart=true startretries=3 startsecs=10 redirect_stderr=true stdout_logfile=/www/wwwroot/auto_trade_sys/backend/logs/api.log environment= DB_HOST="localhost", DB_PORT="3306", DB_USER="root", DB_PASSWORD="your_password", DB_NAME="auto_trade_sys" ``` ## 3. 前端部署 ### 开发环境 ```bash cd frontend npm install npm run dev ``` ### 生产环境 ```bash cd frontend npm install npm run build # 使用nginx部署 # 将dist目录内容复制到nginx目录 cp -r dist/* /usr/share/nginx/html/ ``` ### Nginx配置 ```nginx server { listen 80; server_name your-domain.com; root /usr/share/nginx/html; index index.html; # 前端路由 location / { try_files $uri $uri/ /index.html; } # API代理 location /api { proxy_pass http://localhost:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } ``` ## 4. 交易系统部署 交易系统需要从数据库读取配置,修改 `config.py`: ```python # 在config.py开头添加 try: import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent / 'backend')) from config_manager import get_trading_config TRADING_CONFIG = get_trading_config() except: # 回退到原有配置 TRADING_CONFIG = { # ... 原有配置 ... } ``` ## 5. 环境变量配置 ### 后端环境变量 ```bash # .env 文件(backend/.env) DB_HOST=localhost DB_PORT=3306 DB_USER=root DB_PASSWORD=your_password DB_NAME=auto_trade_sys CORS_ORIGINS=http://localhost:3000,http://your-domain.com ``` ### 前端环境变量 ```bash # .env 文件(frontend/.env) VITE_API_URL=http://localhost:8000 ``` ## 6. 启动顺序 1. 启动MySQL数据库 2. 启动后端API服务 3. 启动前端应用(开发)或部署前端(生产) 4. 启动交易系统 ## 7. 验证 - 后端API: http://localhost:8000/docs - 前端应用: http://localhost:3000 - 健康检查: http://localhost:8000/api/health ## 8. 数据记录 交易系统需要集成数据记录功能,在以下位置添加: - `position_manager.py`: 开仓/平仓时记录到数据库 - `strategy.py`: 扫描和信号记录 - `main.py`: 账户快照记录