133 lines
3.8 KiB
Python
133 lines
3.8 KiB
Python
|
|
"""
|
|||
|
|
实盘交易客户端启动脚本
|
|||
|
|
|
|||
|
|
使用 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()
|