Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on calling 'OnBarUpdate' method on bar -1 (etc)

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

    Error on calling 'OnBarUpdate' method on bar -1 (etc)

    Hello, hope this finds you well out there in support land.

    I have been getting this error:

    "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. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

    I've seen this in the past a few times and not really understood what was happening.

    But first... Error checking code like this seems to be pretty standard in strategy templates at the top of OnBarUpdate():

    Code:
    if (CurrentBars[0] < BarsRequiredToTrade)
    {
        Log("Number of bars (" + CurrentBars[0] + ") is less than BarsRequiredToTrade (" + BarsRequiredToTrade + "); Returning");
        return;
    }
    I printed the State and it was always historical when I got that log message, so apparently CurrentBars[0] counts up from zero when it does historical, which makes sense. I thought, no reason to throw an error on the historical run....

    My strategy uses BiP = 0 & 1 (for tick data). So I have my code like this:

    Code:
    protected override void OnBarUpdate()
    {
        ​if (State != State.Historical)
        {
            if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade)
            {
                Log("STATE = " + State.ToString() + "; Number of bars (BiP[0] = " + CurrentBars[0] + " & BiP[1] = " + CurrentBars[1] +
                       ") is less than BarsRequiredToTrade (" + BarsRequiredToTrade + "); Returning");
    
                return;
            }
        }
        else if (!PlotHist)  // input to turn off historical processing to speed up init
            return;
    ​
        // rest of code...
    }
    ​But each time I enabled the strategy, I get that original error:

    Strategy 'FiftyLevel': 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. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    So it's not hitting the "BarsRequiredToTrade" check now; maybe the two are unrelated. But I'd like to get rid of that error, and I do not see any Log messages after the error.

    Can you shed some light on this?

    #2
    More confusingly... I changed the code back to this:

    Code:
    protected override void OnBarUpdate()
    {
        ​if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade)
        {
            Log("STATE = " + State.ToString() + "; Number of bars (BiP[0] = " + CurrentBars[0] + " & BiP[1] = " + CurrentBars[1] +
                   ") is less than BarsRequiredToTrade (" + BarsRequiredToTrade + "); Returning");
            return;
        }
    
        if (State == State.Historical)
            if (!PlotHist)
                return;
    ​​
        // rest of code...
    }
    ...and I do not get the error​ anymore; historical runs fine. Of course I get a couple thousand lines like this at first:

    STATE = Historical; Number of bars (BiP[0] = -1 & BiP[1] = 0) is less than BarsRequiredToTrade (20); Returning

    but then it runs normally. I thought all I did was refactor code a bit and skip all the log lines on the hist run. What am I missing?

    Comment


      #3
      Hello DerkWehler,

      You need to wait until that index exists.

      Either the condition should check CurrentBars[0] and CurrentBars[1] are greater than (not less than) BarsRequiredToTrade (or the largest index used).

      if (CurrentBars[0] > BarsRequiredToTrade && CurrentBars[1] > BarsRequiredToTrade)
      {
      // execute code
      }

      OR you should return (and not run further below) if CurrentBars[0] or CurrentBars[1] is less than BarsRequiredToTrade (or the largest index used).

      if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade)
      return;

      // code below is only executed if condition above is false and the method is not returned.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_ChelseaB View Post
        Hello DerkWehler,

        .....

        OR you should return (and not run further below) if CurrentBars[0] or CurrentBars[1] is less than BarsRequiredToTrade (or the largest index used).

        if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade)
        return;

        // code below is only executed if condition above is false and the method is not returned.
        Hi Chelsea:

        Thank you for fast reply.

        This
        Code:
        if (CurrentBars[0] [B]<[/B] BarsRequiredToTrade[B] || [/B]CurrentBars[1] [B]<[/B] BarsRequiredToTrade)
        return;
        is what I was doing in my first post... Are you saying that not checking it during historical is causing a crash later?

        What is the real purpose of the 'BarsRequiredToTrade' setting? I presumed it was for things like adding an indicator that needed that many bars for it's period -stuff like that. Is that the case? Because it seems like such figures are often better calculated dynamically...

        So regardless why it's there, the hist will always calculate from zero, so if I had is set to 20 for a 20-period MA I used in the strategy, you always just want to throw away those first 20 in history to make the test more accurate. That would makes sense; I guess I needed to not skip the "return" if it is the historical run; I should just skip the logging.

        Comment


          #5
          Hello DerkWehler,

          In your first post I'm seeing you are attempting to use the non-existent indexes of BiP before calling the return.

          if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade)
          {
          Log("STATE = " + State.ToString() + "; Number of bars (BiP[0] = " + CurrentBars[0] + " & BiP[1] = " + CurrentBars[1] +
          ") is less than BarsRequiredToTrade (" + BarsRequiredToTrade + "); Returning");

          return;
          }​

          No, you are not calling a return before attempting to use the series index, you are calling the return after.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ChelseaB View Post
            Hello DerkWehler,

            No, you are not calling a return before attempting to use the series index, you are calling the return after.
            Hi Chelsea, thanks again.

            You mean the use in the Log function?? What about the first use, in the conditional itself? Wouldn't then that cause the same issue?

            I presume CurrentBars[0] exists by the first call to OnBarUpdate(?); when does CurrentBars[1] get created?

            Comment


              #7
              Hello DerkWehler,

              CurrentBars[series index] holds the bar number of the currently updating bar. The index is the BarsInProgress index of the price series.

              The BiP[bar index] is a specific bar value. The index is for a specific bar slot.

              The bar index used for the BiP series must be less than the number of bars updated, provided by CurrentBar.

              if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < BarsRequiredToTrade)

              As long as an addition series has been added with AddDataSeries, CurrentBars[1] will exist and will hold the number of bars processed for second price series.

              "I presume CurrentBars[0] exists by the first call to OnBarUpdate(?); when does CurrentBars[1] get created?"

              CurrentBars[1] gets created when AddDataSeries() is called.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Okay, good things to keep in mind; thanks again.


                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                58 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                133 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                73 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                45 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                50 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X