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
- Press F5 to compile.
- 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 closePeriodbars ago; theCurrentBar < Periodguard ensures it exists before you divide.- Divide-by-zero guard.
past != 0protects 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 plotSMA(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
- NinjaScript system indicator methods (NinjaTrader)
