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

Indicator bars error "index was out of bounds of the array"

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

    Indicator bars error "index was out of bounds of the array"

    Hi, I'm trying to run a custom indicator from market analyzer to generate alerts. I have the common error of the index being out of bounds of the array. I thought i had headed this off with my CurrentBar check at the beginning of on bar update. It's a multi- instrument, if that impacts things. There are three variables, a slow, med and fast value for moving averages. I'm passing 50, 20, 10. So my code as I see it should need 60 bars... I'm looking back 70 in the strategy analyzer. Can anyone point out the obvious? Many thanks in advance, the code is below...

    Code:
    protected override void OnBarUpdate()
            {
                if(CurrentBar <= SlowMA+12){return;}
                else
                {
                    if(BarsInProgress==0)
                    {
                        //Check for uptrend
                        int uptrendCheckCount = 0;
                        int countOfClosesOverSlowEma = 0;
                        double keyPriceViolationLevelPriorDay = EMA(Closes[1],slowMA)[0]*1.0025;
                        double keyPriceViolationLevelToday = EMA(Closes[1],slowMA)[0] + Math.Abs(EMA(Closes[1],slowMA)[0] - EMA(Closes[1],slowMA)[1]);
                        
                        for(int i=0;i<10;i++)
                        {
                            if((EMA(Closes[1],fastMA)[i] >= EMA(Closes[1],medMA)[i]) && (EMA(Closes[1],medMA)[i] >= EMA(Closes[1],slowMA)[i]))
                            {
                                uptrendCheckCount++;
                            }
                            
                            if(Closes[1][i+1] >= EMA(Closes[1],slowMA)[i+1])
                            {
                                countOfClosesOverSlowEma++;
                            }
                        }
                        
                        if((countOfClosesOverSlowEma==10)&&(uptrendCheckCount==10))
                        {
                            if(Lows[1][0] <= keyPriceViolationLevelPriorDay)
                            {
                                if((Close[0] > Open[0])&&(Close[0] > keyPriceViolationLevelToday))
                                {
                                    Plot0.Set(1);
                                }
                            }
                        }
                    }
                }
            }

    #2
    Hello CSharpTrader,
    Thanks for your post.

    You have to check the secondary bars series too. A sample code will be like

    Code:
    if(CurrentBars[0] <= SlowMA+12 || CurrentBars[0] < SlowMa + 12){return;}
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Hi Joydeep, thanks.. I changed the line to:

      if(CurrentBars[0] < (slowMA + 12) || CurrentBars[1] < (slowMA + 12)){return;}

      and I'm still getting the errors when I reload the market analyzer.

      Comment


        #4
        Originally posted by CSharpTrader View Post
        Hi, I'm trying to run a custom indicator from market analyzer to generate alerts. I have the common error of the index being out of bounds of the array. I thought i had headed this off with my CurrentBar check at the beginning of on bar update. It's a multi- instrument, if that impacts things. There are three variables, a slow, med and fast value for moving averages. I'm passing 50, 20, 10. So my code as I see it should need 60 bars... I'm looking back 70 in the strategy analyzer. Can anyone point out the obvious? Many thanks in advance, the code is below...

        Code:
        protected override void OnBarUpdate()
                {
                    if(CurrentBar <= SlowMA+12){return;}
                    else
                    {
                        if(BarsInProgress==0)
                        {
                            //Check for uptrend
                            int uptrendCheckCount = 0;
                            int countOfClosesOverSlowEma = 0;
                            double keyPriceViolationLevelPriorDay = EMA(Closes[1],slowMA)[0]*1.0025;
                            double keyPriceViolationLevelToday = EMA(Closes[1],slowMA)[0] + Math.Abs(EMA(Closes[1],slowMA)[0] - EMA(Closes[1],slowMA)[1]);
         
                            for(int i=0;i<10;i++)
                            {
                                if((EMA(Closes[1],fastMA)[i] >= EMA(Closes[1],medMA)[i]) && (EMA(Closes[1],medMA)[i] >= EMA(Closes[1],slowMA)[i]))
                                {
                                    uptrendCheckCount++;
                                }
         
                                if(Closes[1][i+1] >= EMA(Closes[1],slowMA)[i+1])
                                {
                                    countOfClosesOverSlowEma++;
                                }
                            }
         
                            if((countOfClosesOverSlowEma==10)&&(uptrendCheckCount==10))
                            {
                                if(Lows[1][0] <= keyPriceViolationLevelPriorDay)
                                {
                                    if((Close[0] > Open[0])&&(Close[0] > keyPriceViolationLevelToday))
                                    {
                                        Plot0.Set(1);
                                    }
                                }
                            }
                        }
                    }
                }
        You need to check all your BarSeries for validity.

        Code:
        private int _intEscapeBars = 1;
        Code:
                protected override void OnBarUpdate()
                {
                    for (int index = 0; index < BarsArray.Length; index++) 
                    {
                        if (CurrentBars[index] < this._intEscapeBars) return;
                    }
                    // the rest goes here ... 
                }
        When written this way, you do not have to even think about how many Bars objects you add to the indicator: the escapes will be automatically handled, dynamically, for the correct number of Bars objects.

        Comment


          #5
          I appreciate the help. I'll try implementing that. If you could help me understand it though... I know the loop checks any data series/ bars array. But it's checking that CurrentBars < 1... don't I need CurrentBars < barsRequiredForAllLogic? (in my case, 60).

          Comment


            #6
            Hello CSharpTrader,
            You need to check the CurrentBars logic in your code.
            You are only checking it for the primary bar and not for all the bar series.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Hi Joydeep, I don't follow your latest response..

              Comment


                #8
                Originally posted by CSharpTrader View Post
                I appreciate the help. I'll try implementing that. If you could help me understand it though... I know the loop checks any data series/ bars array. But it's checking that CurrentBars < 1... don't I need CurrentBars < barsRequiredForAllLogic? (in my case, 60).
                Sort of. The code as written merely validates that the barSeries exist. If you need a further filter, you can always use another filter to your code proper: the part that goes "// the rest goes here ... ".

                Alternatively, you can think of the "1" as just a place holder, and replace that with however many bars that you want to check. However, best practice is to check the validity of the barSeries independently of any escapes that you want to impose on the code proper. That avoids any side effects: your code processing should be independent of if the bars are valid, which should be an independent gate on the code..
                Last edited by koganam; 11-27-2012, 01:52 PM. Reason: Corrected spelling.

                Comment


                  #9
                  Hello CSharpTrader,
                  If I try the below code then I can get it work fine

                  Code:
                  if(CurrentBars[0] <= slowMA+12 || CurrentBars[1] < slowMA + 12){return;}
                              else
                              {
                                  if(BarsInProgress==0)
                                  {
                                      //Check for uptrend
                                      int uptrendCheckCount = 0;
                                      int countOfClosesOverSlowEma = 0;
                                      double keyPriceViolationLevelPriorDay = EMA(Closes[1],slowMA)[0]*1.0025;
                                      double keyPriceViolationLevelToday = EMA(Closes[1],slowMA)[0] + Math.Abs(EMA(Closes[1],slowMA)[0] - EMA(Closes[1],slowMA)[1]);
                                      
                                      for(int i=0;i<10;i++)
                                      {
                                          if((EMA(Closes[1],fastMA)[i] >= EMA(Closes[1],medMA)[i]) && (EMA(Closes[1],medMA)[i] >= EMA(Closes[1],slowMA)[i]))
                                          {
                                              uptrendCheckCount++;
                                          }
                                          
                                          if(Closes[1][i+1] >= EMA(Closes[1],slowMA)[i+1])
                                          {
                                              countOfClosesOverSlowEma++;
                                          }
                                      }
                                      
                                      if((countOfClosesOverSlowEma==10)&&(uptrendCheckCount==10))
                                      {
                                          if(Lows[1][0] <= keyPriceViolationLevelPriorDay)
                                          {
                                              if((Close[0] > Open[0])&&(Close[0] > keyPriceViolationLevelToday))
                                              {
                                                  Plot0.Set(1);
                                              }
                                          }
                                      }
                                  }
                              }
                  JoydeepNinjaTrader Customer Service

                  Comment


                    #10
                    So it's not checking for required bars, just the existence. Got it. I'm all for best practices, but for now to get it running, I'm using:

                    for (int index = 0; index < BarsArray.Length; index++)
                    {
                    if (CurrentBars[index] < (slowMA+12)) return;
                    }

                    And I'm still getting errors.

                    Comment


                      #11
                      Hello CSharpTrader,
                      To assist you further may I know what code you are using in the Initialize section of the code.
                      JoydeepNinjaTrader Customer Service

                      Comment


                        #12
                        Joydeep, thanks... how many bars are you using in market analyzer? And you're getting no out of bounds errors at all?

                        Comment


                          #13
                          Sure... I never deleted this default code, still the same original method:

                          protected override void Initialize()
                          {
                          Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                          Overlay = false;
                          }

                          Comment


                            #14
                            Originally posted by CSharpTrader View Post
                            So it's not checking for required bars, just the existence. Got it. I'm all for best practices, but for now to get it running, I'm using:

                            for (int index = 0; index < BarsArray.Length; index++)
                            {
                            if (CurrentBars[index] < (slowMA+12)) return;
                            }

                            And I'm still getting errors.
                            Are you still getting the "index out of bounds" error?

                            Comment


                              #15
                              Hi Koganam, I am... it's logically the same as Joydeeps' earlier suggestion, that just checked them both individually with the ||.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bortz, 11-06-2023, 08:04 AM
                              47 responses
                              1,609 views
                              0 likes
                              Last Post aligator  
                              Started by jaybedreamin, Today, 05:56 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              19 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              16 views
                              0 likes
                              Last Post Javierw.ok  
                              Working...
                              X