Skip to content

Commit 0a76e96

Browse files
authored
BUG: Fix price AssertionError while using TrailingStrategy (#322)
* 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
1 parent 0b2325f commit 0a76e96

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

backtesting/lib.py

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

442442
def next(self):
443443
super().next()
444+
# Can't use index=-1 because self.__atr is not an Indicator type
445+
index = len(self.data)-1
444446
for trade in self.trades:
445447
if trade.is_long:
446448
trade.sl = max(trade.sl or -np.inf,
447-
self.data.Close[-1] - self.__atr[-1] * self.__n_atr)
449+
self.data.Close[index] - self.__atr[index] * self.__n_atr)
448450
else:
449451
trade.sl = min(trade.sl or np.inf,
450-
self.data.Close[-1] + self.__atr[-1] * self.__n_atr)
452+
self.data.Close[index] + self.__atr[index] * self.__n_atr)
451453

452454

453455
# Prevent pdoc3 documenting __init__ signature of Strategy subclasses

backtesting/test/_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ def next(self):
860860
self.buy()
861861

862862
stats = Backtest(GOOG, S).run()
863-
self.assertEqual(stats['# Trades'], 51)
863+
self.assertEqual(stats['# Trades'], 57)
864864

865865

866866
class TestUtil(TestCase):

0 commit comments

Comments
 (0)