2017-06-27 13:52:36 +00:00
|
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
2018-05-07 16:37:09 +00:00
|
|
|
|
from __future__ import print_function
|
2018-01-09 09:55:07 +00:00
|
|
|
|
import sys
|
2018-05-07 16:37:09 +00:00
|
|
|
|
try:
|
|
|
|
|
reload(sys) # Python 2
|
|
|
|
|
sys.setdefaultencoding('utf8')
|
|
|
|
|
except NameError:
|
|
|
|
|
pass # Python 3
|
2018-01-09 09:55:07 +00:00
|
|
|
|
|
2017-06-27 13:52:36 +00:00
|
|
|
|
import multiprocessing
|
|
|
|
|
from time import sleep
|
|
|
|
|
from datetime import datetime, time
|
|
|
|
|
|
|
|
|
|
from vnpy.event import EventEngine2
|
2018-02-02 01:58:31 +00:00
|
|
|
|
from vnpy.trader.vtEvent import EVENT_LOG, EVENT_ERROR
|
2017-09-01 06:33:39 +00:00
|
|
|
|
from vnpy.trader.vtEngine import MainEngine, LogEngine
|
2017-06-27 13:52:36 +00:00
|
|
|
|
from vnpy.trader.gateway import ctpGateway
|
|
|
|
|
from vnpy.trader.app import ctaStrategy
|
|
|
|
|
from vnpy.trader.app.ctaStrategy.ctaBase import EVENT_CTA_LOG
|
|
|
|
|
|
2018-02-02 01:58:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def processErrorEvent(event):
|
|
|
|
|
"""
|
|
|
|
|
处理错误事件
|
|
|
|
|
错误信息在每次登陆后,会将当日所有已产生的均推送一遍,所以不适合写入日志
|
|
|
|
|
"""
|
|
|
|
|
error = event.dict_['data']
|
2018-05-07 16:37:09 +00:00
|
|
|
|
print(u'错误代码:%s,错误信息:%s' %(error.errorID, error.errorMsg))
|
2018-02-02 01:58:31 +00:00
|
|
|
|
|
2017-06-27 13:52:36 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def runChildProcess():
|
|
|
|
|
"""子进程运行函数"""
|
2018-05-07 16:37:09 +00:00
|
|
|
|
print('-'*20)
|
2017-09-01 06:33:39 +00:00
|
|
|
|
|
|
|
|
|
# 创建日志引擎
|
|
|
|
|
le = LogEngine()
|
|
|
|
|
le.setLogLevel(le.LEVEL_INFO)
|
|
|
|
|
le.addConsoleHandler()
|
|
|
|
|
le.addFileHandler()
|
|
|
|
|
|
|
|
|
|
le.info(u'启动CTA策略运行子进程')
|
2017-06-27 13:52:36 +00:00
|
|
|
|
|
|
|
|
|
ee = EventEngine2()
|
2017-09-01 06:33:39 +00:00
|
|
|
|
le.info(u'事件引擎创建成功')
|
2017-06-27 13:52:36 +00:00
|
|
|
|
|
|
|
|
|
me = MainEngine(ee)
|
|
|
|
|
me.addGateway(ctpGateway)
|
|
|
|
|
me.addApp(ctaStrategy)
|
2017-09-01 06:33:39 +00:00
|
|
|
|
le.info(u'主引擎创建成功')
|
2017-06-27 13:52:36 +00:00
|
|
|
|
|
2017-09-01 06:33:39 +00:00
|
|
|
|
ee.register(EVENT_LOG, le.processLogEvent)
|
|
|
|
|
ee.register(EVENT_CTA_LOG, le.processLogEvent)
|
2018-02-02 01:58:31 +00:00
|
|
|
|
ee.register(EVENT_ERROR, processErrorEvent)
|
2017-09-01 06:33:39 +00:00
|
|
|
|
le.info(u'注册日志事件监听')
|
2017-06-27 13:52:36 +00:00
|
|
|
|
|
|
|
|
|
me.connect('CTP')
|
2017-09-01 06:33:39 +00:00
|
|
|
|
le.info(u'连接CTP接口')
|
2017-06-27 13:52:36 +00:00
|
|
|
|
|
2018-05-07 15:32:46 +00:00
|
|
|
|
sleep(10) # 等待CTP接口初始化
|
|
|
|
|
me.dataEngine.saveContracts() # 保存合约信息到文件
|
2017-06-28 07:36:24 +00:00
|
|
|
|
|
2017-09-03 06:51:17 +00:00
|
|
|
|
cta = me.getApp(ctaStrategy.appName)
|
2017-06-27 13:52:36 +00:00
|
|
|
|
|
|
|
|
|
cta.loadSetting()
|
2017-09-01 06:33:39 +00:00
|
|
|
|
le.info(u'CTA策略载入成功')
|
2017-06-27 13:52:36 +00:00
|
|
|
|
|
|
|
|
|
cta.initAll()
|
2017-09-01 06:33:39 +00:00
|
|
|
|
le.info(u'CTA策略初始化成功')
|
2017-06-27 13:52:36 +00:00
|
|
|
|
|
|
|
|
|
cta.startAll()
|
2017-09-01 06:33:39 +00:00
|
|
|
|
le.info(u'CTA策略启动成功')
|
2017-06-27 13:52:36 +00:00
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
sleep(1)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def runParentProcess():
|
|
|
|
|
"""父进程运行函数"""
|
2017-09-01 06:33:39 +00:00
|
|
|
|
# 创建日志引擎
|
|
|
|
|
le = LogEngine()
|
|
|
|
|
le.setLogLevel(le.LEVEL_INFO)
|
|
|
|
|
le.addConsoleHandler()
|
|
|
|
|
|
|
|
|
|
le.info(u'启动CTA策略守护父进程')
|
2017-06-27 13:52:36 +00:00
|
|
|
|
|
|
|
|
|
DAY_START = time(8, 45) # 日盘启动和停止时间
|
|
|
|
|
DAY_END = time(15, 30)
|
|
|
|
|
|
|
|
|
|
NIGHT_START = time(20, 45) # 夜盘启动和停止时间
|
|
|
|
|
NIGHT_END = time(2, 45)
|
|
|
|
|
|
|
|
|
|
p = None # 子进程句柄
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
currentTime = datetime.now().time()
|
|
|
|
|
recording = False
|
|
|
|
|
|
|
|
|
|
# 判断当前处于的时间段
|
|
|
|
|
if ((currentTime >= DAY_START and currentTime <= DAY_END) or
|
|
|
|
|
(currentTime >= NIGHT_START) or
|
|
|
|
|
(currentTime <= NIGHT_END)):
|
|
|
|
|
recording = True
|
|
|
|
|
|
|
|
|
|
# 记录时间则需要启动子进程
|
|
|
|
|
if recording and p is None:
|
2017-09-01 06:33:39 +00:00
|
|
|
|
le.info(u'启动子进程')
|
2017-06-27 13:52:36 +00:00
|
|
|
|
p = multiprocessing.Process(target=runChildProcess)
|
|
|
|
|
p.start()
|
2017-09-01 06:33:39 +00:00
|
|
|
|
le.info(u'子进程启动成功')
|
2017-06-27 13:52:36 +00:00
|
|
|
|
|
|
|
|
|
# 非记录时间则退出子进程
|
|
|
|
|
if not recording and p is not None:
|
2017-09-01 06:33:39 +00:00
|
|
|
|
le.info(u'关闭子进程')
|
2017-06-27 13:52:36 +00:00
|
|
|
|
p.terminate()
|
|
|
|
|
p.join()
|
|
|
|
|
p = None
|
2017-09-01 06:33:39 +00:00
|
|
|
|
le.info(u'子进程关闭成功')
|
2017-06-27 13:52:36 +00:00
|
|
|
|
|
|
|
|
|
sleep(5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-06-28 07:36:24 +00:00
|
|
|
|
runChildProcess()
|
|
|
|
|
|
|
|
|
|
# 尽管同样实现了无人值守,但强烈建议每天启动时人工检查,为自己的PNL负责
|
2018-05-07 16:37:09 +00:00
|
|
|
|
#runParentProcess()
|