Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with identifying the most recent bars

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Help with identifying the most recent bars

    I have an indicator where close[0] is used within OnBarUpdate() to refer to the most recent bar.

    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.
    By adding that code, it also changed the functionality of close[0] from the original indicator, changing it to the 1st left most bar, instead of the right most recent bar.

    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:

    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;
    }
    Last edited by backtester831; 08-31-2020, 08:18 AM.

    #2
    Hello backtester831,

    Thank you for your note.

    I don't see anywhere in your code where you are checking for enough bars in the data series before processing data. Indicators have to process starting from the very first bar all the way on the left of the chart. When it tries to process that bar, you are having it check for the high and low of the bar previous to that, which doesn't exist. Therefore, we have to make sure there are enough bars to be able to retrieve that data.

    You can do this by adding the following to the top of OnBarUpdate:

    if (CurrentBar < 1)
    return;

    This will check to make sure that there is at least one bar in the data series prior to beginning processing.

    Here's a link to our help guide that goes over making sure you have enough bars:



    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Thank you very much, this worked!

      Originally posted by NinjaTrader_Kate View Post
      Hello backtester831,

      Thank you for your note.

      I don't see anywhere in your code where you are checking for enough bars in the data series before processing data. Indicators have to process starting from the very first bar all the way on the left of the chart. When it tries to process that bar, you are having it check for the high and low of the bar previous to that, which doesn't exist. Therefore, we have to make sure there are enough bars to be able to retrieve that data.

      You can do this by adding the following to the top of OnBarUpdate:

      if (CurrentBar < 1)
      return;

      This will check to make sure that there is at least one bar in the data series prior to beginning processing.

      Here's a link to our help guide that goes over making sure you have enough bars:



      Please let us know if we may be of further assistance to you.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      672 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      379 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      111 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      575 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      582 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X