Example:
public class SupportingIndicator : Indicator
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"SupportingIndicator";
Name = "SupportingIndicator";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = false;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
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;
AddPlot(Brushes.Red, "SupportingPlot");
}
else if (State == State.Configure)
{
AddDataSeries(Data.BarsPeriodType.Tick, 1);
}
}
protected override void OnBarUpdate()
{
if(BarsInProgress != 0 || State != State.Realtime) return;
Value[0] = Close[0];
Print("Sup Last bar: " + CurrentBars[1] + " Time: " + Time[0]);
}
public class PrimaryIndicator : Indicator
{
private SupportingIndicator supportingIndicator;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"";
Name = "PrimaryIndicator";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
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;
AddPlot(Brushes.Red, "PrimaryPlot");
}
else if (State == State.Configure)
{
AddDataSeries(Data.BarsPeriodType.Tick, 1);
} else if(State == State.DataLoaded) {
supportingIndicator = SupportingIndicator();
}
}
protected override void OnBarUpdate()
{
if(BarsInProgress != 0 || State != State.Realtime) return;
Value[0] = supportingIndicator[0];
Print("Pri Last bar: " + CurrentBars[1] + " Time: " + Time[0]);
}
Sup Last bar: 1226965 Time: 19/02/2020 11:23:28 Pri Last bar: 1227331 Time: 19/02/2020 11:23:28 Sup Last bar: 1227332 Time: 19/02/2020 11:23:37 Pri Last bar: 1227593 Time: 19/02/2020 11:23:37 Sup Last bar: 1227594 Time: 19/02/2020 11:23:59 Pri Last bar: 1227938 Time: 19/02/2020 11:23:59 Sup Last bar: 1227939 Time: 19/02/2020 11:24:36 Pri Last bar: 1228297 Time: 19/02/2020 11:24:36 Sup Last bar: 1228298 Time: 19/02/2020 11:25:06 Pri Last bar: 1228635 Time: 19/02/2020 11:25:06
My solution is to use a helper class instead and use it to calculate the data without calling a secondary indicator;
Example:
namespace NinjaTrader.NinjaScript.Indicators.Helper
{
public class CalculationHelper {
private TimeSeries[] Times;
private int[] CurrentBars;
private int SeriesIndex;
private int MasterIndex;
private Bars Bars;
private Series<double> OutputSeries;
public CalculationHelper(
Bars bars
, TimeSeries[] times
, Series<double> outputSeries
, int[] currentBars
, int seriesIndex
, int masterIndex
) {
this.Bars = bars;
this.Times = times;
this.OutputSeries = outputSeries;
this.CurrentBars = currentBars;
this.SeriesIndex = seriesIndex;
this.MasterIndex = masterIndex;
}
public void OnBarUpdate() {
//do something
OutputSeries[0] = whatever
}
}
public class PrimaryIndicator : Indicator
{
private Series<double> supportingSeries;
private NinjaTrader.NinjaScript.Indicators.Helper.CalculationHelper calculationHelper = null;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"";
Name = "PrimaryIndicator";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
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;
AddPlot(Brushes.Red, "PrimaryPlot");
}
else if (State == State.Configure)
{
AddDataSeries(Data.BarsPeriodType.Tick, 1);
} else if(State == State.DataLoaded) {
supportingSeries = new Series<double>(this, MaximumBarsLookBack.TwoHundredFiftySix);
calculationHelper = new NinjaTrader.NinjaScript.Indicators.Helper.CalculationHelper(BarsArray[1], Times, supportingSeries, CurrentBars, 1, 0);
}
}
protected override void OnBarUpdate()
{
if(BarsInProgress != 0 || State != State.Realtime) return;
calculationHelper.OnBarUpdate();
Value[0] = supportingSeries[0];
}
Unfortunately I'm not well versed in NinjaScript but I'd like to at least save time for other people facing the same issue.

Comment