From 86aa4f8fab5213a757d2c599d0895fa5f822581c Mon Sep 17 00:00:00 2001 From: msincenselee Date: Tue, 8 Nov 2016 10:48:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=EF=BC=9A=E5=8D=95=E4=B8=80?= =?UTF-8?q?=E7=AD=96=E7=95=A5=E4=B8=AD=EF=BC=8C=E6=94=AF=E6=8C=81=E5=A4=9A?= =?UTF-8?q?=E4=B8=AA=E8=AE=A2=E9=98=85=E3=80=82=EF=BC=88=E8=8B=A5=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=A4=9A=E4=B8=AA=E5=90=88=E7=BA=A6=EF=BC=8C=E5=8E=9F?= =?UTF-8?q?=E6=9C=89=E7=AD=96=E7=95=A5=E6=A8=A1=E6=9D=BF=E7=9A=84=E5=BC=80?= =?UTF-8?q?=E4=BB=93=E5=8A=9F=E8=83=BD=E4=B8=8D=E8=83=BD=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E7=94=A8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vn.trader/ctaAlgo/ctaEngine.py | 69 +++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/vn.trader/ctaAlgo/ctaEngine.py b/vn.trader/ctaAlgo/ctaEngine.py index 00e35b5d..ae77773d 100644 --- a/vn.trader/ctaAlgo/ctaEngine.py +++ b/vn.trader/ctaAlgo/ctaEngine.py @@ -14,6 +14,10 @@ 感到功能不足的用户(即希望更高频的交易),交易策略不应该出现4中所述的情况 6. 对于想要实现4中所述情况的用户,需要实现一个策略信号引擎和交易委托引擎分开 的定制化统结构(没错,得自己写) + + Modified by IncenseLee(李来佳) + 1、增加单一策略里,多个vtSymbol的配置。 + ''' import json @@ -404,6 +408,17 @@ class CtaEngine(object): # 注册持仓更新事件 self.eventEngine.register(EVENT_POSITION, self.processPositionEvent) + + # 注册定时器事件 + self.eventEngine.register(EVENT_TIMER, self.processTimerEvent) + + def processTimerEvent(self, event): + """定时器事件""" + + # 触发每个策略的定时接口 + for strategy in self.strategyDict.values(): + strategy.onTimer() + # ---------------------------------------------------------------------- def insertData(self, dbName, collectionName, data): @@ -488,29 +503,33 @@ class CtaEngine(object): self.strategyDict[name] = strategy # 2.保存Tick映射关系(symbol <==> Strategy[] ) - if strategy.vtSymbol in self.tickStrategyDict: - l = self.tickStrategyDict[strategy.vtSymbol] - else: - l = [] - self.tickStrategyDict[strategy.vtSymbol] = l - l.append(strategy) - - # 3.订阅合约 - contract = self.mainEngine.getContract(strategy.vtSymbol) - if contract: - # 4.构造订阅请求包 - req = VtSubscribeReq() - req.symbol = contract.symbol - req.exchange = contract.exchange - - # 对于IB接口订阅行情时所需的货币和产品类型,从策略属性中获取 - req.currency = strategy.currency - req.productClass = strategy.productClass + # modifid by Incenselee 支持多个Symbol的订阅 + symbols = strategy.vtSymbol.split(';') - # 5.调用主引擎的订阅接口 - self.mainEngine.subscribe(req, contract.gatewayName) - else: - self.writeCtaLog(u'%s的交易合约%s无法找到' %(name, strategy.vtSymbol)) + for symbol in symbols: + if symbol in self.tickStrategyDict: + l = self.tickStrategyDict[symbol] + else: + l = [] + self.tickStrategyDict[symbol] = l + l.append(strategy) + + # 3.订阅合约 + contract = self.mainEngine.getContract(symbol) + if contract: + # 4.构造订阅请求包 + req = VtSubscribeReq() + req.symbol = contract.symbol + req.exchange = contract.exchange + + # 对于IB接口订阅行情时所需的货币和产品类型,从策略属性中获取 + req.currency = strategy.currency + req.productClass = strategy.productClass + + # 5.调用主引擎的订阅接口 + self.mainEngine.subscribe(req, contract.gatewayName) + else: + self.writeCtaLog(u'%s的交易合约%s无法找到' %(name, symbol)) #---------------------------------------------------------------------- def initStrategy(self, name, force = False): @@ -666,6 +685,12 @@ class CtaEngine(object): # 5.保存策略数据 strategy.saveBar() + def clearData(self): + """清空运行数据""" + self.orderStrategyDict = {} + self.workingStopOrderDict = {} + self.posBufferDict = {} + self.stopOrderDict = {} ######################################################################## class PositionBuffer(object):