Now I wanted to add to this indicator by creating a multi plot. I added the following to the end of OnBarUpdate():
Values[1][0] = High[1] + .25;
Values[0][0] = Low[1] - .25;
With this addition, the indicator no longer works, and outputs the error message
| 8/31/2020 9:32:30 AM | Default | Indicator 'TickCounterEnhanced': 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. |
The strange thing to me is when I made the changes over the weekend, the indicator was working and the plots were correct. That was with historical data. However with real time data today, the indicator stopped working because the index is referring to the left most bars instead of the right most bars. What should I do to fix this?
Here is the entire OnBarUpdate code:
protected override void OnBarUpdate()
{
double periodValue = (BarsPeriod.BarsPeriodType == BarsPeriodType.Tick) ? BarsPeriod.Value : BarsPeriod.BaseBarsPeriodValue;
double tickCount = ShowPercent ? CountDown ? (1 - Bars.PercentComplete) * 100 : Bars.PercentComplete * 100 : CountDown ? periodValue - Bars.TickCount : Bars.TickCount;
string tick1 = (BarsPeriod.BarsPeriodType == BarsPeriodType.Tick || (BarsPeriod.BarsPeriodType == BarsPeriodType.HeikenAshi && BarsPeriod.BaseBarsPeriodType == BarsPeriodType.Tick) ? ((CountDown
? tickCount.ToString() : tickCount.ToString()) + (ShowPercent ? "%" : ""))
: NinjaTrader.Custom.Resource.TickCounterBarError);
if (CurrentBar == 0)
{
ChkLevel = (BarsPeriod.BarsPeriodType == BarsPeriodType.HeikenAshi && BarsPeriod.BaseBarsPeriodType == BarsPeriodType.Tick) ? BarsPeriod.BaseBarsPeriodValue : BarsPeriod.Value;
if (ShowPercent && AlertLevel > 100)
{
AlertLevel = 100; // When percent, limit to 100%.
Print ("TickCounterEnhanced: Alert setting percent exceeds 100, setting alert level to be 100% ");
Log ("TickCounterEnhanced: Alert setting percent exceeds 100, setting alert level to be 100% ", LogLevel.Error);
}
else if (AlertLevel > ChkLevel) // reset alert level to bar ticks if exceeds tick in bar.
{
if (BarsPeriod.BarsPeriodType == BarsPeriodType.HeikenAshi && BarsPeriod.BaseBarsPeriodType == BarsPeriodType.Tick)
{
AlertLevel = ChkLevel;
Print ("TickCounterEnhanced: Alert setting exceeds ticks in bar, setting alert level to be same as bar ticks: "+ChkLevel);
Log("TickCounterEnhanced: Alert setting exceeds ticks in bar, setting alert level to be same as bar ticks: "+ChkLevel, LogLevel.Error);
}
else
{
AlertLevel = ChkLevel;
Print ("TickCounterEnhanced: Alert setting exceeds ticks in bar, setting alert level to be same as bar ticks: "+ChkLevel);
Log("TickCounterEnhanced: Alert setting exceeds ticks in bar, setting alert level to be same as bar ticks: "+ChkLevel, LogLevel.Error);
}
}
}
if (tickCount <= AlertLevel)
{
if (CurrentBar != lastBar) // playsound once per bar
{
lastBar = CurrentBar;
if (SoundsOn)
PlaySound(UpSoundFile);
}
RemoveDrawObject ("NinjaScriptInfo");
//Draw.TextFixed(this,"NinjaScriptInfo1", tick1, myTextBox, TextAlertColor, TextFont, Brushes.Transparent, TextAlertBackColor, 100);
Draw.Text(this, "NinjaScriptInfo1", false, tick1, -2, Close[0], 0, TextAlertColor, TextFont, TextAlignment.Left, Brushes.Transparent, TextAlertBackColor, 100);
}
else
{
RemoveDrawObject ("NinjaScriptInfo1");
//Draw.TextFixed(this, "NinjaScriptInfo", tick1, myTextBox, ChartControl.Properties.AxisPen.Brush, TextFont, Brushes.Transparent, Brushes.Transparent, 0);
Draw.Text(this, "NinjaScriptInfo", false, tick1, -2, Close[0], 0, ChartControl.Properties.AxisPen.Brush, TextFont, TextAlignment.Left, Brushes.Transparent, Brushes.Transparent, 0);
}
//Draw Time
Draw.Text(this, "Time", false, DateTime.Now.ToString("HH:mm:ss"), -2, Close[0], -25, ChartControl.Properties.AxisPen.Brush, TextFont2, TextAlignment.Left, Brushes.Transparent, Brushes.Transparent, 0);
//Attach indicator to auto trail
//Values[1][0] = High[1] + .25;
//Values[0][0] = Low[1] - .25;
}

Comment