From f58f2be4ee16efca688a96b10de5db16f275f3be Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Tue, 27 Jun 2017 21:52:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=A1=8C=E6=83=85=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E5=B7=A5=E5=85=B7=EF=BC=8C=E6=96=B0=E5=A2=9E=E6=97=A0?= =?UTF-8?q?=E5=9B=BE=E5=BD=A2=E7=95=8C=E9=9D=A2CTA=E7=AD=96=E7=95=A5?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/CtaTrading/runCtaTrading.py | 104 +++++++++++++++++++++ examples/DataRecording/runDataRecording.py | 5 +- examples/README.md | 11 +++ vnpy/trader/app/ctaStrategy/ctaEngine.py | 20 +++- vnpy/trader/app/ctaStrategy/uiCtaWidget.py | 9 +- 5 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 examples/CtaTrading/runCtaTrading.py create mode 100644 examples/README.md diff --git a/examples/CtaTrading/runCtaTrading.py b/examples/CtaTrading/runCtaTrading.py new file mode 100644 index 00000000..70fa4c22 --- /dev/null +++ b/examples/CtaTrading/runCtaTrading.py @@ -0,0 +1,104 @@ +# 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 +from vnpy.trader.vtEngine import MainEngine +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'] + content = '%s:%s' %(log.gatewayName, log.logContent) + printLog(content) + +#---------------------------------------------------------------------- +def runChildProcess(): + """子进程运行函数""" + print '-'*20 + printLog(u'启动CTA策略运行子进程') + + ee = EventEngine2() + printLog(u'事件引擎创建成功') + + me = MainEngine(ee) + me.addGateway(ctpGateway) + me.addApp(ctaStrategy) + printLog(u'主引擎创建成功') + + ee.register(EVENT_LOG, processLogEvent) + ee.register(EVENT_CTA_LOG, processLogEvent) + printLog(u'注册日志事件监听') + + me.connect('CTP') + printLog(u'连接CTP接口') + + cta = me.appDict[ctaStrategy.appName] + + cta.loadSetting() + printLog(u'CTA策略载入成功') + + cta.initAll() + printLog(u'CTA策略初始化成功') + + cta.startAll() + printLog(u'CTA策略启动成功') + + while True: + sleep(1) + +#---------------------------------------------------------------------- +def runParentProcess(): + """父进程运行函数""" + printLog(u'启动CTA策略守护父进程') + + 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: + printLog(u'启动子进程') + p = multiprocessing.Process(target=runChildProcess) + p.start() + printLog(u'子进程启动成功') + + # 非记录时间则退出子进程 + if not recording and p is not None: + printLog(u'关闭子进程') + p.terminate() + p.join() + p = None + printLog(u'子进程关闭成功') + + sleep(5) + + +if __name__ == '__main__': + runParentProcess() \ No newline at end of file diff --git a/examples/DataRecording/runDataRecording.py b/examples/DataRecording/runDataRecording.py index 73c77f6f..dc6846b8 100644 --- a/examples/DataRecording/runDataRecording.py +++ b/examples/DataRecording/runDataRecording.py @@ -7,7 +7,8 @@ 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.gateway import ctpGateway +from vnpy.trader.app import dataRecorder #---------------------------------------------------------------------- def printLog(content): @@ -32,6 +33,8 @@ def runChildProcess(): printLog(u'事件引擎创建成功') me = MainEngine(ee) + me.addGateway(ctpGateway) + me.addApp(dataRecorder) printLog(u'主引擎创建成功') ee.register(EVENT_LOG, processLogEvent) diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..148cecf1 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,11 @@ +# vn.py项目的应用示例 + +本文件夹中的内容主要是关于如何在交易业务中使用vn.py的示例 + +* VnTrader:最常用的vn.py图形交易界面 + +* DataRecording:全自动行情记录工具(无需用户每日定时重启) + +* CtaTrading:纯命令行模式的CTA策略交易(尽管同样实现了无人值守,但强烈建议每天启动时人工检查,为自己的PNL负责) + +* CtaBacktesting:CTA策略的回测和优化 \ No newline at end of file diff --git a/vnpy/trader/app/ctaStrategy/ctaEngine.py b/vnpy/trader/app/ctaStrategy/ctaEngine.py index 2a2178b5..319209eb 100644 --- a/vnpy/trader/app/ctaStrategy/ctaEngine.py +++ b/vnpy/trader/app/ctaStrategy/ctaEngine.py @@ -463,7 +463,25 @@ class CtaEngine(object): if so.strategy is strategy: self.cancelStopOrder(stopOrderID) else: - self.writeCtaLog(u'策略实例不存在:%s' %name) + self.writeCtaLog(u'策略实例不存在:%s' %name) + + #---------------------------------------------------------------------- + def initAll(self): + """全部初始化""" + for name in self.strategyDict.keys(): + self.initStrategy(name) + + #---------------------------------------------------------------------- + def startAll(self): + """全部启动""" + for name in self.strategyDict.keys(): + self.startStrategy(name) + + #---------------------------------------------------------------------- + def stopAll(self): + """全部停止""" + for name in self.strategyDict.keys(): + self.stopStrategy(name) #---------------------------------------------------------------------- def saveSetting(self): diff --git a/vnpy/trader/app/ctaStrategy/uiCtaWidget.py b/vnpy/trader/app/ctaStrategy/uiCtaWidget.py index 62366a89..7223a673 100644 --- a/vnpy/trader/app/ctaStrategy/uiCtaWidget.py +++ b/vnpy/trader/app/ctaStrategy/uiCtaWidget.py @@ -228,20 +228,17 @@ class CtaEngineManager(QtWidgets.QWidget): #---------------------------------------------------------------------- def initAll(self): """全部初始化""" - for name in self.ctaEngine.strategyDict.keys(): - self.ctaEngine.initStrategy(name) + self.ctaEngine.initAll() #---------------------------------------------------------------------- def startAll(self): """全部启动""" - for name in self.ctaEngine.strategyDict.keys(): - self.ctaEngine.startStrategy(name) + self.ctaEngine.startAll() #---------------------------------------------------------------------- def stopAll(self): """全部停止""" - for name in self.ctaEngine.strategyDict.keys(): - self.ctaEngine.stopStrategy(name) + self.ctaEngine.stopAll() #---------------------------------------------------------------------- def load(self):