-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmain.py
More file actions
118 lines (91 loc) · 3.42 KB
/
main.py
File metadata and controls
118 lines (91 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ATMQuant主程序入口
基于vnpy框架的AI量化交易系统
"""
import sys
from pathlib import Path
# 添加项目根目录到Python路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
def main():
"""主函数"""
print("=" * 60)
print("ATMQuant - AI量化交易系统")
print("基于vnpy 4.1框架")
print("=" * 60)
# 加载ATMQuant配置
try:
from config.settings import apply_settings
apply_settings()
except Exception as e:
print(f"⚠️ 配置加载失败: {e}")
try:
# 导入vnpy核心模块
from vnpy.event import EventEngine
from vnpy.trader.engine import MainEngine
from vnpy.trader.ui import MainWindow, create_qapp
print("✓ vnpy核心模块导入成功")
# 创建Qt应用
qapp = create_qapp()
# 创建事件引擎
event_engine = EventEngine()
print("✓ 事件引擎创建成功")
# 创建主引擎
main_engine = MainEngine(event_engine)
print("✓ 主引擎创建成功")
# 导入CTP网关
from vnpy_ctp import CtpGateway
main_engine.add_gateway(CtpGateway)
print("✓ CTP交易网关加载成功")
# 导入并添加插件应用
from vnpy_ctastrategy import CtaStrategyApp
from vnpy_datamanager import DataManagerApp
from vnpy_ctabacktester import CtaBacktesterApp
from vnpy_charttrader import ChartTraderApp
main_engine.add_app(CtaStrategyApp)
print("✓ CTA策略引擎加载成功")
#main_engine.add_app(DataManagerApp)
#print("✓ 数据管理模块加载成功")
main_engine.add_app(CtaBacktesterApp)
print("✓ 回测引擎加载成功")
main_engine.add_app(ChartTraderApp)
print("✓ 图表交易模块加载成功")
# 创建主窗口
main_window = MainWindow(main_engine, event_engine)
main_window.showMaximized()
print("✓ 图形界面启动成功")
print("\n🎉 ATMQuant启动完成!")
# 自动连接CTP网关
try:
import os
from dotenv import load_dotenv
from vnpy.trader.utility import load_json
load_dotenv()
auto_connect = os.getenv("CTP_AUTO_CONNECT", "false").lower() == "true"
if auto_connect:
# 读取connect_ctp.json配置文件
filename = "connect_ctp.json"
ctp_setting = load_json(filename)
if ctp_setting:
main_engine.connect(ctp_setting, "CTP")
print("✓ CTP网关自动连接中...")
else:
print("⚠️ 未找到connect_ctp.json配置文件,跳过自动连接")
except Exception as e:
print(f"⚠️ CTP自动连接失败: {e}")
print("\n🎉 ATMTrader启动完成!")
# 运行应用
qapp.exec()
except ImportError as e:
print(f"✗ 模块导入失败: {e}")
print("请确保已正确安装vnpy环境")
return False
except Exception as e:
print(f"✗ 程序启动失败: {e}")
return False
return True
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)