Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Referencing previous bar values

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

    Referencing previous bar values

    Hi All,

    Despite multiple attempts I cannot get beyond this error I am getting when trying to read values from previous bars:

    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.

    my code is simple : double _prevclose = Close[1];

    When I search the documentation I find the following example:


    For example, if we wanted to subtract the high and low of 10 bars ago from each other we would do this:
    double value = High[10] - Low[10];
    This is similar to what I want to do but I keep getting the above error. Any suggestions?

    Diepy

    #2
    Hello Diepy,

    Thanks for your post.

    The error message is advising that you are trying to access a bar that does not exist. When a script is added to a chart, the script will process every historical bar in sequence starting with the oldest bar first.

    If on the very first bar you are accessing a previous bar (Close[1]) there is no previous bar to access.

    You would need to prevent processing your code until the script has processed enough bars. In the help guide example it was 10, in your code (that you have shown) it would be one.

    To prevent processing your code until the script has processed enough bars to allow correct accessing, you would need to add a check of the CurrentBar at the top of your script. CurrentBar is the system bar counter and it is an int variable that will hold the number of the bar being processed by your script.

    At the top of OnBarUpate() you could add:

    if (CurrentBar < 1) return; // do not process code below until at least 1 bar processed.

    Please see the help guide here: https://ninjatrader.com/support/help...currentbar.htm

    Note that if you have other prior bar accesses like [3], you would want to use the highest bars ago value in the current bar check.

    Comment


      #3
      Thanks Paul, that did it :-)

      I had this exception in the code:


      if (Bars == null)
      return;


      I figured this would be enough but apparently there is more to it...

      Comment

      Latest Posts

      Collapse

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