Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bars indices out of range

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

    Bars indices out of range

    Hello!
    is there here in Ninjatrader away to solve such problem?
    I am building a strategy, but I am facing a problem while compiling the code. I am reciving the message like: " Error on calling 'OnBarUpdate' method on bar 14: 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."

    I have the following code:

    HTML Code:
    if(CurrentBar < 14) return;
    for(int i = 14; i >= 0; i--)
    {
        double  myRsi = RSI(14, 3)[i+8];
    }
    When I have: if(CurrentBar < 0) return;, I received the error message already for the bar 0;

    In my code, when i = 14,
    HTML Code:
     myRsi = RSI(14, 3)[14+8]
    ,
    what ist already beyong the 14 I have in my loop.

    How could I solve this problem since I am frequently encountering such problems in my codes? Which condition could be suitable in this case so that I could have the value of myRsi at position 14+8 for example?

    #2
    if (CurrentBar < 14+8) return;

    Comment


      #3
      Thanks Bldavid for your reply.

      Sorry for the post before this correction. Yes it works.
      Many thanks!!!
      Last edited by Stanfillirenfro; 07-26-2021, 07:09 AM.

      Comment


        #4
        Hello Stanfillirenfro,

        Thanks for your post.

        bltdavid is correct. You would need to ensure that there are enough bars loaded on the chart for the data being processed. For example, if you want to call the RSI with a barsAgo value of 14 + 8 (22), then you would need to use a CurrentBar check to see if 22 bars have loaded. ( ex: if (CurrentBar < 22) return; )

        See this help guide page for more information: https://ninjatrader.com/support/help...nough_bars.htm

        Let us know if we 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
        81 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        41 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        64 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        66 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        54 views
        0 likes
        Last Post CarlTrading  
        Working...
        X