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