-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Conversation
the code shouldn't be using atr[-1]. It should be using atr[index] where index = len(self.data)-1
backtesting/test/__init__.py
Outdated
@@ -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.""" |
There was a problem hiding this comment.
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. 😬
There was a problem hiding this comment.
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.
@kernc It passed all the checks! What are the next steps? |
Merging. Many thanks for taking the time to investigate and fix this! 🍰 |
Fixes #316
I have found the culprit. It's
self.__atr[-1]
.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
ininit()
is different from self.data innext()
.self.data
ininit()
has all the data. while the one innext()
gets accumulated data over iterations. i.e. 5 iterations -> 5 data. n iteration -> n data. so doingself.data.Close[-1]
innext()
gets you the last record. but the same is not true forself.__atr
, becauseself.__atr
always has full computed set frominit()
, so you need to use absolute index while accessing atr value fromself.__atr
.