OPEN-SOURCE SCRIPT
MA Distance Stretch & Mean Reversion Levels

Shows how far price is from the 50 and 200 MA, and whether that distance is normal or overstretched relative to the ticker's own history.
Most "distance from MA" tools give you a raw percentage and leave you to guess whether +23% is a lot. This one measures the rolling mean and standard deviation of that distance over a lookback, converts the current reading into a z‑score, and tells you in plain words where you are.
What you see
📐 % distance from each MA (optional $ distance)
📊 μ / σ — historic mean and standard deviation of that distance over the lookback
🎯 z‑score — how many σ price is from its normal distance
🚦 Status — Normal / Extended / OVERSTRETCHED, with direction arrow and colour‑coded row
📍 ±σ bands — dotted price levels at MA × (1 + μ ± kσ): the actual prices where the stretch would hit your threshold. Use them as mean‑reversion targets or as the level price must hold to stay extended
Settings
Group Setting Default
Averages Fast / Slow MA length 50 / 200
MA type SMA (or EMA)
Use last closed bar Off — excludes the live candle when on
Stretch Stats lookback 250 bars
Extended at 1.0σ
Overstretched at 2.0σ
Draw ±σ bands / band width On / 2.0σ
Display Show $ distance Off
Padding rows 1 — stacks the readout above other bottom‑left tables
Notes
Mean is rarely zero: a name in a long uptrend "normally" sits above its 200 MA, so the z‑score measures stretch against that baseline, not against the MA itself.
Lookback sets the regime. 250 bars on a daily chart ≈ one year; shorten it if you want the stats to reflect recent behaviour only.
Both rows show na until the chart has MA length + lookback bars of history.
All calculations run on every bar; the closed‑bar toggle only shifts what's displayed.
Works on any symbol and timeframe.
Most "distance from MA" tools give you a raw percentage and leave you to guess whether +23% is a lot. This one measures the rolling mean and standard deviation of that distance over a lookback, converts the current reading into a z‑score, and tells you in plain words where you are.
What you see
📐 % distance from each MA (optional $ distance)
📊 μ / σ — historic mean and standard deviation of that distance over the lookback
🎯 z‑score — how many σ price is from its normal distance
🚦 Status — Normal / Extended / OVERSTRETCHED, with direction arrow and colour‑coded row
📍 ±σ bands — dotted price levels at MA × (1 + μ ± kσ): the actual prices where the stretch would hit your threshold. Use them as mean‑reversion targets or as the level price must hold to stay extended
Settings
Group Setting Default
Averages Fast / Slow MA length 50 / 200
MA type SMA (or EMA)
Use last closed bar Off — excludes the live candle when on
Stretch Stats lookback 250 bars
Extended at 1.0σ
Overstretched at 2.0σ
Draw ±σ bands / band width On / 2.0σ
Display Show $ distance Off
Padding rows 1 — stacks the readout above other bottom‑left tables
Notes
Mean is rarely zero: a name in a long uptrend "normally" sits above its 200 MA, so the z‑score measures stretch against that baseline, not against the MA itself.
Lookback sets the regime. 250 bars on a daily chart ≈ one year; shorten it if you want the stats to reflect recent behaviour only.
Both rows show na until the chart has MA length + lookback bars of history.
All calculations run on every bar; the closed‑bar toggle only shifts what's displayed.
Works on any symbol and timeframe.
開源腳本
秉持TradingView一貫精神,這個腳本的創作者將其設為開源,以便交易者檢視並驗證其功能。向作者致敬!您可以免費使用此腳本,但請注意,重新發佈代碼需遵守我們的社群規範。
免責聲明
這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。
開源腳本
秉持TradingView一貫精神,這個腳本的創作者將其設為開源,以便交易者檢視並驗證其功能。向作者致敬!您可以免費使用此腳本,但請注意,重新發佈代碼需遵守我們的社群規範。
免責聲明
這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。