Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator error "outside the bounds of the array"

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

    Indicator error "outside the bounds of the array"

    I'm writing an indicator. When I apply it to a price chart, I get this error:
    Error on calling 'OnBarUpdate' method on bar 44: Index was outside the bounds of the array.
    Which array does it refer to? Setting:
    MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
    in
    State.SetDefaults
    does not solve this issue.

    Finally, I looked at bar 44 but didn't find anything wrong with it.

    #2
    Hello Gianpiero,

    Thanks for your post.

    This 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 also 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 a 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/helpGuides/nt8/make_sure_you_have_enough_bars.htm

    Also, below are links to the help guide on CurrentBar and CurrentBars.
    https://ninjatrader.com/support/helpGuides/nt8/currentbar.htm
    https://ninjatrader.com/support/helpGuides/nt8/currentbars.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.


    Ultimately, debugging prints would have to be added to your script that prints out each index you are accessing in your script along with the CurrentBar to find the exact line of code in your script causing the error. Note that CurrentBar must be greater than or equal to the index you are accessing in the script and negative indexes cannot be accessed.

    ​Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121
    Last edited by NinjaTrader_BrandonH; 10-30-2023, 07:35 AM.
    <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


      #3
      Thank you but beware some links return error 404, page not found.

      Comment


        #4
        Compile in debug mode, then examine the trace file in Documents -> NinjaTrader 8 -> trace -> today's date.txt to see the line number of your script that is the source of the error.
        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          Hello Gianpiero,

          Thanks for your notes.

          I have updated the links in post # 2 and also shared the link below.

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

          Also, below are links to the help guide on CurrentBar and CurrentBars.
          https://ninjatrader.com/support/helpGuides/nt8/currentbar.htm
          https://ninjatrader.com/support/helpGuides/nt8/currentbars.htm​​
          <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


            #6
            You are awesome!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            595 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            343 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            103 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            556 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            554 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X