Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Index was outside the bounds of the array

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

    Index was outside the bounds of the array

    I'm using this as a condition in a strategy, and I get the following error message "Index was outside the bounds of the array." Any cause for this? Thanks

    && VOLMA(14)[0] < 20

    #2
    Baseheadz,

    I don't see any reason why that would produce out of range message. It's likely some other part of the script or related to the VOLMA() indicator.

    If you want help isolating which line is causing it, you can use try-catch blocks.


    The most common reasons for running into this are explained here:
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      I tried many different indicators and I'm getting the same error message.

      I am also using this in OBU:
      if (CurrentBars[0] < 0 || CurrentBars[1] < 0) return;

      Comment


        #4
        What is the exact error message you're getting? It should list the file and the method that produced the error.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          It compiles fine and then when I set the strategy to true, it comes up with an error in the log.

          It says:

          Strategy Error on calling 'OnBarUpdate' method for strategy 'TestStrategy/ea0e7bb49a544162938e052ef65625d2': Index was outside the bounds of the array.

          Comment


            #6
            There's no way to know how to resolve this for you. You have to use try- catch blocks to isolate what's causing it. It may also be easier to simplify your strategy until it works without that message. If you want to post the script here will give a look.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              I reloaded the Ninjascript and I'm no longer getting the error in the log!

              Interesting

              Comment


                #8
                Glad to hear and thanks for the update. Any code changes made will not be reflected until a reload NinjaScript. If you make changes to Initialize() you need to remove and add script again to see these.
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Dear Ryan,

                  I've the same error when trying to plot the DMIndex on a chart inside a custom strategy

                  Code:
                  DMIndex(3).Panel = 1;
                  DMIndex(3).Lines[0].Value = Vleveldown;
                  DMIndex(3).Lines[1].Value = Vlevelup;
                  Add(DMIndex(3));
                  There is clearly something NT doesn't like but I've no idea what!

                  Cheers,
                  Chris

                  Comment


                    #10
                    Hi Chris,

                    For this type of error, you can use try-catch to identify what is contributing.


                    What if you switch the order of your Initialize() statements so that the Add() is before the others.

                    Code:
                    Add(DMIndex(3));
                    DMIndex(3).Panel = 1;
                    DMIndex(3).Lines[0].Value = 3;
                    DMIndex(3).Lines[1].Value = 3;
                    Last edited by NinjaTrader_RyanM1; 11-15-2011, 09:21 AM.
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Ryan,

                      Indeed I always wondered why the add came at the end and not at the beginning of the statement.

                      I took my cue from here http://www.ninjatrader.com/support/f...ead.php?t=3228 but I prefer your logic of starting with the add.

                      I'll test because the DMIndex generates out of memory exception in my custom strategy (and not the RSI which is strange because the DMIndex is very close to the RSI).

                      Cheers,
                      Chris

                      Comment


                        #12
                        preventing index out of range problems

                        One approach that often works is to make sure that the DataSeries or other type of array contains enough elements so that what you are trying to do can actually happen. For example, if you are using if(Risng(Values[0])) as a condition, don't do it unless Values[0].Count >=2, because if an array has less than 2 elements, Rising and Falling are meaningless.

                        Sometimes the errror occurs during startup when your array is empty, ie contains no elements and has a Count property of zero.. So for anything that accesses an array called MyArray, using if(MyArray.Count>3) as an enabling condition before trying to access a value of the array will take care of that condition.

                        Or before accessing the value for b bars back , check that MyArray.ContainsValue(b) is True.

                        If b is negative you can have problems. Unless you take special precautions and are more than b bars back from the CurrentBar, the value you are looking for will not exist. Avoid doing this.
                        Last edited by Ricam; 11-15-2011, 12:21 PM.

                        Comment


                          #13
                          Thank you Ricam, I am truly a newbee and I don't think I am using array in my custom strategy, just some basics indicators like RSI or MACD.

                          In any case thank you for your advice, I need to pay closer attention to some of my conditions to avoid some of the loopholes re failing, crossabove, etc.

                          cheers, chris

                          Comment


                            #14
                            A related problem sometimes happens when you don't exclude some opening bars that produce invalid values. This is the unstable period of the indicator. An example would be looking for the value of a 14 period MA when the CurrentBar is less than 14.

                            So, let's say you have an indicator that uses two moving averages with periods Fast and Slow. A generally safe approach is to avoid those bars by starting OnBarUpdate with

                            Code:
                            if(CurrentBar < Math.Max(Fast,Slow) )  return;
                            Sometimes there will be errors only when CurrentBar is -1 or 0, and only those values need to be excluded. It's good to have your catch block print out the CurrentBar value as well as the error message to help narrow this down.
                            Last edited by Ricam; 11-16-2011, 07:22 AM.

                            Comment


                              #15
                              Is it the equivalent (in a more sophisticated way) of the minimum bars required (backtest)?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by sjsj2732, Yesterday, 04:31 AM
                              0 responses
                              33 views
                              0 likes
                              Last Post sjsj2732  
                              Started by NullPointStrategies, 03-13-2026, 05:17 AM
                              0 responses
                              286 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              286 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              133 views
                              1 like
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              91 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Working...
                              X