OPEN-SOURCE SCRIPT

Renaissance Mean Reversion [JOAT]

3 395
RENAISSANCE MEAN REVERSION [JOAT]

A tribute to the Medallion-style statistical-arbitrage approach: do not trade price — trade the spread between price and its smoothed expectation, measure how reliably that spread mean-reverts using an AR(1) half-life regression, and only fire signals when the half-life is fast enough to be tradeable. The result is a discipline-enforcing engine that says no more often than yes: it refuses to take a reversion trade until the spread shows it actually reverts.

snapshot

The synthetic spread

The script builds a synthetic pair from two views of the same instrument:

  • Leg 1 — current price (configurable source).
  • Leg 2 — long EMA of price (default 50-bar; configurable).
  • Spread = price − long EMA (or ln(price) − ln(EMA) when log-spread mode is on, removing scale).


The spread is then Z-scored over a configurable lookback (default 100 bars) to produce a stationary stationary signal that says: "how many standard deviations is the spread from its own mean right now?". This is the textbook stat-arb construction, single-leg version.

Half-life regression — the gate

This is what makes the script institutional rather than retail. Reversion is meaningless if the spread does not actually revert. The script fits an AR(1) regression over a configurable window (default 120 bars):

Δspread_t = α + β · spread_{t−1} + ε_t

The half-life of mean reversion is then:

HL = −ln(2) / ln(1 + β)

When β is negative and close to zero, HL is short — the spread reverts quickly. When β approaches −1, HL is huge — the spread barely reverts. When β is positive, the spread is anti-mean-reverting (trending) and the script will refuse to trade.

A configurable Max Half-Life (default 20 bars) gates signals — entries only fire when HL is below this threshold. A configurable Min Half-Life (default 0.5) floors the estimate to avoid degenerate near-zero values that would otherwise produce explosive signals.

This is the headline filter. Roughly 50–70% of bars on most instruments fail it — which is the point. You only trade when the spread has earned the right.

Entry / Exit logic

  • R-LONG — fires when Z < −entryZ (default −2.0) AND HL is below the max threshold AND the re-entry cooldown has elapsed. Spread is stretched too far below, will revert.
  • R-SHORT — fires when Z > +entryZ AND HL is below the max threshold AND cooldown elapsed.
  • R-EXIT — fires when |Z| drops below exitZ (default 0.25) OR when Max Hold Bars (default 40) has been reached, whichever comes first.


A configurable re-entry cooldown (default 3 bars) prevents immediate re-firing on the same side.

Visual system — minimal mono institutional

The aesthetic is intentionally austere — Renaissance's research-paper minimalism. Pure monochrome:

  • Z guides on right side of chart — small text labels showing current Z, HL, and tradeable status.
  • R-LONG / R-SHORT / R-EXIT labels — clean text tags on entry and exit bars.
  • Half-Life overlay label — current HL value displayed near the live close.
  • Shaded ribbon between price and slow EMA (configurable transparency).
  • Trade entry/exit shapes — small markers at signal bars.
  • Single-hue tradeable-regime tint (off by default) — subtle bgcolor when HL is fast AND Z is stretched.


A locked Minimal Mono palette: white bull / gray bear / pure-black background. No accent colours. The chart looks like a quant research paper. Intentional.

Dashboard

Monospaced table positionable to any of eight corners. Surfaces:

  • Current spread value and Z score.
  • Current half-life (in bars) with tradeable / non-tradeable flag.
  • AR(1) β coefficient (the regression's directional read).
  • Z thresholds in use.
  • Last signal direction with bars-ago.
  • Max-hold bars remaining (when in a position).


snapshot

Rolling backtest tracker

The script tracks the last N closed reversion trades (configurable, default 200) and surfaces:

  • Total trades, wins, losses.
  • Win rate.
  • Average bars-to-exit.
  • Average Z magnitude at entry.
  • Hit-rate by side (R-LONG vs R-SHORT).


This is the script's own performance audit — you see whether the engine is finding genuine reversion or whether the current regime is breaking it.

Alerts

Three alert conditions, each independently controllable:

  • Reversion Entry (R-LONG or R-SHORT)
  • Reversion Exit (R-EXIT)
  • Half-Life crosses Max Half-Life (regime change — reversion is becoming unreliable)


How to read it

Three reads, in order of conviction:

  1. R-LONG / R-SHORT with very fast HL (e.g. HL = 4 bars on a 1H chart) — the script's intended high-conviction setup. The spread is stretched, the math says it will revert quickly, the chart agrees. This is the institutional setup.
  2. Half-life crossing above max (alert) — regime warning. The instrument is shifting from mean-reverting to trending. Any open R-positions should be re-evaluated; new R-entries should be paused until HL re-tightens.
  3. Sustained R-EXIT triggers from time-stop (max-hold) rather than from Z returning to neutral — the script is exiting because the trade ran out of time, not because the thesis played out. Recurring time-stop exits mean the current parameters do not fit the instrument.


The rolling backtest win-rate is your auditor. When it climbs, the engine is finding edge. When it grinds flat or declines, the regime has changed and the parameters need adjustment.

Suggested settings

Defaults (long EMA 50, Z lookback 100, regression window 120, max HL 20 bars, entry Z 2.0, exit Z 0.25) are tuned for 1H–4H on liquid markets where mean reversion is statistically meaningful. For lower timeframes drop everything proportionally (long EMA 25, Z 50, regression 60). For HTF raise everything (long EMA 100, Z 200, regression 200). The max HL is the most sensitive parameter — narrow it (10–15) for high-conviction-only filtering; widen it (25–30) for more frequent signals.

Originality / what's reused

The synthetic-pair Z-score construction is textbook stat-arb. The AR(1) half-life regression is published quantitative finance — the Ornstein–Uhlenbeck-process speed-of-reversion estimator. The implementation here — the dual-leg synthetic spread with optional log construction, the rolling Z-normalisation pipeline, the AR(1) regression with HL formula and min/max-HL gating, the entry/exit state machine with cooldown and max-hold, the rolling N-trade backtest tracker, and the minimal-mono institutional aesthetic — is JOAT-original. No third-party code reused. The script is a tribute to the Medallion-style approach, not a direct replication of any proprietary Renaissance Technologies code.

Limitations

The single-leg "synthetic pair" (price vs its own EMA) is a degenerate stat-arb construction by design — true stat-arb uses two genuinely co-integrated instruments. Pine's per-script symbol limitation makes a two-instrument cointegration construction impractical for a standalone indicator; this script captures the methodology of stat-arb (spread + Z + HL gate) on the single-instrument case. The HL estimate is statistical and needs the regression window populated; early bars give a warm-up read.




-made with passion by jackofalltrades

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.