PDA

View Full Version : Index out of bounds error...


funk101
05-23-2007, 12:51 AM
I understand *why* this happens, normally, but cannot figure out *why* in this instance:
My indicator is simply supposed to draw a green line when up, and red when down. Here's the logic:

#region Variables
private DataSeries vol;
#endregion

protected override void Initialize()
{
Add(new Plot(new Pen(Color.Orange, 2), PlotStyle.Line, "Plot0"));
Add(new Plot(new Pen(Color.Indigo, 2), PlotStyle.Line, "Plot1"));
Add(new Plot(new Pen(Color.LimeGreen, 2), PlotStyle.Line, "Up"));
Add(new Plot(new Pen(Color.DarkRed, 2), PlotStyle.Line, "Down"));
Add(new Plot(new Pen(Color.Yellow, 2), PlotStyle.Line, "Neutral"));
vol = new DataSeries(this);
}

protected override void OnBarUpdate()
{
double newSlope = 0;
double study = 0;
double primary = 0;
if (CurrentBar == 0)
{
return;
}
else
{
newSlope = (Volume[0] - Volume[1]) / (ToTime(Time[0]) - ToTime(Time[1]));

vol.Set(newSlope);
study = SMA(vol, 6)[0];
primary = vol[1]+vol[0];
//
//Plot0.Set(primary);
Plot1.Set(study);

if (vol[0] > vol[1])
{

Up.Set(primary);
}
else if (vol[0] < vol[1])
{

Down.Set(primary);
}
else
{
Neutral.Set(primary);
}
//DrawHorizontalLine("L", Low[1], Color.Yellow);
}

}

If I comment out the if/elseif/else condition, the study works(without colors of course). AND if I print out "hey" in the second condition (vol[0] < vol[1]) "hey" prints to the output window. There's something funky in the vol[n] ...why is this giving me the "Index out of bounds of array" error?

NinjaTrader_Ray
05-23-2007, 07:49 AM
Likely vol[1] does not exist when CurrentBar == 1 since on this bar, this is the first time you call vol.Set(). When CurrentBar == 0 add vol.Set(0) and see if this resolves the issue.

funk101
05-23-2007, 09:37 AM
Of course. That was it. Thanks.

funk101
05-23-2007, 02:44 PM
Trying to get the different colored line. I'm getting empty patches. Check image, and code please. Everything works fine, the thumb.gif shows up, but the line is missing the neutral color:

protected override void OnBarUpdate()
{
if (CurrentBar == 0)
{
Plot0.Set(0);
Plot1.Set(0);
vol.Set(0);
Up.Set(0);
Down.Set(0);
Neutral.Set(0);
}
else
{
newSlope = (Volume[0] - Volume[1]) / (ToTime(Time[0]) - ToTime(Time[1]));

vol.Set(newSlope);
study = SMA(vol, 6)[0];
primary = vol[1]+vol[0];
//
//Plot0.Set(primary);
Plot1.Set(study);
Print("new "+newSlope+" old "+primary);
if (newSlope >= primary)
{
Up.Set(primary);
//Plot0.Set(primary);
image = new Bitmap(imagePath+"thumbsUp.gif");
}
else if (newSlope < primary)
{
//Print("down");
Down.Set(primary);
//Plot0.Set(primary);
image = new Bitmap(imagePath+"thumbsDown.gif");
}
else
{
//Plot0.Set(primary);
Neutral.Set(primary);
}


DrawHorizontalLine("L", Low[1], Color.Yellow);
}

}

NinjaTrader_Ray
05-23-2007, 03:18 PM
Dealing with the "gap" is discussed in the following thread.

http://www.ninjatrader-support.com/vb/showthread.php?t=1696

NinjaTrader_Ray
05-23-2007, 03:20 PM
Additional thread dealing with gaps.

http://www.ninjatrader-support.com/vb/showthread.php?t=1675