Momentum is the simplest NinjaScript oscillator there is — one subtraction against a
bar Period ago. See the explainer for the concept.
What you’ll need
- NinjaTrader 8
- New → NinjaScript Editor → Indicators → New Indicator, name it
AlgoGenMomentum
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 AlgoGenMomentum : Indicator
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "AlgoGen Momentum";
Description = "Momentum (difference form)";
Period = 10;
IsOverlay = false;
AddPlot(new Stroke(Brushes.DodgerBlue, 2), PlotStyle.Bar, "Mom");
AddLine(Brushes.Gray, 0, "Zero");
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < Period)
return;
Value[0] = Close[0] - Close[Period]; // difference form
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 Momentum to a chart; the histogram 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.- Difference vs ratio. This is the difference form (around 0); for the ratio form
(around 100) use
100.0 * Close[0] / Close[Period]. - No fixed levels. Colour by sign (vs zero) for a clean regime read; judge extremes relative to recent range, or add a signal-line SMA for crossovers.
- Signal line. Store Momentum in a
Series<double>and plotSMA(momSeries, 3)for smoother crossover signals than the raw line. - Trend filter. In a strategy, gate the zero-cross on a longer average —
Close[0] > SMA(200)[0]— to avoid the range-bound whipsaw that plagues any unfiltered momentum tool. - Built-in.
Momentum(Period)[0]exists in NinjaTrader (difference form); the hand-rolled version is for learning and for exposing the ratio option.
That’s the five-language set: Python, MQL5, Pine Script, EasyLanguage, and this one. Now test a Momentum 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)
