2026-01-04 22:43:13 +08:00
|
|
|
|
# coding:utf-8
|
|
|
|
|
|
"""
|
2026-01-10 04:06:35 +08:00
|
|
|
|
QMT多终端交易系统启动器
|
|
|
|
|
|
版本:V2.0 (Multi-Terminal Edition)
|
2026-01-04 22:43:13 +08:00
|
|
|
|
"""
|
|
|
|
|
|
import sys
|
|
|
|
|
|
import os
|
2026-01-10 04:06:35 +08:00
|
|
|
|
import threading
|
|
|
|
|
|
import uvicorn
|
2026-01-04 22:43:13 +08:00
|
|
|
|
|
2026-01-10 04:06:35 +08:00
|
|
|
|
# 将当前目录添加到Python路径,确保模块导入正常
|
2026-01-04 22:43:13 +08:00
|
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
if current_dir not in sys.path:
|
|
|
|
|
|
sys.path.insert(0, current_dir)
|
|
|
|
|
|
|
2026-01-10 04:06:35 +08:00
|
|
|
|
# 导入升级后的多终端管理器
|
|
|
|
|
|
from qmt_engine import MultiEngineManager
|
2026-01-04 22:43:13 +08:00
|
|
|
|
from api_server import create_api_server
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2026-01-10 04:06:35 +08:00
|
|
|
|
"""主函数 - 启动多终端QMT交易引擎管理中心和API服务器"""
|
|
|
|
|
|
# 强制设置环境变量,确保Python在Windows控制台输出不因编码崩溃
|
|
|
|
|
|
os.environ["PYTHONUTF8"] = "1"
|
|
|
|
|
|
|
|
|
|
|
|
print("==================================================")
|
|
|
|
|
|
print(" QMT Multi-Terminal System Starting... ")
|
|
|
|
|
|
print("==================================================")
|
2026-01-04 22:43:13 +08:00
|
|
|
|
|
2026-01-10 04:06:35 +08:00
|
|
|
|
# 1. 获取多终端管理器单例
|
|
|
|
|
|
manager = MultiEngineManager()
|
2026-01-04 22:43:13 +08:00
|
|
|
|
|
|
|
|
|
|
try:
|
2026-01-10 04:06:35 +08:00
|
|
|
|
# 2. 初始化引擎(加载配置、连接Redis、初始化各终端执行单元)
|
|
|
|
|
|
manager.initialize('config.json')
|
|
|
|
|
|
print("Done: Multi-Manager initialized successfully.")
|
2026-01-04 22:43:13 +08:00
|
|
|
|
except Exception as e:
|
2026-01-10 04:06:35 +08:00
|
|
|
|
print(f"Error: System initialization failed: {repr(e)}")
|
|
|
|
|
|
import traceback
|
|
|
|
|
|
traceback.print_exc()
|
2026-01-04 22:43:13 +08:00
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
2026-01-10 04:06:35 +08:00
|
|
|
|
# 3. 启动全局监控与交易路由主循环线程
|
|
|
|
|
|
# 该线程负责:终端健康检查、断线重连、消息路由、收盘结算
|
|
|
|
|
|
trading_thread = threading.Thread(target=manager.run_trading_loop, name="MainTradeLoop", daemon=True)
|
2026-01-04 22:43:13 +08:00
|
|
|
|
trading_thread.start()
|
2026-01-10 04:06:35 +08:00
|
|
|
|
print("Done: Global trading loop thread started.")
|
2026-01-04 22:43:13 +08:00
|
|
|
|
|
2026-01-10 04:06:35 +08:00
|
|
|
|
# 4. 创建适配多终端的API服务器
|
|
|
|
|
|
app = create_api_server(manager)
|
|
|
|
|
|
print("Done: API server created with multi-terminal support.")
|
2026-01-04 22:43:13 +08:00
|
|
|
|
|
2026-01-10 04:06:35 +08:00
|
|
|
|
# 5. 启动Web服务
|
|
|
|
|
|
print(">>> Web Dashboard: http://localhost:8001")
|
2026-01-04 22:43:13 +08:00
|
|
|
|
try:
|
2026-01-10 04:06:35 +08:00
|
|
|
|
# 建议关闭 access_log 以减少控制台刷屏
|
2026-01-04 22:43:13 +08:00
|
|
|
|
uvicorn.run(
|
|
|
|
|
|
app,
|
|
|
|
|
|
host="0.0.0.0",
|
|
|
|
|
|
port=8001,
|
|
|
|
|
|
log_level="warning",
|
|
|
|
|
|
access_log=False
|
|
|
|
|
|
)
|
|
|
|
|
|
except KeyboardInterrupt:
|
2026-01-10 04:06:35 +08:00
|
|
|
|
print("\n>>> Shutdown signal received. Closing terminals...")
|
|
|
|
|
|
manager.stop()
|
|
|
|
|
|
print(">>> System safely closed.")
|
2026-01-27 00:52:35 +08:00
|
|
|
|
_write_exit_code(0)
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
def _write_exit_code(code):
|
|
|
|
|
|
"""将退出码写入临时文件,供 start.bat 读取"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
exit_code_file = os.path.join(os.environ.get('TEMP', ''), 'exit_code.txt')
|
|
|
|
|
|
with open(exit_code_file, 'w') as f:
|
|
|
|
|
|
f.write(str(code))
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
pass
|
2026-01-04 22:43:13 +08:00
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2026-01-10 04:06:35 +08:00
|
|
|
|
# 最佳实践:使用 python -u run.py 运行以获得实时日志输出
|
|
|
|
|
|
main()
|