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 calling 'OnBarUpdate' method at Bar 1

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

    Error calling 'OnBarUpdate' method at Bar 1

    Hello,

    I get the following error message when I try to test a strategy that has been created: "Error calling 'OnBarUpdate' method at Bar 1: You are accessing an index with an invalid value that is outside the expected range. For example, Access a row with [BarsAgo] with a value of 5 if there are only 4 candles in the chart."

    Does anyone have an idea / solution?

    Thanks
    Martin

    #2
    Hello martinb089,

    This error generally occurs when you use a BarsAgo before the data is present. For example on bar 1 if you used 2 BarsAgo, that data is not yet present so it would have this error.

    As a first step lets make sure this is not a BarsAgo problem, take a look in your code and look for the highest BarsAgo you have used for example: Close[5].

    Once you find the highest number of BarsAgo used, try adding the following code to OnBarUpdate:

    Code:
    protected override void OnBarUpdate()
    {
        if(CurrentBar < 5) return;
    This checks that the current processing bar is greater or equal to the maximum BarsAgo before starting.

    If that is not the problem or if you need further assistance with this change can you attach the script so we can see what has been coded so far? You can find the file in the folder: Documents\NinjaTrader 8\bin\Custom\Strategies

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello,

      thanks for your quick reply.

      Is not a BarsAgo problem. But I use AddDataSeries (Data.BarsPeriodType.Day, 1);

      If I don't call them, the error doesn't appear either.

      My calls are: "Closes [1] [0]" or "Bollinger3.Middle [0]".

      Both refer to the additional data series.

      Does this help you to make a further assessment?

      Thanks
      Martin

      Comment


        #4
        Hello martinb089,

        This still sounds like a BarsAgo problem. If you used multiple series you will need to expand the data check at the start of OnBarUpdate:

        Code:
         if(CurrentBars[0] < 1 || CurrentBars[1] < 1) return;
        Depending on where you are accessing Closes[1][0] it may not have data yet. Your series will process with a BarsInProgress identifier, if you executed Closes[1][0] from BarsInProgress 0 before the secondary series had processed 1 bar you would get this error.

        You may also be running into the same problem using the Bollinger if that is using secondary data. Did you manually code this script or use the strategy builder to generate the code?
        If you did not use the strategy builder I would suggest using that to generate multi series scripts initially. This will include the data requirement conditions to prevent errors like this and also will place the indicator assignments correctly. If you generate code with the builder keep in mind it adds a BarsInProgress check to make only the primary series process, if you plan on using the secondary series for processing you need to remove the condition if (BarsInProgress != 0) return;

        You can read more on these concepts in the following page: https://ninjatrader.com/support/help...lightsub=multi

        If you are still having difficulty please upload your script so that I can see what you actually coded, that helps in understanding the problem when we can see where the code is within the file.


        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello Jesse,

          many thanks for your response.

          With the condition it worked now:
          if (CurrentBars [0] <0
          || CurrentBars [1] <0)

          Thank you very much
          Martin

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by GussJ, 03-04-2020, 03:11 PM
          16 responses
          3,279 views
          0 likes
          Last Post Leafcutter  
          Started by WHICKED, Today, 12:45 PM
          2 responses
          19 views
          0 likes
          Last Post WHICKED
          by WHICKED
           
          Started by Tim-c, Today, 02:10 PM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by Taddypole, Today, 02:47 PM
          0 responses
          5 views
          0 likes
          Last Post Taddypole  
          Started by chbruno, 04-24-2024, 04:10 PM
          4 responses
          51 views
          0 likes
          Last Post chbruno
          by chbruno
           
          Working...
          X