Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Potential error

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

    Potential error

    Hey, been a while.

    I posted previously on a much older bot, I was getting the infamous "OnBarUpdate" error.

    HOWEVER, this time is very different and I cannot figure out what is going on!

    First of all, the message is different:
    "Strategy 'HeadAndShouldersStrategyUnlocked': Error on calling 'OnBarUpdate' method on bar 52727: 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."

    What causes the error to shift from bar 1 to bar 52727?

    Here is where things get weird: I *can* activate the bot on NQ 24 range without errors, but I *can't* activate it (and also get this error) on the 12 range.

    Thoughts?

    #2
    Hello Mistoffelees,

    Thank you for your post.

    This message is indicating the specific index requested from a collection does not exist. Indexes must be a non-negative number and less than the size of the collection. The error may be indicating the index requested is larger than the number of elements in the collection.

    In the case of barsAgo values with Series, any barsAgo index must be less than CurrentBar (the total number of bars is the size of a Series collection).

    Code:
    // require BarsInProgress 0 to have 3 bars processed, require BarsInProgress 1 to have 4 bars processed
    if (CurrentBars[0] < 3 || CurrentBars[1] < 4)
    return;
    
    Print(Times[0][3]); // this does not cause an error as CurrentBars[0] is equal to or greater than 3
    
    Print(Times[1][4]); // this does not cause an error as CurrentBars[1] is equal to or greater than 4
    
    Print(Times[0][5]); // this line of code will cause an invalid index error. When CurrentBars[0] is 4 there is no bar 5 bars ago

    The help guide discusses ‘Make sure you have enough bars in the data series you are accessing’ .
    NinjaScript > Educational Resources > Tips > Make sure you have enough bars in the data series you are accessing

    Indexing errors can also occur when using an invalid index with arrays, lists, and other collections where the index used is equal to or larger than the .Count() or .Length of the collection, as well as with method calls that use indexes such as: <string>.Subtring() which uses a string position index, <string>.Format() which uses indexes for format items in the string (placeholders {0}), and Draw methods which use barsAgo indexes.

    Dotnetperls is a 3rd party educational site with an in-depth review of arrays.

    www.dotnetperls.com/array

    Comment


      #3
      I do not think that is correct in this case.

      You see, I did have all of these errors on a much older variant (six versions ago) of this kind of bot, and got things worked out.

      I do not think your advice is applicable in this point because it is not talking about bar ONE - the error message is different.

      Moreover, my bot DOES work without error on the 24 range, it only throws an error on the 12 range. On bar 52,727…

      This is NOT an error on bar one, and I have already addressed CurrentBar.

      That did not fix this.

      Comment


        #4
        Hello Mistoffleless,

        The error message is saying you are getting an index error on bar 52727. Moreover, I'm not clear why you are mentioning bar 1 in specific? This bar isn't mentioned in my previous post, nor in your error message, and the code is just an example to demonstrate what indexing errors mean. Simply because you are not getting it on bar one doesn't mean you can't get an index error on any other bar.

        The data series supplied to the script does make a difference in how the script processes. The fact is you are getting an indexing error in your script, the error would not be thrown if this wasn't the case.

        As mentioned in my previous post as well, CurrentBar may not be enough to address this as you can also get an invalid index error on other lists, arrays, and series.

        We suggest using Print statements to identify which specific line is having an error, once you know which line is having an error it will be clearer what is going wrong in the script.

        Comment


          #5
          Gaby,

          I have no idea how to debug this because the script WORKS.

          The problem is it only works on 24 range, it won’t work on the NQ 12 range.

          The error message is only thrown on the 12 range.

          How is this possible? I have spent two years trying to work with NinjaTrader, the canned answers are not helpful at all.

          *Why* is there an indexing array if I simply change my CHART? The script runs under all other circumstances, it is simply NQ and the 12 range.

          And *what* print command could I possibly use to try to figure out what the error is in this?

          The entire script runs, it just won’t activate and throws this error in one case, which unfortunately is the very case I need it to work in.

          I’ve been around computers long enough to know that when an error message changes in some way, it usually means the underlying cause has changed.

          And indeed it has! When I was going through this a couple of months ago, the index error wouldn’t let me activate my script on ANYTHING.

          I fixed the error, everything worked.

          That is not true for this case.

          So what in the world could be wrong with the chart that is causing issues with my script, please?

          I need you to give me a detailed answer as to EXACTLY how I should do whatever you are suggesting.

          Because I’ve done the “put prints over everything to find the line where the error occurs”.

          But the error is not consistent in this case.

          That means there is something in the CHART and not my SCRIPT which is causing an index error, don’t you see?

          Or is there some other possible explanation I am not familiar with?

          Comment


            #6
            Hello Mistoffleless,

            Thank you for your response.

            I have no idea how to debug this because the script WORKS.
            If the script was working, you wouldn't be getting runtime errors about indexing. As explained, an indexing error means your script is trying to access that specific index from a collection but it does not exist.

            *Why* is there an indexing array if I simply change my CHART? The script runs under all other circumstances, it is simply NQ and the 12 range.
            Changing the data series your strategy is running is directly changing the data you are supplying to strategy, so even if it works on another series it doesn't guarantee it is going to work on all other series.

            I need you to give me a detailed answer as to EXACTLY how I should do whatever you are suggesting.
            You need to debug the script in order to identify the specific line of code causing the error.

            To achieve this, you could print out the index you are trying to access from the collection along with the size of the collection. You could add prints throughout the script to try to identify the line of code that is causing the issue by looking at the last thing printed out before the error was hit. You could comment out logic until the error stops, then uncomment out logic until it returns to identify the line of code.


            Because I’ve done the “put prints over everything to find the line where the error occurs”.​
            If you've already done the debugging, please supply the output from debugging so we can assist in analyzing the output. Without the output from debugging, we have no information to go off of in order to assist. All we know is that your script is trying to access an invalid index. We need the output from debugging to see what this index is and why your script is trying to access it.

            Our support team cannot debug the script for you. If you don't know how to debug the script, you may need to reach out to third party developer to assist you.

            You can search our extensive library of NinjaScript consultants through the link below. Simply enter a consultant name or search by using our filter categories. Once you have identified your consultants of choice, please visit each consultant's site for more information or contact them directly to learn more:

            https://ninjatraderecosystem.com/sea...mming-services

            You can locate the contact information for the consultants on their direct websites for any additional questions you may have. Since these consultants are third-party services for NinjaTrader, all pricing and support information will need to be obtained through the consultant.

            The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The companies and services listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem, LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

            Let me know if I may be of further assistance.

            Comment


              #7
              Alright, had to do "some life things" earlier, but here is a snippet of code where the error (or at least a first error) is located (see Set 2):

              Code:
              Print(@"Made it to 4");
              // Set 2
              if ((TriggerState == 0)
              && (Position.MarketPosition == MarketPosition.Flat)
              && (BarsSinceEntryExecution(0, "", 0) != 0)
              && (Low[Swing(2).SwingLowBar(0, 1, 50)] < Low[Swing(2).SwingLowBar(0, 2, 50)])
              && (Close[2] < Open[2])
              && (High[1] < High[2])
              && (Low[1] < Low[3])
              && (Low[1] >= Swing1.SwingLow[0])
              && (CrossAbove(Close, High[2], 1)))
              {
              EnterLong(1, @"LongEntryRunner");
              TriggerState = 1;
              LongStop = Low[1];
              Print(@"LongStop = ");
              Print(Convert.ToString(LongStop));
              }
              Print(@"Made it to 5");​
              Output from the log is as follows:

              ...
              ...
              ...
              Made it to 10
              Made it to 11
              Made it to 12
              Made it to 13
              Made it to 14
              Made it to 15
              Made it to 16
              Made it to 4
              Strategy 'HeadAndShouldersStrategyUnlocked': Error on calling 'OnBarUpdate' method on bar 52727: 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 what should my next step be? Do you see any likely place where such an error could be thrown, from your experience?

              What am I looking for?
              Last edited by Mistoffelees; 08-09-2024, 08:53 PM.

              Comment


                #8
                Hello,

                To figure out which exact line is throwing the index error, you could comment out logic until the error stops. Then, uncomment logic until the error returns to identify the exact line throwing the error.

                Comment


                  #9
                  So… I’m honestly stumped now.

                  Without making ANY changes whatsoever, simply starting everything up this morning is now permitting the strategy to work.

                  I don’t like this. How can I trust something that randomly does and doesn’t work and it has nothing to do with what I have done?

                  I closed down NinjaTrader 8 and restarted multiple times on Friday. Is there some way to manually flush any data out of NinjaTrader that may be causing issues?

                  Comment


                    #10
                    Hello,

                    Without the output from debugging to determine which line of code is throwing the error and identifying which value in particular is not able to be accessed, it's not possible to determine why the code is throwing that error.

                    You can try clearing the cache and historical data folders, however, it is unlikely this will do anything as it typically doesn't affect indexing error and we still don't know what code in the code is causing the error.

                    You may clear old historical and cache data by doing the following:
                    • Shut down NinjaTrader 8
                    • Go to the 'Documents/NinjaTrader 8/db' folder on your computer
                    • Remove the files within the 'cache', 'day', 'minute', and 'tick' folders
                    • Important: Do not remove any other files within the NinjaTrader 8 Documents folder

                    Comment


                      #11
                      Thank you, Gaby.

                      I guess I'll just have to wait until the error manifests itself in a way that can be isolated to determine its cause.

                      Comment

                      Latest Posts

                      Collapse

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