Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions backtesting/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,15 @@ def set_trailing_sl(self, n_atr: float = 6):

def next(self):
super().next()
# Can't use index=-1 because self.__atr is not an Indicator type
index = len(self.data)-1
for trade in self.trades:
if trade.is_long:
trade.sl = max(trade.sl or -np.inf,
self.data.Close[-1] - self.__atr[-1] * self.__n_atr)
self.data.Close[index] - self.__atr[index] * self.__n_atr)
else:
trade.sl = min(trade.sl or np.inf,
self.data.Close[-1] + self.__atr[-1] * self.__n_atr)
self.data.Close[index] + self.__atr[index] * self.__n_atr)


# Prevent pdoc3 documenting __init__ signature of Strategy subclasses
Expand Down
2 changes: 1 addition & 1 deletion backtesting/test/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ def next(self):
self.buy()

stats = Backtest(GOOG, S).run()
self.assertEqual(stats['# Trades'], 51)
self.assertEqual(stats['# Trades'], 57)


class TestUtil(TestCase):
Expand Down