[Mod]修改CTA策略模块内置策略中和委托相关的代码细节

This commit is contained in:
vn.py 2017-09-20 15:01:13 +08:00
parent 239f335900
commit 7822d4ae27
4 changed files with 51 additions and 46 deletions

View File

@ -221,9 +221,14 @@ class CtaEngine(object):
else:
price = tick.lowerLimit
so.status = STOPORDER_TRIGGERED
# 发出市价委托
self.sendOrder(so.vtSymbol, so.orderType, price, so.volume, so.strategy)
# 从活动停止单字典中移除该停止单
del self.workingStopOrderDict[so.stopOrderID]
# 更新停止单状态,并通知策略
so.status = STOPORDER_TRIGGERED
so.strategy.onStopOrder(so)
#----------------------------------------------------------------------

View File

@ -212,8 +212,8 @@ class AtrRsiStrategy(CtaTemplate):
# 计算多头移动止损
longStop = self.intraTradeHigh * (1-self.trailingPercent/100)
# 发出本地止损委托,并且把委托号记录下来,用于后续撤单
orderID = self.sell(longStop, abs(self.pos), stop=True)
self.orderList.append(orderID)
l = self.sell(longStop, abs(self.pos), stop=True)
self.orderList.extend(l)
# 持有空头仓位
elif self.pos < 0:
@ -221,8 +221,8 @@ class AtrRsiStrategy(CtaTemplate):
self.intraTradeHigh = bar.high
shortStop = self.intraTradeLow * (1+self.trailingPercent/100)
orderID = self.cover(shortStop, abs(self.pos), stop=True)
self.orderList.append(orderID)
l = self.cover(shortStop, abs(self.pos), stop=True)
self.orderList.extend(l)
# 发出状态更新事件
self.putEvent()

View File

@ -167,47 +167,47 @@ class DualThrustStrategy(CtaTemplate):
if self.pos == 0:
if bar.close > self.dayOpen:
if not self.longEntered:
vtOrderID = self.buy(self.longEntry, self.fixedSize, stop=True)
self.orderList.append(vtOrderID)
l = self.buy(self.longEntry, self.fixedSize, stop=True)
self.orderList.extend(l)
else:
if not self.shortEntered:
vtOrderID = self.short(self.shortEntry, self.fixedSize, stop=True)
self.orderList.append(vtOrderID)
l = self.short(self.shortEntry, self.fixedSize, stop=True)
self.orderList.extend(l)
# 持有多头仓位
elif self.pos > 0:
self.longEntered = True
# 多头止损单
vtOrderID = self.sell(self.shortEntry, self.fixedSize, stop=True)
self.orderList.append(vtOrderID)
l = self.sell(self.shortEntry, self.fixedSize, stop=True)
self.orderList.extend(l)
# 空头开仓单
if not self.shortEntered:
vtOrderID = self.short(self.shortEntry, self.fixedSize, stop=True)
self.orderList.append(vtOrderID)
l = self.short(self.shortEntry, self.fixedSize, stop=True)
self.orderList.extend(l)
# 持有空头仓位
elif self.pos < 0:
self.shortEntered = True
# 空头止损单
vtOrderID = self.cover(self.longEntry, self.fixedSize, stop=True)
self.orderList.append(vtOrderID)
l = self.cover(self.longEntry, self.fixedSize, stop=True)
self.orderList.extend(l)
# 多头开仓单
if not self.longEntered:
vtOrderID = self.buy(self.longEntry, self.fixedSize, stop=True)
self.orderList.append(vtOrderID)
l = self.buy(self.longEntry, self.fixedSize, stop=True)
self.orderList.extend(l)
# 收盘平仓
else:
if self.pos > 0:
vtOrderID = self.sell(bar.close * 0.99, abs(self.pos))
self.orderList.append(vtOrderID)
l = self.sell(bar.close * 0.99, abs(self.pos))
self.orderList.extend(l)
elif self.pos < 0:
vtOrderID = self.cover(bar.close * 1.01, abs(self.pos))
self.orderList.append(vtOrderID)
l = self.cover(bar.close * 1.01, abs(self.pos))
self.orderList.extend(l)
# 发出状态更新事件
self.putEvent()

View File

@ -51,8 +51,8 @@ class KkStrategy(CtaTemplate):
intraTradeHigh = 0 # 持仓期内的最高点
intraTradeLow = 0 # 持仓期内的最低点
buyOrderID = None # OCO委托买入开仓的委托号
shortOrderID = None # OCO委托卖出开仓的委托号
buyOrderIDList = [] # OCO委托买入开仓的委托号
shortOrderIDList = [] # OCO委托卖出开仓的委托号
orderList = [] # 保存委托代码的列表
# 参数列表,保存了参数的名称
@ -220,18 +220,18 @@ class KkStrategy(CtaTemplate):
self.intraTradeHigh = max(self.intraTradeHigh, bar.high)
self.intraTradeLow = bar.low
orderID = self.sell(self.intraTradeHigh*(1-self.trailingPrcnt/100),
l = self.sell(self.intraTradeHigh*(1-self.trailingPrcnt/100),
abs(self.pos), True)
self.orderList.append(orderID)
self.orderList.extend(l)
# 持有空头仓位
elif self.pos < 0:
self.intraTradeHigh = bar.high
self.intraTradeLow = min(self.intraTradeLow, bar.low)
orderID = self.cover(self.intraTradeLow*(1+self.trailingPrcnt/100),
l = self.cover(self.intraTradeLow*(1+self.trailingPrcnt/100),
abs(self.pos), True)
self.orderList.append(orderID)
self.orderList.extend(l)
# 发出状态更新事件
self.putEvent()
@ -243,21 +243,21 @@ class KkStrategy(CtaTemplate):
#----------------------------------------------------------------------
def onTrade(self, trade):
# 多头开仓成交后,撤消空头委托
if self.pos > 0:
self.cancelOrder(self.shortOrderID)
if self.buyOrderID in self.orderList:
self.orderList.remove(self.buyOrderID)
if self.shortOrderID in self.orderList:
self.orderList.remove(self.shortOrderID)
# 反之同样
elif self.pos < 0:
self.cancelOrder(self.buyOrderID)
if self.buyOrderID in self.orderList:
self.orderList.remove(self.buyOrderID)
if self.shortOrderID in self.orderList:
self.orderList.remove(self.shortOrderID)
if self.pos != 0:
# 多头开仓成交后,撤消空头委托
if self.pos > 0:
for shortOrderID in self.shortOrderIDList:
self.cancelOrder(shortOrderID)
# 反之同样
elif self.pos < 0:
for buyOrderID in self.buyOrderIDList:
self.cancelOrder(buyOrderID)
# 移除委托号
for orderID in (self.buyOrderIDList + self.shortOrderIDList):
if orderID in self.orderList:
self.orderList.remove(orderID)
# 发出状态更新事件
self.putEvent()
@ -272,12 +272,12 @@ class KkStrategy(CtaTemplate):
3. 一个方向的停止单成交后会立即撤消另一个方向的
"""
# 发送双边的停止单委托,并记录委托号
self.buyOrderID = self.buy(buyPrice, volume, True)
self.shortOrderID = self.short(shortPrice, volume, True)
self.buyOrderIDList = self.buy(buyPrice, volume, True)
self.shortOrderIDList = self.short(shortPrice, volume, True)
# 将委托号记录到列表中
self.orderList.append(self.buyOrderID)
self.orderList.append(self.shortOrderID)
self.orderList.extend(self.buyOrderIDList)
self.orderList.extend(self.shortOrderIDList)
#----------------------------------------------------------------------
def onStopOrder(self, so):