-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
ATR introduces look-ahead bias #963
Comments
True, the backtesting.py/backtesting/lib.py Line 466 in 0ce24d8
The assumption was that the ATR for a trailing strategy would be fairly stable. PR fix welcome! |
A simple fix is to do a
this ensures that only the leading (oldest prices chronologically) are backfilled for the purpose of calculating HPR. Also, you may want to consider using the
Last but not least, possibly the f-b fills need to be moved to before the rolling mean calculation, not sure what the data is but if it is prices then it would make more sense:
I need to get more familiarity with the backtesting code before submitting PRs :) |
@EladioRocha thanks a lot for reporting this. Can you please double-check my PR #1230 to make sure that it works for you now? |
@bravegag As far as I understand, ATR is computed from true range, which is computed from OHLC data, which is checked beforehand to contain no NaNs ... backtesting.py/backtesting/lib.py Lines 464 to 466 in 0ce24d8
backtesting.py/backtesting/backtesting.py Lines 1110 to 1113 in 0ce24d8
Therefore, there should be no NaNs in ATR other than the leading ones! Which are |
OK then if you are fine, close the issue and the PR, my fix just makes sure there won't be any LAB without making any assumptions. Also the bfill should come before the rolling. Otherwise any leading NAs will introduce exactly 99 (100-1) NA prices ahead due to the default |
|
Expected Behavior
When we trade with trailing stop loss the ATR is calculated. Fo example suppose that we calculate the ATR for 100 periods the first 100 periods should be removed because they don't have values and if you try to open a trade in the period 10 there isn't an available ATR value therefore isn't possible to determine the stop loss. So in that case all the values that contains NaN values should be removed.
Actual Behavior
In the code when the ATR is calculated the values are backward filled with the future values and according to our previous example if a trade is opened in the period 10, then the ATR used is the ATR in the period 101
Additional info
It maintins the ATR calculation and changes until the period 101
My code is the following
The text was updated successfully, but these errors were encountered: