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

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.
    Create and loop over a string array. Access array Length and get elements at indexes.


    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.
    Brandon H.NinjaTrader Customer Service

    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​​
          Brandon H.NinjaTrader Customer Service

          Comment


            #6
            You are awesome!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cre8able, Yesterday, 01:16 PM
            3 responses
            11 views
            0 likes
            Last Post cre8able  
            Started by ChartTourist, Today, 08:22 AM
            0 responses
            5 views
            0 likes
            Last Post ChartTourist  
            Started by LiamTwine, Today, 08:10 AM
            0 responses
            2 views
            0 likes
            Last Post LiamTwine  
            Started by Balage0922, Today, 07:38 AM
            0 responses
            5 views
            0 likes
            Last Post Balage0922  
            Started by JoMoon2024, Today, 06:56 AM
            0 responses
            6 views
            0 likes
            Last Post JoMoon2024  
            Working...
            X