• 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

You are accessing an index with a value that is invalid since it is out-of-range.

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

    You are accessing an index with a value that is invalid since it is out-of-range.

    Hi all,

    Have a peculiar problem where the error message " Error on calling 'OnBarUpdate' method on bar 291: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart." keeps appearing despite the code looking fine.

    Here's the State.Configure portion of the code:

    Code:
    else if (State == State.Configure)
                {
                    AddDataSeries(Data.BarsPeriodType.Minute, 10);
                    AddDataSeries(Data.BarsPeriodType.Minute, 15);
                    AddDataSeries(Data.BarsPeriodType.Minute, 30);
                    AddDataSeries(Data.BarsPeriodType.Minute, 60);
    
                    M10Highs = Highs[1];
                    M10Lows = Lows[1];
    
                    M15Highs = Highs[2];
                    M15Lows = Lows[2];
    
                    M30Highs = Highs[3];
                    M30Lows = Lows[3];
    
                    H1Highs = Highs[4];
                    H1Lows = Lows[4];
                }
    The extra timeframes could added to the BarsArray without issues.


    Here's the OnBarUpdate portion of the code:

    Code:
    protected override void OnBarUpdate()
            {
                Print("On bar update method called");
    
                if((CurrentBars[1] < NumberOfCandles) || (CurrentBars[2] < NumberOfCandles) || (CurrentBars[3] < NumberOfCandles) || (CurrentBars[4] < NumberOfCandles))
                {
                    Print("Insufficient candles");
    
                    return;
                }
    
                else
                {    
                    M10ValuesList = IndicatorToolBox.GetValues(M10Highs, M10Lows, NumberOfCandles, NumberOfValues); Print("M10 Values found");
                    M15ValuesList = IndicatorToolBox.GetValues(M15Highs, M15Lows, NumberOfCandles, NumberOfValues); Print("M15 Values found");
                    M30ValuesList = IndicatorToolBox.GetValues(M30Highs, M30Lows, NumberOfCandles, NumberOfValues); Print("M30 Values found");
                    H1ValuesList = IndicatorToolBox.GetValues(H1Highs, H1Lows, NumberOfCandles, NumberOfValues); Print("H1 Values found");
    
                    RemoveDrawObjects();
    
                    for(int i = 0; i < M10ValuesList.Count(); i++)
                    {
                        if(M10ValuesList[i] > M10Highs[0])
                        {
                            Draw.HorizontalLine(this, ("M10 Values " + i), M10ValuesList[i], Brushes.Green);
                        }
    
                        else if(M10ValuesList[i] < M10Lows[0])
                        {
                            Draw.HorizontalLine(this, ("M10 Values " + i), M10ValuesList[i], Brushes.Green);
                        }
                    }
    
                        for(int i = 0; i < M15ValuesList.Count(); i++)
                        {
                            if(M15ValuesList[i] > M15Highs[0])
                            {
                                Draw.HorizontalLine(this, ("M15 Values " + i), M15ValuesList[i], Brushes.Yellow);
                            }
    
                            else if(M15ValuesList[i] < M15Lows[0])
                            {
                                Draw.HorizontalLine(this, ("M15 Values" + i), M15ValuesList[i], Brushes.Yellow);
                            }
                        }
    
                        for(int i = 0; i < M30ValuesList.Count(); i++)
                        {
                            if(M30ValuesList[i] > M30Highs[0])
                            {
                                Draw.HorizontalLine(this, ("M30 Values " + i), M30ValuesList[i], Brushes.Orange);
                            }
    
                            else if(M30ValuesList[i] < M30Lows[0])
                            {
                                Draw.HorizontalLine(this, ("M30 Values " + i), M30ValuesList[i], Brushes.Orange);
                            }
                        }
                        /*
                        for(int i = 0; i < H1ValuesList.Count(); i++)
                        {
                            if(H1ValuesList[i] > H1Highs[0])
                            {
                                Draw.HorizontalLine(this, ("H1 Values " + i), H1ValuesList[i], Brushes.Red);
                            }
    
                            else if(M10ValuesList[i] < H1Lows[0])
                            {
                                Draw.HorizontalLine(this, ("H1 Values " + i), H1ValuesList[i], Brushes.Red);
                            }
                        }*/
                    }
                }
            }
    Code runs fine up to the M15ValuesList but upon trying to get the M30ValuesList, I get the above mentioned error and I can't seem to find anything wrong with my code. Anyone have any clue?

    Thanks and Regards,
    Somebody

    #2
    Hello MrSomebody,

    I recommend debugging your code and finding the exact index that is causing the error and then check the size of that collection to understand the cause.
    https://ninjatrader.com/support/foru...13#post1048513

    Use prints to find the line of code triggering the error.
    https://ninjatrader.com/support/foru...121#post791121

    Print the BarsInProgress and CurrentBars[BarsInProgress] along with the time of the bar.


    As a heads up, H1ValuesList.Count() is always going to be larger than CurrentBar. Most NinjaScript developers loop from 0 bars ago to the value of CurrentBar.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      From the debugging, I get the message "Error on calling 'OnBarUpdate' method on bar 291: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart." when my custom function attempts to add the value to the local list in my custom function. Here's the code for the custom function:

      Code:
              public static IList<double> GetUpperValues(ISeries<double> HighPrices, int NumberOfCandles, int NumberOfValues)
              {    
                  int Counter = 0;
                  double[] SlidingWindow = new double[3];
      
                  IList<double> HighMaximaValues = new List<double>();
      
                  for(int i = 0; i < NumberOfCandles; i++)
                  {
                      SlidingWindow[0] = HighPrices[i];
                      SlidingWindow[1] = HighPrices[i+1];
                      SlidingWindow[2] = HighPrices[i+2];
      
                      if(Counter == 0)
                      {
                          if((SlidingWindow[1] > SlidingWindow[0]) && (SlidingWindow[1] > SlidingWindow[2]) && (SlidingWindow[1] >= HighPrices[0]))
                          {    
                              HighMaximaValues.Add(SlidingWindow[1]); <Error line here
                              Counter ++;
                          }
      
                          else
                          {    
                              ;
                          }
                      }
      
                      else if((Counter > 0) && (Counter < NumberOfValues))
                      {
                          if((SlidingWindow[1] > SlidingWindow[0]) && (SlidingWindow[1] > SlidingWindow[2]) && (SlidingWindow[1] > HighMaximaValues[Counter-1]))
                          {                
                              HighMaximaValues.Add(SlidingWindow[1]);
                              Counter ++;
                          }
      
                          else
                          {
                              ;
                          }
                      }
      
                      else
                      {
                          break;
                      }
                  }
      
                  return HighMaximaValues;
              }
      I've checked the number of highs in the M30Highs and there are sufficient values for this function to run. I think it's very strange that this only happens on the M30ValuesList.
      Last edited by MrSomebody; 03-31-2020, 09:41 AM.

      Comment


        #4
        Hello MrSomebody,

        You have provided a block code and not the index that is causing the error.

        What is the specific index causing the error?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          My apologies,

          The line causing the error is this line.

          Code:
          SlidingWindow[2] = HighPrices[i+2];
          But it's still strange because when I checked, there were 552 values in the M30Highs series. Even if there were really insufficient values, how does one go about increasing the number of values available?

          Comment


            #6
            Hello MrSomebody,

            What kind of object is HighPrices?
            Is this a series or an array?

            What is the value of CurrentBars[0]?

            What is the value of i+2?

            Is this method called from OnBarUpdate()?
            If not, is this method called from TriggerCustomEvent() in a non-data-driven method?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea,

              It was an amateur mistake an I managed to fix it. Realized that the error was happening because the i value of the for only loop goes up to 95 but I only have 96 candles which would correspond to an index value of 95 max so it would not be possible to reach index 97 with i+2.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by ahm1992, Today, 04:56 PM
              4 responses
              26 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by firstlanetech, Today, 11:24 AM
              2 responses
              29 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by grayfrog, Today, 09:29 PM
              0 responses
              5 views
              0 likes
              Last Post grayfrog  
              Started by alcorr247, Today, 12:04 PM
              1 response
              13 views
              0 likes
              Last Post NinjaTrader_EricB  
              Started by HiddenPhilosopher, Today, 11:08 AM
              1 response
              8 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Working...
              X