OPEN-SOURCE SCRIPT

MA Z-Score Deviation

421
A valuation oscillator that measures how far price has stretched from its statistical fair value defined here as its moving average, expressed as a standardized score rather than raw price units.

How it works

Base: A moving average (SMA, EMA, or WMA) serves as the reference "fair value" line. Deviation from this baseline is calculated as a log ratio (log(price / MA)) rather than a simple price difference, so the reading stays comparable across different price levels and time periods: a 5% premium or discount reads the same whether the instrument is at $10 or $500.
Standardization: the deviation is divided by its own rolling dispersion to produce a score that expresses how rich or cheap price is relative to its typical range, not just in absolute terms.

Two dispersion methods are available:

Standard Deviation: the classical measure of typical variability.

MAD: a median-based measure that's more resistant to distortion from one-off outlier bars.

Blend: a weighted combination of both, adjustable via the MAD Weight input.
Display scaling: the score is passed through a bounded transform for cleaner visual scaling on the chart, while the underlying valuation ranking is preserved.

Inputs

Source & MA type (SMA/EMA/WMA) and length
Lookback length for the dispersion calculation, independent from the MA length, letting you compare long-term fair value against recent volatility conditions
Dispersion method (Stdev / MAD / Blend) and blend weight
Display scaling factor

Reading the indicator

Positive values indicate price is trading at a premium to its baseline; negative values indicate a discount.
Values beyond ±2 suggest a notable premium/discount relative to recent conditions; beyond ±3 suggests a substantial one.
Because the baseline is statistical rather than fixed, the "fair value" and its bands adjust automatically as market conditions evolve.

Notes / limitations

This measures statistical richness/cheapness relative to a moving average, not intrinsic or fundamental value; it's a technical valuation lens, not a fundamental one.
Financial return distributions have fatter tails than a normal distribution assumes, so extreme readings can occur more frequently than the underlying statistical model implies; treat scores as relative positioning, not precise probabilities.
Alerts trigger only on confirmed bar closes to avoid firing on intrabar noise.

Clause de non-responsabilité

Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.