• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on calling 'OnBarUpdate' method

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

    Error on calling 'OnBarUpdate' method

    Good evening all,

    I am getting the following error for my very simple Indicator script:

    Error on calling 'OnBarUpdate' method for indicator 'aatest' 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.

    Did some troubleshooting, and it turns out that if I switch the Data Series option "Load data based on" to "Bars", this error disappears and my indicator works perfectly. If I switch "Load data based on" to "Days" (my preferred option), I get the above error.

    I note that the error doesn't happen in "Days" mode when my code is as simple as possible. The following code works:

    Print(Time[0].ToString());

    The Output window is filled with a list of strings. Perfect. But the following code gives me an error on the second line:

    Print(Time[0].ToString());
    DrawText("test","Testing",1,Close[1],Color.Blue);


    It outputs one line of DateTime, then shows the error above. It looks like I'm having some sort of problem accessing barsAgo when the chart is in "Days" mode, but not when accessing Time. "Days" mode is the default for all my charts and what I want to use. How can I fix this?

    EDIT: just tried the following code:

    Print(Time[1].ToString());

    This error appeared in the Output box:

    Error on calling 'OnBarUpdate' method for indicator 'aatest' on bar 0: Bar index needs to be greater/equal 0
    Last edited by thesorehead; 01-31-2019, 03:52 PM.

    #2
    Hello thesorehead,
    Thanks for your post and welcome to the NinjaTrader forum.

    This error is caused when there are not enough bars contained in the data series you are accessing. Please see the following link for more information on how to resolve this error.

    https://ninjatrader.com/support/help...nough_bars.htm

    Please let me know if any questions come up.
    JoshNinjaTrader Customer Service

    Comment


      #3
      Hi JoshG, thank you for your response. I guess I have one question:

      I am trying to access parts of the current bar (i.e. the bar on the far right of the chart, involved in the present-time price, e.g. Input[0] or Open[0]). The indicator should work with only one bar in the series. How do I ensure my indicator script can access this bar while the data series is loading data based on Days?

      Or to put it another way: if the problem is that there aren't enough bars in the data series I'm accessing, how do I ensure there are enough bars in the data series? What is the difference between "Load data based on Days" vs. "Load data based on Bars", that causes the data series to not load in the chart while in "Load data based on Days" mode?

      Thanks again.
      Last edited by thesorehead; 01-30-2019, 08:59 PM. Reason: Clarified circumstance of data not loading.

      Comment


        #4
        thesorehead,

        Sounds like you need to check for at least one bar. If I still got the error after checking for one bar, I would remove all the code from OnBarUpdate() and re-introduce logic until I hit the error again. How many bars are you currently checking for? The "How" is going to be detailed in the link from my post#2.

        Code:
         What is the difference between "Load data based on Days" vs. "Load data based on Bars"
        The difference is one loads data based on a number of days(which could include dozens of bars), and the other loads data based on an actual number of bars.

        JoshNinjaTrader Customer Service

        Comment


          #5
          Hi JoshG,

          So the only difference is the number of bars loaded?

          Without checking for any bars:
          • My indicator works perfectly when the chart is set to "Load data based on Bars".
          • My indicator is unresponsive, producing the error in my OP, when the chart is set to "Load data based on days".

          In both cases, the chart itself looks normal and displays real-time prices as normal.
          In both cases, the other indicators I might load work as expected.

          Can you think of any reason why my indicator does work when the data series is set to "Load data based on Bars", and does not work when the data series is set to "Load data based on Days"? What is it about the bar index that changes between these two ways of loading data?

          I have done some of the "code removal" troubleshooting as you suggested, testing OnbarUpdate() with single lines of code while the chart was set to "Load data based on Days". For example:

          Print(Time[0].ToString());

          prints the timestamp for the current bar to the output window on each tick. I then tried this line:

          DrawText("test","Testing",1,Close[1],Color.Blue);

          which produced the error. There was a curious thing when I combined the two lines like so:

          Print(Time[0].ToString());
          DrawText("test","Testing",1,Close[1],Color.Blue);


          The Output window contains a single line with a timestamp, and then shows the same error as usual. What is curious is that if the number of Days is set to 10, the timestamp shown is from 10 days ago.

          Thank you for your help so far, I will do further testing tonight if I get a chance.

          Comment


            #6
            thesorehead,

            Yes, the reason is you are not checking for enough bars based on your logic. Please show me what steps you are taking in your code to avoid this error.
            JoshNinjaTrader Customer Service

            Comment


              #7
              Hi JoshG,

              Thank you for your help and patience. I finally got a chance to try out your solution tonight, and it worked!


              Code:
              Print(Time[0].ToString());
              DrawText("test","Testing",1,Low[1]-5,Color.Blue);
              just gave the error and refused to show anything on the chart, as before. But,



              Code:
              if (CurrentBar < 1) return;
              Print(Time[0].ToString());
              DrawText("test","Testing",1,Low[1]-5,Color.Blue);
              worked exactly as expected, with time strings in Output and "Testing" in blue below the low of the prior candle.

              My only question now is why does it work? Some of the NT tutorial indicators have a form of bar checking (e.g. if(CurrentBar < Period) return; ) others do not. Should I just include ( if (CurrentBar < 1) return; ) in every indicator from now on, or is there a way to tell (other than testing) whether or not it will be needed?


              Thanks again!
              Last edited by thesorehead; 02-02-2019, 12:50 AM.

              Comment


                #8
                thesorehead,

                If you are referencing price data you will need to do a barcheck. As there are likely several different reason for it, the indicators that do not have a bar check inside their own code are doing something that logically allows them to get away with that.

                The most simple way is to look through the code and find the largest number of BarsAgo you used and prevent the script from processing until that many bars have been processed.
                JoshNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Steve L, Today, 05:01 PM
                0 responses
                5 views
                0 likes
                Last Post Steve L
                by Steve L
                 
                Started by Trader-Joe, Today, 04:06 PM
                1 response
                13 views
                0 likes
                Last Post NinjaTrader_ShawnB  
                Started by vantojo, Today, 04:06 PM
                0 responses
                5 views
                0 likes
                Last Post vantojo
                by vantojo
                 
                Started by tonynt, Today, 02:39 PM
                2 responses
                13 views
                0 likes
                Last Post tonynt
                by tonynt
                 
                Started by StevenNelson, Today, 12:23 PM
                1 response
                12 views
                0 likes
                Last Post NinjaTrader_PatrickG  
                Working...
                X