Skip to content

AssertionError while using TrailingStrategy #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 29, 2021
Merged
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