OPEN-SOURCE SCRIPT

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

7 715
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.

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:

  1. Pair / Mode — active analysis configuration
  2. Current Z-Score — real-time smoothed reading
  3. Target Price Level — estimated Z=0 price
  4. Avg Closure Time — historical mean reversion duration (bars)
  5. Spread Duration — active deviation duration
  6. Time-Stop Status — Normal or Warning
  7. Signal Win Rate (%) — historical confirmed signal success rate


Status indicators: Normal, Active Trade, Warning.

---

How to Use

  1. Add the indicator to your chart (separate oscillator pane).
  2. In default Asset vs SMA mode, set the SMA length (default: 50).
  3. For pair analysis, switch to Pair Trading mode and select Asset B.
  4. 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
  5. Compare Spread Duration against Avg Closure Time in the dashboard.
  6. If Time-Stop shows WARNING, the deviation may be persisting longer than historically normal — review risk management.
  7. 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が提供または承認する金融、投資、取引、またはその他の種類の助言もしくは推奨であることを意図したものではなく、またこれらに該当するものでもありません。詳細は利用規約をご覧ください。