ta.roc in MQL5: percentage change with no built-in iROC
Short answer
MetaTrader 5 has no iROC, and the sample ROC indicator divides by the current price. Write 100 * (price[i] - price[i - len]) / price[i - len] in the OnCalculate loop.
ta.roc(source, length) is the rate of change: the percentage by which the source moved over the last length bars, 100 * (source - source[length]) / source[length]. MetaTrader 5 has no iROC among its built-in indicator functions, and the sample ROC indicator that ships in the Examples folder divides by the current price rather than the old one, so neither gives TradingView's number. The MQL5 equivalent is the formula written out, three lines in an OnCalculate loop.
What ta.roc computes
ta.roc(src, len) = 100 * ta.change(src, len) / src[len]. It is na on the first len bars, where the older value does not exist, and na when either value is na. A zero old value makes the division undefined, which Pine also reports as na; on real price data this does not occur. The denominator is the old value, so a move from 100 to 110 is +10% and the move back from 110 to 100 is -9.09%, not -10%. Like ta.mom, ROC has no state: each value depends on two source values only, so there is no seed and no warm-up beyond the first len bars, and the realtime bar's value follows the current price tick by tick.
The MQL5 equivalent
//@version=6
indicator("ROC ref")
r = ta.roc(close, 9)
plot(r, "ROC")The MQL5 indicator uses the short OnCalculate form, which gives the user an "Apply to" price selector in place of Pine's source. price[i - InpLength] is source[length] because the arrays arrive oldest-first.
//+------------------------------------------------------------------+
//| ta.roc(source, length): percentage change over length bars |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_label1 "ROC"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_level1 0
input int InpLength = 9; // Length
double RocBuf[];
int OnInit()
{
SetIndexBuffer(0, RocBuf, 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++)
{
if(i < InpLength) { RocBuf[i] = EMPTY_VALUE; continue; }
const double prev = price[i - InpLength]; // the denominator is the OLD value
RocBuf[i] = prev == 0.0 ? EMPTY_VALUE : 100.0 * (price[i] - prev) / prev;
}
return rates_total;
}If you need the ROC of a computed series, replace price[] with your own calculation buffer; nothing else changes.
Where the two differ
| Aspect | Pine ta.roc | MQL5 |
|---|---|---|
| Built-in | ta.roc | None; iMomentum is a ratio times 100, not a percentage change |
| Examples\ROC.mq5 | Divides by the old value | Divides by the current value, so the sign is the same but the magnitude differs |
| First value | Bar index len; na before | Whatever you write; use EMPTY_VALUE |
| Zero denominator | na | Must be guarded; an unguarded double division gives inf |
| Index direction | src[len] is len bars back | price[i - len] in an oldest-first loop |
The relationship with iMomentum is exact and worth knowing: iMomentum = 100 * src / src[len], therefore ta.roc = iMomentum - 100. If an Expert Advisor already holds an iMomentum handle, subtracting 100 from its buffer gives TradingView's ROC without further work. The Examples ROC, by contrast, cannot be rescaled into ta.roc, because (p - p[n]) / p and (p - p[n]) / p[n] differ by a bar-dependent factor; for small moves the two are close, for a 10% move they differ by about one percentage point.
What the Stratlate converter emits
The converter emits 100.0 * pine_div(pine_change(src, bar, len), pine_hist(src, bar, len)): pine_change is the len-bar difference, pine_hist is src[len] with Pine's out-of-range guard, and pine_div returns na for a zero denominator, so a missing old value or a zero price gives na instead of a crash. No handle is created. The mapping is reported as exact; the test script above converts CLEAN. Compile the output and backtest it before trading on it.
Pitfalls
- Using the Examples ROC or copying its line. The divisor is the current price, so the values are a few percent off TradingView's on large moves and a threshold such as "ROC > 5" fires on different bars.
- Reaching for
iMomentumas "the momentum indicator" and forgetting the- 100. The ROC then shows 100 instead of 0 and every threshold is off by 100. - Integer inputs in the formula:
100 * (a - b) / bis fine with doubles, but(a - b) / b * 100with anintsource buffer truncates to 0 before the multiplication. - Reading
price[i + len]in an oldest-first loop. That is a future bar, and on the lastlenbars it is out of range. - Plotting 0 instead of
EMPTY_VALUEon the firstlenbars, which draws a line from zero at the left edge.
FAQ
Is there an iROC function in MQL5?
No. The built-in list has iMomentum, which returns the price ratio times 100, and there is a ROC sample indicator in MetaEditor's Examples folder that divides by the current price. Neither equals ta.roc; write the percentage change yourself or subtract 100 from iMomentum.
How do I get ta.roc from an existing iMomentum handle?
Subtract 100 from the copied buffer value: ta.roc(src, len) equals iMomentum(len) - 100 on every bar, because both use the price len bars ago as the denominator. Make sure the applied price matches the Pine source.
Why does ta.roc differ from ta.mom?
ta.mom is the raw difference in price units and ta.roc is that difference divided by the old price, in percent. On a chart where the price level changes a lot, ROC is comparable across time and ta.mom is not; the two cross zero on the same bars.
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