QTrade/realtrade_runner.py
cuizhan 6284f98eda 重构策略状态管理:统一由StrategyManager管理状态持久化
主要变更:
1. BaseStrategy新增get_status()和restore_state()方法
2. StrategyManager统一管理状态保存和恢复
   - close()方法调用get_status()保存状态
   - initialize()方法调用restore_state()恢复状态
3. GridStrategy实现状态管理接口
4. 网格策略支持断点续传
5. 新增消息类型常量定义
6. 重构MiniQMT客户端适配器的K线数据处理
7. 统一使用Logger替换LazyLogger
8. 配置文件支持多数据库类型
2026-01-17 14:37:57 +08:00

133 lines
3.8 KiB
Python
Raw Permalink 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.

"""
实盘交易客户端启动脚本
使用 MiniQMT 进行实盘交易,先连接策略服务器获取信号,然后执行实盘交易
"""
import time
from datetime import datetime
import pandas as pd
import subprocess
import sys
import time
from pathlib import Path
from src.core.config import Config
from src.core.logger import Logger
from src.realtrade.miniqmt_client_adapter import MiniQMTClientAdapter
workspace = Path(__file__).parent
"""
股票池配置
定义实盘交易的股票池,包含股票代码、名称和K线周期
"""
STOCK_POOL = [
# {"symbol": "601288.SH", "symbol_name": "农业银行", "period": "1m"},
{"symbol": "600593.SH", "symbol_name": "大连圣亚", "period": "1m"},
# 添加更多股票...
]
def start_server_newwindow():
"""启动服务器进程(在新的控制台窗口)"""
print("=" * 60)
print("正在启动 QTrade Server(新窗口)...")
print("=" * 60)
subprocess.Popen(
[sys.executable, "-m", "src.qtrade_server"],
cwd=workspace,
creationflags=subprocess.CREATE_NEW_CONSOLE, # Windows下在新控制台窗口启动
)
print("✓ 服务器已在新窗口启动,等待5秒...")
def start_server():
"""启动服务器进程(直接显示输出)"""
print("=" * 60)
print("正在启动 QTrade Server...")
print("=" * 60)
try:
subprocess.run([sys.executable, "-m", "src.qtrade_server"], cwd=workspace)
except KeyboardInterrupt:
print("\n✓ 服务器已正常关闭")
def main():
config = Config.initialize("production")
Logger.initialize("realtrade")
Logger.info("=" * 60)
Logger.info("启动 QTrade 实盘交易客户端")
Logger.info("=" * 60)
if not config:
Logger.error("✗ 配置加载失败,退出程序")
return
Logger.info("配置加载完成")
"""实盘交易主函数"""
# 启动服务端
Logger.info("\n启动策略服务器...")
start_server_newwindow()
Logger.info("✓ 策略服务器启动命令已发送,等待5秒...")
time.sleep(5) # 等待服务器启动
# 启动miniQMT客户端适配器
Logger.info("\n初始化 MiniQMT 客户端适配器...")
# 从配置获取参数
server_url = config.get("client_adapters.production_adapter.server_url")
timeout = config.get("client_adapters.production_adapter.timeout", 30)
miniqmt_path = config.get("miniqmt.userdata_path")
session_id = config.get("miniqmt.session_id", 123456)
account_id = config.get("miniqmt.account.account_id")
account_type = config.get("miniqmt.account.account_type", "STOCK")
Logger.info(f"服务器地址: {server_url}")
Logger.info(f"MiniQMT 路径: {miniqmt_path}")
Logger.info(f"交易账号: {account_id}")
Logger.info(f"账户类型: {account_type}")
# 创建实盘客户端适配器
adapter = MiniQMTClientAdapter(
server_url=server_url,
timeout=timeout,
miniqmt_path=miniqmt_path,
session_id=session_id,
account_id=account_id,
account_type=account_type,
stockpool=STOCK_POOL,
)
# 连接 MiniQMT
Logger.info("\n连接 MiniQMT 交易接口...")
if not adapter.run():
Logger.error("MiniQMT 连接失败,退出程序")
return
Logger.info("✓ MiniQMT 连接成功")
try:
# 保持连接,接收交易推送
Logger.info("\n" + "=" * 60)
Logger.info("实盘交易客户端运行中...")
Logger.info("按 Ctrl+C 退出")
Logger.info("=" * 60)
except KeyboardInterrupt:
Logger.info("\n\n收到退出信号,正在关闭...")
except Exception as e:
Logger.error(f"运行异常: {e}", exc_info=True)
finally:
# 关闭连接
adapter.close()
Logger.info("✓ 实盘交易客户端已关闭")
if __name__ == "__main__":
main()