ta.cmo in MQL5: Chande Momentum Oscillator by hand
Short answer
MetaTrader 5 has no iCMO, and iChaikin is a different indicator. Write a short loop: the sum of up moves minus the sum of down moves over length bars, divided by the sum of both, times 100.
ta.cmo(series, length) is Tushar Chande's Momentum Oscillator: the sum of the up moves minus the sum of the down moves over the last length bars, divided by the sum of both, times 100. It runs from -100 (every bar fell) to +100 (every bar rose). MetaTrader 5 has no iCMO: the built-in list stops at RSI, Momentum and the Chaikin Oscillator (iChaikin, a different indicator despite the similar initials). The MQL5 equivalent is a short loop, shown in full below.
What ta.cmo computes
For each of the last length bars take the one-bar change d = series - series[1]. Add positive changes to up and the absolute value of negative changes to down; a zero change adds to neither. Then cmo = 100 * (up - down) / (up + down). Unlike the RSI there is no smoothing: the sums are plain sliding sums, so there is no seed and no slow convergence. Because length changes need length + 1 values, the first result appears on bar index length, and it is na before that and whenever a value in the window is na. A window with no movement at all has up + down = 0, which is a division by zero, and the result is na.
A useful identity: with plain sums instead of Wilder averages, cmo = 2 * rsi - 100 would hold if RSI used simple sums. It does not (it uses ta.rma), so you cannot get the CMO by rescaling iRSI.
The MQL5 equivalent
//@version=6
indicator("CMO ref")
c = ta.cmo(close, 9)
plot(c, "CMO")The MQL5 indicator uses the short form of OnCalculate, so the "Apply to" setting in the indicator dialog plays the role of Pine's series argument for price sources. For a computed series, replace price[] with your own calculation buffer.
//+------------------------------------------------------------------+
//| Chande Momentum Oscillator matching ta.cmo(source, length) |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_label1 "CMO"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_minimum -100
#property indicator_maximum 100
#property indicator_level1 -50
#property indicator_level2 50
input int InpLength = 9; // Length
double CmoBuf[];
int OnInit()
{
SetIndexBuffer(0, CmoBuf, 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++)
{
// `length` one-bar changes need length + 1 prices
if(i < InpLength) { CmoBuf[i] = EMPTY_VALUE; continue; }
double up = 0.0, dn = 0.0;
for(int k = 0; k < InpLength; k++)
{
const double d = price[i - k] - price[i - k - 1];
if(d > 0.0) up += d; else dn -= d;
}
CmoBuf[i] = (up + dn) == 0.0 ? EMPTY_VALUE : 100.0 * (up - dn) / (up + dn);
}
return rates_total;
}The inner loop is O(length) per bar, which is negligible for typical lengths. If you need a length in the hundreds, keep running sums and subtract the change that leaves the window instead.
Where the two differ
| Aspect | Pine ta.cmo | MQL5 |
|---|---|---|
| Built-in | Yes | No; write it, or use a custom indicator via iCustom |
| First value | Bar index length | Whatever your loop writes; use EMPTY_VALUE before length |
| Flat window | na | Must be guarded in your code; write EMPTY_VALUE |
| Source | Any series | price[] from the short OnCalculate form, or any buffer |
| Index direction | series[1] is the previous bar | price[i - 1] in an oldest-first loop |
Because the CMO is stateless, a correct MQL5 version matches TradingView from its first value. A persistent difference points at the data (bid prices, a different session calendar) rather than the arithmetic.
What the Stratlate converter emits
The converter treats ta.cmo as a windowed indicator and emits its runtime helper pine_cmo(src, bar, length). The helper returns na until length + 1 values exist or when any value in the window is na, sums the positive and negative one-bar changes, and divides through pine_div, which returns na for a zero denominator. A computed source is first materialised into an array. The mapping is reported as exact; the test script above converts CLEAN. Compile and backtest the result before relying on it.
Pitfalls
- Looking for
iCMOand settling foriChaikin. The Chaikin Oscillator is an MACD of the accumulation/distribution line; it has nothing to do with Chande's oscillator. - Starting the window at
length - 1. The loop readsprice[-1]on the first eligible bar and the indicator stops with "array out of range". - Counting up and down bars instead of summing the size of the moves. The result is a different, coarser oscillator that saturates at ±100 far more often.
- Rescaling
iRSIwith2 * rsi - 100. The shape is similar, but RSI smooths with Wilder's average and the values differ by several points. - Smoothing with an EMA "to reduce noise" and then comparing with
ta.cmo. That is a different indicator (closer to a VIDYA input); compare raw against raw.
FAQ
Does MetaTrader 5 have a Chande Momentum Oscillator?
Not as a built-in function. MT5 uses the CMO internally in its VIDYA moving average, but it does not expose it. Write the oscillator yourself as on this page, or load a custom indicator with iCustom.
When does ta.cmo produce its first value?
On bar index length, because length one-bar changes need length + 1 values. Before that it is na, and an MQL5 version should write EMPTY_VALUE on those bars so nothing is drawn.
Is ta.cmo the same as an RSI rescaled to -100..100?
No. The CMO uses plain sums of up and down moves over a sliding window, while the RSI uses Wilder-smoothed averages that carry information from all earlier bars. They move in the same direction but differ in 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