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

Out of range error on Values[0].Set(x) ??

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

    Out of range error on Values[0].Set(x) ??

    This is weird. I even got and installed Visual Studio to help, but I still can't figure it out.

    I have an indicator having a primary series of 1 minute ES bars and an additional series of 1 tick bars. It works well enough when the Data Series session template is set to "Default 24/7" but if I set it to <use instrument settings> I consistently get an error message "Error on calling 'OnBarUpdate' method for indicator 'PriceSpeed' on bar 1: You are accessing an index with a value that is invalid since its out of range."

    Debugging in Visual Studio, this error occurs on a line that sets a value to a Plot!

    The line is:
    Values[0].Set(x);

    There's nothing wrong with the value in x either. Why would the Set() method cause an index out of range error?

    The code is pretty short. As I said, it works fine on the 24/7 session template but not on the default for the ES 06-12 market.

    -Alex
    Last edited by anachronist; 04-19-2012, 12:33 PM.

    #2
    Hi Alex,

    It's important to prevent the script from attempting to access bars before they are available. In a multiseries script, you can do this by adding the line below to OnBarUpdate:

    if (CurrentBars[0] < 0 || CurrentBars[1] < 0) return;
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      That did the trick. CurrentBar[1] had to climb up to 395 before CurrentBar[0] became non-negative. It seems like the check on CurrentBars[1] is unnecessary, though. In a multi-time frame indicator, the second data series uses smaller increments, so wouldn't CurrentBars[1] always be equal or greater than CurrentBars[0]?

      -Alex
      Last edited by anachronist; 04-19-2012, 12:40 PM.

      Comment


        #4
        You may be able to adjust it a bit for your own indicator, but the basic idea is not do do any processing until there are enough bars to work with. Although this was something to keep in mind for NT 6.5, there is no longer a restriction that secondary series must be smaller than primary series.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_RyanM View Post
          ...there is no longer a restriction that secondary series must be smaller than primary series.
          I didn't know that. Thanks.
          -A

          Comment


            #6
            Now that this problem is solved, I was hoping this simple example would help me with another one, but unfortunately it didn't shed any light.

            I have a much more complicated indicator that gives sporadic and difficult-to-repeat "index out of range" errors that I can't explain by using Visual Studio to step through the code.

            This indicator uses only one time frame, and it declares a private class with its own private methods.

            On the bar where the error occurs, OnBarUpdate() executes normally. Then when the code execution gets to the closing brace of OnBarUpdate(), I press F11 for one more execution step.... and it inexplicably jumps into the middle of a private method of the indicator's private class! This landing point happens to be the inside of an if() block for which the test condition is false, and an array index is then calculated that is out of bounds.

            Any idea what could be causing this? It almost seems like a bug in C#.

            IntelliTrace unfortunately doesn't work when attaching to NinjaTrader or any other process already running.

            (This is for my reference: I have found one repeatable instance of this bug. It occurs on bar 8 if I load 5 days of 24/7 session 1-minute ES 06-12 data ending 4/3/2012. Problem doesn't appear for other ending dates or data lengths.)
            -Alex
            Last edited by anachronist; 04-19-2012, 06:58 PM.

            Comment


              #7
              Hard to know for sure, but we'd be happy to take a look to see if we can spot it. The simplest version showing the issue is best, and you could post here or send to support 'at' ninjatrader 'dot' com. Standard disclaimer here that if it only happens with a lot of custom items, we may not be able to offer much insight.
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                OK, I have cut out code and commented out stuff as much as I can while still reproducing the error. I found that if I put an 'else' between two simple if() statements, the problem goes away. Without the 'else', OnBarUpdate() execution ends with an inexplicable jump into the middle of a private method.

                I don't expect you guys to debug my code but I am curious if the problem is my doing or something else. I am skeptical that this is a bug in my code, since it works nearly all the time except for a special case dependent on the data series loaded. Unlike me, you guys can trace what happens outside the indicator after OnBarUpdate() ends to determine if the problem is in NinjaTrader, in C#, or in my indicator.

                I will just outline how to reproduce the problem and provide a line to use as a breakpoint for tracing in Visual Studio.

                -Alex

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Johnny Santiago, 10-11-2019, 09:21 AM
                95 responses
                6,193 views
                0 likes
                Last Post xiinteractive  
                Started by xiinteractive, 04-09-2024, 08:08 AM
                2 responses
                11 views
                0 likes
                Last Post xiinteractive  
                Started by Irukandji, Today, 09:34 AM
                1 response
                3 views
                0 likes
                Last Post NinjaTrader_Clayton  
                Started by RubenCazorla, Today, 09:07 AM
                1 response
                5 views
                0 likes
                Last Post RubenCazorla  
                Started by TraderBCL, Today, 04:38 AM
                3 responses
                25 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Working...
                X