You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: Scale overlay indicator columns of FractionalBacktest trades - #1426
FractionalBacktest.run() scales EntryPrice, ExitPrice, SL, TP and the overlay indicators back up after the run, but the Entry_<indicator> / Exit_<indicator> columns in stats._trades stayed at the fractional scale, so they no longer matched the prices next to them:
Those columns are now scaled for overlay indicators too, using the same column names as _stats.py (including the _0, _1 suffixes for multi-dimensional indicators). Added an assertion to test_FractionalBacktest, which fails without the fix; the rest of the suite and flake8 pass.
I ran this branch (pull/1426/head, Python 3.12, current pandas) against a strategy with a one-dimensional overlay (SMA), a two-row overlay (name='band', rolling max over rolling min) and a non-overlay indicator (name='ret', a percent change), with fractional_unit=1/1e6 on BTCUSD['2015':].
The overlay columns now sit in the same units as EntryPrice, including the _0/_1 suffixed ones, which matches the suffix rule in _stats.py lines 82-85 (len(ind) > 1). The non-overlay ret column is untouched, which is what you want: it is a percentage, not a price.
The trades.columns.intersection(cols) guard also covers Entry_/Exit_ columns that are absent when there are no trades (if len(trades_df) and strategy_instance in _stats.py), so an empty run does not raise.
One thing I did not test and the diff doesn't settle: a non-overlay indicator that is still price-denominated, such as an ATR or a High - Low spread. Before this PR those were already left in fractional units in _indicators, and overlay is the only switch the class looks at, so the new trade columns stay consistent with the plotted arrays. If that is intended, a sentence in the FractionalBacktest docstring ("only overlay indicators are rescaled") would save the next reader from finding it by surprise.
Ran this on f403424 (Python 3.12, BTCUSD from 2015, fractional_unit=1/1e6) with a strategy using two overlay SMAs, an SL, and one non-overlay indicator (overlay=False, constant 1). On master, Entry_SMA(C,2) for trade 0 is 0.000274 next to EntryPrice 261.7. With this branch it is 273.945, Exit_SMA(C,3) comes out as 259.82 and the non-overlay column stays 1.0, so the overlay filter is respected.
One thing I could not tell from the diff: the multi-dimensional case (_0, _1 suffixes). The test only covers the single-output SMA. Is there an existing indicator that returns a 2-row array with overlay=True that could be added to the test, so the suffix branch is exercised too?
This branch has not been deployed
No deployments
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
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.
FractionalBacktest.run()scalesEntryPrice,ExitPrice,SL,TPand the overlay indicators back up after the run, but theEntry_<indicator>/Exit_<indicator>columns instats._tradesstayed at the fractional scale, so they no longer matched the prices next to them:Those columns are now scaled for overlay indicators too, using the same column names as
_stats.py(including the_0,_1suffixes for multi-dimensional indicators). Added an assertion totest_FractionalBacktest, which fails without the fix; the rest of the suite and flake8 pass.