Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on calling OnBarUpdate: Index was of range.

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

    Error on calling OnBarUpdate: Index was of range.

    Hello Support,

    My strategy uses multitimeframe bars. All the BarsArray access are done with BarsArray[BarsInProgress], the same applies to accessing Closes, Highs etc.
    It is running fine normally, but it would get the Index was out of range error when bars are appearing ultra-fast like sometimes happens on CL. This usually more than 10 bars appearing in a second.

    The first thing my OnBarUpdate does is to print

    if (debug >= dbgBip)
    Print(Time.ToString() +
    " " + Instrument.FullName + " <<<>>> BAR STARTS: BIP " + BarsInProgress + " CBar: " + CurrentBar + " CLOSE: " + Closes[BarsInProgress][0]);

    if (debug >= dbgBip)
    Print(Time.ToString() +
    " " + Instrument.FullName + " " + BarsArray[BarsInProgress].Period.ToString() + " BAR STARTS: BIP " + BarsInProgress + " CBar: " + CurrentBar + " CLOSE: " + Closes[BarsInProgress][0]);

    After running, I found exactly the CurrentBar values when it crashes, then I add after the above codes:
    if (BarsInProgress == 0 && CurrentBar >= dbgBar0) {
    Print(Time.ToString() + " " + Instrument.FullName + " " + BarsArray[BarsInProgress].Period.ToString() + " OnBarUpdate0 BIP " + BarsInProgress + " CBar: " + CurrentBar + " CLOSE: " + Closes[BarsInProgress][0]);
    return;
    }

    if (BarsInProgress == 0 && CurrentBar >= dbgBar1) {
    Print(Time.ToString() + " " + Instrument.FullName + " " + BarsArray[BarsInProgress].Period.ToString() + " OnBarUpdate1 BIP " + BarsInProgress + " CBar: " + CurrentBar + " CLOSE: " + Closes[BarsInProgress][0]);
    return;
    }

    dbgBar0 and dbgBar1 is set at initialize to appropriate values, after finding out value of CurrentBar just before the error during previous market replay run. Note that I run at 1X near the error time.

    Just before error happens appropriate prints came out.
    Now when the error happens, none of the above prints! This seems to suggest something else has gone wrong but likely not inside my code. Also I do not have parameter named Index or index.

    What is going on here ? And how can I solve this issue?

    Thanks.

    Regards,
    EdwardK.

    #2
    Edward, can you please try extending the CurrentBars[x] check to all series in the script?

    Comment


      #3
      Hello Bertrand,

      Not sure what you meant. I already print it out on every bar close event.
      If I set the low time frame period to be exactly the same at the main bar. The error does not happen normally, if currentbar from the two series is exactly the same. Even with same period, if upon loading the strategy I see the two series currentbar differs by some value. Then this error will come out during ultrafast bars.

      I am also wondering, would NT faithfully call OnBarUpdate even when the compute time is more that the next bar time. Ie. Before the current OnBarUpdate is finished, next bar is already closed, what would happen in this case?

      Is there CurrentBar specific for the different barsperiod series ?

      Thanks.

      EdwardK.

      Comment


        #4
        Yes Edward, that's what I meant: for example CurrentBars[1] would be the CurrentBar for the second series (the first Add()ed one).

        I don't follow on your OnBarUpdate() timing question - in realtime / replay the OnBarUpdate() would be called as the closing tick / opening tick of new bar is seen.

        Comment


          #5
          Hello Bertrand,

          I tried CurrentBars[BarsInProgress] before and get the error: The name 'CurrentBars' does not exist in the current context.

          Regarding fast bars, as I understand when we run replay at high speed, we may not get that fast because NT will slow it down to ensure each OnBarUpdate completes before it is called again. But if we only running 1X speed, and bars appearing about 20x on the second, OnBarUpdate may not have completed in 50mS time. Could some of these global variables become out of sync with the bars?

          Thanks.

          Regards.
          EdwardK.

          Comment


            #6
            Hello Bertrand,

            I conclude that Time,ToString() is the one crash. I change the first two print to catch the appropriate bars and print without time. The strategy does not crash and I found that when fast bars appears, when BarsInProgress is 0, the CurrentBar has value of the next value of secondary CurrentBar! This happen twice then there is no more prints from Main bar's OnBarUpdate, ie, When OnBarUpdate is called BarsInProgress is always 1!

            Something has gone wrong. Please check.

            Thanks.

            Regards.
            EdwardK.

            Comment


              #7
              Edward, weird that the CurrentBars[bip] would give you a compile issue, are you on latest NT7 R2 here?

              In replay the events would be synched to ensure correct processing - would you please send me the script and market / date you test with to support at ninjatrader dot com Attn Bertrand so I can look into it further here?

              Comment


                #8
                Hello Bertrand,

                I am on NT6.5 latest version. There is no CurrentBars in NT6.5
                Before fast bars appear I run replay at 1X.
                If you have CL02-11, Jan 3rd, 9AM. I believe any ultra fast bars with multi-timeframe would exhibit this issue. Then I believe, the strategy just need a lower period bars with print(Time.ToString()) on OnBarUpdate to simulate the issue.

                Thanks.

                Regards.
                EdwardK

                Comment


                  #9
                  Thanks Edward, I was not able to reproduce any issue here on a MultiSeries strategy using 10, 20, 50 tick bars on latest NT7 - I would suggest you upgrade or install it side by side with NT 6.5 and then retry your scenario there - there were many enhancements and optimizations worked into the Market Replay feature.

                  Comment


                    #10
                    Hello Bertrand,

                    Can you try it again with Renko bars main bar 3 period and secondary 2 period?
                    Can you insert a computation loop to simulate time spent in the OnBarUpdate after the print?
                    Lastly, although on NT7 can you use CurrentBar instead of CurrentBars.
                    The fast bars should appears at least 15 or 20 bars in one second.
                    I appreciate your effort in this. Thanks.

                    Regards.
                    EdwardK.

                    Comment


                      #11
                      Sure, will give it a try using this setup Edward.

                      Comment


                        #12
                        Edward, I gave a basic MTF strategy a run here for a few days on CL 02-11 using 3 / 2 / 1 Renko bars in the code and did not run into any issue - I suggest you give your scenario a try as well on NT7 R2 or if you forward your script I can give it a run here as well for you.

                        Comment


                          #13
                          Hello Bertrand,

                          Sorry been away for a week.
                          I add the following code at the start of OnBarUpdate for protection:
                          if (BarsInProgress == 0) {
                          if (savedCBar == 0) savedCBar = CurrentBar;
                          else {
                          savedCBar++;
                          if (CurrentBar!= savedCBar) {
                          Print(Instrument.FullName +
                          " <<<<< ERROR DETECTED >>>>> BAR STARTS: BIP " + BarsInProgress + " WRONG CBar: " + CurrentBar);
                          return;
                          } } }

                          So now it does not crash on the same data, but hence I also do not get yellow strategy logs. But the issue is there, after three error prints, BarsInProgress is never 0 again.

                          My strategy code also called Indicator with OnMarketData calls. It will take sometime for me to downsize a releasable code.

                          Thanks.
                          Regards,
                          EdwardK.

                          Comment


                            #14
                            Thanks for the update Edward, are you testing now still on 6.5 or is the same happening on NT7 with your code? I look forward to your 'stripped' down example to check into.

                            Comment


                              #15
                              Hello Bertrand,

                              I am still testing on 6.5.

                              Regards,
                              EdwardK

                              Comment

                              Latest Posts

                              Collapse

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