From 8b572228971a06ca9790334b8a16fe562ee18879 Mon Sep 17 00:00:00 2001 From: vnpy Date: Thu, 5 May 2016 21:52:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 - vn.trader/ctaAlgo/ctaEngine.py | 5 ++++- vn.trader/dataRecorder/drEngine.py | 3 ++- vn.trader/ibGateway/ibGateway.py | 22 ++++++++-------------- vn.trader/vtFunction.py | 6 ++++++ 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 045a4b99..c0b231cf 100644 --- a/.gitignore +++ b/.gitignore @@ -14,7 +14,6 @@ Release/ # Python编译文件 *.pyc -*.pyd # WingIDE文件 *.wpr diff --git a/vn.trader/ctaAlgo/ctaEngine.py b/vn.trader/ctaAlgo/ctaEngine.py index affae16e..59410dec 100644 --- a/vn.trader/ctaAlgo/ctaEngine.py +++ b/vn.trader/ctaAlgo/ctaEngine.py @@ -14,6 +14,7 @@ from ctaSetting import STRATEGY_CLASS from eventEngine import * from vtConstant import * from vtGateway import VtSubscribeReq, VtOrderReq, VtCancelOrderReq, VtLogData +from vtFunction import todayDate ######################################################################## @@ -29,7 +30,7 @@ class CtaEngine(object): self.eventEngine = eventEngine # 当前日期 - self.today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) + self.today = todayDate() # 保存策略实例的字典 # key为策略名称,value为策略实例,注意策略名称不允许重复 @@ -429,5 +430,7 @@ class CtaEngine(object): """触发策略状态变化事件(通常用于通知GUI更新)""" event = Event(EVENT_CTA_STRATEGY+name) self.eventEngine.put(event) + + diff --git a/vn.trader/dataRecorder/drEngine.py b/vn.trader/dataRecorder/drEngine.py index c9249970..06ea803c 100644 --- a/vn.trader/dataRecorder/drEngine.py +++ b/vn.trader/dataRecorder/drEngine.py @@ -15,6 +15,7 @@ from datetime import datetime, timedelta from eventEngine import * from vtGateway import VtSubscribeReq, VtLogData from drBase import * +from vtFunction import todayDate ######################################################################## @@ -31,7 +32,7 @@ class DrEngine(object): self.eventEngine = eventEngine # 当前日期 - self.today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) + self.today = todayDate() # 主力合约代码映射字典,key为具体的合约代码(如IF1604),value为主力合约代码(如IF0000) self.activeSymbolDict = {} diff --git a/vn.trader/ibGateway/ibGateway.py b/vn.trader/ibGateway/ibGateway.py index 5658ff88..e00cdfd6 100644 --- a/vn.trader/ibGateway/ibGateway.py +++ b/vn.trader/ibGateway/ibGateway.py @@ -68,6 +68,7 @@ productClassMap[PRODUCT_EQUITY] = 'STK' productClassMap[PRODUCT_FUTURES] = 'FUT' productClassMap[PRODUCT_OPTION] = 'OPT' productClassMap[PRODUCT_FOREX] = 'CASH' +productClassMapReverse = {v:k for k,v in productClassMap.items()} # 期权类型映射 optionTypeMap = {} @@ -184,6 +185,7 @@ class IbGateway(VtGateway): contract.m_strike = subscribeReq.strikePrice contract.m_right = optionTypeMap.get(subscribeReq.optionType, '') + # 考虑设计为针对期货用代码_到期日的方式来代替单纯的代码 if contract.m_secType == 'FUT' and not subscribeReq.expiry: # 期货 如果没有设置过期时间, 默认设置为下个月 dt_obj = datetime.now() @@ -469,30 +471,22 @@ class IbWrapper(EWrapper): contract.name = contractDetails.m_summary.m_localSymbol.decode('UTF-8') # 合约类型 - if contractDetails.m_summary.m_secType == 'STK': - contract.productClass = PRODUCT_EQUITY - elif contractDetails.m_summary.m_secType == 'CASH': - contract.productClass = PRODUCT_FOREX - elif contractDetails.m_summary.m_secType == 'FUT': - contract.productClass = PRODUCT_FUTURES - elif contractDetails.m_summary.m_secType == 'OPT': - contract.productClass = PRODUCT_OPTION - else: - contract.productClass = PRODUCT_UNKNOWN + contract.productClass = productClassMapReverse.get(contractDetails.m_summary.m_secType, + PRODUCT_UNKNOWN) + # 推送 self.gateway.onContract(contract) #---------------------------------------------------------------------- def bondContractDetails(self, reqId, contractDetails): """ generated source for method bondContractDetails """ + pass #---------------------------------------------------------------------- def contractDetailsEnd(self, reqId): """ 获取合约结束 """ - log = VtLogData() - log.gatewayName = self.gatewayName - log.logContent = u'交易合约信息获取完成' - self.gateway.onLog(log) + # 因为IB的合约获取是一个个合约进行的,并不会用于触发其他操作,因此无需发出日志 + pass #---------------------------------------------------------------------- def execDetails(self, reqId, contract, execution): diff --git a/vn.trader/vtFunction.py b/vn.trader/vtFunction.py index 708de59e..46f0b27c 100644 --- a/vn.trader/vtFunction.py +++ b/vn.trader/vtFunction.py @@ -6,6 +6,7 @@ import decimal import json +from datetime import datetime MAX_NUMBER = 10000000000000 MAX_DECIMAL = 4 @@ -40,4 +41,9 @@ def loadMongoSetting(): return host, port +#---------------------------------------------------------------------- +def todayDate(): + """获取当前本机电脑时间的日期""" + return datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) +