ta.mom in MQL5: not iMomentum, which returns a ratio
Short answer
Not iMomentum: it returns price / price[length] * 100, a ratio around 100. ta.mom(src, len) is src[i] - src[i - len], so write the subtraction; a test like ta.mom(close, 10) > 0 would otherwise become iMomentum > 100.
ta.mom(source, length) is the plain momentum: source - source[length], the change over length bars in price units, oscillating around zero. MetaTrader 5's iMomentum has the same name and the same inputs but returns source / source[length] * 100, a ratio that oscillates around 100. They are not interchangeable: a Pine script that tests ta.mom(close, 10) > 0 must test iMomentum > 100, and anything that uses the magnitude (a threshold in points, a smoothed momentum, a histogram) needs the subtraction written out, which is a one-liner.
What ta.mom computes
ta.mom(src, len) equals src - src[len], and is identical to ta.change(src, len). It is na on the first len bars, where src[len] does not exist, and na whenever either operand is na. There is no smoothing, no seed and no state: each bar's value depends only on two source values, so a correct MQL5 version agrees from the first value and on the realtime bar it simply follows the current price. The unit is the source's unit (price), so the scale of the line changes with the instrument: 5 points of momentum on an index future is nothing, on EURUSD it is everything.
The MQL5 equivalent
//@version=6
indicator("Momentum ref")
m = ta.mom(close, 10)
plot(m, "Momentum")The MQL5 indicator uses the short OnCalculate form so the user can choose the applied price, the equivalent of Pine's source. Arrays are oldest-first, so source[length] is price[i - InpLength].
//+------------------------------------------------------------------+
//| ta.mom(source, length): the plain difference, not iMomentum's ratio |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_label1 "Momentum"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_level1 0
input int InpLength = 10; // Length
double MomBuf[];
int OnInit()
{
SetIndexBuffer(0, MomBuf, INDICATOR_DATA);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpLength);
return INIT_SUCCEEDED;
}
int OnCalculate(const int rates_total, const int prev_calculated,
const int begin, const double &price[])
{
int start = prev_calculated > 1 ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
MomBuf[i] = i < InpLength ? EMPTY_VALUE // source[length] does not exist yet
: price[i] - price[i - InpLength];
return rates_total;
}If a built-in handle is already in the program, you can turn the ratio into the difference, but you need the old price for it, so the subtraction is simpler:
// iMomentum(symbol, timeframe, length, applied_price) returns
// price / price[length] * 100 -- a ratio centred on 100,
// while ta.mom(src, length) is src - src[length] -- a difference centred on 0.
int momHandle = iMomentum(_Symbol, _Period, 10, PRICE_CLOSE);
// Converting one to the other needs the old price: mom = (ratio / 100 - 1) * price[i - length]Where the two differ
| Aspect | Pine ta.mom | MQL5 iMomentum |
|---|---|---|
| Formula | src - src[len] | src / src[len] * 100 |
| Centre line | 0 | 100 |
| Unit | Price units | Percent of the old price, plus 100 |
| First value | Bar index len; na before | Bar index len; 0 before, hidden by the plot |
| Source | Any series | Applied price constant |
| Zero old price | Fine; a difference | Division by zero (only on synthetic data) |
The sign test survives the translation: ta.mom > 0 is exactly iMomentum > 100, and a zero-line cross of one is a 100-line cross of the other, so a strategy that only uses the sign gives the same trades. Everything else differs. ta.sma(ta.mom(close, 10), 5) is an average of price differences; the average of iMomentum values is an average of ratios, and the two cross their centre lines on different bars. Levels such as "momentum above 50 points" have no counterpart on the ratio at all, because the same ratio means a different number of points at every price level.
What the Stratlate converter emits
The converter maps ta.mom(src, len) to its runtime helper pine_change(src, bar, len), the same helper used for ta.change: it returns src[i] - src[i - len] and na when the older value is out of range or either value is na. It never emits iMomentum on the chart timeframe, so the sign and the scale are Pine's. Inside request.security(...) on another timeframe the engine does create an iMomentum handle for a momentum of a price; the higher-timeframe read is flagged in the report as an approximation because session boundaries differ, and the ratio-versus-difference question applies to that path, so check the values in that case. The chart-timeframe mapping is reported as exact and the test script converts CLEAN. Compile and backtest the result.
Pitfalls
- Replacing
ta.momwithiMomentumand keeping> 0as the condition. The ratio is always positive, so the condition is always true and the strategy is permanently long. - Subtracting 100 from
iMomentumto "convert" it. You get a percentage change, which ista.roc, notta.mom; thresholds in points are still wrong. - Writing
price[i + InpLength]in an oldest-first loop. That is the future bar; the indicator looks prescient in the tester and reads past the array on the newest bars. - Plotting 0 on the first
lenbars instead ofEMPTY_VALUE. A spike from zero appears at the left edge of the chart. - Confusing
ta.momwithta.roc. ROC divides by the old price; a Pine script that uses ROC needs the division, which this page's code does not do.
FAQ
Is iMomentum the MQL5 version of ta.mom?
No. iMomentum returns price divided by the price length bars ago, times 100, so it is centred on 100 and measured in percent. ta.mom returns the price difference, centred on 0 and measured in price units. Only the sign test (above or below the centre line) is the same.
What is the quickest MQL5 expression for ta.mom(close, 10)?
close[i] - close[i - 10] in an oldest-first OnCalculate loop, guarded by i >= 10. With series-indexed arrays (ArraySetAsSeries true) it is close[i] - close[i + 10] instead, guarded against the end of the array.
Does ta.mom need a warm-up or a seed?
No. It has no state, so there is nothing to seed and no slow convergence; the first value on bar index length is already exact. Differences from TradingView on later bars come from different candles, not from the formula.
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