diff --git a/vnpy/trader/app/ctaStrategy/ctaBacktesting.py b/vnpy/trader/app/ctaStrategy/ctaBacktesting.py index 10759b70..c3d63f6f 100644 --- a/vnpy/trader/app/ctaStrategy/ctaBacktesting.py +++ b/vnpy/trader/app/ctaStrategy/ctaBacktesting.py @@ -272,8 +272,8 @@ class BacktestingEngine(object): so.price = self.roundToPriceTick(price) so.volume = volume so.strategy = strategy - so.stopOrderID = stopOrderID so.status = STOPORDER_WAITING + so.stopOrderID = stopOrderID if orderType == CTAORDER_BUY: so.direction = DIRECTION_LONG @@ -292,6 +292,9 @@ class BacktestingEngine(object): self.stopOrderDict[stopOrderID] = so self.workingStopOrderDict[stopOrderID] = so + # 推送停止单初始更新 + self.strategy.onStopOrder(so) + return stopOrderID #---------------------------------------------------------------------- @@ -302,6 +305,7 @@ class BacktestingEngine(object): so = self.workingStopOrderDict[stopOrderID] so.status = STOPORDER_CANCELLED del self.workingStopOrderDict[stopOrderID] + self.strategy.onStopOrder(so) #---------------------------------------------------------------------- def crossLimitOrder(self): @@ -395,6 +399,11 @@ class BacktestingEngine(object): # 如果发生了成交 if buyCross or sellCross: + # 更新停止单状态,并从字典中删除该停止单 + so.status = STOPORDER_TRIGGERED + if stopOrderID in self.workingStopOrderDict: + del self.workingStopOrderDict[stopOrderID] + # 推送成交数据 self.tradeCount += 1 # 成交编号自增1 tradeID = str(self.tradeCount) @@ -414,19 +423,15 @@ class BacktestingEngine(object): orderID = str(self.limitOrderCount) trade.orderID = orderID trade.vtOrderID = orderID - trade.direction = so.direction trade.offset = so.offset trade.volume = so.volume trade.tradeTime = str(self.dt) trade.dt = self.dt - self.strategy.onTrade(trade) self.tradeDict[tradeID] = trade # 推送委托数据 - so.status = STOPORDER_TRIGGERED - order = VtOrderData() order.vtSymbol = so.vtSymbol order.symbol = so.vtSymbol @@ -439,14 +444,14 @@ class BacktestingEngine(object): order.tradedVolume = so.volume order.status = STATUS_ALLTRADED order.orderTime = trade.tradeTime - self.strategy.onOrder(order) self.limitOrderDict[orderID] = order - # 从字典中删除该限价单 - if stopOrderID in self.workingStopOrderDict: - del self.workingStopOrderDict[stopOrderID] - + # 按照顺序推送数据 + self.strategy.onStopOrder(so) + self.strategy.onOrder(order) + self.strategy.onTrade(trade) + #---------------------------------------------------------------------- def insertData(self, dbName, collectionName, data): """考虑到回测中不允许向数据库插入数据,防止实盘交易中的一些代码出错""" diff --git a/vnpy/trader/app/ctaStrategy/ctaEngine.py b/vnpy/trader/app/ctaStrategy/ctaEngine.py index edc34197..fc6f69d9 100644 --- a/vnpy/trader/app/ctaStrategy/ctaEngine.py +++ b/vnpy/trader/app/ctaStrategy/ctaEngine.py @@ -210,6 +210,9 @@ class CtaEngine(object): self.stopOrderDict[stopOrderID] = so self.workingStopOrderDict[stopOrderID] = so + # 推送停止单状态 + strategy.onStopOrder(so) + return stopOrderID #---------------------------------------------------------------------- @@ -220,6 +223,7 @@ class CtaEngine(object): so = self.workingStopOrderDict[stopOrderID] so.status = STOPORDER_CANCELLED del self.workingStopOrderDict[stopOrderID] + so.strategy.onStopOrder(so) #---------------------------------------------------------------------- def processStopOrder(self, tick): @@ -244,6 +248,7 @@ class CtaEngine(object): so.status = STOPORDER_TRIGGERED self.sendOrder(so.vtSymbol, so.orderType, price, so.volume, so.strategy) del self.workingStopOrderDict[so.stopOrderID] + so.strategy.onStopOrder(so) #---------------------------------------------------------------------- def processTickEvent(self, event): diff --git a/vnpy/trader/app/ctaStrategy/ctaTemplate.py b/vnpy/trader/app/ctaStrategy/ctaTemplate.py index f9cf9459..b4213d1f 100644 --- a/vnpy/trader/app/ctaStrategy/ctaTemplate.py +++ b/vnpy/trader/app/ctaStrategy/ctaTemplate.py @@ -90,6 +90,11 @@ class CtaTemplate(object): """收到Bar推送(必须由用户继承实现)""" raise NotImplementedError + #---------------------------------------------------------------------- + def onStopOrder(self, so): + """收到停止单推送(必须由用户继承实现)""" + raise NotImplementedError + #---------------------------------------------------------------------- def buy(self, price, volume, stop=False): """买开""" diff --git a/vnpy/trader/app/ctaStrategy/strategy/strategyAtrRsi.py b/vnpy/trader/app/ctaStrategy/strategy/strategyAtrRsi.py index 3669a66b..c9606b96 100644 --- a/vnpy/trader/app/ctaStrategy/strategy/strategyAtrRsi.py +++ b/vnpy/trader/app/ctaStrategy/strategy/strategyAtrRsi.py @@ -237,3 +237,7 @@ class AtrRsiStrategy(CtaTemplate): # 发出状态更新事件 self.putEvent() + #---------------------------------------------------------------------- + def onStopOrder(self, so): + """停止单推送""" + pass \ No newline at end of file diff --git a/vnpy/trader/app/ctaStrategy/strategy/strategyDualThrust.py b/vnpy/trader/app/ctaStrategy/strategy/strategyDualThrust.py index bc5b99b5..c7f7815a 100644 --- a/vnpy/trader/app/ctaStrategy/strategy/strategyDualThrust.py +++ b/vnpy/trader/app/ctaStrategy/strategy/strategyDualThrust.py @@ -221,3 +221,8 @@ class DualThrustStrategy(CtaTemplate): def onTrade(self, trade): # 发出状态更新事件 self.putEvent() + + #---------------------------------------------------------------------- + def onStopOrder(self, so): + """停止单推送""" + pass \ No newline at end of file diff --git a/vnpy/trader/app/ctaStrategy/strategy/strategyEmaDemo.py b/vnpy/trader/app/ctaStrategy/strategy/strategyEmaDemo.py index 5047c444..e1fee112 100644 --- a/vnpy/trader/app/ctaStrategy/strategy/strategyEmaDemo.py +++ b/vnpy/trader/app/ctaStrategy/strategy/strategyEmaDemo.py @@ -188,6 +188,11 @@ class EmaDemoStrategy(CtaTemplate): # 对于无需做细粒度委托控制的策略,可以忽略onOrder pass + #---------------------------------------------------------------------- + def onStopOrder(self, so): + """停止单推送""" + pass + ######################################################################################## class OrderManagementDemoStrategy(CtaTemplate): @@ -294,3 +299,8 @@ class OrderManagementDemoStrategy(CtaTemplate): """收到成交推送(必须由用户继承实现)""" # 对于无需做细粒度委托控制的策略,可以忽略onOrder pass + + #---------------------------------------------------------------------- + def onStopOrder(self, so): + """停止单推送""" + pass diff --git a/vnpy/trader/app/ctaStrategy/strategy/strategyKingKeltner.py b/vnpy/trader/app/ctaStrategy/strategy/strategyKingKeltner.py index 5681a56d..84163ea3 100644 --- a/vnpy/trader/app/ctaStrategy/strategy/strategyKingKeltner.py +++ b/vnpy/trader/app/ctaStrategy/strategy/strategyKingKeltner.py @@ -278,3 +278,8 @@ class KkStrategy(CtaTemplate): # 将委托号记录到列表中 self.orderList.append(self.buyOrderID) self.orderList.append(self.shortOrderID) + + #---------------------------------------------------------------------- + def onStopOrder(self, so): + """停止单推送""" + pass \ No newline at end of file