iATR in Pine Script: ta.sma(ta.tr, n), not ta.atr
Short answer
Use ta.sma(ta.tr(false), n), not ta.atr. MetaTrader's iATR is a simple moving average of true range, while ta.atr is Wilder's smoothed average.
iATR is the handle to MetaTrader's Average True Range. Despite the shared name, it is not the indicator Pine Script calls ta.atr: the stock MT5 ATR is a simple moving average of true range, while ta.atr is Wilder's smoothed average. The exact Pine port of iATR(symbol, tf, n) is therefore ta.sma(ta.tr(false), n).
What iATR computes
The signature is iATR(symbol, timeframe, ma_period); there is no applied price because true range is defined on high, low and the previous close. The stock indicator (Indicators\Examples\ATR.mq5) fills a true-range buffer with TRi = max(highi, closei−1) − min(lowi, closei−1) for i ≥ 1 and sets TR0 = 0. The first ATR value sits at bar index n and is the mean of TR1..TRn; every later value is the previous value plus (TRi − TRi−n)/n, which is just a rolling simple average written incrementally. Nothing in the recursion carries weight beyond n bars, so the line forgets a volatility spike exactly n bars later; Wilder's ATR fades it out gradually. Cells before index n hold 0.0.
The Pine Script equivalent
ta.tr(false) returns max(high − low, |high − close[1]|, |low − close[1]|), which is the same quantity as the MT5 formula and is na on bar 0 where close[1] does not exist. ta.sma of that over n bars gives its first value on bar n, the same bar as MetaTrader, and the same number. ta.sma(ta.tr(true), n) is equally correct from bar n on: ta.tr(true) only substitutes high − low on bar 0, and from bar n the window no longer contains bar 0, so the two forms print the same numbers. The only difference is one extra value on bar n−1 (where MetaTrader still shows 0.0); false leaves that bar empty, as MetaTrader effectively does. The script below plots both the MT5 definition and ta.atr so you can see the gap.
//@version=6
indicator("iATR port", overlay = false)
len = input.int(14, "Period", minval = 1)
// MetaTrader's ATR is a simple average of true range, not Wilder's RMA.
atrMt5 = ta.sma(ta.tr(false), len)
// Pine's own ATR for comparison: RMA of true range.
atrPine = ta.atr(len)
plot(atrMt5, "ATR (MT5 definition)", color.rgb(30, 144, 255))
plot(atrPine, "ta.atr", color.gray)The MQL5 indicator that this replaces:
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
input int InpPeriod = 14; // period
double AtrBuffer[];
int atrHandle;
int OnInit()
{
SetIndexBuffer(0, AtrBuffer, INDICATOR_DATA);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpPeriod);
atrHandle = iATR(_Symbol, _Period, InpPeriod);
if(atrHandle == INVALID_HANDLE)
return(INIT_FAILED);
return(INIT_SUCCEEDED);
}
int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[],
const double &open[], const double &high[], const double &low[], const double &close[],
const long &tick_volume[], const long &volume[], const int &spread[])
{
if(BarsCalculated(atrHandle) < rates_total)
return(0);
int to_copy = (prev_calculated == 0) ? rates_total : rates_total - prev_calculated + 1;
if(CopyBuffer(atrHandle, 0, 0, to_copy, AtrBuffer) <= 0)
return(0);
return(rates_total);
}Where the two differ
| Topic | MQL5 iATR | Pine ta.atr | Pine ta.sma(ta.tr(false), n) |
|---|---|---|---|
| Smoothing | Simple mean of the last n true ranges | ta.rma: SMA seed, then α = 1/n | Simple mean, same as MT5 |
| First value | Bar n, averaging TR1..TRn | Bar n−1, averaging TR0..TRn−1 with TR0 = high − low | Bar n, same value as MT5 |
| Later bars | Window of n | Infinite memory with decay; differs from MT5 on every bar | Equal to MT5 |
| Warm-up cells | 0.0 | na | na |
| Bar 0 | TR forced to 0, never used | ta.tr(true) internally: high − low | na, never used |
The two Pine lines start one bar apart with different seeds and never meet; on a 14-period daily chart the gap is typically a few percent of the ATR value, which matters for stop distances and position sizing.
What the Stratlate converter emits
The MQL5 → Pine engine does not map iATR to ta.atr. Its catalog entry emits ta.sma(ta.tr(false), int(period)), with a comment recording that MetaTrader's ATR is the plain mean of true range and that a converted indicator stayed far from parity while ta.atr was used. Our probe of a handle-plus-CopyBuffer ATR indicator converted with no warnings and produced B0 := ta.sma(ta.tr(false), int(14)). The mapping is treated as exact; the differential verifier then runs the output against the original on the same bars, so compile the Pine and check the first n bars yourself.
Pitfalls
- Porting
iATRasta.atr. Stops, trailing stops and volatility filters end up a few percent off on every bar, and a backtest that matched MetaTrader will not match TradingView. - Porting the other way with the same assumption: a Pine strategy using
ta.atrneeds a hand-written Wilder ATR in MQL5, notiATR. See moving averages in Pine vs MQL5. - Hand-rolling true range as
high − low. That ignores gaps from the previous close and understates ATR on every gapping bar; useta.tr, which includes the previous close. - Reading
AtrBuffer[0]in MT5 and expecting the current value. WithoutArraySetAsSeries, index 0 is the oldest bar; the current ATR isAtrBuffer[rates_total − 1]. - Comparing values inside the first n bars of either chart: MetaTrader shows 0.0 there and Pine shows nothing.
FAQ
Why does MetaTrader's ATR differ from TradingView's?
They use different averages of the same true range. MetaTrader averages the last n values equally; TradingView's ta.atr applies Wilder's exponential smoothing with α = 1/n. Same inputs, different filters, so they never converge.
Can I get Wilder's ATR in MetaTrader?
Not from iATR. Compute true range in OnCalculate and apply atr = atrprev + (TR − atrprev)/n after seeding with the mean of the first n values; that reproduces ta.atr.
Does the period argument mean the same thing?
Yes, both are the number of bars. iATR(…, 14) and ta.sma(ta.tr(false), 14) both produce their first value on bar 14 and agree from then on.
Convert the whole program
Paste an MQL4 or MQL5 indicator or Expert Advisor and get Pine Script v6 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.
Open the converter