Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on calling 'OnBarUpdate' method on bar 118: You are accessing and index...

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

    Error on calling 'OnBarUpdate' method on bar 118: You are accessing and index...

    For the life of me I can't figure out what is wrong with this code. I have enabled debug printing and where it is erroring makes no sense. I have stripped the indicator down the most bare bones example of what I started with and am running it against a single 5 minute NQ chart with 90 days of history. I have attached the zip file with indicator source code as well as a screenshot of the output I am seeing from the debug prints.

    Any help would be greatly appreciated.

    ExampleBlowUp.zip

    Thanks,
    Jeremy
    Attached Files

    #2
    Hello jmappus,

    The error means that an invalid index was used.

    This could be from a series, array, list, or other collection, or could be from a method call that uses barsAgo values like Draw methods or string.Format().


    You will need to identify the specific line of code causing the error, and the indexes used by adding debugging prints to the code.

    Below is a link to a support article on using Print to understand behavior.


    Add a simple print of the code line number above each condition, method call, and assignment in OnBarUpdate().

    The last print to appear before the error is likely one line above the line with the error.
    What is that line of code?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello jmappus,

      The error means that an invalid index was used.

      This could be from a series, array, list, or other collection, or could be from a method call that uses barsAgo values like Draw methods or string.Format().


      You will need to identify the specific line of code causing the error, and the indexes used by adding debugging prints to the code.

      Below is a link to a support article on using Print to understand behavior.


      Add a simple print of the code line number above each condition, method call, and assignment in OnBarUpdate().

      The last print to appear before the error is likely one line above the line with the error.
      What is that line of code?
      Hey Chelsea, thanks for the reply... That's the thing, I have added a ton of debug prints in order to isolate the code. If you look at the output, the only line of code called between the last output line and the next print (that never shows up before the error) is this:
      DateTime copyDt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute + BarsPeriod.Value, dt.Second);

      dt is a passed in variable and a value type copy of Time[0] from the calling function. Here is the whole three line segment of code for reference:
      Print(string.Format("{0} Opposite direction candle", dt));
      DateTime copyDt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute + BarsPeriod.Value, dt.Second);
      Print(string.Format("{0} Accessing Open[0], Open[1], Close[0], Close[1], Low[0], Low[1], High[0], and High[1] - Engulfing check", dt));

      Doesn't really make any sense where it is erroring, even if there are multiple threads accessing the data without locks. Perhaps there is some subtle behavior I am missing somewhere else in the code or in the way that Ninja creates and manages this data. Please take a look at the included code because I believe I have done all of the recommended things to prevent this error and yet here we are.

      Thanks,
      Jeremy

      Comment


        #4
        Hello Jeremy,

        The EngulfingTest() method is being called from OnBarUpdate() and would be updating on the same thread. Using the dt variable to hold the Time[0] value is fine.

        It looks like you are trying to add minutes to a datetime object.

        A simple way to do this would be:

        DateTime copyDt = dt.AddMinutes(BarsPeriod.Value);

        However, lets confirm this is the line causing the error.

        Comment the suspected line out and reload the script (add to slashes // to the start of the line).
        Is the error still appearing?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello Jeremy,

          The EngulfingTest() method is being called from OnBarUpdate() and would be updating on the same thread. Using the dt variable to hold the Time[0] value is fine.

          It looks like you are trying to add minutes to a datetime object.

          A simple way to do this would be:

          DateTime copyDt = dt.AddMinutes(BarsPeriod.Value);

          However, lets confirm this is the line causing the error.

          Comment the suspected line out and reload the script (add to slashes // to the start of the line).
          Is the error still appearing?
          Well hell... thought I had tried that already, but apparently not. That is indeed the line that is causing the error.

          So I put a try... catch... around this block of code and it is throwing an exception because the minute value exceeds 59 in some cases. Would be good if NT actually passed on and printed the actual error rather than what appears to be a generic catch all error when indexing a value. Basically creates a false positive situation where people are looking for indexing operators on bar operations as the culprit and instead it literally could be anything within OnBarUpdate() that throws an exception.

          Thanks for the help Chelsea,
          Jeremy

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          571 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
          549 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X