From e1ca7de271ddfd28f71aec9814f816b5d199f000 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 10 Jul 2017 20:39:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0CTA=E7=AD=96=E7=95=A5?= =?UTF-8?q?=E6=9C=AB=E7=8F=AD=E7=9A=84=E5=81=9C=E6=AD=A2=E5=8D=95=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=9B=B4=E6=96=B0=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/trader/app/ctaStrategy/ctaBacktesting.py | 25 +++++++++++-------- vnpy/trader/app/ctaStrategy/ctaEngine.py | 5 ++++ vnpy/trader/app/ctaStrategy/ctaTemplate.py | 5 ++++ .../ctaStrategy/strategy/strategyAtrRsi.py | 4 +++ .../strategy/strategyDualThrust.py | 5 ++++ .../ctaStrategy/strategy/strategyEmaDemo.py | 10 ++++++++ .../strategy/strategyKingKeltner.py | 5 ++++ 7 files changed, 49 insertions(+), 10 deletions(-) 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