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

Index Outside of the bounds of the array at getBars()

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

    Index Outside of the bounds of the array at getBars()

    I have been getting error Index was outside the bounds of the array in OnCalculateMinMax() method. I have placed try/catch blocks around the different parts of my OnCalculateMinMax method and the error seems to be coming from this section:

    Code:
    public override void OnCalculateMinMax()
            {
                try{
                    if (Bars == null || ChartControl == null)return;
                }
    
                catch (Exception ex) {
                    string exMessage = FormatExceptionMessage(ex);
                    Print(exMessage + " CurrentBar: " + CurrentBar + "|ActiveBar: " + activeBar + "|idx: " + ChartBars.ToIndex + " [COLOR=#FF0000][B]MinMax2[/B][/COLOR]");
                    throw;
                }
    }
    Here is the exception:

    Code:
    <EXCEPTION: Index was outside the bounds of the array.>
       at NinjaTrader.NinjaScript.NinjaScriptBase.get_Bars()
       at NinjaTrader.NinjaScript.Indicators.SectorJunky.sjMarketCapBasket.OnCalculateMinMax()
     CurrentBar: 340|ActiveBar: 340|idx: 340 [COLOR=#FF0000][B]MinMax2[/B][/COLOR]
    Indicator 'sjMarketCapBasket': Error on calling 'CalculateMinMax' method on bar 340: Index was outside the bounds of the array.
    Funny thing is, it will run for hours just fine and then I get the exception. Any idea why this would be happening?

    #2
    Hello swcooke,

    From the given syntax I don't see what that may be, have you tried commenting out the objects until you find what specific object is throwing the error? From the error message my guess would be the Bars object but we would have to confirm that.

    Were you using try/catch already or did you add that after seeing this exception? Was it the same exception when not using the try/catch?

    As to why that would happen after some time I couldn't really say without more details. Generally errors from this override would relate to the data being used or with the logic being used so from the given example that does not give me much to go on. Is the script being used in any special way or is this an indicator manually applied to the chart? Are any other items running with it in the same panel/chart?

    I look forward to being of further assistance.


    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Here are some answers to your questions:

      Were you using try/catch already or did you add that after seeing this exception? Was it the same exception when not using the try/catch?
      I was not using try/catch blocks until I got this error by itself:
      Indicator 'sjMarketCapBasket': Error on calling 'CalculateMinMax' method on bar 340: Index was outside the bounds of the array.

      Then I added the Try/Catch blocks to try to get more information. Given that I can catch it, is there anyway to Print more info that I have?


      Is the script being used in any special way or is this an indicator manually applied to the chart? Are any other items running with it in the same panel/chart?
      It's just a standalone Indicator. It is not calling other indicators or being called. I have two charts open, one in each chart. I don't have two in the same chart. It errors randomly on each chart (never at the exact same time).

      Here is my whole OnCalculateMinMax() method. Notice what I just added in red. This actually testing true and printing to the log showing the ToIndex is 363 while CurrentBars[0] is 362. How can this be?
      Code:
                 public override void OnCalculateMinMax()
              {
                  base.OnCalculateMinMax();
                  if (Bars == null || ChartControl == null)return;
      
                  foreach (KeyValuePair<int, Basket> kvp in BasketsDict)
                  {                
                      if(kvp.Value.Show){
                          if(!BasketsDict[kvp.Key].Initialized) continue;
                          for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex; idx++){
      [COLOR=#FF0000][B]                       if(ChartBars.ToIndex > CurrentBars[0]){
                                  Print("Race Condition: " + CurrentBars[0].ToString() + " " + ChartBars.ToIndex.ToString());
                              } [/B] [/COLOR]                      
      
                              if(!BasketsDict[kvp.Key].SjSeries.IsValidDataPointAt(idx))continue;                        
                              double tmpHigh     =     kvp.Value.SjSeries.GetValueAt(idx).High;
                              double tmpLow     =     kvp.Value.SjSeries.GetValueAt(idx).Low;
      
                              if (tmpHigh != 0 && tmpHigh > MaxValue)
                                  MaxValue = tmpHigh;
                              if (tmpLow != 0 && tmpLow < MinValue)
                                  MinValue = tmpLow;                            
                          }
                      }
                  }            
              }
      The SjSeries is a Series of Objects called SjBars so each time OnBarUpdate advances to a new bar, I create a new class like this (in red):
      Code:
      protected override void OnBarUpdate()
      {            
          if(BarsInProgress > 0) return;
          if (CurrentBars < 0) return;
      
          if (CurrentBar > activeBar)
          {
              foreach (KeyValuePair<int, Basket> kvp in BasketsDict)
              {
      [B][COLOR=#FF0000]            BasketsDict[kvp.Key].SjSeries[0] = new SjBars();[/COLOR][/B]
                  if(isFirstBar){
                      BasketsDict[kvp.Key].SjSeries[0].Open = BasketsDict[kvp.Key].Value;
                      BasketsDict[kvp.Key].SjSeries[0].High = BasketsDict[kvp.Key].Value;
                      BasketsDict[kvp.Key].SjSeries[0].Low = BasketsDict[kvp.Key].Value;
                      BasketsDict[kvp.Key].SjSeries[0].Close = BasketsDict[kvp.Key].Value;
                      BasketsDict[kvp.Key].SjSeries[0].IsFirstTick = false;
                  }else{
                      if(Bars.IsFirstBarOfSession){
                          BasketsDict[kvp.Key].SetOpensToZero();
                      }else{
                          BasketsDict[kvp.Key].SjSeries[0].Open = BasketsDict[kvp.Key].SjSeries[1].Close;
                          BasketsDict[kvp.Key].SjSeries[0].High = BasketsDict[kvp.Key].SjSeries[1].Close;
                          BasketsDict[kvp.Key].SjSeries[0].Low = BasketsDict[kvp.Key].SjSeries[1].Close;
                          BasketsDict[kvp.Key].SjSeries[0].Close = BasketsDict[kvp.Key].SjSeries[1].Close;
                          BasketsDict[kvp.Key].SjSeries[0].IsFirstTick = false;                            
                      }    
                  }                    
              }
              activeBar = CurrentBar;
              if(isFirstBar){
                  isFirstBar = false;
              }
          }
      }
      Is there anything here that raises any red flags to you? Is it possible that the ChartBars.ToIndex advances by one but right before my OnBarUpdate() creates a new SjBars Object?

      Comment


        #4
        Hi Jesse,

        Just following up on this to see if my last reply shed any light on things.

        Comment


          #5
          Hello swcooke,

          Thank you for the additional details.

          From the given syntax nothing specifically sticks out other that you are using CurrentBar in the min max override. Otherwise the only observation I can make is that you are using quite a few items which could have an index so isolating the specific part of the code that causes the exception will still be important to know what object that is.

          Regarding the ChartBars.ToIndex and CurrentBar, Generally the CurrentBar wouldn't be used in OnCalculateMinMax. This override is not relevant to a specific CurrentBar in processing but is relevant to what is being viewed in the chart right now. The To and From indexes are used to gather data from what is in view now so you can correctly scale what is in view or omit something that should not be scaled in. The ToIndex will not match your CurrentBar as that relates to what you are looking at, you would see this become less than current bar if you scroll back in the chart.

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

          Comment


            #6
            Jesse,

            Given that I am able to catch this exception, is there any other data I can print out to offer more clues? Seems crazy that I can catch it and still not know what it is. You can see my catch block above.

            Comment


              #7
              Hello swcooke,

              Not really, adding additional try/catch will just do the same as what the platform is already doing however that may also allow execution beyond the problem if you are not breaking as well.

              From here it would be best focus on finding what specific syntax causes that exception by removing the other parts and finding it. Knowing more about the exception but not knowing what exact syntax causes it is less helpful than knowing what specific syntax is causing the error. If we know what object is throwing the error we can focus understanding if this relates to that object or something you are doing with an index. There are items which should not be used in OnCaclulateMinMax however from what you are showing the only item I can see would be CurrentBars object.

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

              Comment


                #8
                Thanks Jesse. I have one more general question. You can see above how my OnBarUpdate checks if CurrentBars[0] > activeBar and if it is, it creates the SjBars Object and adds it to the series. I am expecting that the code below will confirm (for sure) that the SjBars Object has been created. Will this code do this or should I check in some other way?

                Code:
                //abbreviated for clarity
                public override void OnCalculateMinMax()
                {
                       if(!SjSeries.IsValidDataPointAt(idx))return;
                }
                Last edited by swcooke; 03-17-2020, 02:54 PM.

                Comment


                  #9
                  Hello swcooke,

                  Right the IsValidDataPoint system is used for checking if a point was set or not. If you don't set a point to the plot at that index or if you had called Reset() for that index it will return false.

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

                  Comment


                    #10
                    Jesse,

                    I installed Visual Studio and ran in Debug and got the exact error that I opened this ticket with. Attached is a screenshot of the error. I am reviewing this forum post where the user seems to have the same issue and Chelsea directs him to using TriggerCustomEvent() however I don't understand where I should be using this in my OnCalculateMinMax(). The example shown is not used in the context of OnCalculateMinMax(). Frankly, I can't understand why this is being thrown. if(ChartBars == null) seems pretty darn basic. I also just realized that the if statement I am using is not used in the example on the OnCalculateMinMax help guide page. Is it possible I don't need to check if Bars == null? Or ChartsBars == null?
                    Attached Files

                    Comment


                      #11
                      Maybe you could check if BarsArray != null and/or BarsArray.Length > 0 instead, just guessing.

                      Comment


                        #12
                        Thanks MojoJojo. What would be the difference between what you are suggesting and what I am doing? I am also curious if I need to do any check at all. The OnCalculateMinMax() help guide example does not show a Bars check.

                        Comment


                          #13
                          Hello swcooke,

                          From the image it looks like it could be the Bars object, however you also have ChartControl on the same line so I would still suggest doing what I had suggested in post#2 or to find the exact object causing the exception. If that was the Bars object, are you using that later in the override? I don't see why you are checking if that is null in the sample. Did you mean ChartBars?

                          I would not suggest using TriggerCustomEvent for this, again you need to isolate the problem and find the specific object causing the error. If it was Bars, can you clarify why was that used and do you still see the error if you do not use Bars?

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

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by jpeep, 08-16-2020, 08:31 AM
                          17 responses
                          502 views
                          0 likes
                          Last Post notenufftime  
                          Started by ETFVoyageur, 05-07-2024, 07:05 PM
                          15 responses
                          124 views
                          0 likes
                          Last Post ETFVoyageur  
                          Started by esmall, Today, 07:14 PM
                          0 responses
                          9 views
                          0 likes
                          Last Post esmall
                          by esmall
                           
                          Started by Option Whisperer, 05-09-2024, 07:58 PM
                          6 responses
                          26 views
                          0 likes
                          Last Post Option Whisperer  
                          Started by rayyyu12, Today, 05:38 PM
                          0 responses
                          13 views
                          0 likes
                          Last Post rayyyu12  
                          Working...
                          X