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 outside bounds of array error

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

    Index outside bounds of array error

    Hi this is my code that seems to be producing an error.
    I can't see where the error is??

    Code:
            protected override void OnBarUpdate()
            {
        if(CurrentBar < 50)
                return;
            ///Only works on Volumetric Bar Types
             NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsType barsType = BarsArray[0].BarsType as
             NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsType;    
             if (barsType == null)
              return;​
    }

    #2
    Hello Mindset,

    Thanks for your post.

    This error message means an invalid index was used somewhere in your script.

    For example, with the error:
    "Error on calling 'OnBarUpdate' method on bar 0: 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". This message indicates an invalid index was used.

    As one example, calling Close[1] when CurrentBar is 0 (the very first bar) will cause this error. The reason for this is because there will not be a bar ago; essentially there is not enough data at bar 0 look back one bar.

    The script will start from the far left of the chart at the very beginning where the CurrentBar will equal 0 and then start working to the right, calling OnBarUpdate() for each bar. In this specific example, it would be needed to wait for the second bar to be built (from all series if there are multiple series added), to ensure that you have enough data to call an index 1 bar ago. This is especially important when multiple series are added to a single script as the data for all series may not start at the same time.

    Below is a link to the help guide on this specific example of an indexing error.
    https://ninjatrader.com/support/help...nough_bars.htm

    Also, below are links to the help guide on CurrentBar and CurrentBars.
    http://ninjatrader.com/support/helpG...currentbar.htm
    https://ninjatrader.com/support/help...urrentbars.htm

    Indexing errors can also occur when using an invalid index with arrays or collections.

    Below are public links to 3rd party educational sites on arrays and index out of range errors.
    Create and loop over a string array. Access array Length and get elements at indexes.


    What was the exact index you tried to use that was invalid?

    Did you ensure the Count of that object is equal to or greater than the index used?

    Let me know if I may assist further.​
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Segwin, 05-07-2018, 02:15 PM
    10 responses
    1,769 views
    0 likes
    Last Post Leafcutter  
    Started by Rapine Heihei, 04-23-2024, 07:51 PM
    2 responses
    30 views
    0 likes
    Last Post Max238
    by Max238
     
    Started by Shansen, 08-30-2019, 10:18 PM
    24 responses
    943 views
    0 likes
    Last Post spwizard  
    Started by Max238, Today, 01:28 AM
    0 responses
    10 views
    0 likes
    Last Post Max238
    by Max238
     
    Started by rocketman7, Today, 01:00 AM
    0 responses
    7 views
    0 likes
    Last Post rocketman7  
    Working...
    X