QTrade/realtrade_runner.py
cuizhan 593565bb36 feat: 完善交易系统和实盘模块
核心改进:
- Account模型重命名为Asset,增加完整资产管理字段
- Logger增加prompt_level列表过滤,支持灵活日志级别控制
- 新增TradingDataStorage和TradingCache,完善交易对象存储
- 实现MiniQMTClientAdapter实盘交易适配器
- 新增realtrade_runner.py实盘启动脚本

数据库表更新:
- 重构Order/Trade/Position/Asset表结构
- 所有交易表增加instance_id字段实现多策略隔离
- Signal表增加instance_id字段

文档完善:
- 新增logger_prompt_level_guide.md日志配置说明
- 新增realtrade_guide.md实盘交易使用指南
- 新增table_cleanup_summary.md表结构清理总结

代码优化:
- 全局WSClient使用get_wsclient()函数封装访问
- 移除Symbol模型,信息移至配置文件
- 清理废弃表定义约320行代码
2026-01-15 22:11:28 +08:00

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

"""
实盘交易客户端启动脚本
使用 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": "5m"},
# 添加更多股票...
]
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()