Chande Momentum Oscillator in NinjaScript — NinjaTrader 8

The built-in CMO and a from-scratch C# version

Chande Momentum Oscillator in NinjaScript
Chande Momentum Oscillator in NinjaScript

NinjaTrader 8 ships a CMO indicator you can call from any script, and coding it from scratch is a short C# loop. See the explainer.

What you’ll need

  • NinjaTrader 8 and the NinjaScript Editor (New → Indicator)

The built-in

// Anywhere in OnBarUpdate:
double cmoVal = CMO(9)[0];   // 9-period Chande Momentum Oscillator of Close

From scratch

To own the math and prove what the built-in does:

public class AlgoGenCMO : Indicator
{
    private int period = 9;

    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            Name = "AlgoGen CMO";
            IsOverlay = false;
            AddPlot(Brushes.DodgerBlue, "CMO");
            AddLine(Brushes.Gray,  50, "OB");
            AddLine(Brushes.Gray, -50, "OS");
            AddLine(Brushes.DarkGray, 0, "Zero");
        }
    }

    protected override void OnBarUpdate()
    {
        if (CurrentBar < period)
            return;

        double su = 0, sd = 0;
        for (int k = 0; k < period; k++)
        {
            double diff = Close[k] - Close[k + 1];
            if (diff > 0) su += diff;
            else          sd += -diff;
        }
        double denom = su + sd;
        Value[0] = denom == 0 ? 0 : 100.0 * (su - sd) / denom;
    }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name = "Period", GroupName = "Parameters", Order = 0)]
    public int Period
    {
        get { return period; }
        set { period = Math.Max(1, value); }
    }
}

Close[k] indexes back k bars, so Close[k] - Close[k+1] is each bar’s change; up-moves accumulate into su, down-moves (as positives) into sd. Then 100 * (su - sd) / (su + sd) is the CMO — the ±100 line from the output chart.

Gotchas

  • No smoothing. Plain summation; adding an EMA would turn it into RSI’s cousin.
  • Warm-up guard. if (CurrentBar < period) return; because Close[k+1] reads one bar past the window.
  • Guard the zero denominator. Flat window → su + sd == 0; return 0.
  • ±50 lines. Wider zones than RSI, matching the CMO’s faster swings.

A zero-cross signal

if (Value[0] > 0 && Value[1] <= 0)
    Draw.ArrowUp(this, "up" + CurrentBar, false, 0, Low[0] - TickSize, Brushes.Green);

The zero cross is the CMO’s cleanest directional cue — easy to test in AlgoGen.

Same indicator elsewhere: Python, MQL5, Pine Script, EasyLanguage.


This post is educational, not financial advice. Indicators describe the past; they don’t predict the future. Backtest anything before you risk real money on it.

Sources and further reading

  1. NinjaScript system indicator methods (NinjaTrader)

Historical research from the Algogen archive. Not investment advice.

Blog

Have a strategy idea?

Describe it in plain English, preview where your rules fire, then decide whether the evidence is worth a backtest.

Keep me postedHow it works