ta.rma in MQL5: Wilder smoothing, iMA MODE_SMMA, SMA seed
Short answer
Use iMA with MODE_SMMA, MetaTrader's smoothed moving average: it is Wilder's average with alpha = 1/length, seeded with a simple average, and agrees bar for bar. Replacing it with an ordinary EMA is the mistake that breaks conversions.
ta.rma(source, length) is the moving average Welles Wilder used inside RSI and ATR: an exponential average with alpha = 1 / length, started from a simple average of the first length values. MetaTrader calls the same thing the smoothed moving average, iMA with MODE_SMMA. The two agree bar for bar given the same candles; the mistake that breaks conversions is replacing it with an ordinary EMA.
What ta.rma computes
The result is na on the first length - 1 bars. On bar length - 1 (the first bar with a full window) it equals the SMA of those length values. From then on rma = alpha * source + (1 - alpha) * rma[1] with alpha = 1 / length. MetaTrader writes the same recursion as (prev * (length - 1) + price) / length, which is algebraically identical.
Compared with ta.ema of the same length, the RMA is slower: for length = 14 its alpha is 0.0714 against the EMA's 0.1333. An RMA of length n reacts like an EMA of length 2n - 1. This is why a converted RSI or ATR built on an EMA "looks right" but never matches: the smoothing constant is nearly half of what it should be. The start also matters more: because alpha is smaller, the seed takes roughly twice as many bars to fade as the EMA's.
The MQL5 equivalent
//@version=6
indicator("RMA", overlay = true)
len = input.int(14, "Length")
plot(ta.rma(close, len), "RMA")A complete MQL5 indicator with the same seed rule and recursion, arrays oldest-first:
//+------------------------------------------------------------------+
//| ta.rma(source, length): Wilder smoothing, alpha = 1 / length, |
//| seeded with the SMA of the first `length` values |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_label1 "RMA (ta.rma)"
input int InpLength = 14; // length
double RmaBuffer[];
int OnInit()
{
SetIndexBuffer(0, RmaBuffer, INDICATOR_DATA);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpLength - 1);
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(InpLength <= 0)
return 0;
const double alpha = 1.0 / InpLength;
int start = prev_calculated > 0 ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
{
if(i < InpLength - 1)
{
RmaBuffer[i] = EMPTY_VALUE; // na before the seed
continue;
}
if(i == InpLength - 1 || RmaBuffer[i - 1] == EMPTY_VALUE)
{
double sum = 0.0; // seed: SMA of the first `length` values
for(int k = 0; k < InpLength; k++)
sum += close[i - k];
RmaBuffer[i] = sum / InpLength;
continue;
}
RmaBuffer[i] = alpha * close[i] + (1.0 - alpha) * RmaBuffer[i - 1];
}
return rates_total;
}For a price source the built-in smoothed average is the same calculation and saves the loop:
// OnInit
maHandle = iMA(_Symbol, _Period, InpLength, 0, MODE_SMMA, PRICE_CLOSE);
// OnCalculate
int toCopy = prev_calculated == 0 ? rates_total : rates_total - prev_calculated + 1;
if(CopyBuffer(maHandle, 0, 0, toCopy, RmaBuffer) <= 0)
return 0;When the source is not a price, for example the true range for an ATR or the gain and loss series for an RSI, keep the loop and run it over your own buffer. That is also the only way to seed exactly where Pine seeds.
Where the two differ
| Topic | Pine Script | MQL5 |
|---|---|---|
| Seed | SMA of the first length values | MODE_SMMA: SMA of the first length prices of the loaded history; hand-written: as coded above |
| Warm-up | na for length - 1 bars | EMPTY_VALUE if you write it; the built-in's values before the first full window are not na and should not be read |
| Recursion | alpha * src + (1 - alpha) * prev | Same, or (prev * (len - 1) + src) / len |
| Start of history | TradingView's first bar | MT5's first bar; the seed differs because the first length candles differ, and the gap fades over roughly ten times length bars |
| na inside the series | Result is na on that bar | Your choice; the indicator above re-seeds with an SMA after an EMPTY_VALUE, which is what the Stratlate runtime does too |
| Realtime bar | Recomputed from rma[1] every tick | Same if the loop reads RmaBuffer[i - 1] and restarts at prev_calculated - 1 |
What the Stratlate converter emits
The engine maps ta.rma to a stateful runtime call, pine_rma(close, __st0, __pineBar, (int)(len)), with one state array per call site so two RMAs never share a recursion. The runtime returns na before length bars exist, seeds with the SMA of the first length values and then applies Wilder's alpha, reading the previous bar's slot so a recalculated realtime bar is idempotent. A computed source without an array behind it is handled by pine_rma_step, which keeps its own count of valid values so the SMA seed lands on the right bar even when the source starts with a run of na. The mapping is reported as exact, and the Pine script above converts CLEAN. The same helper sits underneath the engine's ta.rsi and ta.atr. Compile and backtest the result and compare recent bars.
Pitfalls
- Using
MODE_EMAor2 / (length + 1). The line follows price too fast and the gap never closes. RSI built this way overshoots 70 and 30 more often than TradingView's. - Seeding with the first value instead of the SMA. Early values differ and the plot starts
length - 1bars too early. The error fades, but a backtest over the first bars sees different signals. - Comparing the left edge of the chart. With alpha = 1/14 the seed still carries about 5% weight after 40 bars. Compare bars several hundred bars into the history.
- Integer arithmetic.
1 / InpLengthis 0 in MQL5. Write1.0 / InpLength. - Reading a state variable updated per tick. Every tick of the forming bar compounds the smoothing. Keep the state per bar.
FAQ
Is iMA MODE_SMMA really the same as ta.rma?
Yes. The smoothed moving average is (prev * (length - 1) + price) / length, which equals alpha * price + (1 - alpha) * prev with alpha = 1 / length, and it is seeded with a simple average of the first length values. The only sources of difference are the candles and how far back each history starts.
What EMA length behaves like an RMA of length n?
Matching alphas gives 2 / (L + 1) = 1 / n, so L = 2n - 1. An RMA of 14 reacts like an EMA of 27. This is a useful sanity check when a converted line looks about twice as fast as it should.
Can I read the first bars of iMA MODE_SMMA in an EA?
Do not rely on them. Before the first full window the built-in has no seed, and what the buffer holds there is not a missing-value marker you can test like Pine's na. Skip the first length bars of history, check BarsCalculated on the handle, or compute the average yourself and write EMPTY_VALUE.
Convert the whole script
Paste a Pine Script indicator or strategy and get MQL5 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