Announcement

Collapse
No announcement yet.

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.


    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.​
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    41 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    20 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    28 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    45 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    37 views
    0 likes
    Last Post CarlTrading  
    Working...
    X