vnpy/examples/VnTrader/run.py

76 lines
2.1 KiB
Python
Raw Normal View History

2017-06-15 10:36:39 +00:00
# encoding: UTF-8
2018-01-30 15:15:32 +00:00
2017-06-15 10:36:39 +00:00
# 重载sys模块设置默认字符串编码方式为utf8
import sys
2018-01-30 15:15:32 +00:00
#reload(sys)
#sys.setdefaultencoding('utf8')
2017-06-15 10:36:39 +00:00
2018-01-30 15:15:32 +00:00
import sys
2017-06-16 08:49:34 +00:00
import os
2018-01-30 15:15:32 +00:00
import ctypes
import platform
system = platform.system()
2017-06-16 08:49:34 +00:00
2018-01-30 15:15:32 +00:00
# 将repostory的目录作为根目录添加到系统环境中。
ROOT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..' , '..'))
sys.path.append(ROOT_PATH)
2017-06-16 08:49:34 +00:00
2017-06-15 10:36:39 +00:00
from vnpy.trader.vtEngine import MainEngine
2018-01-30 15:15:32 +00:00
from vnpy.trader.uiQt import createQApp
from vnpy.trader.uiMainWindow import *
2017-06-15 10:36:39 +00:00
# 加载底层接口
2018-01-30 15:15:32 +00:00
from vnpy.trader.gateway import ctpGateway
# 初始化的接口模块,以及其指定的名称,CTP是模块value是该模块下的多个连接配置文件,如 CTP_JR_connect.json 'CTP_Prod', 'CTP_JR', , 'CTP_JK', 'CTP_02'
2019-01-20 15:31:22 +00:00
init_gateway_names = {'CTP': ['CTP','CTP01','CTP02']}
2017-06-15 10:36:39 +00:00
2019-01-20 15:31:22 +00:00
from vnpy.trader.app import (ctaStrategy, riskManager, spreadTrading,dataRecorder) #,
2017-06-15 10:36:39 +00:00
2018-01-30 15:15:32 +00:00
# 文件路径名
path = os.path.abspath(os.path.dirname(__file__))
ICON_FILENAME = 'vnpy.ico'
ICON_FILENAME = os.path.join(path, ICON_FILENAME)
2017-06-15 10:36:39 +00:00
2018-01-30 15:15:32 +00:00
from vnpy.trader.setup_logger import setup_logger
# ----------------------------------------------------------------------
2017-06-15 10:36:39 +00:00
def main():
"""主程序入口"""
2018-01-30 15:15:32 +00:00
logger = setup_logger(filename='logs/vnpy.log', debug=False)
# 创建Qt应用对象
qApp = createQApp()
2017-06-15 10:36:39 +00:00
# 创建事件引擎
2018-01-30 15:15:32 +00:00
ee = EventEngine2()
# 初始化主引擎和主窗口对象
mainEngine = MainEngine(ee)
mainEngine.logger = logger
# 添加Gatway
for gw_name in init_gateway_names['CTP']:
print('add {0}'.format(gw_name))
mainEngine.addGateway(ctpGateway, gw_name)
# 添加应用
mainEngine.addApp(ctaStrategy)
mainEngine.addApp(riskManager)
mainEngine.addApp(spreadTrading)
2019-01-20 15:31:22 +00:00
mainEngine.addApp(dataRecorder)
2018-01-30 15:15:32 +00:00
mainWindow = MainWindow(mainEngine, ee)
mainWindow.showMaximized()
2017-06-15 10:36:39 +00:00
# 在主线程中启动Qt事件循环
sys.exit(qApp.exec_())
if __name__ == '__main__':
2018-01-30 15:15:32 +00:00
try:
main()
except Exception as ex:
print(str(ex))
traceback.print_exc()