From c36a58d5e35a5f3cc1b69234cbb0715ddb96cbf2 Mon Sep 17 00:00:00 2001 From: zhu4ling3 Date: Thu, 12 Apr 2018 23:57:02 -0400 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90dataRecord?= =?UTF-8?q?=E7=9A=84=E5=85=A8=E8=BF=87=E7=A8=8B=E6=97=B6=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/HistDataCollector/HD_setting.json | 15 +++ .../HistDataCollector/runHistDataCollector.py | 105 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 examples/HistDataCollector/HD_setting.json create mode 100644 examples/HistDataCollector/runHistDataCollector.py diff --git a/examples/HistDataCollector/HD_setting.json b/examples/HistDataCollector/HD_setting.json new file mode 100644 index 00000000..3294e417 --- /dev/null +++ b/examples/HistDataCollector/HD_setting.json @@ -0,0 +1,15 @@ +{ + "working": true, + + "tick": + [ + ["700", "IB", "SEHK", "HKD", "股票"], + ["USD.JPY", "IB", "IDEALPRO", "JPY", "外汇"] + ], + + "bar": + [ + ["700", "IB", "SEHK", "HKD", "股票"], + ["USD.JPY", "IB", "IDEALPRO", "JPY", "外汇"] + ] +} \ No newline at end of file diff --git a/examples/HistDataCollector/runHistDataCollector.py b/examples/HistDataCollector/runHistDataCollector.py new file mode 100644 index 00000000..604e20a9 --- /dev/null +++ b/examples/HistDataCollector/runHistDataCollector.py @@ -0,0 +1,105 @@ +# encoding: UTF-8 + +import multiprocessing +from time import sleep +from datetime import datetime, time + +from vnpy.event import EventEngine2 +from vnpy.trader.vtEvent import EVENT_LOG, EVENT_ERROR +from vnpy.trader.vtEngine import MainEngine, LogEngine +from vnpy.trader.gateway import ibGateway +from vnpy.trader.app import dataRecorder + + +#---------------------------------------------------------------------- +def processErrorEvent(event): + """ + 处理错误事件 + 错误信息在每次登陆后,会将当日所有已产生的均推送一遍,所以不适合写入日志 + """ + error = event.dict_['data'] + print u'错误代码:%s,错误信息:%s' %(error.errorID, error.errorMsg) + +#---------------------------------------------------------------------- +def runChildProcess(): + """子进程运行函数""" + print '-'*20 + + # 创建日志引擎 + le = LogEngine() + le.setLogLevel(le.LEVEL_DEBUG) + le.addConsoleHandler() + le.info(u'启动行情记录运行子进程') + + ee = EventEngine2() + le.info(u'事件引擎创建成功') + + me = MainEngine(ee) + me.addGateway(ibGateway) + me.addApp(histDataCollector) + le.info(u'主引擎创建成功') + + ee.register(EVENT_LOG, le.processLogEvent) + ee.register(EVENT_ERROR, processErrorEvent) + le.info(u'注册日志事件监听') + + me.connect('IB') + le.info(u'连接IB接口') + + while True: + sleep(1) + +#---------------------------------------------------------------------- +def runParentProcess(): + """父进程运行函数""" + # 创建日志引擎 + le = LogEngine() + le.setLogLevel(le.LEVEL_INFO) + le.addConsoleHandler() + le.info(u'启动HistData守护父进程') + + DAY_START = time(8, 57) # 日盘启动和停止时间 + DAY_END = time(15, 18) + NIGHT_START = time(20, 57) # 夜盘启动和停止时间 + NIGHT_END = time(2, 33) + + 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 ((datetime.today().weekday() == 6) or + (datetime.today().weekday() == 5 and currentTime > NIGHT_END) or + (datetime.today().weekday() == 0 and currentTime < DAY_START)): + recording = False + + recording = True + # 记录时间则需要启动子进程 + if recording and p is None: + le.info(u'启动子进程') + p = multiprocessing.Process(target=runChildProcess) + p.start() + le.info(u'子进程启动成功') + + # 非记录时间则退出子进程 + if not recording and p is not None: + le.info(u'关闭子进程') + p.terminate() + p.join() + p = None + le.info(u'子进程关闭成功') + + sleep(5) + + +if __name__ == '__main__': + #runChildProcess() + runParentProcess()