Strategy 'MovingAvgTest': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
I can get the plot lines to output properly over historical data, but once I enable the strategy, the plot lines quickly converge and overlap each other even if the settings are a 21 MA and a 200 MA. I am not sure what I'm missing. I've done a ton of reading on the "Values" command and the AddPlot command, but can't seem to make any of them talk to each other and output properly.
This is the condensed code I have
namespace NinjaTrader.NinjaScript.Strategies
{
public class MovingAvgTest: Strategy
{
private double smma1;
private double smma2;
private double smma3;
private double smma4;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"N/A";
Name = "MovingAveragesTest";
Calculate = Calculate.OnEachTick;
AddPlot(Brushes.White, "21 SMMA");
AddPlot(Brushes.Lime, "50 SMMA");
AddPlot(Brushes.Yellow, "100 SMMA");
AddPlot(Brushes.Red, "200 SMMA");
}
else if (State == State.Configure)
{
//AddDataSeries(Data.BarsPeriodType.Minute, 1);
}
else if (State == State.DataLoaded)
{
//PlaceHolder
}
}
protected override void OnBarUpdate()
{
double len1 = 21;
double len2 = 50;
double len3 = 100;
double len4 = 200;
smma1 = CalculateSMMA(Closes[0], len1, smma1);
smma2 = CalculateSMMA(Closes[0], len2, smma2);
smma3 = CalculateSMMA(Closes[0], len3, smma3);
smma4 = CalculateSMMA(Closes[0], len4, smma4);
Values[0][0] = smma1;
Values[1][0] = smma2;
Values[2][0] = smma3;
Values[3][0] = smma4;
}
private double CalculateSMMA(PriceSeries source, double length, double previousValue)
{
double smma = (previousValue * (length - 1) + source[0]) / length;
return smma;
}
[HASHTAG="t3322"]region[/HASHTAG] Properties
[NinjaScriptProperty]
public bool TrendFill { get; set; } = true;
#endregion
}
}
As I understand: Values[1][0] refers to the Values of Data set 1, 0 bars ago. However if I try to put in something like Highs[1][0], it gives me the error I stated up at the beginning. I'm stumped as to how to get this program to understand what it's doing.

Comment