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' 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.



    Please let me know if any questions come up.
    Josh G.NinjaTrader 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.

        Josh G.NinjaTrader 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.
            Josh G.NinjaTrader 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:
              [INDENT] [LEFT][COLOR=#252C2F][FONT=courier new]Print(Time[0].ToString());[/FONT][/COLOR][/LEFT]
               
              [LEFT][COLOR=#252C2F][FONT=courier new]DrawText("test","Testing",1,Low[1]-5,Color.Blue);[/FONT][/COLOR][/LEFT]
               
              [/INDENT]
              just gave the error and refused to show anything on the chart, as before. But,



              Code:
              [INDENT][FONT=courier new]if (CurrentBar < 1)
              return;[/FONT]    [LEFT][FONT=courier new]Print(Time[0].ToString());[/FONT][/LEFT]
               
              [LEFT][FONT=courier new][FONT=courier new]DrawText("test","Testing",1,Low[1]-5,Color.Blue);[/FONT][/FONT][/LEFT]
               
              [/INDENT]
              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.
                Josh G.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by warreng86, 11-10-2020, 02:04 PM
                7 responses
                1,360 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by Perr0Grande, Today, 08:16 PM
                0 responses
                5 views
                0 likes
                Last Post Perr0Grande  
                Started by elderan, Today, 08:03 PM
                0 responses
                9 views
                0 likes
                Last Post elderan
                by elderan
                 
                Started by algospoke, Today, 06:40 PM
                0 responses
                10 views
                0 likes
                Last Post algospoke  
                Started by maybeimnotrader, Today, 05:46 PM
                0 responses
                14 views
                0 likes
                Last Post maybeimnotrader  
                Working...
                X