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

Conversation

zlpatel
Copy link
Contributor

@zlpatel zlpatel commented Apr 27, 2021

Fixes #316

I have found the culprit. It's self.__atr[-1].

/usr/local/lib/python3.8/site-packages/backtesting/lib.py(433)set_atr_periods()
-> self.__atr = atr
(Pdb) pp atr[-1]
62.03802490234375

/usr/local/lib/python3.8/site-packages/backtesting/lib.py(440)set_trailing_sl()
-> self.__n_atr = n_atr
(Pdb) pp n_atr
1.5

/usr/local/lib/python3.8/site-packages/backtesting/lib.py(446)next()
-> trade.sl = max(trade.sl or -np.inf,
(Pdb) pp self.data.Close[-1]
22.170000076293945

so per the formula: trade.sl = max(trade.sl or -np.inf, self.data.Close[-1] - self.__atr[-1] * self.__n_atr)

22.17 - (62.04*1.5) = -70.89

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

Because, when self.data in init() is different from self.data in next(). self.data in init() has all the data. while the one in next() gets accumulated data over iterations. i.e. 5 iterations -> 5 data. n iteration -> n data. so doing self.data.Close[-1] in next() gets you the last record. but the same is not true for self.__atr, because self.__atr always has full computed set from init(), so you need to use absolute index while accessing atr value from self.__atr.

@@ -15,6 +15,8 @@ def _read_file(filename):
EURUSD = _read_file('EURUSD.csv')
"""DataFrame of hourly EUR/USD forex data from April 2017 to February 2018."""

AMZN = _read_file('AMZN.csv')
"""DataFrame of daily NASDAQ:AMZN (Amazon) stock price data from 15/05/1997 to 23/04/2021."""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm quite opposed to vendoring a new data set in a bug fix like this. I meant if you're able to easily construct a small, contrived unit test / test case, i.e., something that fails on atr[-1], but not with atr[index]. Otherwise, no test is preferred to this. 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea. It's little difficult to test atr[-1] unless I write that piece of code out in the test case to do the computation But then it won't really be considered as a unit test for the TrailingStrategy. Rather it would be a logic test. Is it okay to skip the test for this one then? I can add descriptive comment in that line of code as to why I am using index instead of -1.

@zlpatel
Copy link
Contributor Author

zlpatel commented Apr 29, 2021

@kernc It passed all the checks! What are the next steps?

@kernc
Copy link
Owner

kernc commented Apr 29, 2021

Merging. Many thanks for taking the time to investigate and fix this! 🍰

@kernc kernc changed the title AssertionError while using TrailingStrategy kernc#316 AssertionError while using TrailingStrategy Apr 29, 2021
@kernc kernc merged commit 0a76e96 into kernc:master Apr 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AssertionError while using TrailingStrategy
2 participants