-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Trailing pct instead of ATR #223
Comments
This is the whole of backtesting.py/backtesting/lib.py Lines 406 to 450 in 1ee5670
It should be fairly simple to adapt/duplicate one for percent values. |
Yup, the trailing strategy only supports ATR multiples, but most of the cyrpto bots trail in percentage would be nice to implement as option for trailing strategy so you can choose between ATR or pct for backtesting the best trailing. |
As a quick workaround, something like this might work: def pct_to_atr(pct: float, data: pd.DataFrame):
"""Express `pct` change in units of ATR."""
assert 0 < pct < 1
h, l, c_prev = data.High, data.Low, pd.Series(data.Close).shift(1)
tr = np.max([h - l, (c_prev - h).abs(), (c_prev - l).abs()], axis=0)
atr = np.nanmean(tr)
return (pct * data.Close.iloc[-1]) / atr |
Actually i would need the oppsite, as with backtesting i can find the optimal atr value for trailing the will have to calculate the equivalent pct for program the bot. |
Should be rather easy to invert, something like: def atr_to_pct(n_atr: float, data: pd.DataFrame):
"""Express `n_atr` in units of ATR as percentage of current price."""
h, l, c_prev = data.High, data.Low, pd.Series(data.Close).shift(1)
tr = np.max([h - l, (c_prev - h).abs(), (c_prev - l).abs()], axis=0)
atr = np.nanmean(tr)
return n_atr * atr / data.Close.iloc[-1] |
I am working on this enhancement. |
Add code description and use float type for default percentage value.
Trailing pct instead of ATR kernc#223
Trailing pct instead of ATR kernc#223
@kernc can you review this change? |
Expected Behavior
Allow trailing with PRC instead of ATR
Actual Behavior
Currently the backtesting allow trailing sels only based on ATR, most of crypto bots works in pct trailing (pct for trailing activation and then pct trailing for position close).
The text was updated successfully, but these errors were encountered: