iMomentum in Pine Script: a ratio times 100, not ta.mom
Short answer
Use src / src[n] * 100, a ratio around 100. Pine's ta.mom subtracts instead of dividing and oscillates around 0, so it is a different indicator.
iMomentum returns a handle to MetaTrader's Momentum indicator, which divides the current price by the price n bars ago and multiplies by 100, so the line oscillates around 100. Pine Script's ta.mom(source, length) has the same name but subtracts instead of dividing and oscillates around 0. The exact port is src / src[n] * 100; ta.mom is a different indicator.
What iMomentum computes
The signature is iMomentum(symbol, timeframe, mom_period, applied_price) with one buffer. The stock indicator (Indicators\Examples\Momentum.mq5) sets, for every bar i ≥ n, Momentumi = pricei · 100 / pricei−n over the applied price. A reading of 102 means the price is 2% above where it was n bars ago; 98 means 2% below. There is no smoothing and no recursion, so each value depends on exactly two prices and nothing about the history start matters. The first n cells are not computed and are hidden by PLOT_DRAW_BEGIN.
The Pine Script equivalent
Write the ratio with the history operator. src[len] is na on the first len bars, so the result is na there, matching MetaTrader's undrawn warm-up. If you prefer a percentage around zero, ta.roc(src, len) is 100 · (src − src[len]) / src[len], which equals MetaTrader's momentum minus 100. The script shows the MetaTrader form and computes ta.mom for contrast.
//@version=6
indicator("iMomentum port", overlay = false)
len = input.int(14, "Period", minval = 1)
src = input.source(close, "Applied price")
momMt5 = src / src[len] * 100 // MetaTrader: ratio, oscillates around 100
momPine = ta.mom(src, len) // Pine: difference, oscillates around 0
plot(momMt5, "Momentum (iMomentum)", color.rgb(30, 144, 255))
hline(100, "Centre", color.gray)The MQL5 indicator this replaces, with the level line at 100:
#property indicator_separate_window
#property indicator_level1 100
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
input int InpPeriod = 14; // mom_period
input ENUM_APPLIED_PRICE InpPrice = PRICE_CLOSE; // applied_price
double MomBuffer[];
int momHandle;
int OnInit()
{
SetIndexBuffer(0, MomBuffer, INDICATOR_DATA);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpPeriod);
momHandle = iMomentum(_Symbol, _Period, InpPeriod, InpPrice);
if(momHandle == 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(momHandle) < rates_total)
return(0);
int to_copy = (prev_calculated == 0) ? rates_total : rates_total - prev_calculated + 1;
if(CopyBuffer(momHandle, 0, 0, to_copy, MomBuffer) <= 0)
return(0);
return(rates_total);
}Where the two differ
| Topic | MQL5 iMomentum | Pine ta.mom | Pine ta.roc |
|---|---|---|---|
| Formula | p / p[n] · 100 | p − p[n] | 100 · (p − p[n]) / p[n] |
| Centre line | 100 | 0 | 0 |
| Units | Percent of the old price | Price units (pips, dollars) | Percent change |
| Relation to MT5 | — | Different scale; crossings of the centre line happen on the same bars | Exactly MT5 − 100 |
| Warm-up | First n bars not drawn | na for n bars | na for n bars |
| Thresholds | Levels like 99.5 / 100.5 | Depend on the instrument's price level | Levels like −0.5 / 0.5 |
The sign of p − p[n] always agrees with whether p / p[n] is above 100, so a strategy that only tests "above or below the centre" works with either form. Anything that compares magnitudes, smooths the line or uses fixed levels does not.
What the Stratlate converter emits
The MQL5 → Pine engine emits the ratio, not ta.mom: its iMomentum entry renders as ((src) / ((src)[n])) * 100, and the catalog comment records that an earlier ta.mom mapping put every consumer in the wrong units. Our probe of the indicator above converted with no momentum warnings and produced MomBuffer := (((close) / ((close)[math.max(0, int(InpPeriod))])) * 100). In that probe the applied-price input became an input.source but the expression used close, so a script whose applied price is not the close should be checked by hand. Compile and compare the converted line against the MT5 window.
Pitfalls
- Porting
iMomentumasta.mom. The line moves from around 100 to around 0 and every level in the original strategy (for example "momentum > 100.2") becomes meaningless. - Subtracting 100 from
ta.momto fix the scale. That gives a price difference minus 100, not a percentage; use the ratio orta.roc. - Dividing by zero on instruments that can trade at 0 (some spreads and synthetic series). MetaTrader's indicator would fail the same way; guard with
src[len] != 0. - Writing
src[len - 1]because MQL arrays are zero-based. The MT5 formula uses the bar exactly n bars back, which issrc[len]in Pine. - Smoothing the ratio with a different average than the MQL5 code used. If the original called
iMAon the momentum handle, port that average explicitly.
FAQ
Is Pine's ta.mom the same as MetaTrader Momentum?
No. ta.mom is the price difference; MetaTrader's Momentum is the price ratio times 100. They agree only on which side of their centre line they are.
Which Pine function is closest to MetaTrader Momentum?
ta.roc(src, n), which is MetaTrader's value minus 100. Add 100 to it, or write src / src[n] * 100, to get identical numbers.
Does the applied price matter?
Yes, it is the series being compared. PRICE_CLOSE is close, PRICE_TYPICAL is hlc3, PRICE_MEDIAN is hl2, PRICE_WEIGHTED is hlcc4.
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