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

Problem with CountIf

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

    Problem with CountIf

    Hi,

    I have a strategy that I'm migrating over from NT7 that works. I use the CountIf function 3 times in the code, when I take these instances out, I have no issues. Is there something wrong with the function call? I've included the code and the error response below.

    Thanks!

    int countCross3 = CountIf(() => CrossAbove(Close, EMA(44),1),55);
    int countCross4 = CountIf(() => CrossAbove(Close, EMA(44),1),55);
    CountIf(() => Close[0]-Low[0] >= .75, 8) >=4

    Strategy 'MomentumUpTrade': Error on calling 'OnBarUpdate' method on bar 50: condition
    Parameter name: 'CountIf' on bar 0 threw exception: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    #2
    Hello ganbare112,

    Thanks for your post.

    Based on the error message I recommend you use a current bar check before accessing the countif statements. Something like:

    if (CurrentBar < 55) return;

    Reference: http://ninjatrader.com/support/helpG...currentbar.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Ah yes, I had CurrentBar < 30 but that wasn't enough, thanks so much!

      Comment


        #4
        Hello Paul, I have a similar error with countif()
        Time Category Message
        23/03/2022 15:19:08 Default Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 11: 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.
        I tried increasing the CurrentBar check but that's not solving the error.

        I use countif() with bars series of current bars' Highs and previous bars' Highs (for example).

        I'm trying to get something like this

        High[0] - High[1] ranges in a list, then count the number for >= 13 Ticks with countif().


        Below is a simplified version of my script

        PHP Code:
        namespace NinjaTrader.NinjaScript.Indicators
        {
           public class 
        TestaIndicator
           
        {

              private 
        Series<doubleRangeTopRangeBottom;

              private List<
        doublelistOfSeriesValueslistOfSeriesValuesBarsBack;


            protected 
        override void OnStateChange()
            {
              if (
        State == State.SetDefaults)
              {
                
        BarsBack                10;
                
        TopIndex               0;
                
        BottomIndex             0;

              }
              else if (
        State == State.Configure)
              {
              }
              else if (
        State == State.DataLoaded)
              {
                
        RangeTopRangeBottom = new Series<double>(thisMaximumBarsLookBack.Infinite);

                
        listOfSeriesValues = new List<double>();
                
        listOfSeriesValuesBarsBack = new List<double>();
              }
            }


            protected 
        override void OnBarUpdate()
            {

               if (
        CurrentBar BarsBack) return;

               
        double var1 CountIf(() => High[TopIndex] - High[BottomIndex] >= 13*TickSizeBarsBack);

               
        RangeTopRangeBottom[TopIndex] = High[TopIndex] - High[BottomIndex]; 
        As you can see I have properties variables

        TopIndex and BottomIndex for the indexes, and BarsBack for the Countif() period.

        I think I can understand the reason for the error is that at bar 11 Countif() can't access bar 10 High value. Is that the correct reason?
        If it is I wonder why it cannot access it since there should be enough bars available.

        But then I'm wondering if the CurrentBar check is the cause as it's stating to discard the previous bars from bar 11 and not use their values for calculations.
        if that's the cause, I can't understand/guess how to adjust the CurrentBar check for it to take into account the previous Bar's values.
        If I increase the number of Bars for the CurrentBar check that doesn't solve the problem.

        PHP Code:
                    if (CurrentBar BarsBack+1) return; 
        Time Category Message
        23/03/2022 15:19:08 Default Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 11: 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.
        Same if I decrease it
        PHP Code:
                    if (CurrentBar BarsBack-1) return; 
        Time Category Message
        23/03/2022 15:19:08 Default Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 9: 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.
        What would be a start for the solution to this error? Thanks!

        Comment


          #5
          Hello PaulMohn,

          Thank you for your reply.

          I've plugged the code you provided into an indicator script and run it and do not receive an error. Can you confirm you receive an error running just the code provided in your post?

          Thanks in advance; I look forward to assisting you further.
          Kate W.NinjaTrader Customer Service

          Comment


            #6
            Hello Kate and thanks for the reply. I've isolated further the error line

            it occurred on this line lower in the script

            PHP Code:
            listOfSeriesValuesBarsBack listOfSeriesValues.GetRange(((listOfSeriesValues.Count() - 1) - BarsBack), (BarsBack 1)); 

            I then noticed the line needed correction as
            PHP Code:
            listOfSeriesValuesBarsBack listOfSeriesValues.GetRange((listOfSeriesValues.Count() - (BarsBack 1)), (BarsBack)); 

            I then tested with
            PHP Code:
            if (CurrentBar <= (BarsBack*2)) return; 

            and it removed the error.

            Then i tested successively with +1 to + 8 and the error remained until + 9
            PHP Code:
            if (CurrentBar <= (BarsBack+1)) return; 
            PHP Code:
            if (CurrentBar <= (BarsBack+2)) return; 
            PHP Code:
            if (CurrentBar <= (BarsBack+3)) return; 
            PHP Code:
            if (CurrentBar <= (BarsBack+4)) return; 
            PHP Code:
            if (CurrentBar <= (BarsBack+5)) return; 
            PHP Code:
            if (CurrentBar <= (BarsBack+6)) return; 
            PHP Code:
            if (CurrentBar <= (BarsBack+7)) return; 
            PHP Code:
            if (CurrentBar <= (BarsBack+8)) return; 


            The error stopped at
            PHP Code:
            if (CurrentBar <= BarsBack+9) return; 

            +9 or *2 works
            PHP Code:
            if (CurrentBar <= (BarsBack+9)) return;

            listOfSeriesValuesBarsBack listOfSeriesValues.GetRange((listOfSeriesValues.Count() - (BarsBack 1)), (BarsBack)); 
            PHP Code:
            if (CurrentBar <= (BarsBack*2)) return;

            listOfSeriesValuesBarsBack listOfSeriesValues.GetRange((listOfSeriesValues.Count() - (BarsBack 1)), (BarsBack)); 


            So the solution seems to be
            if (CurrentBar <= BarsBack + (BarsBack-1)) return;

            The new simplified working snippet
            PHP Code:
            namespace NinjaTrader.NinjaScript.Indicators
            {
               public class 
            TestaIndicator
               
            {

                  private 
            Series<doubleRangeTopRangeBottom;

                  private List<
            doublelistOfSeriesValueslis tOfSeriesValuesBarsBack;


                protected 
            override void OnStateChange()
                {
                  if (
            State == State.SetDefaults)
                  {
                    
            BarsBack                10;
                    
            TopIndex               0;
                    
            BottomIndex             0;

                  }
                  else if (
            State == State.Configure)
                  {
                  }
                  else if (
            State == State.DataLoaded)
                  {
                    
            RangeTopRangeBottom = new Series<double>(t hisMaximumBarsLookBack.Infinite);

                    
            listOfSeriesValues = new List<double>();
                    
            listOfSeriesValuesBarsBack = new List<doub le>();
                  }
                }


                protected 
            override void OnBarUpdate()
                {

                   if (
            CurrentBar BarsBack) return;

                   
            double var1 CountIf(() => High[TopIndex] - High[BottomIndex] >= 13*TickSizeBarsBack);

                   
            RangeTopRangeBottom[TopIndex] = High[TopIndex] - High[BottomIndex];


                if (
            CurrentBar <= BarsBack + (BarsBack-1)) return;

                
            listOfSeriesValuesBarsBack listOfSeriesValues.GetRange((listOfSeriesValues.Count() - (BarsBack 1)), (BarsBack)); 

            I'm not sure i understand why it required a BarsBack + (BarsBack-1) Currentbar Check. Do you know why it does? Thanks!
            Last edited by PaulMohn; 03-23-2022, 11:08 AM.

            Comment


              #7
              Hello PaulMohn,

              Thank you for your reply.

              In my testing with just the above code I still get an error using if (CurrentBar <= BarsBack + (BarsBack-1)) return;

              I suspect this is related to not having assigned any values to listOfSeriesValues at this point in the script and I note that further code has been omitted, so I would not be able to say for sure.

              Please let us know if we may be of further assistance to you.

              Kate W.NinjaTrader Customer Service

              Comment


                #8
                I checked with more scenarii and it returned errors so I increased to

                PHP Code:
                if (CurrentBar <= (BarsBack + (BarsBack*3))) return;


                listOfSeriesValuesBarsBack listOfSeriesValues.GetRange((listOfSeriesValues.Count() - (BarsBack 1)), (BarsBack)); 

                and it seems to work for all cases now. I'll try to ajdust some more and be back asa. Thnaks!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by nandhumca, Today, 03:41 PM
                0 responses
                4 views
                0 likes
                Last Post nandhumca  
                Started by The_Sec, Today, 03:37 PM
                0 responses
                3 views
                0 likes
                Last Post The_Sec
                by The_Sec
                 
                Started by GwFutures1988, Today, 02:48 PM
                1 response
                5 views
                0 likes
                Last Post NinjaTrader_Clayton  
                Started by ScottWalsh, 04-16-2024, 04:29 PM
                6 responses
                33 views
                0 likes
                Last Post ScottWalsh  
                Started by frankthearm, Today, 09:08 AM
                10 responses
                36 views
                0 likes
                Last Post frankthearm  
                Working...
                X