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

Can't access more than five bars in the past??

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

    Can't access more than five bars in the past??

    I am a brand new user, so I'm sure this is some sort of screw up that only new users would make.
    I'm using the simulated data feed.
    I created a custom indicator, which I translated from a working TOS script. It compiles cleanly, but gives no output on the chart.
    I opened an output window, and see the following error:

    Error on calling 'OnBarUpdate' method for indicator 'SecondDer' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    I put in some print statements to see where exactly the error was occurring. I deliberately put one before accessing the SMA function, then after, as I'm using an SMA with 10 and one with 11 periods.
    These work fine, as the print statements before and after show up on the output window.
    After that, I print the value of the counter I'm using in a while statement where I'm accessing bars via the Close[subscript] array, and it is set to the expected value of 10.
    The above error pops up after the print of the counter, so obviously the while loop is causing this.
    Output window output now looks like this:

    Before SMA
    After SMA
    10
    Error on calling 'OnBarUpdate' method for indicator 'SecondDer': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.


    Below is the while loop. If someone can tell me what I'm doing wrong, I'd be much obliged:

    Scratch = AverageDiv - 1; (this is set to 11 at the top of the program; this statement sets this to 10, which is printed in the output window output above after the "After SMA" output.)
    Print(Scratch);
    while (Scratch >= 0)
    {
    ROCNow = (Close[Scratch] - Close[Scratch + 1]) / Close[Scratch + 1];
    ROCPrev = (Close[Scratch + 1] - Close[Scratch + 2]) / Close[Scratch + 2];
    if (Math.Abs(Close[Scratch + 1] - Close[Scratch + 2]) > 0)
    {
    SecondDerValue[Scratch] = ((ROCNow - ROCPrev) / Math.Abs(ROCPrev));
    }
    {
    SecondDerValue[Scratch] = ROCNow;
    }
    SumSecondDer = SumSecondDer + SecondDerValue[Scratch];
    Scratch = Scratch - 1;
    }
    SecondDerAverage = SumSecondDer / AverageDiv;
    Print("Average");
    Print(SecondDerAverage);

    #2
    Hello Phantasm,

    This can be resolved with a CurrentBar check, so that you're not accessing objects that don't exist. This technique is discussed in more details here:
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thanks, I'm going to try that now, although it will take a while for me to report back whether it works or not (have to go run an errand).
      In the meantime, the chart I tried it on had lots of bars on it, so I don't know why it got hung up at precisely 5 bars. Is this some limitation of using the simulated data feed, or do I have to tell the program something if I want to refer back more than 5 bars? I see a property for 256 bars back, so I don't think that's it, but you never know.

      Comment


        #4
        You can still get this error message when your chart has lots of bars. It's caused when attempting to access objects that don't exist. If you attempt to access Close[1] on bar 0, it does not exist and will generate an error message, even if you have many bars.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Still getting the same error, but I think I know why. It must be this:

          When I attach an indicator to a chart, does it start processing from the leftmost bar and then move right? That sounds like what you're saying above, and what the posts you linked to are saying, but it's not clear, or at least not clear to me, anyway. So, what I want to do is not let this loop do anything until the CurrentBar is equal to 11, after which I should be OK. Let me know if I'm on the right track here.
          Actually, can I just exit the entire OnBarUpdate call in this case, since until I reach bar 11 I won't have enough info to process.

          Comment


            #6
            That indeed was it. I put in a test right at the top of OnBarUpdate, as follows:

            if (CurrentBar > AverageDiv)
            {
            .....
            }

            and that did it.
            Sorry for the lack of comprehension there. Thanks for the help.

            Comment


              #7
              Yes, you're on the right track.

              if (Currentbar < 11) return;
              Ryan M.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by mkouitra, 10-23-2021, 04:40 PM
              17 responses
              1,959 views
              0 likes
              Last Post NinjaTrader_Jason  
              Started by Vietanhnguyen2hotmailcom, 05-03-2024, 10:29 AM
              4 responses
              27 views
              0 likes
              Last Post Vietanhnguyen2hotmailcom  
              Started by PhillT, 04-19-2024, 02:16 PM
              4 responses
              36 views
              0 likes
              Last Post PhillT
              by PhillT
               
              Started by ageeholdings, 05-01-2024, 05:22 AM
              5 responses
              38 views
              0 likes
              Last Post ageeholdings  
              Started by reynoldsn, Yesterday, 02:34 PM
              0 responses
              16 views
              0 likes
              Last Post reynoldsn  
              Working...
              X