Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

"index was outside the bounds of the array" : Where??

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

    "index was outside the bounds of the array" : Where??

    Hi,

    Indicator 'aaa': Error on calling 'OnBarUpdate' method on bar 41812: Index was outside the bounds of the array.

    How can I find the source code line # that gives this error?

    I need to mention that :

    1) This error doesn't seem to prevent my indicator from running correctly, it appears only once.
    2) The source code is VERY long with many arrays, so it's almost impossible to fix this bug without a minimum of clues...

    Thanks!

    #2
    Hello vindiou,

    Thank you for your note.

    It's likley one of your arrays is trying to loop further back than data is available. You could add prints to the script, such as Print("Getting to line 100"); Print("Getting to line 150"); etc, so that on the BarUpdate which returns the error, you can know which line or part of the script which triggers this error. For example, I you see "Getting to Line 100", then the error, you'll know it was after line 100 and before line 150.

    I’ve provided a link to a youtube video which covers an example of using prints to understand behavior:
    Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.NinjaTrader 7 is an award winning end to end online ...


    I’ve provided a link covering debugging which you may find helpful.
    Debugging: http://ninjatrader.com/support/forum...ead.php?t=3418

    You may also try debugging in Visual Studio, see the following link,


    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      I have, of course, already put "Prints" every 20 lines but it didn't help since the indicator doesn't stop running correctly...

      Moreover, this error only shows once in the Log and when it shows, the Prints are much too fast to be able to locate the line number that generates this error...

      Any other suggestion?

      Thanks

      Comment


        #4
        Hello vindiou,

        I would suggest commenting out large blocks of code until you do not get the error any more as a way to isolate the issue.

        Using Visual Studio and setting points would also be a approach you could use.

        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by vindiou View Post
          Indicator 'aaa': Error on calling 'OnBarUpdate' method on bar 41812: Index was outside the bounds of the array.
          Anything special happening just before CurrentBar=41812?

          Comment


            #6
            Nothing special at this line.
            I don't want to install Visual Studio, it's a Rube Goldberg machine.
            NinjaTrader breakpoints would be very useful without having to install VS.

            Comment


              #7
              Originally posted by vindiou View Post
              Nothing special at this line.
              I don't want to install Visual Studio, it's a Rube Goldberg machine.
              NinjaTrader breakpoints would be very useful without having to install VS.
              Bar 41812 is a bar number, not a line number.

              Asking for NinjaTrader breakpoints without having to install Visual Studio is like asking for a way to get to Mars without having to use a rocket.
              Last edited by bltdavid; 09-03-2018, 01:55 AM.

              Comment


                #8
                Originally posted by bltdavid View Post
                Bar 41812 is a bar number, not a line number.
                I wanted to say bar, mistake.
                Originally posted by bltdavid View Post
                Asking for NinjaTrader breakpoints without having to install Visual Studio ...
                Why? Is it so difficult to implement??

                Comment


                  #9
                  Hello vindiou,

                  Code:
                  Asking for NinjaTrader breakpoints without having to install Visual Studio is like asking for a way to get to Mars without having to use a rocket.
                  bltdavid uses a great analogy here, because breakpoints are a feature of Visual Studio. In this case the rocket is visual studio and Mars is your goal.

                  Please let us know if you need further assistance.
                  Alan P.NinjaTrader Customer Service

                  Comment


                    #10
                    Anybody's got a better solution, a knack/trick?

                    Thanks!

                    Comment


                      #11
                      Use Try/Catch around your entire OnBarUpdate

                      Originally posted by vindiou View Post
                      Anybody's got a better solution, a knack/trick?
                      Put your entire OnBarUpdate inside a Try/Catch and print the stack trace of the exception when caught, something like,

                      Code:
                              protected override void OnBarUpdate()
                              {
                                  try {
                                      OnBarUpdate_MyHandler();
                                  }
                                  catch (Exception ex) {
                                      string exMessage = FormatExceptionMessage(ex);
                                      Print(exMessage);
                                      throw;
                                  }
                              }
                      Simply rename your original OnBarUpdate to OnBarUpdate_MyHandler, then change its signature to look like this,

                      Code:
                              [COLOR=Red]private void[/COLOR] OnBarUpdate_MyHandler()
                              {
                              .... all your original OnBarUpdate code ....
                              }
                      The purpose of FormatExceptionMessage is to unwind the Exception chain, in case any inner exceptions were thrown as well, and build the stack trace into a printable string. Here is exactly what I use in my own code,

                      Code:
                              protected static string FormatExceptionMessage(Exception ex)
                              {
                                  return FormatExceptionMessage(null, ex);
                              }
                      
                              // return formatted message (w/StackTrace) for all messages in exception chain
                              protected static string FormatExceptionMessage(string MessagePrefix, Exception ex)
                              {
                                  StringBuilder sb = new StringBuilder();
                      
                                  MessagePrefix = MessagePrefix ?? "EXCEPTION";
                      
                                  do {
                                      sb.AppendLine("<" + MessagePrefix + ": " + ex.Message.Trim() + ">");
                                      sb.AppendLine(ex.StackTrace);
                                      ex = ex.InnerException;
                                  } while (ex != null);
                      
                                  return sb.ToString();
                              }
                      Lastly, make sure you import this namespace,

                      Code:
                      Using System.Text;
                      at the top of your indicator -- because StringBuilder lives there.

                      Good luck!

                      Comment


                        #12
                        Thanks, do you believe that an index outside the bounds of its array would generate an exception?
                        Is the exception message easily readable or does it look like a memory dump?
                        Last edited by vindiou; 09-04-2018, 11:40 AM.

                        Comment


                          #13
                          Originally posted by vindiou View Post
                          Thanks, do you believe that an index outside the bounds of its array would generate an exception?
                          I think so. There is an exception named IndexOutOfRangeException, but I'm not sure if the NT code is actually throwing that exception ...

                          Originally posted by vindiou View Post
                          Is the exception message easily readable or does it look like a memory dump?
                          No, the stack trace printed by the exception is quite readable.

                          It's usually pretty easy to discern new details about the location of the defective code from the exception's stack trace.

                          Did you try it?
                          Did it help?

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by PhillT, Today, 02:16 PM
                          2 responses
                          3 views
                          0 likes
                          Last Post PhillT
                          by PhillT
                           
                          Started by Kaledus, Today, 01:29 PM
                          3 responses
                          9 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by frankthearm, Yesterday, 09:08 AM
                          14 responses
                          47 views
                          0 likes
                          Last Post NinjaTrader_Clayton  
                          Started by gentlebenthebear, Today, 01:30 AM
                          2 responses
                          14 views
                          0 likes
                          Last Post gentlebenthebear  
                          Started by PaulMohn, Today, 12:36 PM
                          2 responses
                          17 views
                          0 likes
                          Last Post PaulMohn  
                          Working...
                          X