[Mod]修复之前海龟策略回测中的每日盈亏计算bug
This commit is contained in:
parent
7675bf8a2e
commit
270847132a
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -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线数据回放结束')
|
||||
|
||||
|
@ -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
|
||||
@ -112,7 +113,7 @@ class TurtleSignal(object):
|
||||
if bar.high >= shortExit:
|
||||
self.cover(shortExit)
|
||||
return
|
||||
|
||||
|
||||
# 没有仓位或者持有多头仓位的时候,可以做多(加仓)
|
||||
if self.unit >= 0:
|
||||
trade = False
|
||||
@ -164,12 +165,14 @@ 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)
|
||||
|
@ -115,7 +115,9 @@ class TurtleTradingStrategy(CtaTemplate):
|
||||
# 计算指标数值
|
||||
self.entryUp, self.entryDown = self.am.donchian(self.entryWindow)
|
||||
self.exitUp, self.exitDown = self.am.donchian(self.exitWindow)
|
||||
self.atrVolatility = self.am.atr(self.atrWindow)
|
||||
|
||||
if not self.pos:
|
||||
self.atrVolatility = self.am.atr(self.atrWindow)
|
||||
|
||||
# 判断是否要进行交易
|
||||
if self.pos == 0:
|
||||
@ -132,19 +134,17 @@ 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()
|
||||
|
Loading…
Reference in New Issue
Block a user