Problem with my code. MAX and SMA are not accepting myDataSeries

Hi, I am trying to code my own indicator but when I try to add myDataseries to MAX or SMA the code does not work.
The code compiles correctly but nothing gets plotted. Can you please verify as I am not able to figure the problem?
Main code is below:
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionBuySellVolume;
Name = “GRINDISpread”;
BarsRequiredToPlot = 10;
Calculate = Calculate.OnEachTick;

			IsOverlay				= false;
			DisplayInDataBox							= true;
			DrawOnPricePanel							= true;
			DrawHorizontalGridLines						= true;
			DrawVerticalGridLines						= true;
			PaintPriceMarkers							= true;
			
			IsSuspendedWhileInactive					= true;
			
			AveragePeriod = 10;
            StdPeriod = 10;

		
			AddPlot(Brushes.Orange, "buys");
			AddPlot(Brushes.WhiteSmoke, "sells");
			AddPlot(Brushes.WhiteSmoke, "PriceVolumeDeltaPlot");
	
		}
		

	
		else if (State == State.DataLoaded)
		{
	
						
			SpreadNormalized	= new Series<double>(this);
			SpreadAvg			= new Series<double>(this);
			
			Max_Accepted			= new Series<double>(this);
			Min_Accepted			= new Series<double>(this);
				
			
		}

	}	
		
	protected override void OnBarUpdate()
	{
		if (CurrentBar < activeBar || CurrentBar <= BarsRequiredToPlot)
			return;

		if (CurrentBar != activeBar)
		{
				
			activeBar = CurrentBar;
		}

	SpreadAvg[0]		=  (Close[0] - Open[0]);	
	
	if (CurrentBar >= AveragePeriod)
	{ 	
	Spread_Bar		=	MAX(SpreadAvg, AveragePeriod)[0];
	}

	Series<double> myDataSeries = new Series<double>(this);
//	double value = SMA(myDataSeries, 20)[0];
	
	
	Sells[0]	=	Max_Accepted[0];//VolumeNormalized[0];	
	Buys[0]		=	Spread_Bar;//SpreadAvg[0];//SpreadNormalized[0];	
	
}

The code you posted appears incomplete so I can’t give you specific suggestions, but I do see a few issues that pop at me.

  1. I don’t see how Max_Accepted and Spread_Bar are calculated. Yet you assign their values to Sells and Buys. So it makes sense that those plots would be empty.

  2. The following line should be in State.DataLoaded with the rest of Series initializations and not in OnBarUpdate

    Series myDataSeries = new Series(this);

  3. The only line that has the SMA() method is commented out.

What you probably want to do is to assign the values that you want to average into one of the Series. Then pass that Series to SMA(). But you’ll need to assign values to that Series before they are passed to SMA().

Hope this helps.

Hi, Thanks and Sorry for the incomplete code.
I am trying to isolate the issue thus eliminating parts of the code.

Please see the revise code below.

If I comment these lines below plots are ok, but as soon as they are active code nothing gets plotted.

Spread_Bar = MAX(SpreadAvg, AveragePeriod)[0];
double value = SMA(myDataSeries, 20)[0];

public class GRINDISpread : Indicator
{
	private		int 				activeBar = 0;
	private		Series<double>		SpreadNormalized;
	
	private		Series<double>		SpreadAvg;
	
	private		Series<double>		Max_Accepted;
	private		Series<double>		Min_Accepted;
	
	private		Series<double>		myDataSeries; 
	
	private  double				Spread_Bar;

	protected override void OnStateChange()
	{
		if (State == State.SetDefaults)
		{
			Description				= NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionBuySellVolume;
			Name					= "GRINDISpread";
			BarsRequiredToPlot		= 10;
			Calculate				= Calculate.OnEachTick;
		
			IsOverlay				= false;
			DisplayInDataBox							= true;
			DrawOnPricePanel							= true;
			DrawHorizontalGridLines						= true;
			DrawVerticalGridLines						= true;
			PaintPriceMarkers							= true;
			
			IsSuspendedWhileInactive					= true;
			
			AveragePeriod = 10;
            StdPeriod = 10;

		
			AddPlot(Brushes.Orange, "buys");
			AddPlot(Brushes.WhiteSmoke, "sells");
			AddPlot(Brushes.WhiteSmoke, "PriceVolumeDeltaPlot");
	
		}
		

		else if (State == State.DataLoaded)
		{
			SpreadNormalized	= new Series<double>(this);
			SpreadAvg			= new Series<double>(this);
			
			Max_Accepted			= new Series<double>(this);
			Min_Accepted			= new Series<double>(this);
			
			myDataSeries 		= new Series<double>(this);
			
		}

	}	
		
	protected override void OnBarUpdate()
	{
		if (CurrentBar < 30)
			return;


	SpreadAvg[0]		=  (Close[0] - Open[0]);	
	Max_Accepted[0] 	=	Close[0];
	myDataSeries[0]		= 	Open[0];
	Spread_Bar			= 	Typical[0];
		
	Spread_Bar		=	MAX(SpreadAvg, AveragePeriod)[0];
		
	double value = SMA(myDataSeries, 20)[0];
	
	Sells[0]	=	Max_Accepted[0];//VolumeNormalized[0];	
	Buys[0]		=	Spread_Bar;//SpreadAvg[0];//SpreadNormalized[0];	
	
}
	
	


	#region Properties
	

	[Browsable(false)]
	[XmlIgnore]
	public Series<double> Buys
	{
		get { return Values[0]; }
	}
	
	[Browsable(false)]
	[XmlIgnore]
	public Series<double> Sells
	{
		get { return Values[1]; }
	}
	
	
	[Browsable(false)]
	[XmlIgnore]
	public Series<double> PriceVolumeDeltaPlot
	{
		get { return Values[2]; }
	}
	
	[Browsable(false)]
	[XmlIgnore]
	public Series<double> VolumeDeltaPlot
	{
		get { return Values[3]; }
	}
	
	
	 [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name = "AveragePeriod", Order = 80, GroupName = "Parameters")]
    public int AveragePeriod
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name = "StdPeriod", Order = 90, GroupName = "Parameters")]
    public int StdPeriod
    { get; set; }
	#endregion

Try these changes:
protected override void OnBarUpdate()
{
//if (CurrentBar < 30)
//return;

SpreadAvg[0]	=  (Close[0] - Open[0]);	
Max_Accepted[0] 	=	Close[0];
myDataSeries[0]	= 	Open[0];
//Spread_Bar	= 	Typical[0];

if (CurrentBar < 30)
{
	Spread_Bar	= 	Typical[0];
	return;
}
	
//Spread_Bar	=	MAX(SpreadAvg, AveragePeriod)[0];
Spread_Bar	=	MAX(SpreadAvg[0], AveragePeriod)[0];
	
double value 	= 	SMA(myDataSeries, 20)[0];   // Isn't this the same as SMA(Close, 20)[0]; ??

Sells[0]	=	Max_Accepted[0];//VolumeNormalized[0];	    // Isn't this the same as Sells[0] = Close[0]; ??
Buys[0]	=	Spread_Bar;//SpreadAvg[0];//SpreadNormalized[0];	

}

Hi Danny,

I figured out the issue.

In the region properties I had 4 variables listed to plot:

#region Properties

[Browsable(false)]
[XmlIgnore]
public Series<double> Buys
{
	get { return Values[0]; }
}

[Browsable(false)]
[XmlIgnore]
public Series<double> Sells
{
	get { return Values[1]; }
}


[Browsable(false)]
[XmlIgnore]
public Series<double> PriceVolumeDeltaPlot
{
	get { return Values[2]; }
}

[Browsable(false)]
[XmlIgnore]
public Series<double> VolumeDeltaPlot
{
	get { return Values[3]; }
}

But in the OnStateChange I only had 3 AddPlots:

protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
AddPlot(Brushes.Orange, “buys”);
AddPlot(Brushes.WhiteSmoke, “sells”);
AddPlot(Brushes.WhiteSmoke, “PriceVolumeDeltaPlot”);
}
}

I have no idea why affect some indicators like SMA and StdDev and why does not affect other plots, but it was the solution.