Skip to content

Commit

Permalink
BUG: Fix price AssertionError while using TrailingStrategy (#322)
Browse files Browse the repository at this point in the history
* AssertionError while using TrailingStrategy #316 

the code shouldn't be using atr[-1]. It should be using atr[index] where index = len(self.data)-1

* AssertionError while using TrailingStrategy  - Adding Unit Test #316

* Adding AMZN test data file #316

* Added AMZN data #316

* Fix inconsistent tabs issue #316

* Removed Tabs and used Spaces #316

* Backing out additional test case #316

* Delete AMZN.csv file #316

* Remove AMZN data import #316

* Add code comment for change #316

* Update backtesting/lib.py

* Added extra line as lint was complaining #316

* Added extra line as lint was complaining #316

* Added extra line as lint was complaining #316
  • Loading branch information
zlpatel committed Apr 29, 2021
1 parent 0b2325f commit 0a76e96
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
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

2 comments on commit 0a76e96

@rmy084

This comment was marked as off-topic.

@rmy084

This comment was marked as off-topic.

Please sign in to comment.