Add warmup=False option to Strategy.I() to exclude an indicator from forced backtest warm-up (#1339) - #1404
Open
Harsh4r0ra wants to merge 1 commit into
Open
Add warmup=False option to Strategy.I() to exclude an indicator from forced backtest warm-up (#1339)#1404Harsh4r0ra wants to merge 1 commit into
warmup=False option to Strategy.I() to exclude an indicator from forced backtest warm-up (#1339)#1404Harsh4r0ra wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Backtest.run()currently determines the first bar on whichStrategy.next()is called by taking the max over all declared indicators of the index
of their first non-NaN value (
_indicator_warmup_nbars()in_util.py).In other words, one slow-to-warm-up indicator delays the whole backtest,
even if that indicator isn't actually needed for trading decisions at that
point.
This becomes a real problem when mixing timeframes with
backtesting.lib.resample_apply(). As reported in #1339, a strategy runningon 30-minute bars that also computes a 100-period SMA on the resampled
daily timeframe ends up with ~100 days worth of NaNs at the base
resolution — i.e. thousands of 30-min bars — before the backtest places a
single order, even though the strategy's actual entry/exit logic only
depends on fast, lower-timeframe indicators. Users currently have no way to
tell
backtesting.py"this indicator is secondary/informational, don't letits warm-up gate the whole run."
Fix
Added a
warmup: bool = Truekeyword argument toStrategy.I():Summary
declared indicator has non-NaN values, which is painful for indicators
computed on a higher/resampled time frame via
resample_apply(theirwarm-up in base-timeframe bars can be huge) when those indicators aren't
actually required from the very first bar.
warmup: bool = Trueparameter toStrategy.I(). Indicatorsdeclared with
warmup=Falseare excluded from the automatic warm-up-barcalculation in
_indicator_warmup_nbars, so the backtest can start as soonas its required indicators are ready.
resample_apply(..., warmup=False)since it forwards unrecognized kwargs to
Strategy.I().Test plan
TestStrategy.test_indicator_warmup_false_excluded_from_start_barpytest backtesting/test/_test.py, 81 passed)flake8clean