Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on calling 'OnBarUpdate' on using EMA()

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

    Error on calling 'OnBarUpdate' on using EMA()

    Hello,

    I'm getting the:
    Indicator 'DodaStochasticsJN: Error on calling 'OnBarUpdate' method on bar 25: You are accessing an index with a value that is invalid since it is out-of-range...
    message on my below code for a custom indicator. It looks like it's the one line in my 'for loop' in the 2nd block of code.



    Code:
    public class DodaStochasticsJN : Indicator
    {
    private double[] ExtHistoBuffer;
    private double[] ExtHistoBuffer2;
    private double[] ExtHistoBuffer3;
    private double[] ExtHistoBuffer4;
    
    private int SlwChat = 8;
    private int PdsChat = 13;
    private double SlwsignalChat = 9;
    
    private int myMaxBars = 500;​
    
    ... More stuff
    }




    Code:
    protected override void OnBarUpdate() {
      if (CurrentBar < 25)
       return;
    
    int shift;
    
    double minval;
    double maxval;
    
    for (shift = Count - 1; shift >= 0; shift--)
    {
       ExtHistoBuffer3[shift] = EMA(Close, SlwChat)[shift];                                                                    // It looks like this is where the error is happening
      }



    I feel like it may be something silly/simple, but still being a super newbie, I just can't figure out what I'm doing wrong here.

    Thank you,
    Jamie

    #2
    Hello Jamie,

    Thanks for your post.

    I recommend debugging your code and finding the exact index that is causing the error and then check the size of that collection to understand the cause.
    https://ninjatrader.com/support/foru...13#post1048513

    Use prints to find the line of code triggering the error.
    https://ninjatrader.com/support/foru...121#post791121
    <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
      Hi BrandonH,

      Thank you for the reply. I have been trying to debug the code. I know that the following line is the culprit:

      Code:
      ExtHistoBuffer3[shift] = EMA(Close, SlwChat)[shift];
      (The 2nd to the very last line in my 2nd code snippet in my first post)

      But I just can't figure out why.

      If I comment out the above line, and put:

      Code:
      Print("Shift = " + shift.ToString());
      In it's place, it prints out the lines:
      Shift = 564
      Shift = 563
      Shift = 562
      Shift = 561
      ...
      Shift = 3
      Shift = 2
      Shift = 1
      Shift = 0
      when there are 564 bars on the chart, the 564th being the one that is still being fully formed. I think this looks right, but I'm pretty new to this.

      The only thing I can think of is that the problem line:
      Code:
      ExtHistoBuffer3[shift] = EMA(Close, SlwChat)[shift];


      is either freaking out on me when shift = 0 or shift = 564? Would that be why?

      Comment


        #4
        Hello Jamie,

        Thanks for your notes.

        The index being used cannot exceed the CurrentBar number on the chart.

        You would need to further debug the script by adding prints that print out the indexes being accessed in the script and prints out the CurrentBar value.

        For any index used, the index must be less than the size of the collection.

        With BarsAgo values, CurrentBar is the size of the collection.​

        CurrentBar: https://ninjatrader.com/support/help...currentbar.htm

        See this forum thread for more information about debugging index errors: https://forum.ninjatrader.com/forum/...13#post1048513

        Note that in the Support department at NinjaTrader it is against our policy to debug a script on your behalf. It would be up to you to debug the script to determine exactly why your logic is throwing the invalid index error message.
        <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 Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        647 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        369 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        108 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        572 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        573 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X