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

Need help related to index out-of-range error on [barsAgo]

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

    Need help related to index out-of-range error on [barsAgo]

    Hi,

    I ran into this error causing my strategy to be disabled. This happens in simulation trading (forward testing). The problem is this error does not occur during replay via historical data. I am guessing this could due to data error during simulation trading, but somehow corrected in historical data. Any tips / hints on how to handle such occurrences ?
    • Indicator 'WisemanAlligator': Error on calling 'OnBarUpdate' method on bar 1480: 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.

    #2
    Hello elirion,

    This message is indicating the specific index requested from a collection does not exist. Indexes must be a non-negative number and less than the size of the collection. The error may be indicating the index requested is larger than the number of elements in the collection.

    In the case of barsAgo values with Series, any barsAgo index must be less than CurrentBar (the total number of bars is the size of a Series collection).
    Code:
    // require BarsInProgress 0 to have 3 bars processed, require BarsInProgress 1 to have 4 bars processed
    if (CurrentBars[0] < 3 || CurrentBars[1] < 4)
    return;
    
    Print(Times[0][3]); // this does not cause an error as CurrentBars[0] is equal to or greater than 3
    
    Print(Times[1][4]); // this does not cause an error as CurrentBars[1] is equal to or greater than 4
    
    Print(Times[0][5]); // this line of code will cause an invalid index error. When CurrentBars[0] is 4 there is no bar 5 bars ago​
    The help guide discusses ‘Make sure you have enough bars in the data series you are accessing’ .
    NinjaScript > Educational Resources > Tips > Make sure you have enough bars in the data series you are accessing

    Further, Series are updated when OnBarUpdate() is updated. Attempting to use a Series or Series dependent object (such as Draw methods) from a non-data-driven method, including from: the OnRender() method, a button click event handler, or a timer elapsed method hander, will result in an indexing error as the Series indexes are not up-to-date.

    Call TriggerCustomEvent() from any non-data-driven method to update all Series before using the series in the coded logic.

    NinjaScript > Language Reference > Common > TriggerCustomEvent()

    This also applies to barsAgo values supplied to Drawing Objects.
    Code:
    if (CurrentBar < 1)
    return;
    
    Draw.Dot(this, “myDot”, 5, Brushes.Blue); // this line of code will cause an invalid index error as the start barsAgo parameter is 5 when the CurrentBar processed is bar 1.​
    Indexing errors can also occur when using an invalid index with arrays, lists, and other collections where the index used is equal to or larger than the .Count() or .Length of the collection, as well as with method calls that use indexes such as: <string>.Subtring() which uses a string position index, <string>.Format() which uses indexes for format items in the string (placeholders {0}), and Draw methods which use barsAgo indexes.

    Dotnetperls is a 3rd party educational site with an in-depth review of arrays.
    C# Array Examples - Dot Net Perls



    ​What is the specific line of code in your strategy that is causing the error?

    If you are unsure, add a print above each line to determine the line under the last print to appear.
    Developer Guide - Debugging using Print() and TraceOrders
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      Thank you so much for the help.

      Unfortunately, I can never simulate the same error when I run in replay mode. The error simply doesn't appear again. As such I can't really identify which line of code causing the problem.

      Comment


        #4
        Hello elirion,

        Do you have real-time data from a broker or data provider to test the script in real-time on the Sim101 account?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea,

          Yes I have real time data from Tradovate. In fact I got the error when I ran in real-time (via simulated account). When I want to run with replay function to fix the problem, it simply doesn't pop up any more.

          Comment


            #6
            Don't worry too much Chelsea, since I can't really simulate the error again, this is probably going nowhere

            My main concern is how to handle strategy crashes elegantly with the ability to flatten and cancel all pending orders.

            I have posted a separate question, which Brandon is helping me.
            Scenario I encountered during my forward testing: When I got a signal to go long, I will submit order for long limit entry. Once the long limit entry is filled, I submitted a pair of profit taking and stop loss orders. I encountered an exception/error and my strategy was disabled. However my long position and pair of bracket


            thank you !

            Comment


              #7
              Hello elirion,

              Since you are only reproducing the error in real-time, run the script in real-time while connected to Tradovate to reproduce the error.

              As this is a run time logic error and not an order error, it will not be possible to gracefully exit the position and cancel orders. The strategy will come to an immediate halt.
              To prevent the issue, it will be necessary to identify the line of code, and correct this so that an invalid index is not used on the collection causing the error.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Thank you so much Chelsea... I will study your notes in greater details.
                That's all for now

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by anton_tymoshchuk, Yesterday, 01:14 AM
                4 responses
                24 views
                0 likes
                Last Post anton_tymoshchuk  
                Started by percy3687, Today, 12:28 AM
                0 responses
                7 views
                0 likes
                Last Post percy3687  
                Started by sofortune, Yesterday, 11:45 PM
                1 response
                8 views
                0 likes
                Last Post sofortune  
                Started by zzeric, Yesterday, 11:23 PM
                1 response
                5 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by Pep de lou, Yesterday, 08:14 PM
                2 responses
                23 views
                0 likes
                Last Post bltdavid  
                Working...
                X