Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator stops working, generates error at random times

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

    Indicator stops working, generates error at random times

    Hello,

    I am having trouble with an indicator that I have written. It terminates at random times and gives a totally inapplicable error.

    The error message is: "Indicator 'MyIndicator': Error on calling 'OnBarUpdate' method on bar 435: 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.

    The problematic code seems to be this:

    Code:
    endBarsAgo = Bars.GetBar(Time[0]);
    
    if(EntryTime == Time[0])
                    { PriceVar3 = Close[0]; }
                        
                    else
                    {
                        startBarsAgo = Bars.GetBar(new DateTime(EntryTime.Year, EntryTime.Month, EntryTime.Day, EntryTime.Hour, EntryTime.Minute + 1, 0));
                        PriceVar3 = MAX(High, endBarsAgo - startBarsAgo  + 1)[CurrentBar - endBarsAgo];
                    }​
    EntryTime is set when the entry conditions are met, and the above snippet of code is inside the larger "if" statement that triggers when the exit conditions appear.

    I am trying to get the High of the range between when the indicator first triggers and when the exit conditions appear, but I want to exclude the first (trigger) bar from the range. So, I set the startBarsAgo variable equal to the time of the bar after the trigger bar. (This is designed exclusively for the 1-minute chart.)

    It works perfectly fine for 434 bars, and there is absolutely nothing about bar 435 that would distinguish it in any way from the other bars. Since I am already doing a comparison to see that at least one bar had been stamped since the entry, there must be a valid range. And in bar 435's particular case, there have been several bars printed since the entry trigger occurred, so the assertion that there is an invalid range is total nonsense.

    Any idea what might be going on here?

    Thanks in advance!

    #2
    Hello NjTMatthew,

    The error indicates an invalid index was used.

    Below is a link to a forum post that discusses.


    With the code you have suggested, ensure that (endBarsAgo - startBarsAgo + 1) is equal to or greater than 0 and less than CurrentBar.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post

      With the code you have suggested, ensure that (endBarsAgo - startBarsAgo + 1) is equal to or greater than 0 and less than CurrentBar.
      Below is the output from when the error is returned:

      Code:
      8/6/2024 7:59:00 AM Start Bars Ago: 414 EndBarsAgo: 431
      8/6/2024 8:00:00 AM Start Bars Ago: 414 EndBarsAgo: 432
      8/6/2024 8:00:00 AM Start Bars Ago: 414 EndBarsAgo: 432
      8/6/2024 8:01:00 AM Start Bars Ago: 414 EndBarsAgo: 433
      8/6/2024 8:01:00 AM Start Bars Ago: 414 EndBarsAgo: 433
      8/6/2024 8:02:00 AM Start Bars Ago: 414 EndBarsAgo: 434
      8/6/2024 8:02:00 AM Start Bars Ago: 414 EndBarsAgo: 434
      8/6/2024 8:03:00 AM Start Bars Ago: 414 EndBarsAgo: 435
      22, 435
      Indicator 'MyIndicator': Error on calling 'OnBarUpdate' method on bar 435: 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.
      
      ​

      Comment


        #4
        Hello NjTMatthew,

        Are you certain this is the line causing the error?

        Have you added prints above each line and the last print appearing in the output is above this line?

        This print doesn't show what CurrentBar is. This shows the start bars ago and end bars ago but what is this being compared to in the output?

        Is CurrentBar 0?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello NjTMatthew,

          Are you certain this is the line causing the error?

          Have you added prints above each line and the last print appearing in the output is above this line?

          This print doesn't show what CurrentBar is. This shows the start bars ago and end bars ago but what is this being compared to in the output?

          Is CurrentBar 0?
          The code that I used to generate that output is as follows:

          Code:
          else
                          {
                              Print((endBarsAgo - startBarsAgo  + 1).ToString() + ", " + CurrentBar);
                              startBarsAgo = Bars.GetBar(new DateTime(EntryTime.Year, EntryTime.Month, EntryTime.Day, EntryTime.Hour, EntryTime.Minute + 1, 0));
                              PriceVar3 = MAX(Close, endBarsAgo - startBarsAgo  + 1)[CurrentBar - endBarsAgo];
                              
                          }​
          As you can see, the last line that is processed correctly is the one that prints the startBarsAgo math and the value of CurrentBar. Also, if I comment out the line that assigns the startBarsAgo value, it works. Well, it no longer crashes at bar 435, but it also no longer assigns the proper value to startBarsAgo.

          Comment


            #6
            Hello,

            To confirm, the line causing the error is:
            startBarsAgo = Bars.GetBar(new DateTime(EntryTime.Year, EntryTime.Month, EntryTime.Day, EntryTime.Hour, EntryTime.Minute + 1, 0));

            And not:
            PriceVar3 = MAX(Close, endBarsAgo - startBarsAgo + 1)[CurrentBar - endBarsAgo];

            If so, what is the date time being supplied to Bars.GetBar()? Is this a bar that is printing to the output window?​
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              To confirm, the line causing the error is:
              startBarsAgo = Bars.GetBar(new DateTime(EntryTime.Year, EntryTime.Month, EntryTime.Day, EntryTime.Hour, EntryTime.Minute + 1, 0));

              And not:
              PriceVar3 = MAX(Close, endBarsAgo - startBarsAgo + 1)[CurrentBar - endBarsAgo];

              If so, what is the date time being supplied to Bars.GetBar()? Is this a bar that is printing to the output window?​
              Correct. If I put the two slashes in front of the "startBarsAgo" line, it works. If I comment out the "PriceVar3" line, but keep the "startBarsAgo" line, it crashes.

              I have even tried putting the "startBarsAgo" line in a different place in the code. So now, it only gets assigned once per entry trigger, and the "PriceVar3" line is only executed when the exit is triggered. But still, as soon as it runs that "startBarsAgo" line after the entry trigger at line 431, it crashes.

              EntryTime is a DateTime value that is assigned to Time[0] when the entry condition is met. It is only assigned inside that "if" statement, and remains static until after the exit trigger resets the global boolean and it sees a new set of entry conditions.

              The loop that now sets the startBarsAgo value is run immediately after the entry condition loop, but is not within it. I have tried putting the "startBarsAgo" line inside the entry condition loop, but that results in strange output from the (endBarsAgo - startBarsAgo + 1) math, so I think it's better to keep it where it is for now.

              The annoying thing is that it works perfectly until the entry that it sees at bar 431. I have tried deleting and re-downloading the market replay data for Aug. 6, but that has not resolved the issue.

              Comment


                #8
                Hello NjTMatthew,

                What is the date time being supplied to Bars.GetBar()? Is this a bar that is printing to the output window?​
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello NjTMatthew,

                  What is the date time being supplied to Bars.GetBar()? Is this a bar that is printing to the output window?​
                  The EntryTime variable that is being passed to Bars.GetBar() is assigned to the time that the entry conditions were first met. The characteristics of the trigger bar in particular are not important. I just need to record the time at which the entry conditions were met, so I can use the following bar as the start of the range in which I am scanning for the High.

                  Comment


                    #10
                    Hello NjTMatthew,

                    If the call to Bars.GetBar() is causing an error, this would imply invalid values supplied as parameters.

                    What is the date time being supplied to Bars.GetBar()? Is this a bar that is printing to the output window?​
                    (By this I mean the actual datetime value as printed in the output window)
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post

                      If the call to Bars.GetBar() is causing an error, this would imply invalid values supplied as parameters.
                      Yes, that is exactly the problem!

                      I changed the code to create a second time variable and assign it immediately, rather than doing the new DateTIme when the Bars.GetBar() was being called. This allowed me to output the value of the second time variable.

                      It turns out the problem was in the syntax of my assignment of the new time variable. Since all I was doing was incrementing the minute by +1, if the EntryTime was at x:59:00, it would be trying to increment that to 60, which is not within the acceptable range.

                      Now, it's just a question of writing a simple condition to test if the minute is 59, and then setting the new time variable with an incremented hour instead, and then setting the minute to 0.

                      Thank you for your help!

                      Comment

                      Latest Posts

                      Collapse

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