Skip to content

Commit 5a59a97

Browse files
authored
AssertionError while using TrailingStrategy kernc#316
the code shouldn't be using atr[-1]. It should be using atr[index] where index = len(self.data)-1
1 parent 0b2325f commit 5a59a97

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

backtesting/lib.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,14 @@ def set_trailing_sl(self, n_atr: float = 6):
441441

442442
def next(self):
443443
super().next()
444+
index = len(self.data)-1
444445
for trade in self.trades:
445446
if trade.is_long:
446447
trade.sl = max(trade.sl or -np.inf,
447-
self.data.Close[-1] - self.__atr[-1] * self.__n_atr)
448+
self.data.Close[index] - self.__atr[index] * self.__n_atr)
448449
else:
449450
trade.sl = min(trade.sl or np.inf,
450-
self.data.Close[-1] + self.__atr[-1] * self.__n_atr)
451+
self.data.Close[index] + self.__atr[index] * self.__n_atr)
451452

452453

453454
# Prevent pdoc3 documenting __init__ signature of Strategy subclasses

0 commit comments

Comments
 (0)