QTrade/scripts/start_services.py
cuizhan eb2179809f 重构:统一策略框架和服务命名,修复所有Pylance错误
主要改动:
1. 策略框架重构
   - 统一实盘和回测策略接口
   - 新增 src/core/strategy/ 核心策略模块
   - BaseStrategy、Signal、StrategyContext 统一定义
   - 内置指标库(TA-Lib封装)和策略注册中心
   - 删除旧策略代码

2. 服务重命名
   - strategy_engine -> strategy_service (统一 _service 后缀)
   - 更新所有配置、导入路径、测试文件
   - 23个文件同步更新

3. 回测服务增强
   - 支持多策略-多股票组合回测
   - 为每个策略-股票组合创建独立管理器
   - BacktestConfig schema 添加 strategy_id 字段
   - 数据回放器支持独立频道

4. 代码质量提升
   - 修复所有 src/ 目录下的 Pylance 错误
   - 添加 get_kline_model 函数
   - 修复 K线字段名(trade_date -> time)
   - 修复类型转换(Decimal -> float)
   - 修复 MA_Type 导入路径
   - 统一代码风格
2025-12-11 23:25:16 +08:00

166 lines
4.5 KiB
Python

"""
QTrade 多环境服务启动脚本
支持启动三套独立环境:
- production: 实盘环境 (端口9000+, DB: QTrade_RealTrade, Redis: real:)
- backtest: 回测环境 (端口10000+, DB: QTrade_BackTestTrade, Redis: bt:)
- simulation: 模拟环境 (端口11000+, DB: QTrade_SimuTrade, Redis: simu:)
用法:
# 启动实盘环境的数据服务和策略引擎
python scripts/start_services.py production data_service strategy_service
# 启动回测环境的所有服务
python scripts/start_services.py backtest data_service strategy_service backtest_service
# 启动模拟环境
python scripts/start_services.py simulation data_service strategy_service trade_service
"""
import os
import subprocess
import sys
import time
from pathlib import Path
# 项目根目录
PROJECT_ROOT = Path(__file__).parent.parent
# 环境配置
ENV_CONFIGS = {
"production": {
"env_file": ".env.production",
"config_file": "configs/production.yaml",
"description": "实盘环境",
},
"backtest": {
"env_file": ".env.backtest",
"config_file": "configs/backtest.yaml",
"description": "回测环境",
},
"simulation": {
"env_file": ".env.simulation",
"config_file": "configs/simulation.yaml",
"description": "模拟环境",
},
}
# 服务配置
SERVICE_APPS = {
"data_service": "src.data_service.app:app",
"strategy_service": "src.strategy_service.app:app",
"trade_service": "src.trade_service.app:app",
"backtest_service": "src.backtest_service.app:app",
}
def start_service(service_name: str, env: str, detach: bool = True):
"""
启动指定环境的服务
Args:
service_name: 服务名称
env: 环境名称 (production/backtest/simulation)
detach: 是否后台运行
"""
if env not in ENV_CONFIGS:
print(f"❌ 未知环境: {env}")
print(f"支持的环境: {', '.join(ENV_CONFIGS.keys())}")
sys.exit(1)
if service_name not in SERVICE_APPS:
print(f"❌ 未知服务: {service_name}")
print(f"支持的服务: {', '.join(SERVICE_APPS.keys())}")
sys.exit(1)
env_config = ENV_CONFIGS[env]
app_path = SERVICE_APPS[service_name]
env_file = PROJECT_ROOT / env_config["env_file"]
if not env_file.exists():
print(f"❌ 环境配置文件不存在: {env_file}")
sys.exit(1)
print(f"🚀 启动 {env_config['description']} - {service_name}")
# 构建命令
cmd = [
"uvicorn",
app_path,
"--env-file",
str(env_file),
"--reload",
]
# 设置环境变量
env_vars = os.environ.copy()
env_vars["ENV"] = env
env_vars["CONFIG_FILE"] = env_config["config_file"]
if detach:
# Windows 后台运行
if sys.platform == "win32":
# 创建新的控制台窗口
subprocess.Popen(
cmd,
env=env_vars,
creationflags=subprocess.CREATE_NEW_CONSOLE,
cwd=PROJECT_ROOT,
)
else:
# Linux/Mac 后台运行
subprocess.Popen(
cmd,
env=env_vars,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
cwd=PROJECT_ROOT,
)
print(f"✓ {service_name} 已在后台启动")
else:
# 前台运行
subprocess.run(cmd, env=env_vars, cwd=PROJECT_ROOT)
def main():
if len(sys.argv) < 3:
print("用法: python scripts/start_services.py <env> <service1> [service2] ...")
print()
print("环境:")
for env, config in ENV_CONFIGS.items():
print(f" {env:12} - {config['description']}")
print()
print("服务:")
for service in SERVICE_APPS.keys():
print(f" {service}")
print()
print("示例:")
print(
" python scripts/start_services.py production data_service strategy_service"
)
print(
" python scripts/start_services.py backtest data_service strategy_service backtest_service"
)
sys.exit(1)
env = sys.argv[1]
services = sys.argv[2:]
print("=" * 60)
print(f"环境: {ENV_CONFIGS[env]['description']}")
print(f"服务: {', '.join(services)}")
print("=" * 60)
print()
for service in services:
start_service(service, env)
time.sleep(1) # 间隔启动
print()
print("✅ 所有服务已启动")
if __name__ == "__main__":
main()