From bed06834e3d1f65be3bfba577c5d0ac3cd371c3f Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Wed, 28 Jun 2017 15:36:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=A0=E7=95=8C=E9=9D=A2CT?= =?UTF-8?q?A=E7=AD=96=E7=95=A5=E4=BA=A4=E6=98=93=E7=9A=84Example=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E6=94=B9DataRecorder=E7=9A=84=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=BAcsv=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/CtaTrading/runCtaTrading.py | 21 +++- examples/DataRecording/runDataRecording.py | 8 +- examples/README.md | 2 +- vnpy/trader/app/dataRecorder/DR_setting.csv | 7 ++ vnpy/trader/app/dataRecorder/DR_setting.json | 32 ------ vnpy/trader/app/dataRecorder/drEngine.py | 105 +++++++------------ 6 files changed, 72 insertions(+), 103 deletions(-) create mode 100644 vnpy/trader/app/dataRecorder/DR_setting.csv delete mode 100644 vnpy/trader/app/dataRecorder/DR_setting.json diff --git a/examples/CtaTrading/runCtaTrading.py b/examples/CtaTrading/runCtaTrading.py index 70fa4c22..bd5eb84b 100644 --- a/examples/CtaTrading/runCtaTrading.py +++ b/examples/CtaTrading/runCtaTrading.py @@ -21,7 +21,17 @@ def printLog(content): def processLogEvent(event): """处理日志事件""" log = event.dict_['data'] - content = '%s:%s' %(log.gatewayName, log.logContent) + 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) #---------------------------------------------------------------------- @@ -39,12 +49,14 @@ def runChildProcess(): printLog(u'主引擎创建成功') ee.register(EVENT_LOG, processLogEvent) - ee.register(EVENT_CTA_LOG, processLogEvent) + ee.register(EVENT_CTA_LOG, processCtaLogEvent) printLog(u'注册日志事件监听') me.connect('CTP') printLog(u'连接CTP接口') + sleep(5) # 等待CTP接口初始化 + cta = me.appDict[ctaStrategy.appName] cta.loadSetting() @@ -101,4 +113,7 @@ def runParentProcess(): if __name__ == '__main__': - runParentProcess() \ No newline at end of file + runChildProcess() + + # 尽管同样实现了无人值守,但强烈建议每天启动时人工检查,为自己的PNL负责 + # runParentProcess() \ No newline at end of file diff --git a/examples/DataRecording/runDataRecording.py b/examples/DataRecording/runDataRecording.py index dc6846b8..b4997c14 100644 --- a/examples/DataRecording/runDataRecording.py +++ b/examples/DataRecording/runDataRecording.py @@ -20,7 +20,10 @@ def printLog(content): def processLogEvent(event): """处理日志事件""" log = event.dict_['data'] - content = '%s:%s' %(log.gatewayName, log.logContent) + if log.gatewayName: + content = '%s:%s' %(log.gatewayName, log.logContent) + else: + content = '%s:%s' %('MainEngine', log.logContent) printLog(content) #---------------------------------------------------------------------- @@ -88,4 +91,5 @@ def runParentProcess(): if __name__ == '__main__': - runParentProcess() \ No newline at end of file + runChildProcess() + #runParentProcess() \ No newline at end of file diff --git a/examples/README.md b/examples/README.md index 148cecf1..c95316f3 100644 --- a/examples/README.md +++ b/examples/README.md @@ -6,6 +6,6 @@ * DataRecording:全自动行情记录工具(无需用户每日定时重启) -* CtaTrading:纯命令行模式的CTA策略交易(尽管同样实现了无人值守,但强烈建议每天启动时人工检查,为自己的PNL负责) +* CtaTrading:无图形界面模式的CTA策略交易 * CtaBacktesting:CTA策略的回测和优化 \ No newline at end of file diff --git a/vnpy/trader/app/dataRecorder/DR_setting.csv b/vnpy/trader/app/dataRecorder/DR_setting.csv new file mode 100644 index 00000000..fa5f396c --- /dev/null +++ b/vnpy/trader/app/dataRecorder/DR_setting.csv @@ -0,0 +1,7 @@ +gateway,symbol,exchange,currency,product,tick,bar,active +CTP,IF1709,,,,y,y,IF0000 +CTP,IC1709,,,,y,, +CTP,m1709,,,,y,, +SGIT,IH1709,,,,y,y,IH0000 +LTS,600036,SZSE,,,y,, +IB,EUR.USD,IDEALPRO,USD,,y,y, diff --git a/vnpy/trader/app/dataRecorder/DR_setting.json b/vnpy/trader/app/dataRecorder/DR_setting.json deleted file mode 100644 index f7c297c5..00000000 --- a/vnpy/trader/app/dataRecorder/DR_setting.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "working": false, - - "tick": - [ - ["m1609", "XSPEED"], - ["IF1606", "SGIT"], - ["IH1606", "SGIT"], - ["IH1606", "SGIT"], - ["IC1606", "SGIT"], - ["IC1606", "SGIT"], - ["600036", "LTS", "SZSE"], - ["EUR.USD", "IB", "IDEALPRO", "USD", "外汇"] - ], - - "bar": - [ - ["IF1605", "SGIT"], - ["IF1606", "SGIT"], - ["IH1606", "SGIT"], - ["IH1606", "SGIT"], - ["IC1606", "SGIT"], - ["IC1606", "SGIT"] - ], - - "active": - { - "IF0000": "IF1605", - "IH0000": "IH1605", - "IC0000": "IC1605" - } -} \ No newline at end of file diff --git a/vnpy/trader/app/dataRecorder/drEngine.py b/vnpy/trader/app/dataRecorder/drEngine.py index 56d8eba2..431c41c3 100644 --- a/vnpy/trader/app/dataRecorder/drEngine.py +++ b/vnpy/trader/app/dataRecorder/drEngine.py @@ -6,7 +6,8 @@ 使用DR_setting.json来配置需要收集的合约,以及主力合约代码。 ''' -import json +#import json +import csv import os import copy from collections import OrderedDict @@ -27,7 +28,7 @@ from vnpy.trader.app.dataRecorder.language import text class DrEngine(object): """数据记录引擎""" - settingFileName = 'DR_setting.json' + settingFileName = 'DR_setting.csv' path = os.path.abspath(os.path.dirname(__file__)) settingFileName = os.path.join(path, settingFileName) @@ -57,84 +58,58 @@ class DrEngine(object): # 载入设置,订阅行情 self.loadSetting() + # 启动数据插入线程 + self.start() + + # 注册事件监听 + self.registerEvent() + #---------------------------------------------------------------------- def loadSetting(self): - """载入设置""" + """加载配置""" with open(self.settingFileName) as f: - drSetting = json.load(f) + drSetting = csv.DictReader(f) - # 如果working设为False则不启动行情记录功能 - working = drSetting['working'] - if not working: - return - - if 'tick' in drSetting: - l = drSetting['tick'] + for d in drSetting: + # 读取配置 + gatewayName = d['gateway'] + symbol = d['symbol'] + exchange = d['symbol'] + currency = d['currency'] + productClass = d['product'] + recordTick = d['tick'] + recordBar = d['bar'] + activeSymbol = d['active'] - for setting in l: - symbol = setting[0] + if exchange: + vtSymbol = '.'.join([symbol, exchange]) + else: vtSymbol = symbol - - req = VtSubscribeReq() - req.symbol = setting[0] - - # 针对LTS和IB接口,订阅行情需要交易所代码 - if len(setting)>=3: - req.exchange = setting[2] - vtSymbol = '.'.join([symbol, req.exchange]) - - # 针对IB接口,订阅行情需要货币和产品类型 - if len(setting)>=5: - req.currency = setting[3] - req.productClass = setting[4] - - self.mainEngine.subscribe(req, setting[1]) - - tick = VtTickData() # 该tick实例可以用于缓存部分数据(目前未使用) - self.tickDict[vtSymbol] = tick - - if 'bar' in drSetting: - l = drSetting['bar'] - for setting in l: - symbol = setting[0] - vtSymbol = symbol - - req = VtSubscribeReq() - req.symbol = symbol - - if len(setting)>=3: - req.exchange = setting[2] - vtSymbol = '.'.join([symbol, req.exchange]) - - if len(setting)>=5: - req.currency = setting[3] - req.productClass = setting[4] - - self.mainEngine.subscribe(req, setting[1]) - - bar = VtBarData() - self.barDict[vtSymbol] = bar - - if 'active' in drSetting: - d = drSetting['active'] + # 订阅行情 + req = VtSubscribeReq() + req.symbol = symbol + req.exchange = exchange + req.currency = currency + req.productClass = productClass + self.mainEngine.subscribe(req, gatewayName) - # 注意这里的vtSymbol对于IB和LTS接口,应该后缀.交易所 - for activeSymbol, vtSymbol in d.items(): + # 设置需要记录的数据 + if recordTick: + self.tickDict[vtSymbol] = VtTickData() + + if recordBar: + self.barDict[vtSymbol] = VtBarData() + + if activeSymbol: self.activeSymbolDict[vtSymbol] = activeSymbol - - # 启动数据插入线程 - self.start() - - # 注册事件监听 - self.registerEvent() #---------------------------------------------------------------------- def procecssTickEvent(self, event): """处理行情推送""" tick = event.dict_['data'] vtSymbol = tick.vtSymbol - + # 转化Tick格式 if not tick.datetime: tick.datetime = datetime.strptime(' '.join([tick.date, tick.time]), '%Y%m%d %H:%M:%S.%f')