OPEN-SOURCE SCRIPT

ATR Extension from Moving Average, with Robust Sigma Bands

141


# ATR Extension from Moving Average, with Robust Sigma Bands

**What it does**
This indicator measures how far price is from a selected moving average, expressed in **ATR multiples**, then overlays **robust sigma bands** around the long run central tendency of that extension. Positive values mean price is extended above the MA, negative values mean price is extended below the MA. The signal adapts to volatility through ATR, which makes comparisons consistent across symbols and regimes.

**Why it can help**

* Normalizes distance to an MA by ATR, which controls for changing volatility
* Uses the **bar’s extreme** against the MA, not just the close, so it captures true stretch
* Computes a **median** and **standard deviation** of the extension over a multi-year window, which yields simple, intuitive bands for trend and mean-reversion decisions

---

## Inputs

* **MA length**: default 50, options 200, 64, 50, 20, 9, 4, 3
* **MA timeframe**: Daily or Weekly. The MA is computed on the chosen higher timeframe through `request.security`.
* **MA type**: EMA or SMA
* **Years lookback**: 1 to 10 years, default 5. This sets the sample for the median and sigma calculation, `years * 365` bars.
* **Line width**: visual width of the plotted extension series
* **Table**: optional on-chart table that displays the current long run **median** and **sigma** of the extension, with selectable text size

**Fixed parameters in this release**

* **ATR length**: 20 on the daily timeframe
* **ATR type**: classic ATR. ADR percent is not enabled in this version.

---

## Plots and colors

* **Main plot**: “Extension from 50d EMA” by default. Value is in **ATR multiples**.
* **Reference lines**:

* `median` line, black dashed
* +2σ orange, +3σ red
* −2σ blue, −3σ green

---

## How it is calculated

1. **Moving average** on the selected higher timeframe: EMA or SMA of `close`.
2. **Extreme-based distance** from MA, as a percent of price:

* If `close > MA`, use `(high − MA) / close * 100`
* Else, use `(low − MA) / close * 100`
3. **ATR percent** on the daily timeframe: `ATR(20) / close * 100`
4. **ATR multiples**: extension percent divided by ATR percent
5. **Robust center and spread** over the chosen lookback window:

* Center: **median** of the ATR-multiple series
* Spread: **standard deviation** of that series
* Bands: center ± 1σ, 2σ, 3σ, with 2σ and 3σ drawn

This design yields an intuitive unit scale. A value of **+2.0** means price is about 2 ATR above the selected MA by the most stretched side of the current bar. A value of **−3.0** means roughly 3 ATR below.

---

## Practical use

* **Trend continuation**

* Sustained readings near or above **+1σ** together with a rising MA often signal healthy momentum.
* **Mean reversion**

* Spikes into **±2σ** or **±3σ** can identify stretched conditions for fade setups in range or late-trend environments.
* **Regime awareness**

* The **median** moves slowly. When median drifts positive for many months, the market spends more time extended above the MA, which often marks bullish regimes. The opposite applies in bearish regimes.

**Notes**

* The MA can be set to Weekly while ATR remains Daily. This is deliberate, it keeps the normalization stable for most symbols.
* On very short intraday charts, the extension remains meaningful since it references the session’s extreme against a higher-timeframe MA and a daily ATR.
* Symbols with short histories may not fill the lookback window. Bands will adapt as data accrues.

---

## Table overlay

Enable **Table → Show** to see:

* “ATR from \<MA>”
* Current **median** and **sigma** of the extension series for your lookback

---

## Recommended settings

* **Swing equities**: 50 EMA on Daily, 5 to 7 years
* **Index trend work**: 200 EMA on Daily, 10 years
* **Position trading**: 20 or 50 EMA on Weekly MA, 5 to 10 years

---

## Interpretation examples

* Reading **+2.7** with price above a rising 50 EMA, near prior highs

* Strong trend extension, consider pyramiding in trend systems or waiting for a pullback if you are a mean-reverter.
* Reading **−2.2** into multi-month support with flattening MA

* Stretch to the downside that often mean-reverts, size entries based on your system rules.

---

## Credits

The concept of measuring stretch from a moving average in ATR units has a rich community history. This implementation and its presentation draw on ideas popularized by **Jeff Sun**, **SugarTrader**, and **Steve D Jacobs**. Thanks to each for their contributions to ATR-based extension thinking.

---

## License

This script and description are distributed under **MPL-2.0**, consistent with the header in the source code.

---

## Changelog

* **v1.0**: Initial public release. Daily ATR normalization, EMA or SMA on D or W timeframe, robust median and sigma bands, optional table.

---

## Disclaimer

This tool is for educational use only. It is not financial advice. Always test on your own data and strategies, then manage risk accordingly.

Wyłączenie odpowiedzialności

Informacje i publikacje przygotowane przez TradingView lub jego użytkowników, prezentowane na tej stronie, nie stanowią rekomendacji ani porad handlowych, inwestycyjnych i finansowych i nie powinny być w ten sposób traktowane ani wykorzystywane. Więcej informacji na ten temat znajdziesz w naszym Regulaminie.