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

Counting Occurences

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

    Counting Occurences

    Hi Guys, I'm struggling with an issue and was hoping for some pointers. I'd like to know how to implement a "count". I've studied countif and it isn't what I'm looking for. I have attached a chart below of the NYSE tick. I'd like to know the command line for example of how many times the close has exceeded the -600 threshold.

    Thanks
    DJ
    Attached Files

    #2
    Hello djkiwi,

    This sounds like CountIf()
    Counts the number of occurrences of the test condition over the look back period expressed in bars.

    The condition is Close[0] > -600.

    What results are you seeing with CountIf() and what are you expecting instead?
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Countif

      Thanks Ryan. Well I read about the countif and not sure how its get the value into a string or an output. For example if there are 20 occurrences for the day then it should put in 20.

      My understanding of the code below from the manual is if in the last 10 bars we have had 8 bar closes above 600 then do something. I'm trying to say just give the total number of closes above 600?

      Thanks
      DJ

      {
      if (CountIf(delegate {return Close[0] > 600;}, 10) > 8)

      {

      DrawTextFixed("tick",delegate {return Close[0] > 600;}, 10.ToString("N0"), blPosition,Color.White,textFontLarge, Color.Black, Color.Red, 10);

      }

      Comment


        #4
        If you want to check against the entire bars available, use CurrentBar as your lookback.

        Print(CountIf(delegate {return Close[0] > 600;}, CurrentBar + 1));
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          It might be clearer code to maintain if you returned a value, and then cast that to string, but it seems that you have to cast the entire CountIf() expression to string.

          Code:
          DrawTextFixed("tick",CountIf(delegate {return Close[0] > 600;}, 10).ToString("N0"), blPosition,Color.White,textFontLarge, Color.Black, Color.Red, 10);

          Comment


            #6
            Thanks guys, that worked perfectly. Special thanks to Koganam who provides valuable input to the forum and many of my issues.

            Cheers
            DJ

            Comment


              #7
              Counting occurrences with Bars Array

              Hi Guys, I have another curly one that has me stumped and would appreciate some pointers. I now have the countif formula which I'm trying to convert to multitimeseries using barsarray:

              protected override void Initialize()
              {
              Overlay = true;
              CalculateOnBarClose = false;
              Add("^TICK", PeriodType.Minute,1);
              }

              protected override void OnBarUpdate()

              double tickupcount = CountIf(delegate {return High[0] >highcount;}, 30);

              I have tried all sorts of iterations of this. This one got me closest but said method name expected.

              double tickupcount = CountIf(BarsArray[1](delegate {return High[0] >highcount;}, 30));

              I am not even sure I can use barsarray in this instance?

              Thanks
              DJ
              Last edited by djkiwi; 04-12-2011, 12:50 PM.

              Comment


                #8
                What is the intent of the code ?

                Comment


                  #9
                  Intent

                  Hi Koganam. I am trying to compare different market internals in the one place.
                  eg: if Tick is > than X and if Vix > than X and Trin is < X then give long signal....


                  protected override void Initialize()
                  {
                  Overlay = true;
                  CalculateOnBarClose = false;
                  Add("^TICK", PeriodType.Minute,1);
                  Add("^ADD", PeriodType.Minute,3);
                  Add("^TRIN", PeriodType.Minute,3);
                  Add("^VIX", PeriodType.Minute,3);
                  }

                  Let me know if you need anything more.

                  Cheers
                  DJ

                  Comment


                    #10
                    CountIf() does not work in multiseries contexts.

                    You would have to custom code this type of function for multiseries.

                    Example with a for loop, untested.
                    //variables region
                    private int OccurrenceTrue = 0;

                    for (int count = 0 ; count < 30; count++)
                    {
                    if (Highs[1][count] > highcount)
                    OccurrenceTrue += 1;
                    }
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      Counting Bars

                      Thanks Ryan. I'm going to try that out and see what I can come up with. Another issue with this is I'm trying to get it perform the countif from the start of the session. Your suggestion below was great for what I have on the screen:

                      "If you want to check against the entire bars available, use CurrentBar as your lookback.

                      Print(CountIf(delegate {return Close[0] > 600;}, CurrentBar + 1)); "

                      I'd like to find out the results just for the current session only. I did find this but there is no example on how to use it in the manual. Any idea how to get this working?

                      Bars.BarsSinceSession

                      Thanks
                      DJ

                      Comment


                        #12
                        Hi DJ,

                        Yes, that's the property you would want for session value. You can use it in place of any integer.

                        Probably the easiest way to code this is increase a counter when your condition is true, and reset this counter at session break. Both snippets provided are designed for COBC = true processing as you only want it evaluated on bar close.

                        //variables region
                        private int myCounter;

                        if (Highs[1][0] > highcount)
                        myCounter++; //adds 1 to counter each time condition is true.


                        if(Bars.FirstBarOfSession) //resets your counter at session break.
                        myCounter = 0;
                        Ryan M.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by djkiwi View Post
                          Hi Koganam. I am trying to compare different market internals in the one place.
                          eg: if Tick is > than X and if Vix > than X and Trin is < X then give long signal....


                          protected override void Initialize()
                          {
                          Overlay = true;
                          CalculateOnBarClose = false;
                          Add("^TICK", PeriodType.Minute,1);
                          Add("^ADD", PeriodType.Minute,3);
                          Add("^TRIN", PeriodType.Minute,3);
                          Add("^VIX", PeriodType.Minute,3);
                          }

                          Let me know if you need anything more.

                          Cheers
                          DJ
                          So then where does the CountIf() come in? I do not need to or even want to see your entire code, but sometimes, it is a bit hard to provide a snippet while in the dark.

                          Comment


                            #14
                            Counting Occurrences

                            Hi Ryan, thanks for the fast response. It's great how a newbie like me can battle on with this stuff and then find the answers to the issues so quickly so progress goes on unhindered. This is a critical part of my system so am being careful with it.

                            Frankly I'm not sure what that you wrote does but gave me some other ideas and came up with:

                            double tickupcount = CountIf(delegate {return High[0] >highcount;}, Bars.BarsSinceSession);
                            double tickdowncount = CountIf(delegate {return Low[0] <lowcount*-1;}, Bars.BarsSinceSession);

                            If you look at the attached 3 min TICK chart using the retail session, this counts the number of occurrences above an 800 tick (top right 5) and below 800 of 13 (bottom right).

                            This works with the retail and extended session (although the retail session has closed). Now if I put in:

                            Bars.BarsSinceSession -1 instead of just Bars.BarsSinceSession then the numbers disappear making the formula invalid. Possibly because the session has closed. So the question is what does the -1 one do? You had -1 in your currentbar -1 formula above?

                            Thanks
                            DJ
                            Attached Files

                            Comment


                              #15
                              Great you're getting some results from it.

                              Actually the CurrentBar example added 1, and this is so you are not specifying a 0 value for a lookback parameter. Lookbacks typically start at 1, and CurrentBar and Bars.BarsSinceSession start at 0.
                              Ryan M.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Aviram Y, 08-09-2023, 09:04 AM
                              10 responses
                              298 views
                              0 likes
                              Last Post MrHump
                              by MrHump
                               
                              Started by jpapa, Today, 07:22 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by kevinenergy, 02-17-2023, 12:42 PM
                              116 responses
                              2,758 views
                              1 like
                              Last Post kevinenergy  
                              Started by franatas, 12-04-2023, 03:43 AM
                              7 responses
                              106 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by Jltarrau, Today, 05:57 AM
                              3 responses
                              9 views
                              0 likes
                              Last Post Jltarrau  
                              Working...
                              X