From a38fe9659876cbe3b1d473d3d578a862f3ac2662 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Fri, 1 Sep 2017 14:33:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9DataRecording=E5=92=8CCtaTrad?= =?UTF-8?q?ing=E7=9A=84=E4=BE=8B=E5=AD=90=EF=BC=8C=E4=BD=BF=E7=94=A8LogEng?= =?UTF-8?q?ine=E8=BE=93=E5=87=BA=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/CtaTrading/VT_setting.json | 16 +++++ examples/CtaTrading/runCtaTrading.py | 70 ++++++++++------------ examples/DataRecording/VT_setting.json | 16 +++++ examples/DataRecording/runDataRecording.py | 43 ++++++++----- examples/VnTrader/VT_setting.json | 3 +- vnpy/trader/VT_setting.json | 1 + vnpy/trader/vtEngine.py | 6 +- 7 files changed, 95 insertions(+), 60 deletions(-) create mode 100644 examples/CtaTrading/VT_setting.json create mode 100644 examples/DataRecording/VT_setting.json diff --git a/examples/CtaTrading/VT_setting.json b/examples/CtaTrading/VT_setting.json new file mode 100644 index 00000000..ef3bb771 --- /dev/null +++ b/examples/CtaTrading/VT_setting.json @@ -0,0 +1,16 @@ +{ + "fontFamily": "微软雅黑", + "fontSize": 12, + + "mongoHost": "localhost", + "mongoPort": 27017, + "mongoLogging": true, + + "darkStyle": true, + "language": "chinese", + + "logActive": false, + "logLevel": "debug", + "logConsole": true, + "logFile": true +} \ No newline at end of file diff --git a/examples/CtaTrading/runCtaTrading.py b/examples/CtaTrading/runCtaTrading.py index bd5eb84b..37e1e22c 100644 --- a/examples/CtaTrading/runCtaTrading.py +++ b/examples/CtaTrading/runCtaTrading.py @@ -6,67 +6,52 @@ from datetime import datetime, time from vnpy.event import EventEngine2 from vnpy.trader.vtEvent import EVENT_LOG -from vnpy.trader.vtEngine import MainEngine +from vnpy.trader.vtEngine import MainEngine, LogEngine from vnpy.trader.gateway import ctpGateway from vnpy.trader.app import ctaStrategy from vnpy.trader.app.ctaStrategy.ctaBase import EVENT_CTA_LOG -#---------------------------------------------------------------------- -def printLog(content): - """输出日志""" - t = datetime.now().strftime('%Y-%m-%d %H:%M:%S') - print '%s\t%s' %(t, content) - -#---------------------------------------------------------------------- -def processLogEvent(event): - """处理日志事件""" - log = event.dict_['data'] - if log.gatewayName: - content = '%s:%s' %(log.gatewayName, log.logContent) - else: - content = '%s:%s' %('MainEngine', log.logContent) - printLog(content) - -#---------------------------------------------------------------------- -def processCtaLogEvent(event): - """处理CTA模块日志事件""" - log = event.dict_['data'] - content = '%s:%s' %('CTA Engine', log.logContent) - printLog(content) - + #---------------------------------------------------------------------- def runChildProcess(): """子进程运行函数""" print '-'*20 - printLog(u'启动CTA策略运行子进程') + + # 创建日志引擎 + le = LogEngine() + le.setLogLevel(le.LEVEL_INFO) + le.addConsoleHandler() + le.addFileHandler() + + le.info(u'启动CTA策略运行子进程') ee = EventEngine2() - printLog(u'事件引擎创建成功') + le.info(u'事件引擎创建成功') me = MainEngine(ee) me.addGateway(ctpGateway) me.addApp(ctaStrategy) - printLog(u'主引擎创建成功') + le.info(u'主引擎创建成功') - ee.register(EVENT_LOG, processLogEvent) - ee.register(EVENT_CTA_LOG, processCtaLogEvent) - printLog(u'注册日志事件监听') + ee.register(EVENT_LOG, le.processLogEvent) + ee.register(EVENT_CTA_LOG, le.processLogEvent) + le.info(u'注册日志事件监听') me.connect('CTP') - printLog(u'连接CTP接口') + le.info(u'连接CTP接口') sleep(5) # 等待CTP接口初始化 cta = me.appDict[ctaStrategy.appName] cta.loadSetting() - printLog(u'CTA策略载入成功') + le.info(u'CTA策略载入成功') cta.initAll() - printLog(u'CTA策略初始化成功') + le.info(u'CTA策略初始化成功') cta.startAll() - printLog(u'CTA策略启动成功') + le.info(u'CTA策略启动成功') while True: sleep(1) @@ -74,7 +59,12 @@ def runChildProcess(): #---------------------------------------------------------------------- def runParentProcess(): """父进程运行函数""" - printLog(u'启动CTA策略守护父进程') + # 创建日志引擎 + le = LogEngine() + le.setLogLevel(le.LEVEL_INFO) + le.addConsoleHandler() + + le.info(u'启动CTA策略守护父进程') DAY_START = time(8, 45) # 日盘启动和停止时间 DAY_END = time(15, 30) @@ -96,18 +86,18 @@ def runParentProcess(): # 记录时间则需要启动子进程 if recording and p is None: - printLog(u'启动子进程') + le.info(u'启动子进程') p = multiprocessing.Process(target=runChildProcess) p.start() - printLog(u'子进程启动成功') + le.info(u'子进程启动成功') # 非记录时间则退出子进程 if not recording and p is not None: - printLog(u'关闭子进程') + le.info(u'关闭子进程') p.terminate() p.join() p = None - printLog(u'子进程关闭成功') + le.info(u'子进程关闭成功') sleep(5) @@ -116,4 +106,4 @@ if __name__ == '__main__': runChildProcess() # 尽管同样实现了无人值守,但强烈建议每天启动时人工检查,为自己的PNL负责 - # runParentProcess() \ No newline at end of file + #runParentProcess() \ No newline at end of file diff --git a/examples/DataRecording/VT_setting.json b/examples/DataRecording/VT_setting.json new file mode 100644 index 00000000..ef3bb771 --- /dev/null +++ b/examples/DataRecording/VT_setting.json @@ -0,0 +1,16 @@ +{ + "fontFamily": "微软雅黑", + "fontSize": 12, + + "mongoHost": "localhost", + "mongoPort": 27017, + "mongoLogging": true, + + "darkStyle": true, + "language": "chinese", + + "logActive": false, + "logLevel": "debug", + "logConsole": true, + "logFile": true +} \ No newline at end of file diff --git a/examples/DataRecording/runDataRecording.py b/examples/DataRecording/runDataRecording.py index c05425ea..43b1674b 100644 --- a/examples/DataRecording/runDataRecording.py +++ b/examples/DataRecording/runDataRecording.py @@ -6,12 +6,12 @@ from datetime import datetime, time from vnpy.event import EventEngine2 from vnpy.trader.vtEvent import EVENT_LOG -from vnpy.trader.vtEngine import MainEngine +from vnpy.trader.vtEngine import MainEngine, LogEngine from vnpy.trader.gateway import ctpGateway from vnpy.trader.app import dataRecorder #---------------------------------------------------------------------- -def printLog(content): +def le.info(content): """输出日志""" t = datetime.now().strftime('%Y-%m-%d %H:%M:%S') print '%s\t%s' %(t, content) @@ -24,27 +24,33 @@ def processLogEvent(event): content = '%s:%s' %(log.gatewayName, log.logContent) else: content = '%s:%s' %('MainEngine', log.logContent) - printLog(content) + le.info(content) #---------------------------------------------------------------------- def runChildProcess(): """子进程运行函数""" print '-'*20 - printLog(u'启动行情记录运行子进程') + + # 创建日志引擎 + le = LogEngine() + le.setLogLevel(le.LEVEL_INFO) + le.addConsoleHandler() + + le.info(u'启动行情记录运行子进程') ee = EventEngine2() - printLog(u'事件引擎创建成功') + le.info(u'事件引擎创建成功') me = MainEngine(ee) me.addGateway(ctpGateway) me.addApp(dataRecorder) - printLog(u'主引擎创建成功') + le.info(u'主引擎创建成功') - ee.register(EVENT_LOG, processLogEvent) - printLog(u'注册日志事件监听') + ee.register(EVENT_LOG, le.processLogEvent) + le.info(u'注册日志事件监听') me.connect('CTP') - printLog(u'连接CTP接口') + le.info(u'连接CTP接口') while True: sleep(1) @@ -52,7 +58,12 @@ def runChildProcess(): #---------------------------------------------------------------------- def runParentProcess(): """父进程运行函数""" - printLog(u'启动行情记录守护父进程') + # 创建日志引擎 + le = LogEngine() + le.setLogLevel(le.LEVEL_INFO) + le.addConsoleHandler() + + le.info(u'启动行情记录守护父进程') DAY_START = time(8, 57) # 日盘启动和停止时间 DAY_END = time(15, 18) @@ -74,22 +85,22 @@ def runParentProcess(): # 记录时间则需要启动子进程 if recording and p is None: - printLog(u'启动子进程') + le.info(u'启动子进程') p = multiprocessing.Process(target=runChildProcess) p.start() - printLog(u'子进程启动成功') + le.info(u'子进程启动成功') # 非记录时间则退出子进程 if not recording and p is not None: - printLog(u'关闭子进程') + le.info(u'关闭子进程') p.terminate() p.join() p = None - printLog(u'子进程关闭成功') + le.info(u'子进程关闭成功') sleep(5) if __name__ == '__main__': - runChildProcess() - #runParentProcess() \ No newline at end of file + #runChildProcess() + runParentProcess() \ No newline at end of file diff --git a/examples/VnTrader/VT_setting.json b/examples/VnTrader/VT_setting.json index 7286c0e0..7f634434 100644 --- a/examples/VnTrader/VT_setting.json +++ b/examples/VnTrader/VT_setting.json @@ -9,7 +9,8 @@ "darkStyle": true, "language": "chinese", - "logLevel": "info", + "logActive": true, + "logLevel": "debug", "logConsole": true, "logFile": true } \ No newline at end of file diff --git a/vnpy/trader/VT_setting.json b/vnpy/trader/VT_setting.json index a397059a..7f634434 100644 --- a/vnpy/trader/VT_setting.json +++ b/vnpy/trader/VT_setting.json @@ -9,6 +9,7 @@ "darkStyle": true, "language": "chinese", + "logActive": true, "logLevel": "debug", "logConsole": true, "logFile": true diff --git a/vnpy/trader/vtEngine.py b/vnpy/trader/vtEngine.py index a2f35ded..f9841926 100644 --- a/vnpy/trader/vtEngine.py +++ b/vnpy/trader/vtEngine.py @@ -287,6 +287,9 @@ class MainEngine(object): #---------------------------------------------------------------------- def initLogEngine(self): """初始化日志引擎""" + if not globalSetting["logActive"]: + return + # 创建引擎 self.logEngine = LogEngine() @@ -311,9 +314,6 @@ class MainEngine(object): # 注册事件监听 self.eventEngine.register(EVENT_LOG, self.logEngine.processLogEvent) - # 记录新启动日志引擎 - self.logEngine.info(u"------日志引擎启动------") - ######################################################################## class DataEngine(object):