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

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

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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Haiasi, 04-25-2024, 06:53 PM
        2 responses
        17 views
        0 likes
        Last Post Massinisa  
        Started by Creamers, Today, 05:32 AM
        0 responses
        5 views
        0 likes
        Last Post Creamers  
        Started by Segwin, 05-07-2018, 02:15 PM
        12 responses
        1,786 views
        0 likes
        Last Post Leafcutter  
        Started by poplagelu, Today, 05:00 AM
        0 responses
        3 views
        0 likes
        Last Post poplagelu  
        Started by fx.practic, 10-15-2013, 12:53 AM
        5 responses
        5,408 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Working...
        X