during coding of an indicator I've noticed that a data series will return the value of the last bar where data has been set even if it was long ago, if there is no data at this index of the data series.
Later I found out that I should have checked isValidDataPointAt
https://ninjatrader.com/support/help...atapointat.htm
Just voicing my opinion that it would be much more intuitive to return null or 0 instead of repeating the previous value.
Here is an example without isValidDataPointAt, relevant line is in OnBarUpdate:
public class VolumeThreshold : Indicator { private MIN minVol; private MAX maxVol; private Series<int> signalSeries; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Volume Threshold"; Name = "VolumeThreshold"; Calculate = Calculate.OnBarClose; IsOverlay = true; DisplayInDataBox = false; DrawOnPricePanel = true; DrawHorizontalGridLines = true; DrawVerticalGridLines = true; PaintPriceMarkers = false; ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right; //Disable this property if your indicator requires custom values that cumulate with each new market data event. //See Help Guide for additional information. IsSuspendedWhileInactive = true; BarsRequiredToPlot = 2; AddPlot(new Stroke(Brushes.Yellow, 2), PlotStyle.Dot, "VolHighPlot"); AddPlot(new Stroke(Brushes.Cyan, 2), PlotStyle.Dot, "VolLowPlot"); } else if (State == State.Configure) { } else if(State == State.DataLoaded) { minVol = MIN(Volume, 20); maxVol = MAX(Volume, 20); signalSeries = new Series<int>(this, MaximumBarsLookBack.TwoHundredFiftySix); } } protected override void OnBarUpdate() { if(CurrentBar < BarsRequiredToPlog) { return; } signalSeries[0] = 0; //initializing, otherwise keeps repeating old value double thresholdMax = maxVol[0] * 0.8; double thresholdMin = minVol[0] * 1.2; if(Volume[0] >= thresholdMax) { signalSeries[0] = 1; } if(Volume[0] <= thresholdMin) { signalSeries[0] = -1; } if(signalSeries[0] == 1 && signalSeries[1] == 1) { Values[0][0] = High[0] + 5 * TickSize; } if(signalSeries[0] == -1 && signalSeries[1] == -1) { Values[1][0] = Low[0] - 5 * TickSize; } } }
Comment