Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.

    Paul H.NinjaTrader Customer Service

    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 XanderT, Today, 01:27 AM
      0 responses
      3 views
      0 likes
      Last Post XanderT
      by XanderT
       
      Started by anton_tymoshchuk, Today, 01:14 AM
      0 responses
      5 views
      0 likes
      Last Post anton_tymoshchuk  
      Started by marco231, Yesterday, 11:32 PM
      1 response
      7 views
      0 likes
      Last Post marco231  
      Started by sofortune, Yesterday, 10:55 PM
      0 responses
      7 views
      0 likes
      Last Post sofortune  
      Started by sofortune, Yesterday, 10:19 PM
      0 responses
      9 views
      0 likes
      Last Post sofortune  
      Working...
      X