增加CTA策略末班的停止单状态更新函数
This commit is contained in:
parent
801e3edd1e
commit
e1ca7de271
@ -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):
|
||||
"""考虑到回测中不允许向数据库插入数据,防止实盘交易中的一些代码出错"""
|
||||
|
@ -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):
|
||||
|
@ -90,6 +90,11 @@ class CtaTemplate(object):
|
||||
"""收到Bar推送(必须由用户继承实现)"""
|
||||
raise NotImplementedError
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onStopOrder(self, so):
|
||||
"""收到停止单推送(必须由用户继承实现)"""
|
||||
raise NotImplementedError
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def buy(self, price, volume, stop=False):
|
||||
"""买开"""
|
||||
|
@ -237,3 +237,7 @@ class AtrRsiStrategy(CtaTemplate):
|
||||
# 发出状态更新事件
|
||||
self.putEvent()
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onStopOrder(self, so):
|
||||
"""停止单推送"""
|
||||
pass
|
@ -221,3 +221,8 @@ class DualThrustStrategy(CtaTemplate):
|
||||
def onTrade(self, trade):
|
||||
# 发出状态更新事件
|
||||
self.putEvent()
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onStopOrder(self, so):
|
||||
"""停止单推送"""
|
||||
pass
|
@ -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
|
||||
|
@ -278,3 +278,8 @@ class KkStrategy(CtaTemplate):
|
||||
# 将委托号记录到列表中
|
||||
self.orderList.append(self.buyOrderID)
|
||||
self.orderList.append(self.shortOrderID)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onStopOrder(self, so):
|
||||
"""停止单推送"""
|
||||
pass
|
Loading…
Reference in New Issue
Block a user