新增无界面CTA策略交易的Example,修改DataRecorder的配置文件为csv格式

This commit is contained in:
vn.py 2017-06-28 15:36:24 +08:00
parent f58f2be4ee
commit bed06834e3
6 changed files with 72 additions and 103 deletions

View File

@ -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()
runChildProcess()
# 尽管同样实现了无人值守但强烈建议每天启动时人工检查为自己的PNL负责
# runParentProcess()

View File

@ -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()
runChildProcess()
#runParentProcess()

View File

@ -6,6 +6,6 @@
* DataRecording全自动行情记录工具无需用户每日定时重启
* CtaTrading纯命令行模式的CTA策略交易尽管同样实现了无人值守但强烈建议每天启动时人工检查为自己的PNL负责
* CtaTrading无图形界面模式的CTA策略交易
* CtaBacktestingCTA策略的回测和优化

View File

@ -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,
1 gateway symbol exchange currency product tick bar active
2 CTP IF1709 y y IF0000
3 CTP IC1709 y
4 CTP m1709 y
5 SGIT IH1709 y y IH0000
6 LTS 600036 SZSE y
7 IB EUR.USD IDEALPRO USD Íâ»ã y y

View File

@ -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"
}
}

View File

@ -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')