Pivot Points in NinjaScript use the platform’s session detection to snapshot the prior session’s high, low, and close, then plot the current session’s levels. See the explainer for the formulas.
What you’ll need
- NinjaTrader 8
- New → NinjaScript Editor → Indicators → New Indicator, name it
AlgoGenPivots - An intraday chart
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 AlgoGenPivots : Indicator
{
private double dayHigh, dayLow, priorH, priorL, priorC, prevClose;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "AlgoGen Pivots";
Description = "Daily Pivot Points from the prior session";
IsOverlay = true;
AddPlot(new Stroke(Brushes.Orange, 2), PlotStyle.Line, "P");
AddPlot(new Stroke(Brushes.IndianRed, 1), PlotStyle.Line, "R1");
AddPlot(new Stroke(Brushes.MediumSeaGreen, 1), PlotStyle.Line, "S1");
AddPlot(new Stroke(Brushes.IndianRed, 1), PlotStyle.Line, "R2");
AddPlot(new Stroke(Brushes.MediumSeaGreen, 1), PlotStyle.Line, "S2");
}
}
protected override void OnBarUpdate()
{
if (Bars.IsFirstBarOfSession)
{
priorH = dayHigh; priorL = dayLow; priorC = prevClose;
dayHigh = High[0]; dayLow = Low[0];
}
else
{
dayHigh = Math.Max(dayHigh, High[0]);
dayLow = Math.Min(dayLow, Low[0]);
}
prevClose = Close[0];
if (priorH == 0) return;
double p = (priorH + priorL + priorC) / 3.0;
double rng = priorH - priorL;
Values[0][0] = p;
Values[1][0] = 2 * p - priorL;
Values[2][0] = 2 * p - priorH;
Values[3][0] = p + rng;
Values[4][0] = p - rng;
}
}
}
Compile & apply
- Press F5 to compile.
- Add AlgoGen Pivots to an intraday chart; the pivot and R/S levels overlay price, holding flat through each session, matching the output chart.
Gotchas
Bars.IsFirstBarOfSession. NinjaTrader’s clean session-boundary detector — snap the prior-session high/low/close here, then reset the trackers.- Prior session, not current. The levels come from the completed prior session; never use the live session’s evolving H/L.
- Session template. The levels depend on the instrument’s trading-hours template, so confirm it matches the “day” you intend.
That’s the five-language set: Python, MQL5, Pine Script, EasyLanguage, and this one. Now test a pivot 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)
