The ta.atr function is one of the most useful (and most misused) tools in Pine Script. It calculates the Average True Range — a volatility measure used to size stops, targets, and position size in proportion to an asset’s “move size.” In this honest guide you’ll see the correct syntax in Pine Script v5, a ready-to-paste code example for TradingView, and the most common mistakes beginners make.

Want to turn a TradingView indicator into automatic order execution? See how to connect signals to a binary options platform with Python.

Automate with the Python API →

What ATR Is and Why It Matters

ATR measures the average range of candles over a period, factoring in the gaps between the previous close and the current open (the “true range”). Unlike directional indicators, ATR does not tell you whether the price will go up or down — it only tells you how much the price tends to move. This is valuable for setting a stop that won’t get blown out by normal market noise, or for reducing position size when volatility spikes.

Rule of thumb: high ATR = large candles = stops need more room and position size should be smaller. Low ATR = a compressed market, often right before an expansion.

ta.atr Function Syntax in Pine Script v5

The signature is simple — it takes only the length (number of candles) and returns a series with the ATR smoothed by RMA:

ta.atr(length) → series float // length: int — number of periods (e.g.: 14) // return: the ATR as a floating-point series

Internally, ta.atr(14) is equivalent to ta.rma(ta.tr(true), 14). Knowing this helps when you want to swap the smoothing (for example, using ta.ema instead of the default RMA).

Ready-Made Example: ATR Panel + Dynamic Stops

Paste this code into the Pine editor in TradingView. It plots the ATR in a separate panel and calculates stop levels based on the current price:

//@version=5 indicator(“ATR + Dynamic Stops”, overlay=false) length = input.int(14, “ATR Period”, minval=1) mult = input.float(1.5, “Multiplier”, step=0.1) atr = ta.atr(length) // Dynamic stops based on the close stopLong = close – atr * mult stopShort = close + atr * mult plot(atr, “ATR”, color=color.orange, linewidth=2) hline(0, “Zero”, color=color.gray) // Show the values in a simple table var table t = table.new(position.top_right, 1, 2) if barstate.islast table.cell(t, 0, 0, “Stop Long: ” + str.tostring(stopLong, “#.#####”)) table.cell(t, 0, 1, “Stop Short: ” + str.tostring(stopShort, “#.#####”))
Tip: for binary options with fixed expiration, ATR is more useful as an entry filter (avoiding trades when volatility is out of the ordinary) than as a stop, since this product has no traditional stop loss.

Using ATR for Position Sizing

In markets where you control position size (forex, indices, crypto), a common approach is to risk a fixed fraction of capital per trade and let the ATR define the stop distance:

// Risk 1% of capital, stop at 2x ATR capital = 1000.0 riscoPct = 0.01 atr = ta.atr(14) distancia = atr * 2 // Size = money risk / stop distance tamanho = (capital * riscoPct) / distancia plot(tamanho, “Suggested size”)

This calculation automates the idea of “always risking the same amount” regardless of whether the asset is calm or agitated. Still, treat the number as a starting point, not an absolute truth.

Common Mistakes with ta.atr

Three pitfalls come up all the time. First: using a length that’s too short (1 to 3), which makes the ATR jumpy and statistically meaningless. Second: comparing the ATR of different assets in absolute value — an ATR of 0.5 on gold and 0.0005 on a currency pair don’t compare directly; normalize by dividing by price if you need to. Third: expecting ATR to predict direction. It’s pure volatility.

FAQ

What’s the best period for the ta.atr function? The default 14 works well on most timeframes. Shorter periods (7-10) react faster; longer ones (20-30) are more stable.

Does ta.atr work on any timeframe? Yes. The value changes with the chart — the ATR of a 1-minute candle is much smaller than that of a daily candle.

Can I use ta.atr inside a strategy? Yes, the exact same way. In strategy() scripts it usually feeds strategy.exit with ATR-based stops and targets.

Does ta.atr repaint? Not on the candle close. Like any indicator, the value of the currently forming candle can change until it closes.

Disclaimer: binary options and leveraged trading are extremely high-risk products and most retail investors lose money. This content is educational, does not constitute investment advice, and guarantees no result. Always test any indicator or strategy on a demo account before risking real money.

Similar Posts