iMA in Pine Script: ta.sma, ta.ema, ta.rma or ta.wma by mode
Short answer
Pick the function by ma_method: MODE_SMA is ta.sma, MODE_EMA is ta.ema, MODE_SMMA is ta.rma and MODE_LWMA is ta.wma. The applied price becomes the source argument and ma_shift becomes plot's offset.
iMA creates a handle to MetaTrader's moving-average indicator; you then read its values with CopyBuffer. In Pine Script there is no handle: the moving average is a one-line call that picks the function from the ma_method you used. MODE_SMA is ta.sma, MODE_EMA is ta.ema, MODE_SMMA is ta.rma, MODE_LWMA is ta.wma, the applied_price becomes the source argument and ma_shift becomes the offset argument of plot().
What iMA computes
The MQL5 signature is iMA(symbol, timeframe, ma_period, ma_shift, ma_method, applied_price) and it returns an int handle, not a value. The handle refers to the stock Moving Average indicator, which computes one of four lines over the applied price p with period n:
MODE_SMA: the arithmetic mean of the last n prices. First value on bar index n−1; earlier buffer cells hold 0.0 and are hidden byPLOT_DRAW_BEGIN.MODE_EMA: ema = p·α + emaprev·(1−α) with α = 2/(n+1), seeded with the first price of the series.MODE_SMMA: seeded with the SMA of the first n prices, then (smmaprev·(n−1) + p)/n. This is Wilder's smoothing, an EMA with α = 1/n.MODE_LWMA: weights n, n−1, …, 1 from the newest price backwards, divided by the sum of the weights.
applied_price picks the input series: PRICE_CLOSE, PRICE_OPEN, PRICE_HIGH, PRICE_LOW, PRICE_MEDIAN (high+low)/2, PRICE_TYPICAL (high+low+close)/3 and PRICE_WEIGHTED (high+low+2·close)/4. ma_shift does not change any computed value: it is a PLOT_SHIFT on the indicator's own plot, so values read through CopyBuffer are unshifted.
The Pine Script equivalent
Pine's four averages use the same definitions, including the seeds: ta.ema starts from the first value, ta.rma starts from an SMA of the first length values and then applies α = 1/length, and ta.wma uses linear weights. The applied prices are the built-in series close, open, high, low, hl2, hlc3 and hlcc4.
//@version=6
indicator("iMA port", overlay = true)
len = input.int(20, "Period (ma_period)", minval = 1)
shift = input.int(0, "Shift (ma_shift)")
method = input.string("SMMA", "Method (ma_method)", options = ["SMA", "EMA", "SMMA", "LWMA"])
src = input.source(close, "Applied price")
ma = switch method
"SMA" => ta.sma(src, len)
"EMA" => ta.ema(src, len)
"SMMA" => ta.rma(src, len)
=> ta.wma(src, len)
plot(ma, "MA", color.rgb(30, 144, 255), offset = shift)The same indicator in MQL5, written the way an MT5 program normally consumes iMA: create the handle once in OnInit, then copy the ready-made values into the plot buffer in OnCalculate.
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
input int InpPeriod = 20; // ma_period
input int InpShift = 0; // ma_shift (display only)
input ENUM_MA_METHOD InpMethod = MODE_SMMA; // ma_method
input ENUM_APPLIED_PRICE InpPrice = PRICE_CLOSE; // applied_price
double MaBuffer[];
int maHandle;
int OnInit()
{
SetIndexBuffer(0, MaBuffer, INDICATOR_DATA);
PlotIndexSetInteger(0, PLOT_SHIFT, InpShift);
maHandle = iMA(_Symbol, _Period, InpPeriod, 0, InpMethod, InpPrice);
if(maHandle == 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(maHandle) < rates_total)
return(0); // handle not ready yet, try next tick
int to_copy = (prev_calculated == 0) ? rates_total : rates_total - prev_calculated + 1;
if(CopyBuffer(maHandle, 0, 0, to_copy, MaBuffer) <= 0)
return(0);
return(rates_total);
}Where the two differ
| Topic | MQL5 iMA | Pine Script |
|---|---|---|
| Call model | Handle created once; values arrive later via CopyBuffer, possibly after a few ticks | Value is available on the same line, every bar |
| Method selection | Enum argument ma_method | Four separate functions; use switch on an input if you need a selector |
| Warm-up | Cells before the first value hold 0.0, hidden by draw-begin | na until the window is full |
| Shift | ma_shift moves the drawn line only; buffer values are not shifted | offset in plot() does the same; the series itself is unshifted |
| Applied price | Enum, fixed at handle creation | Any series expression, including another indicator |
| EMA/SMMA history | Seeded at the first bar of the terminal's loaded history | Seeded at the first bar TradingView loads; early bars differ until the seed effect decays |
| Indexing | CopyBuffer arrays are oldest-first unless ArraySetAsSeries | ma[1] is always the previous bar |
What the Stratlate converter emits
The MQL5 → Pine engine dispatches iMA on the ma_method argument and emits ta.sma, ta.ema, ta.rma or ta.wma directly, with the applied price rewritten to the Pine series (we saw PRICE_MEDIAN become hl2); when the method is an input it resolves from the input's default. A handle variable is replaced by that series and the following CopyBuffer becomes an assignment to the buffer. The conversion is reported as exact (no warning) for the value, but the ma_shift argument is dropped rather than turned into a plot offset, and in our probe an applied-price input was emitted as input.source while the call used its default. Compile the output and compare it against the MT5 chart before relying on it.
Pitfalls
- Translating
MODE_SMMAtota.ema. SMMA is Wilder's smoothing (α = 1/n), which ista.rma;ta.emawith the same period reacts roughly twice as fast and every crossover lands on a different bar. - Applying
ma_shiftto the data. If you writema[shift]in Pine instead ofoffset = shift, signals computed from the line move too, while in MT5 only the drawing moved. - Expecting identical EMA values on the first bars. Both platforms seed the EMA from the first bar of their own history; the values converge within a few hundred bars but are not equal at the start of the chart.
- Reading the handle's buffer before it is calculated.
CopyBufferreturns −1 and your plot stays empty until the next tick; Pine has no such state. - Passing
_Periodand expecting a higher-timeframe average. A different timeframe iniMAis arequest.securitycall in Pine; see the multi-timeframe guide.
FAQ
Which Pine function matches MODE_SMMA?
ta.rma. Both seed with a simple average of the first n values and then apply (prev·(n−1) + price)/n, so the two lines agree once their histories start at the same bar.
How do I port ma_shift?
Pass it as offset to plot(). Positive values draw the line to the right, as in MetaTrader. Do not index the series with it unless the MQL5 code also read the shifted buffer by index.
Does iMA with PRICE_WEIGHTED have a Pine equivalent?
Yes, hlcc4 is (high+low+close+close)/4, the same as MetaTrader's weighted price. ohlc4 has no MetaTrader applied-price counterpart.
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