OPEN-SOURCE SCRIPT
Mean Reversion Half-Life & Spread Tracker [OnlyFibonacci] v3.0

Mean Reversion Half-Life & Spread Tracker [OnlyFibonacci] v3.0
A statistical mean-reversion oscillator for spread analysis — Z-Score normalization, closure-time tracking, confirmed signals, target price projection, and a real-time dashboard. Built in Pine Script v6.
---
What does this indicator do?
Mean Reversion Half-Life & Spread Tracker measures how far a price or spread has deviated from its statistical mean using a Z-Score oscillator. Beyond simple overbought/oversold readings, it tracks how long extreme deviations typically take to revert to zero, whether the current deviation is lasting longer than average, where price may revert if Z-Score returns to 0, and the historical success rate of confirmed signals.
Default mode: Asset vs Single Moving Average (Mean Reversion) — the chart symbol is normalized against a configurable SMA.
---
Core Features
---
Dashboard Table
Live metrics displayed in the top-right corner:
Status indicators: Normal, Active Trade, Warning.
---
How to Use
---
How to Interpret
---
Recommended Use Cases
---
Customizable Inputs
---
Important Notes
---
Keywords
Mean Reversion, Z-Score, Half-Life, Spread Tracker, Pair Trading, Statistical Arbitrage, Oscillator, SMA, Closure Time, Time-Stop, Pine Script v6, OnlyFibonacci
---
Developed with Pine Script v6. For analysis and education only.
A statistical mean-reversion oscillator for spread analysis — Z-Score normalization, closure-time tracking, confirmed signals, target price projection, and a real-time dashboard. Built in Pine Script v6.
This indicator is for educational and analytical purposes only. It does not constitute investment advice, financial advice, or a trading recommendation. Past signal performance does not guarantee future results. Always do your own research and manage risk responsibly.
---
What does this indicator do?
Mean Reversion Half-Life & Spread Tracker measures how far a price or spread has deviated from its statistical mean using a Z-Score oscillator. Beyond simple overbought/oversold readings, it tracks how long extreme deviations typically take to revert to zero, whether the current deviation is lasting longer than average, where price may revert if Z-Score returns to 0, and the historical success rate of confirmed signals.
Default mode: Asset vs Single Moving Average (Mean Reversion) — the chart symbol is normalized against a configurable SMA.
---
Core Features
- Two Operational Modes
- Asset vs SMA (default): Analyzes Close / SMA ratio for single-asset mean reversion
- Pair Trading: Analyzes chart symbol (Asset A) divided by a secondary symbol (Asset B)
- Z-Score Engine
- Spread Ratio = Close/SMA or Close/Asset B
- Z-Score = (Ratio − Rolling Mean) / Rolling Standard Deviation
- Default lookback: 200 bars
- EMA(3) smoothing applied to raw Z-Score to reduce whipsaw noise
- Threshold Levels
- Upper threshold: +2.3 (statistical overbought zone)
- Lower threshold: −2.3 (statistical oversold zone)
- Center line: 0.0 (equilibrium / mean)
- Mean Closure Time
- Tracks the average number of bars required for Z-Score to return to 0 after breaching ±2.3
- Displays active spread duration in real time
- Triggers a Time-Stop WARNING when duration exceeds the historical average
- Confirmed Signal Logic
- BUY: Smoothed Z-Score crosses above −2.3 and holds beyond the threshold for at least 1 full confirmed bar close (bullish mean reversion)
- SELL: Smoothed Z-Score crosses below +2.3 and holds beyond the threshold for at least 1 full confirmed bar close (bearish mean reversion)
- EXIT: Z-Score reaches 0, time-stop is triggered, or duration exceeds 2× average closure time
- Signal Win Rate (%)
- Historical success rate of confirmed signals
- Win: Z-Score returns to 0 before exceeding 2× average closure time
- Loss: Timeout or time-stop triggered before mean reversion completes
- Target Price Level
- Estimated chart price where Z-Score would equal 0
- MA mode: RatioMean × SMA
- Pair mode: RatioMean × Asset B price
- Optional dynamic dashed projection line on the main chart while a signal is active
- Visual Design (v3.0)
- Dynamic gradient Z-Score line (red above 0, green below 0, neon tones at extremes)
- Gradient-filled area between Z-Score and the zero line
- Soft background glow in extreme zones
- Modern dark-theme dashboard table with live status indicators
- Built-in Alerts
- Upper / Lower Threshold Breach
- Time-Stop Warning
- Bullish Mean Reversion BUY
- Bearish Mean Reversion SELL
- Signal EXIT
---
Dashboard Table
Live metrics displayed in the top-right corner:
- Pair / Mode — active analysis configuration
- Current Z-Score — real-time smoothed reading
- Target Price Level — estimated Z=0 price
- Avg Closure Time — historical mean reversion duration (bars)
- Spread Duration — active deviation duration
- Time-Stop Status — Normal or Warning
- Signal Win Rate (%) — historical confirmed signal success rate
Status indicators: Normal, Active Trade, Warning.
---
How to Use
- Add the indicator to your chart (separate oscillator pane).
- In default Asset vs SMA mode, set the SMA length (default: 50).
- For pair analysis, switch to Pair Trading mode and select Asset B.
- Monitor the Z-Score panel:
- Z > +2.3 → spread is statistically extended above mean (potential bearish mean reversion)
- Z < −2.3 → spread is statistically extended below mean (potential bullish mean reversion)
- Z ≈ 0 → statistical equilibrium
- Compare Spread Duration against Avg Closure Time in the dashboard.
- If Time-Stop shows WARNING, the deviation may be persisting longer than historically normal — review risk management.
- Set alerts for threshold breaches, confirmed signals, and exits.
---
How to Interpret
- Mean reversion concept: When price or spread reaches statistical extremes, it tends to revert toward its rolling mean over time.
- Z-Score: Measures deviation in standard deviation units. ±2.3 represents a strong statistical extreme.
- Avg Closure Time: How long past extreme deviations took to revert to zero. If current duration exceeds this, caution is warranted.
- Target Price: A dynamic estimate of where price may revert if Z-Score normalizes — a reference level, not a guaranteed target.
- Win Rate: A summary of past confirmed signal outcomes. Not a promise of future performance.
- Gradient fill: Visually emphasizes the magnitude and direction of deviation from equilibrium.
Horizontal dashed line on the main chart (if enabled): Projects the estimated price level where Z-Score = 0 while a BUY or SELL signal is active. It updates dynamically and disappears on EXIT or target reached. Disable via Show Target Price Projection Line.
---
Recommended Use Cases
- Single-asset mean reversion analysis vs SMA
- Crypto, forex, and equity pair spread monitoring
- Multi-timeframe confluence checks
- Alert-based watchlist monitoring
- Time-stop and duration-based risk awareness
---
Customizable Inputs
- Mode, Asset B Symbol, MA Length
- Z-Score Lookback (200), EMA Smoothing (3)
- Upper/Lower Threshold (±2.3)
- Signal Confirmation Bars (1–5)
- Gradient Area Fill, Background Glow, Target Line, Dashboard
---
Important Notes
- Non-repainting security calls: gaps_off, lookahead_off
- This is an analysis tool — it does not execute trades automatically
- Parameters may require optimization across different markets and timeframes
- Win rate and closure time are based on historical data and may differ in live conditions
- Always use proper position sizing and risk management
---
Keywords
Mean Reversion, Z-Score, Half-Life, Spread Tracker, Pair Trading, Statistical Arbitrage, Oscillator, SMA, Closure Time, Time-Stop, Pine Script v6, OnlyFibonacci
---
Developed with Pine Script v6. For analysis and education only.
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
Love for AlgoTrading
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
Love for AlgoTrading
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。