Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CountIf and DataSeries Creation for Dummies

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

    CountIf and DataSeries Creation for Dummies

    I've used the Wizard for a few strategies but need to get into hard coding to obtain the best results and am a newbie at coding. After using the Help Files and Forum Examples I still can't make something simple like . . .

    If BOP(14) is below zero for at least 11 of the past 13 bars
    &&
    If SMA(14) crosses below SMA(50)
    then Enter Short


    Seems like we can't really use the BOP indicator within a CountIf statement because it produces compile errors. So we have to make a DataSeries before we can use BOP in the statement.

    1] Declare a DataSeries for BOP by doing something like this . . .
    BPower.set = BOP(14)

    2] Then use the created DataSeries (BPower) in the statement like this . . .
    if (CountIf(delegate {return BPower[0] < 0;}, 13) => 11

    3] The next step is to add another "if" statement to look for the SMA cross over. That I can do.

    4] Finally, use the command to "Enter Short", which I can also do.

    Not sure if my problems are syntax related or if I'm just approaching this from the wrong angle. But from all I've read it seems like I'm heading down the right path, just can't get what I need.

    #2
    Hi hondo69, can you try setting BPower with BPower.Set(BOP(14)) and see if that works?
    AustinNinjaTrader Customer Service

    Comment


      #3
      Thanks for the tip Austin, but I think I'm still missing some code as it still does not recognize "BPower" during compile. The help file discusses setting a Dataseries and talks about setting it as a "Class" or a "Variable". This might be what is tripping me up.

      When I read posts in the forum related to "CountIf" there are some good examples, but they all lack code placed above the "CountIf" that initializes the Dataseries.

      For example, when using "Close[0]" in the CountIf statement you don't need to initialize anything. But if using an indicator (SMA, MACD, BOP, etc.) then you need to initialize the Dataseries somehow. I think that's what I'm missing.

      Comment


        #4
        hondo69, per default the system indicators return a data series, thus including indicator historical values. If you just want to access a particular one and compare to a value, you can do so by working with the proper index (BOP(14)[5], BOP value 5 bars ago). It would be helpful if you post the full code you use now so we can take a look, thanks

        Comment


          #5
          Thanks for the info Bertand, you might have explained part of my problem.

          I was using BOP for my test code since it is simple to monitor on a chart. After I butchered the code beyond 8 error messages I just deleted it so I could start fresh. My problem is getting a handle on the CountIf statement and the proper syntax it requires. I feel it changes depending upon what you are counting.

          For example, if you are counting Close[0], no special dataseries is required.

          But if you are counting BOP(14)[0], then it does require a dataseries and this is where the confusion comes in. Your note states a dataseries is automatically created with that indicator. If so, then this should be a valid statement . . .

          Code:
          if ([COLOR=Blue]CountIf[/COLOR](delegate {return BOP(14)[0] < 0;}, 10) >  8)
          No special dataseries need be created since it automatically exists. But it does generate error messages when compiled.

          - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

          So my question is really more of a Big Picture question. Sometimes I might want to count Open or Close. Other times I might want to count indicator values of SMA, MACD, or BOP within a series. How, when and why do I "set" my own custom dataseries on some occasions but not on others?

          Comment


            #6
            You're on the right track for what you seek to do, the issue, at least I believe so, was you were comparing a double to int value in the CountIf statement, thus getting the errors - see if this works for you -

            Code:
             
            protected override void OnBarUpdate()
            {
            if (CountIf(delegate {return BOP(Close, 10)[0] > 0.0 ;}, 10) > 8) 
            
            Plot0.Set(High[0] + 2 * TickSize);
            }
            this would for example mark overbought regions where BOP is higher than 0 for 8 out of the last 10 bars.

            Comment


              #7
              Yes, Bertrand, that is exactly what I'm looking for.

              The "Plot0" statement generated an error, but I just swapped that with a "DrawUpArrow" statement from another script I made and it worked out just fine.

              So it all makes perfect sense now because the error msgs I rec'd earlier lead me to believe it was the problem of comparing a "double" to "int" value. Thanks a million, I now have something I can wrap my head around and do some real experimenting.

              Comment


                #8
                Great, thanks for reporting back the Plot0 may have been an issue as no plot was defined in your indicator Initialize() - http://www.ninjatrader-support.com/H...eV6/Plots.html

                Comment


                  #9
                  Wow, easy.

                  I've always been hesitant in the past to mess around with the code, but I'm finding it's not as bad as I've feared. For me, the trick is to either create a new indicator or simply edit an existing one saved under a new name. That way I can just delete anything I don't like (which means I butcher the code).

                  Between the Help files and examples on the forum it gives me a fighting chance of making what I really want. I can't thank you enough for your help.

                  Comment


                    #10
                    Great, thanks for the kind words - correct right clicking into the existing indicator and the select 'Save as' (under a new name) is the proper way to go to experiment coding.

                    Comment


                      #11
                      I've run into another variation using CountIf that has me stumped.

                      Code:
                      protected override void OnBarUpdate()
                      {
                      if (CountIf(delegate {return BOP(Close, 10)[0] > 0.0 ;}, 10) > 8) 
                      
                      Plot0.Set(High[0] + 2 * TickSize);
                      }
                      The above example you gave me works great when checking a single bar. But some times I want to compare the current bar with one bar ago. Using the example again I would make something like this . . .

                      Code:
                      protected override void OnBarUpdate()
                      {
                      if (CountIf(delegate {return BOP(Close, 10)[0] > BOP(Close, 10)[1]
                       ;}, 10) > 8) 
                      
                      Plot0.Set(High[0] + 2 * TickSize);
                      }
                      So my hunch is that CountIf only works with the current bar's condition. You can't compare it to anything in the past.

                      Comment


                        #12
                        hondo69, please check into this tip here - http://www.ninjatrader-support2.com/...ead.php?t=3170

                        Then add this check to the start of your OnBarUpdate() and recheck the result -

                        Code:
                         
                        If (CurrentBar < 1) return;

                        Comment


                          #13
                          Yep . . . Makes perfect sense now that you've spelled it out.

                          That's the missing piece of my puzzle. I'll definitely put that to good use. Thanks again for the great help.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          637 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          366 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          107 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          569 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          571 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X