Average True Range (ATR) in Pine Script
The average of the true range over 14 bars, a volatility measure in price units that sizes stops, targets, bands and positions to how much the market actually moves.
Definition
Average True Range, ATR, is the average size of a bar measured in price units, where the size of a bar is its true range: the largest of the high-to-low range, the distance from the previous close to the high, and the distance from the previous close to the low. It belongs to the family of volatility measures and it has no direction. A rising ATR says bars are getting bigger, whether the market is rising or falling. Its value is that it converts volatility into the same units as price, so it plugs straight into stops, targets, bands and position size.
True range matters because a plain high-to-low range misses gaps. A stock that closes at 100 and opens at 95 has moved 5 points before the bar's own range begins, and the true range counts that move. The average then tells you how far the market normally travels in a bar, which is the distance a stop must sit beyond to survive ordinary noise, and the distance a target can reasonably ask for in a given number of bars. Sizing a position by ATR makes every trade risk the same money regardless of how volatile the instrument is.
Welles Wilder introduced true range and ATR in 1978, with a 14-period average smoothed his way: each new value is the previous average weighted by 13 parts plus the new true range weighted by 1 part. That smoothing decays slowly, so ATR rises fast on a shock and drifts down over weeks. 14 remains the default almost everywhere. The library uses ATR in two templates: ATR Bands, a 20-period EMA with lines 2 ATR away that marks breakouts, and the Chandelier Exit, a stop hung 3 ATR below the highest high of the last 22 bars.
On the chart ATR clusters. Quiet stretches produce a low, slowly falling line, a shock produces a spike, and the spike fades over several weeks as the smoothing forgets it. Volatility also scales with price, so ATR as a percentage of price is the number to compare across instruments and across years. A bar whose range is 2 or 3 times the ATR is an event bar, often news, and the bars after it are wide too. Stops set in ATR widen after such a bar, which is correct: the market's noise has grown, and a stop that ignored that would be hit by it.
Reading the chart
ATR is read as a level, a trend and a ratio, and then converted into distances.
- Add ATR in a pane and note its value as a percentage of price. Around 1 percent on a daily chart is a calm large cap; 3 to 5 percent is a volatile stock or crypto, and the same rules need wider stops there.
- Read the direction of the ATR line. Rising means ranges are expanding, usually after a breakout or a shock; falling means the market is quietening, often ahead of the next expansion.
- Compare the current bar with the average. A bar of 2 or 3 ATR is an event, and the moves and stops that follow it are measured against the new, larger average.
- Convert ATR into a stop distance. Swing trades usually sit 2 to 3 ATR from entry; the library's Chandelier uses 3 ATR under the highest high and its bands sit 2 ATR from the average.
- Convert ATR into size. Divide the money you will risk by the stop distance in ATR times the ATR value, and the result is the number of shares or contracts.
- Check the timeframe. Use the ATR of the chart you trade from; a daily ATR applied to a 5-minute entry gives a stop the size of the whole day.
The maths
True range is the largest of three distances, and ATR is Wilder's smoothed average of it; the distances a trader uses are multiples of that average.
The first ATR value is a simple average of the first n true ranges; the recursion takes over from there, so a chart with little history shows a slightly different ATR at the start.
Wilder's smoothing is an exponential average with a weight of 1 / n, so a 14-period ATR carries as much memory as a 27-period EMA. Platforms that use a simple or exponential average give livelier values.
ATR is in price units, so it grows with the price level. ATR percent, ATR divided by the close, is the number to compare across instruments and across time.
On 24-hour markets the true range is usually just the high-to-low range; on stocks the overnight gap is often the largest of the three terms.
Inputs that matter
| Input | Typical | What changing it does |
|---|---|---|
| ATR length | 14 | Shorter reacts to the last few bars and jumps on one wide bar; longer gives a stable unit that ignores single events but lags a regime change. |
| ATR multiplier (bands and stops) | 2 for the bands, 3 for the Chandelier | Lower keeps the stop or band close and gets hit by noise; higher survives noise but gives back more when the move ends. |
| EMA length (ATR Bands) | 20 | The trend line the bands are drawn around; shorter tracks price more closely and gives more outside closes. |
| Lookback (Chandelier Exit) | 22 | The window for the highest high the stop hangs from and for its ATR; shorter starts the stop nearer to price after a flip and makes it react faster. |
In practice
- As a stop distance: 2 to 3 ATR beyond the entry or the recent swing, so the stop sits outside ordinary noise and adapts as volatility changes.
- As position size: risk per trade divided by the stop distance in price, which makes a volatile instrument a smaller position and a quiet one larger, at the same money risk.
- As a target: a multiple of ATR from entry gives a target the market can plausibly reach in the holding period, and R multiples fall out of it directly.
- As bands: the library's ATR Bands draw a 20-period EMA with lines 2 ATR away and mark a close above the upper band as a long breakout and a close below the lower band as a short.
- As a trailing stop: the Chandelier Exit hangs 3 ATR below the highest high of the last 22 bars and ratchets up, which is the library's ATR trailing stop.
- As a regime filter: skip breakouts when ATR is at a high after a spike, or wait for a low and falling ATR when trading compressions, because volatility clusters.
Where it goes wrong
- Using the same fixed stop on every instrument. A 1 percent stop is 3 ATR on a slow index and a third of one on a volatile stock; set stops in ATR and let the distance differ.
- Keeping the same size after ATR doubles. The money at risk has doubled too; recompute size from the current ATR on every entry.
- Reading a rising ATR as bearish. ATR has no direction; it rises in fast rallies as well as in fast declines, and it says only that bars are getting bigger.
- Mixing timeframes. A daily ATR on an intraday entry gives a stop the size of the day; use the ATR of the chart the trade is managed on.
Compared with
Bollinger Bands. Bollinger Bands measure the dispersion of closes with a standard deviation. ATR measures the size of bars including gaps, so it reacts to intrabar movement and to overnight gaps that a close-based measure never sees at all.
Keltner Channels. A Keltner channel is one use of ATR: an EMA with lines a multiple of ATR away on each side. This article is about the unit itself; the channel article is about the corridor built from it and how to trade it.
Volatility Squeeze. The squeeze is a regime read, Bollinger Bands inside a Keltner channel, and it compares two volatility measures. ATR is one of those measures, and it is also the unit the stops beyond the squeeze range are set in.
ADX & DMI. ADX measures trend strength, how directional the movement is, and can be high with a small ATR. ATR measures how large bars are with no view on direction; a market can have a high ATR and no trend at all.
Build it in Quant
Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.
Questions
What ATR multiple should a stop use?
2 to 3 ATR is the usual range for swing trades, 1 to 1.5 for tight intraday setups with a clear invalidation level, and 3 or more for trend following. The multiple is a trade-off: closer stops lose less per trade and get hit more often. Test it, and remember that a wider stop means a smaller position, not more money at risk.
How do I size a position with ATR?
Decide the money to risk, say 1 percent of the account, and the stop distance in ATR, say 2. Size is the risk divided by 2 times the ATR value. A stock with an ATR of 4 and a 2 ATR stop risks 8 per share, so 1,000 of risk sizes the position at 125 shares. Recompute on every entry, because ATR changes.
Why does ATR stay high after a spike?
Because Wilder's smoothing weights the new true range by only 1 part in 14. A shock bar raises the average at once and then fades out over several weeks rather than dropping out after 14 bars. The slow decay is deliberate: the bars after a shock are usually wide too, and stops that stayed wide with the average survive them.
Is a rising ATR bullish or bearish?
Neither. ATR measures the size of bars, not their direction, so it rises in a fast rally and in a crash alike. Read it with the trend: a rising ATR in an uptrend means the move is accelerating, which can mean a climax; a rising ATR in a range means the range is about to be resolved.
Should I use the daily ATR for an intraday trade?
Use the ATR of the chart you manage the trade on. A daily ATR describes a whole day of movement and gives an intraday stop the size of the day. It is still useful as context: an intraday move that has already covered a full daily ATR has used up its typical range, and pushing for more is a low-odds trade.
Why does my ATR differ from another platform's?
Smoothing. Wilder's method is the standard, but some platforms average the true range with a simple or exponential moving average, and some seed the first value differently. The differences are small in level but visible after a spike, where Wilder's version decays more slowly. Check the method before comparing numbers across tools.