[Mod]修复之前海龟策略回测中的每日盈亏计算bug

This commit is contained in:
vn.py 2018-11-13 16:11:35 +08:00
parent 7675bf8a2e
commit 270847132a
5 changed files with 682 additions and 1281 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -108,18 +108,18 @@ class BacktestingEngine(object):
for dt, barDict in self.dataDict.items():
self.currentDt = dt
result = DailyResult(dt)
result.updatePos(self.portfolio.posDict)
previousResult = self.result
self.result = DailyResult(dt)
self.result.updatePos(self.portfolio.posDict)
self.resultList.append(self.result)
if previousResult:
self.result.updatePreviousClose(previousResult.closeDict)
for bar in barDict.values():
self.portfolio.onBar(bar)
result.updateBar(bar)
if self.result:
result.updatePreviousClose(self.result.closeDict)
self.resultList.append(result)
self.result = result
self.result.updateBar(bar)
self.writeLog(u'K线数据回放结束')

View File

@ -104,6 +104,7 @@ class TurtleSignal(object):
# 优先检查平仓
if self.unit > 0:
longExit = max(self.longStop, self.exitDown)
if bar.low <= longExit:
self.sell(longExit)
return
@ -164,11 +165,13 @@ class TurtleSignal(object):
self.longEntry2 = self.entryUp + self.atrVolatility * 0.5
self.longEntry3 = self.entryUp + self.atrVolatility * 1
self.longEntry4 = self.entryUp + self.atrVolatility * 1.5
self.longStop = 0
self.shortEntry1 = self.entryDown
self.shortEntry2 = self.entryDown - self.atrVolatility * 0.5
self.shortEntry3 = self.entryDown - self.atrVolatility * 1
self.shortEntry4 = self.entryDown - self.atrVolatility * 1.5
self.shortStop = 0
#----------------------------------------------------------------------
def newSignal(self, direction, offset, price, volume):
@ -391,5 +394,6 @@ class TurtlePortfolio(object):
self.totalShort += unit
# 向回测引擎中发单记录
#self.engine.sendOrder(vtSymbol, direction, offset, price, volume)
self.engine.sendOrder(vtSymbol, direction, offset, price, volume*multiplier)

View File

@ -115,6 +115,8 @@ class TurtleTradingStrategy(CtaTemplate):
# 计算指标数值
self.entryUp, self.entryDown = self.am.donchian(self.entryWindow)
self.exitUp, self.exitDown = self.am.donchian(self.exitWindow)
if not self.pos:
self.atrVolatility = self.am.atr(self.atrWindow)
# 判断是否要进行交易
@ -132,18 +134,16 @@ class TurtleTradingStrategy(CtaTemplate):
self.sendBuyOrders(self.longEntry)
# 止损逻辑
self.longStop = self.longEntry - self.atrVolatility * 2
self.longStop = max(self.longStop, self.exitDown)
self.sell(self.longStop, abs(self.pos), True)
sellPrice = max(self.longStop, self.exitDown)
self.sell(sellPrice, abs(self.pos), True)
elif self.pos < 0:
# 加仓逻辑
self.sendShortOrders(self.shortEntry)
# 止损逻辑
self.shortStop = self.shortEntry + self.atrVolatility * 2
self.shortStop = min(self.shortStop, self.exitUp)
self.cover(self.shortStop, abs(self.pos), True)
coverPrice = min(self.shortStop, self.exitUp)
self.cover(coverPrice, abs(self.pos), True)
# 同步数据到数据库
self.saveSyncData()
@ -161,8 +161,10 @@ class TurtleTradingStrategy(CtaTemplate):
"""成交推送"""
if trade.direction == DIRECTION_LONG:
self.longEntry = trade.price
self.longStop = self.longEntry - self.atrVolatility * 2
else:
self.shortEntry = trade.price
self.shortStop = self.shortEntry + self.atrVolatility * 2
# 发出状态更新事件
self.putEvent()