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 "OnBarUpdate" because index value is out of range?

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

    Error "OnBarUpdate" because index value is out of range?

    Hello,

    My strategy uses two data series. The Primary is the 30 Minute and Secondary is the 1 Tick.

    Here is a snippet of the logic in question:

    --------------------------------------------------------------------------------

    public int barsinProgress0 = 0;
    public int barsinProgress1 = 1;
    public bool OkToTradeDay;

    OnBarUpdate()
    {

    //Checks if today is a CME observed holiday to prevent trading. The dates start from the beginning of 2021 to the end of 2022.
    if (ToDay(Times[barsinProgress1][0]) == 20210117 || // MLK Day
    ToDay(Times[barsinProgress1][0]) == 20210221 || //Presidents Day
    ....... etc.....)
    {
    OkToTradeDay = false;
    }

    else
    {
    OkToTradeDay = true;
    }

    if (OkToTradeDay == true)
    {
    //Perform rest of strategy logic
    }
    }

    --------------------------------------------------------------------------------

    When I run a backtest for the current year (2022), it works fine. When I run the same strategy for the prior year (2021) I receive the error: "Error on calling "OnBarUpdate" method on bar 0: You are accessing an index value that is invalid since it is out of range etc..."
    Then when I change the BarsInProgress index to 0 by using "barsinProgress0" to use the Primary Series (30 Min) to check for holidays, the error says an error is calling on bar -1.

    I placed prints throughout my script and I know the error is coming from this section where it checks for the holidays.

    Any insight as to why this error is appearing?

    Also; The strategy works fine on both years when tested on the ES, but only works fine in 2022 and not in 2021 when testing on the NQ which is what the snippet above is trading. Would this be an issue with downloading the correct data?
    Last edited by Don22Trader1; 11-11-2022, 10:09 AM.

    #2
    Hello Don22Trader1,

    That is because you are not checking if any bars are avaliable before the logic runs. You need to add a CurrentBar check to make sure both series have a bar of data.

    protected override void OnBarUpdate()
    {​
    if(CurrentBars[0] < 1 || CurrentBars[1] < 1) return;
    JesseNinjaTrader Customer Service

    Comment


      #3
      I may be wrong, but I believe if you go to tools>trading hours you can select the times of CME index futures that has all the holidays and things built in. worth a look. I dont have the platform in front of me or I'd check for you.

      Comment


        #4
        NinjaTrader_Jesse,

        I added this logic and it began to work. Thanks for that! However, any data before Nov 16, 2021 will not run for some reason. I am using strategy analyzer for my testing.



        RISKYBUSINEZZ,

        Thanks for that bit of info, I will be using this in the future!

        Comment


          #5
          Hello Don22Trader1,

          Can you open a chart and view the data for that time period? It sounds like the data for that bars type may not be available.
          JesseNinjaTrader Customer Service

          Comment


            #6
            NinjaTrader_Jesse,

            Yes, I opened a chart for this time period with a 30 minute data series and there is data.

            Comment


              #7
              Minute data is not tick data. Try creating with a non time based bar such as range or Renko that far back. You likely do not have that tick data in your system. Nt severs only have about 1 years worth.

              Comment


                #8
                Tasker-182,

                Yes you are correct on that, no data comes out when I switch to a tick data or renko bars.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by futtrader, 04-21-2024, 01:50 AM
                4 responses
                41 views
                0 likes
                Last Post futtrader  
                Started by Option Whisperer, Today, 09:55 AM
                1 response
                11 views
                0 likes
                Last Post bltdavid  
                Started by port119, Today, 02:43 PM
                0 responses
                3 views
                0 likes
                Last Post port119
                by port119
                 
                Started by Philippe56140, Today, 02:35 PM
                0 responses
                4 views
                0 likes
                Last Post Philippe56140  
                Started by 00nevest, Today, 02:27 PM
                0 responses
                2 views
                0 likes
                Last Post 00nevest  
                Working...
                X