OPEN-SOURCE SCRIPT
Aktualisiert

MAD Z [SigmaSeven]

699
MAD Z [SIGMASEVEN]

WHAT IT DOES

MAD Z is a regime oscillator that measures how far price has deviated from its recent median, using the Modified Z-Score, a robust statistical method based on the Median Absolute Deviation (MAD) instead of the classical mean/standard deviation. It classifies the market into three persistent states: Risk-On, Risk-Off, and Hold, and colors the oscillator and (optionally) the price bars accordingly.

WHY MAD INSTEAD OF A CLASSICAL Z-SCORE

A classical z-score uses the mean and standard deviation. Both are heavily distorted by outliers: a single large candle (news spike, liquidation wick) inflates the standard deviation and compresses the score exactly when you need it most. The Modified Z-Score replaces:

- the mean with the rolling median (robust location estimate), and
- the standard deviation with the Median Absolute Deviation (robust scale estimate).

The formula is:

z = 0.6745 * (price - median) / MAD

where MAD = median( |price - median| ) over the lookback window. The constant 0.6745 rescales MAD so the output is comparable to a standard z-score under a normal distribution (0.6745 is the 75th percentile of the standard normal). The practical effect: the score reacts to genuine directional displacement but is far less distorted by single-bar outliers than a standard-deviation-based z-score.

HOW THE STATE LOGIC WORKS

The raw score alone would flip constantly around zero. MAD Z therefore uses a hysteresis state machine with two asymmetric thresholds:

- Score crosses ABOVE the Long Threshold (default +1.30) → state switches to Risk-On and stays there.
- Score crosses BELOW the Short Threshold (default -0.20) → state switches to Risk-Off and stays there.
- Between the thresholds, the last state is held (Hold Zone, shaded band).

The asymmetry (a high bar for entering Risk-On, a much closer bar for exiting into Risk-Off) is deliberate: it demands strong statistical displacement before calling a bullish regime, but exits early when the displacement fades. Both thresholds are fully adjustable, so the behavior can be inverted or symmetrized.

HOW TO READ IT

- Oscillator line: the Modified Z-Score of the selected source (default hlcc4) over the Baseline Length (default 50 bars). Colored by current state.
- Threshold lines and shaded Hold Zone: the hysteresis band described above.
- Gradient fills: visual emphasis of displacement above/below zero.
- Bar Coloring (optional): paints price candles in the current regime color so the state is visible on the main chart without reading the pane.

Typical use: as a regime filter for other strategies (only take longs while Risk-On), or as a standalone mean-displacement gauge on higher timeframes. It is NOT a complete trading system and issues no entries or exits by itself.

SETTINGS

- Source: input series (default hlcc4)
- Baseline Length: lookback for median and MAD (default 50)
- Smoothing / Smoothing (EMA): optional EMA smoothing of the raw score (off by default)
- Long / Short Threshold: hysteresis levels (defaults +1.30 / -0.20)
- Color Mode: 8 preset palettes, or custom Risk-On/Risk-Off colors
- Bar Coloring: toggle candle coloring on the main chart

LIMITATIONS

- The score is computed on the current bar's value and can change until the bar closes; the state can therefore also change intrabar. For confirmed signals, evaluate on bar close.
- The median/MAD baseline needs the full lookback window; the first "Baseline Length" bars show no score.
- Like any displacement measure, it is descriptive of the recent window, not predictive. In prolonged low-volatility ranges MAD becomes small and the score can appear exaggerated.
- Default thresholds were chosen as reasonable general-purpose values, not optimized per market. Adjust them to your instrument and timeframe.

The calculation uses no higher-timeframe requests and no lookahead; it does not repaint past bars.
Versionshinweise
Update v1.1

State evaluation is now gated to confirmed bars: a threshold cross must hold into bar close before the Risk-On/Risk-Off state switches. Live behavior now matches historical bars exactly; a printed state change can no longer appear and disappear intrabar. This adds one bar of confirmation delay to state switches.
Threshold lines changed from broken to solid style (linewidth 2) and removed from the price scale and status line for a cleaner pane.
Reworked color presets: 8 coordinated palettes (default: Cyberpunk).

No changes to the Modified Z-Score calculation (median, MAD, 0.6745 scaling) or the threshold defaults.

Haftungsausschluss

Die Informationen und Veröffentlichungen sind nicht als Finanz-, Anlage-, Handels- oder andere Arten von Ratschlägen oder Empfehlungen gedacht, die von TradingView bereitgestellt oder gebilligt werden, und stellen diese nicht dar. Lesen Sie mehr in den Nutzungsbedingungen.