VolatilityPine Script v6

ta.atr()Average True Range

ta.atr() returns the Relative Moving Average of true range. True range is the largest of high − low, |high − close[1]|, and |low − close[1]|, so ATR includes price gaps as well as each bar’s range.

Pine Script v6 reference verified: · TradingView reference

Syntax

Syntax
ta.atr(length) → series float

Arguments

ParameterTypeDescription
lengthsimple intLength (number of bars back).

Returns

Average true range.

Remarks

na values in the source series are ignored; the function calculates on the length quantity of non-na values.

Code Examples

Pine Script v6 Example
//@version=6
indicator("ta.atr")
plot(ta.atr(14))

//the same on pine
pine_atr(length) =>
    trueRange = na(high[1])? high-low : math.max(math.max(high - low, math.abs(high - close[1])), math.abs(low - close[1]))
    //true range can be also calculated with ta.tr(true)
    ta.rma(trueRange, length)

plot(pine_atr(14))

Trading Applications

Set dynamic stop-loss levels based on market volatility

Calculate position sizes adjusted for current volatility

Identify volatility expansion and contraction phases

Use as a trailing stop distance multiplier

Related Functions

Frequently Asked Questions

ta.atr() computes the Average True Range by smoothing true range with Wilder smoothing (RMA). True range captures the largest of the current bar range and gaps versus the prior close, so ATR reflects how far price typically moves per bar.

A common approach is to place stops a multiple of ATR away from entry (e.g., 1.5–3× ATR). The multiplier depends on timeframe, asset, and risk tolerance. ATR adapts stops to current volatility instead of using a fixed tick distance.

14 is widely used on daily charts because it matches classic literature, but the best period depends on your horizon. Shorter periods react faster; longer periods are smoother. Validate any setting on the markets and timeframes you trade.

ta.tr and ta.tr(true) return true range for the current bar, while ta.atr(length) smooths true range with RMA across multiple bars. The ta.tr variable is equivalent to ta.tr(false); ta.tr(true) falls back to high − low when the previous close is unavailable.

ta.atr() ignores na values in its source calculation and uses the requested length of non-na true-range values. This differs from functions whose windows propagate na results.

Generate ta.atr() Code with AI

Skip the manual coding. Use Pineify's AI Coding Agent to generate Pine Script code using ta.atr() and other built-in functions instantly.