Average True Range Pine Script — Complete TradingView Guide
The Average True Range (ATR) Pine Script indicator is the industry-standard tool for measuring market volatility in TradingView. Developed by J. Welles Wilder Jr. in 1978, ATR calculates the average of True Range values over a lookback period — capturing not just the high-low range but also overnight gaps via the previous close. In Pine Script v6, ATR is implemented with ta.atr(14) or by applying ta.rma() to ta.tr(true). Traders use ATR across stocks, crypto, forex, and futures to set dynamic stop-losses, size positions relative to volatility, and identify breakout conditions. A rising ATR signals expanding volatility often associated with trending moves; a falling ATR signals consolidation. This guide covers the full ATR formula, parameter tuning, three practical trading strategies, and step-by-step instructions for generating ATR code in Pineify.
What Is the Average True Range (ATR)?
The Average True Range (ATR) is a volatility indicator that measures the average magnitude of price movement over a specified number of bars, used to quantify market volatility for risk management and strategy development. Unlike most technical indicators, ATR does not signal trade direction — it only tells you how much the price is moving on average.
History and Origin
ATR was introduced by J. Welles Wilder Jr. in his 1978 book New Concepts in Technical Trading Systems, the same book that introduced RSI and Parabolic SAR. Wilder designed ATR specifically for commodity futures markets where overnight gaps were common and the simple high-low range underestimated true price movement. Over the decades, ATR became a universal volatility tool applied across all asset classes.
Core Formula
ATR is calculated in two steps:
- True Range (TR) = max of:
- Current High − Current Low
- |Current High − Previous Close|
- |Current Low − Previous Close|
- ATR = RMA(True Range, length), where RMA is Wilder's smoothing method (also called Wilder Moving Average), equivalent to an EMA with
alpha = 1 / length.
In Pine Script v6: ta.atr(14) or equivalently ta.rma(ta.tr(true), 14). The true parameter in ta.tr(true) enables gap-adjusted True Range using the previous close.
Applicable Markets and Timeframes
ATR is applicable to all asset classes: stocks, crypto, forex, and futures. It is particularly valuable in futures and crypto markets where overnight gaps and high volatility are common. Recommended timeframes:
- Scalping (1–5 minute): ATR length 5–10, use for micro stop-loss placement
- Day trading (15 minute–1 hour): ATR length 10–14, standard for intraday risk management
- Swing trading (4 hour–Daily): ATR length 14, the original Wilder default and most widely cited
- Position trading (Weekly): ATR length 14–21, for wider stop-loss buffers
The Daily chart with the default 14-period ATR is the most referenced benchmark in trading literature and professional risk systems.
ATR Pine Script Code Example
The code below is a complete, runnable Pine Script v6 ATR indicator generated by Pineify. It uses Wilder's RMA smoothing method — the original calculation specified in Wilder's 1978 book — applied to the gap-adjusted True Range. To use it in TradingView, open the Pine Script Editor (Alt+P), paste the code, click Add to chart, and the ATR line will appear in a separate pane below your price chart.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Pineify
//@version=6
indicator(title="Average True Range", overlay=false, max_labels_count=500)
// ATR calculation using RMA (Wilder's Smoothing Method)
// True Range = max(high-low, |high-close[1]|, |low-close[1]|)
// ATR = RMA(True Range, length)
atr_length = 14
atr_value = ta.rma(ta.tr(true), atr_length)
// Plot ATR
plot(atr_value, "ATR", color.rgb(183, 28, 28, 0), 1)
ATR Parameters
The ATR indicator has two core configurable parameters. The table below lists each parameter, its default value, what it controls, and the recommended range for different trading styles.
| Parameter | Default Value | Description | Recommended Range |
|---|---|---|---|
| length | 14 | Number of bars used to smooth the True Range values via RMA | 5–50 |
| ma_type | RMA | Smoothing method for averaging True Range values (RMA = Wilder's method) | RMA, EMA, SMA |
Tuning Scenarios
- Scalping (1–5 min charts): Use length 5–10 for fast-reacting volatility signals; stop-losses based on 1× ATR
- Swing trading (Daily chart): Use the default length 14 with 2× ATR stop-loss — the most widely validated setting across professional literature
- Position trading (Weekly chart): Use length 20–30 for a smoother, long-term volatility baseline; stop-losses 2.5–3× ATR
ATR Trading Strategies
Strategy 1: ATR Volatility Breakout
Best for: Trending markets | Pairs with: EMA
This strategy enters trades when price breaks out of a range defined by an ATR multiple, confirming that volatility is expanding into a directional move.
- Calculate the 14-period ATR on the daily chart.
- Identify the high and low of the most recent 20-bar range.
- Enter long when price closes above
range_high + 0.5 × ATR, confirming upside breakout with volatility expansion (ATR rising). - Enter short when price closes below
range_low − 0.5 × ATR, confirming downside breakout. - Combine with 50-period EMA: only take long breakouts when price is above EMA; only take short breakouts when price is below EMA.
- Stop-loss: 1.5× ATR from entry price. Take-profit: 3× ATR from entry (2:1 reward-to-risk ratio).
Strategy 2: ATR Trailing Stop (Chandelier Exit)
Best for: Trending markets | Pairs with: Highest/Lowest lookback
The Chandelier Exit uses ATR to set a trailing stop that moves with the highest high (for longs) or lowest low (for shorts), keeping you in winning trades longer.
- Calculate 22-period ATR and the 22-period highest high.
- Long stop level =
ta.highest(high, 22) − 3 × ta.atr(22). - Enter long when price crosses above the long stop level for the first time after a downtrend.
- Trail the stop upward as the highest high rises — never lower it while in the trade.
- Exit long when price closes below the trailing stop level.
- Pair with MACD: only enter when MACD histogram is positive to confirm momentum alignment.
Strategy 3: ATR Squeeze and Expansion
Best for: Pre-breakout consolidation | Pairs with: Bollinger Bands
This strategy identifies low-volatility consolidation periods (ATR below its own moving average) and positions for the volatility expansion that follows.
- Calculate 14-period ATR and a 50-period SMA of ATR.
- Squeeze condition: ATR is below its 50-period SMA for 10+ consecutive bars (low volatility regime).
- Monitor Bollinger Bands width: when bands narrow to a 52-week low, a breakout is imminent.
- Enter long when ATR crosses above its 50-period SMA AND price breaks above the Bollinger upper band.
- Enter short when ATR crosses above its 50-period SMA AND price breaks below the Bollinger lower band.
- Stop-loss: 2× ATR from entry. Exit when ATR peaks and begins declining (volatility contraction).
For educational purposes only. Not investment advice. Past performance does not guarantee future results. Always backtest any strategy before using it with real capital.
How to Generate ATR Pine Script in Pineify
Pineify lets you generate a production-ready ATR Pine Script indicator in seconds — no coding required.
- 1
Open Pineify at pineify.app
Navigate to pineify.app and sign in or create a free account.
- 2
Click "New Indicator" in the dashboard
Select "Technical Indicator" as the type to access the full indicator library.
- 3
Describe your ATR configuration
Type your requirements, e.g. "ATR with length 14, plotted in a separate pane, with a 2× stop-loss level overlay." Pineify understands natural language.
- 4
Copy the generated Pine Script v6 code
Pineify generates complete, runnable code with proper RMA smoothing and all inputs configured. Click "Copy" to get the code.
- 5
Adjust parameters and add to your TradingView chart
Paste the code into TradingView Pine Script Editor (Alt+P), click "Add to chart," and adjust the ATR length, smoothing type, and multiplier directly from the indicator settings panel.
ATR Pine Script FAQ
Related Pine Script Indicators
Bollinger Bands Indicator
Volatility envelope using standard deviation
EMA Indicator
Exponential Moving Average for trend following
SMA Indicator
Simple Moving Average baseline trend indicator
Highest Indicator
Rolling highest value for Chandelier Exit strategies
MA Indicator
Multi-type moving average selection tool
Generate ATR Pine Script in Seconds
Stop copying and pasting scripts manually. Pineify generates production-ready Pine Script v6 ATR code with your exact parameters — for free.
Try Pineify for Free