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

??? what causes this?/? : Error on calling 'OnBarUpdate' method on bar -1:

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

    ??? what causes this?/? : Error on calling 'OnBarUpdate' method on bar -1:

    : Error on calling 'OnBarUpdate' method on bar -1: You are accessing an index with a value that is invalid since it is out-of-range.

    how can OnBarUpdate function on Bar -1???

    would not the first bar etiher be 0 or 1?


    #2
    I am accessing Close[0], Volume[0], Time[0]

    there are three dataseries though....??

    Comment


      #3
      Are you using additional data series?

      Let's say your chart is a 1 min series, but you
      also add a 5-min series with AddDataSeries.

      Well, on the very first bar of the chart, that very
      first candlestick bar is CurrentBar = 0, the 2nd
      bar is CurrentBar = 1, etc.

      Or,
      CurrentBars[0] is 0 on the 1st candlestick, and
      CurrentBars[0] is 1 on the 2nd candlestick, etc.

      Well, guess what, there are no 5-min bars yet.
      Nope, not a one. Zilch. Zippo.

      None have been created, the chart is still waiting
      to create that very first 5-min bar -- it will need
      five of the 1-min bars to create that very first
      5-min bar.

      So, what is CurrentBars[1] on the first five 1-min bars?

      It is -1, which says "no bars exist yet on this data series".

      -=o=-

      In my experience, if you see a -1 you are probably
      using additional data series ... and you need better
      guard code at the top of your OnBarUpdate, at a
      minimum, you probably need,
      Code:
      if (CurrentBars[0] < 0 || CurrentBars[1] < 0)
          return;
      If you have 3 additional data series, then your minimum
      guard code is probably this,
      Code:
      if (CurrentBars[0] < 0 || CurrentBars[1] < 0 ||
          CurrentBars[2] < 0 || CurrentBars[3] < 0)
          return;

      Make sense?

      Last edited by bltdavid; 08-07-2023, 07:10 AM. Reason: arrgh! fix another typo, it's too early in the morning

      Comment


        #4
        And you may need some guard code to avoid processing
        the other data series when OnBarUpdate is called.

        You gotta remember, every data series you add shares the
        same OnBarUpdate method, they all call the OBU when a
        bar in their data series closes.

        Do you need this guard code at the top your OnBarUpdate?
        Code:
        if (BarsInProgress != 0)
            return?
        Who knows?

        You've shown us no code, it will depend on what you're doing
        with those extra data series ...
        Last edited by bltdavid; 08-07-2023, 07:09 AM.

        Comment


          #5
          that worked, thank you!

          there are three data series, all are 10 second

          Calculate is each tick, so there could be data in the beginning on one series and not another



          Comment


            #6
            this worked

            if (CurrentBars[0] < 0)
            return;

            I can't filter on BarsInProgress because I need the data from the second and third data series in OnBarUpdate


            Comment


              #7
              Hello llanqui,

              Thanks for your post.

              bltdavid is correct and I am happy to hear you were able to resolve the error message.

              This error message indicates that you are trying to access a BarsAgo value that is not valid. A more simple example using one series would be on bar 5 you check for 6 BarsAgo. There are not yet 6 bars so the CurrentBar minus 6 would be a negative number or a non-existent bar.

              Keep in mind that when adding additional data series to the script, you need to make sure that you have enough bars processing on each data series that is being used by the script.

              A CurrentBars check could be used in your indicator's logic to ensure that a certain number of bars have processed for each data series in the script before the script begins calculations.

              See the help guide documentation below for more information.

              CurrentBars - https://ninjatrader.com/support/help...urrentbars.htm
              Make sure you have enough bars - https://ninjatrader.com/support/help...nough_bars.htm
              Brandon H.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by mintos, 04-02-2024, 08:22 PM
              7 responses
              36 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by PaulMohn, 04-24-2024, 03:49 AM
              5 responses
              39 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by claxxical, 05-30-2017, 12:30 PM
              38 responses
              4,480 views
              0 likes
              Last Post alancurry  
              Started by Felix Reichert, 04-26-2024, 02:12 PM
              10 responses
              70 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by lightsun47, Today, 11:37 AM
              1 response
              12 views
              0 likes
              Last Post NinjaTrader_Zachary  
              Working...
              X