-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
AssertionError while using TrailingStrategy #316
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
Comments
These are the culprit lines in backtesting.py/backtesting/lib.py Lines 446 to 447 in 0b2325f
The way max() can produce a value for which assert price is None or 0 < price < np.inf fails is if trade.sl is unset (you don't pass initial self.buy(..., sl=) ) and self.data.Close[-1] - self.__atr[-1] * self.__n_atr is negative.Meaning, the ATR in $ is greater than the price in $. Can you propose a fix? |
@kernc I looked at that code earlier. Still not sure how this code would return negative? I changed the ATR period to 20 and sl to 1.5 ATR. Now it fails on Index 1439 for AMZN. I tried printing the ATR value and Close value for index 1439. Values are as below.
22.05 - (0.87*1.5) = 20.745 which is a positive value. The only difference is that I use ta-lib ATR function while your api uses internal implementation to calculate ATR: backtesting.py/backtesting/lib.py Lines 425 to 433 in 0b2325f
Am I missing something here? |
🤔 Ideally, someone would fire up a debugger and place a breakpoint here, then see what's what, and why the assertion fails. |
@kernc so I started the debugger. price value is coming out as -70.88 and hence the AssertionError. I am still checking the price calculation. |
You can always inject a little helper code: if price < 0: # our special condition of interest
import ipdb; ipdb.set_trace() Then you can do an |
@kernc I have found the culprit. It's
so per the formula: 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 Is my understanding correct? |
@kernc do you want me to make changes and submit a merge request? |
Excellent work! Your proposed fix sounds good. If you'd like to submit a PR, that would be splendid! Don't forget a small, contrived unit/regression test. |
@kernc thanks. do I need to have write permission to this repo? |
@kernc oh nevermind. it says it will create a new branch. |
the code shouldn't be using atr[-1]. It should be using atr[index] where index = len(self.data)-1
* 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
@kernc I ran into this with some other data. Below are the values I printed from code. It fails on Index 84. Because 0.375 - (0.20375*2) = -0.0325 Notice atr values for past 2 indexes as well. May be we should look into how ATR is being calculated?
Here's how atr array looks like:
May be somewhere values are getting rounded off in ATR calculation as they might be too close to each other? Below is the code I am using `class DemaCross10_25(SignalStrategy,TrailingStrategy):
Attaching data here if you want to test it. |
I am seeing this error when try to run backtest with 10000 days worth data. It doesn't happen for all the stocks, but some. e.g AAPL, AMZN.
I tried printing the index values for each iteration and I found that for this dataset, it processed till index 1951 and failed on 1952.
If I reduce the backtesting period to 5000, it works because it doesn't come across that piece of data (index 1952). I checked that data and verified the close price, and calculated ATR(14).
Below is the code snippet:
I download data using yfinance python api. Any help will be greatly appreciated.
Originally posted by @zlpatel in #315
The text was updated successfully, but these errors were encountered: