ROC in NinjaTrader — NinjaScript (C#) Source

A custom Rate of Change oscillator for NinjaTrader 8

ROC in NinjaTrader
ROC in NinjaTrader

The Rate of Change is about the simplest NinjaScript oscillator you can write — one comparison to a bar Period ago. See the explainer for the concept.

What you’ll need

  • NinjaTrader 8
  • New → NinjaScript Editor → Indicators → New Indicator, name it AlgoGenROC

The source

#region Using declarations
using System;
using System.Windows.Media;
using NinjaTrader.NinjaScript;
using NinjaTrader.NinjaScript.Indicators;
#endregion

namespace NinjaTrader.NinjaScript.Indicators
{
    public class AlgoGenROC : Indicator
    {
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Name        = "AlgoGen ROC";
                Description = "Rate of Change (percent)";
                Period      = 12;
                IsOverlay   = false;

                AddPlot(new Stroke(Brushes.DodgerBlue, 2), PlotStyle.Line, "ROC");
                AddLine(Brushes.Gray, 0, "Zero");
            }
        }

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

            double past = Close[Period];
            Value[0] = past != 0 ? 100.0 * (Close[0] - past) / past : 0.0;
            PlotBrushes[0][0] = Value[0] >= 0 ? Brushes.MediumSeaGreen : Brushes.IndianRed;
        }

        #region Properties
        [NinjaScriptProperty]
        [System.ComponentModel.DataAnnotations.Range(1, int.MaxValue)]
        [System.ComponentModel.Display(Name = "Period", GroupName = "Parameters", Order = 0)]
        public int Period { get; set; }
        #endregion
    }
}

Compile & apply

  1. Press F5 to compile.
  2. Add AlgoGen ROC to a chart; the oscillator draws in its own panel around a zero line, matching the output chart.

Gotchas

  • Close[Period] is the close Period bars ago; the CurrentBar < Period guard ensures it exists before you divide.
  • Divide-by-zero guard. past != 0 protects the ratio.
  • Percentage vs difference. This is the percent-change ROC; for the absolute Momentum, just plot Close[0] - Close[Period]. Colour by sign (compare to zero) to make the regime pop.
  • Add a signal line. Store ROC in a Series<double> and plot SMA(rocSeries, 9) for crossover signals — the raw line is jumpy on its own.
  • Adaptive extremes. Since ROC has no fixed bounds, flag stretches with a rolling z-score ((Value[0] - SMA(rocSeries,100)[0]) / StdDev(rocSeries,100)[0]) rather than a hard threshold that won’t travel across instruments.

That’s the five-language set: Python, MQL5, Pine Script, EasyLanguage, and this one. Now test an ROC rule in AlgoGen.


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