OPEN-SOURCE SCRIPT
Zaktualizowano

Mean-Reversion Half-Life [RC Tools]

288
RC Tools — Mean-Reversion Half-Life
────────────────────────────────────────────────────────────────────

█ OVERVIEW

Knowing a market is "mean-reverting" isn't enough to trade it — a series that takes 5 bars to snap back and one that takes 50 bars to snap back are both technically mean-reverting, but call for completely different holding periods and expectations. This tool estimates that missing number directly: the half-life, in bars, of mean reversion, using the same Ornstein-Uhlenbeck-style regression approach used in quantitative statistical-arbitrage research.

█ WHAT IT DOES

Estimates mean-reversion speed via a rolling OLS regression and classifies each confirmed bar as Fast Mean-Reversion, Slow Mean-Reversion, or No Mean-Reversion based on the resulting half-life. Colours the chart background accordingly, plots the half-life (in bars, capped for readable scale) as a histogram against Fast and No-Reversion threshold lines, and shows a table with the current state, its estimated half-life, and historical base rates (average forward return and win rate) for each state.

█ THE THEORY BEHIND IT

This is a companion tool to RC Tools' Hurst Exponent Regime script, and answers the natural follow-up question it leaves open. The Hurst Exponent tells you whether a market's statistical character is trending, mean-reverting, or a random walk — but it doesn't say how fast a mean-reverting move actually closes. This tool fits a simple version of the Ornstein-Uhlenbeck model, a classical stochastic process used to describe a quantity that drifts back toward a long-run mean at a speed proportional to its current distance from that mean, and converts the fitted speed into a half-life: the number of bars it takes, on average, to close half the current deviation.

█ HOW IT IS CALCULATED

1. Take the log of price, and regress its bar-to-bar change against its own prior level (a one-lag OLS regression): Δy = α + β·y(prior), where y is log price.
2. A negative β implies mean reversion — the more negative, the faster the pull back toward the mean. A β at or above zero implies no reversion (the series is trending or behaving like a random walk).
3. Convert β into a half-life: -ln(2) / β, in bars.
4. Classify: half-life at or below the Fast threshold (default 10 bars) → Fast Mean-Reversion. Above that but at or below the No-Reversion threshold (default 60 bars) → Slow Mean-Reversion. Above the cap, or β non-negative → No Mean-Reversion (the series isn't reliably reverting, or the estimate is too unstable to trust).

β is smoothed before the half-life calculation (rather than smoothing half-life itself), because half-life is numerically unstable near a zero slope and can spike to extreme values that a direct smoothing pass wouldn't tame cleanly.

Classification occurs ONLY on confirmed bar close — the plotted half-life, the background colour and the table all update together, so nothing here can disagree mid-bar or flip back and forth as the current bar forms.

█ SETTINGS & CONFIGURATION

• Source (default close)
• Regression Window (default 50 bars) — longer windows give a more stable estimate but react slower to a genuine regime change
• Fast Mean-Reversion Threshold (default 10 bars) and No-Reversion Threshold (default 60 bars) — the half-life cutoffs between the three states
• Smoothing Length and Type (default 3-period EMA) — applied to the regression slope, not the half-life itself
• Forward Return Window (default 20 bars) — the horizon used for the base-rate table
• Table visibility, position and colours are fully configurable; the main-chart background painting can be toggled off if you only want the statistics pane

█ HOW TO USE IT

Use it to calibrate holding periods and expectations for a mean-reversion approach, not as a standalone entry signal. A Fast Mean-Reversion reading suggests a short-holding-period approach is appropriate; a Slow reading suggests patience is required and tight stops may cut off the reversion before it completes; a No Mean-Reversion reading suggests a mean-reversion approach isn't currently well-suited to this market at all. Pairs naturally with the Hurst Exponent Regime tool — Hurst tells you IF the market's character favours mean reversion, this tool tells you roughly HOW FAST.

Works on any asset and timeframe with sufficient history for the Regression Window.

█ LIMITATIONS

• This is a SIMPLE linear (OLS) estimate of mean-reversion speed, not a full maximum-likelihood Ornstein-Uhlenbeck fit. It is a practical approximation, not a research-grade estimator.
• Half-life describes an estimated SPEED, not a guarantee of reversion — a series classified as mean-reverting can still trend away for an extended period before, or instead of, reverting.
• Near a regression slope of zero, the raw half-life estimate is numerically unstable and can spike to very large values; display values are capped for readability.
• The regression window assumes the mean-reversion relationship is roughly stable across the window; a structural break partway through will distort the estimate until it fully rolls off.
• Shorter windows react faster but produce noisier, less reliable estimates; longer windows are more stable but slower to reflect a genuine change.
• Historical base-rate stats need a meaningful sample count (check N) before being trusted, especially for the less common states.
• This script does NOT repaint. All classification updates on confirmed bar close only.

█ DISCLAIMER

For educational and informational purposes only. Nothing here is financial advice. Past behaviour of any mean-reversion state does not indicate future results. Trade at your own risk.
Informacje o Wersji
Removed Performance table

Wyłączenie odpowiedzialności

Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.