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

Countif Function

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

    Countif Function

    hi
    I have a question about countIf function whether i wirte it the right way.
    for my short conditions i want to make sure that previous 20 bars i do not have slow PSAR rising for 20 bars. (Slow is red pluses) It still took a short even though there was more then 20 bars with rising slow PSAR. Do i have it right?
    && (CountIf(() => slow[3] > slow[4], 20) < 20)
    Click image for larger version

Name:	image.png
Views:	141
Size:	633.9 KB
ID:	1257573

    #2
    Hello tkaboris,

    This would depend on the values of the slow series 3 bars ago and the slow series 4 bars ago (from the perspective of each bar within the last 20 bars).

    Try printing the returned value to the output window.
    Print(string.Format("{0} | CountIf(() => slow[3] > slow[4], 20): {1}", Time[0], CountIf(() => slow[3] > slow[4], 20) ));


    Where you have mentioned:
    "or my short conditions i want to make sure that previous 20 bars i do not have slow PSAR rising for 20 bars. "

    This does not appear to be what is in the code.

    Are you wanting to check if the slow is rising for the last 20 bars?
    CountIf(() => IsRising(slow), 20)
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      HI I want to make sure previous slow is not rising for 20 consecutive bars. if its a strong trend then i dont want to trade againts it.

      Comment


        #4
        Hello tkaboris,

        You could also use MRO (Most Recent Occurrence) to be -1 for the last 20 bars.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          HI thank you, I am abit confused about what to do either with countif or MRO
          in the example above for preventing going short if previous consecutive slows are rising and >= 20 bars.
          my logic for goinng short is slow[1] > Close[1]. Strategy is tickofbar
          Can you please help me to formulate this?

          would it be something like this?
          int barsAgoRising = MRO(() => slow[1] > slow[2], 1, 19);

          this one throws an error if i want to combine fast med and slow parabolics to be all above close[1]
          int barsAgoRising = MRO(() => slow[2] > slow[3] && slow[1] > Close[1] && med[1] > Close[1] && fast[1] > Close[1] , 1, 19);
          Last edited by tkaboris; 06-26-2023, 01:02 PM.

          Comment


            #6
            Hello tkaboris,

            MRO() works similar to CountIf() in that you supply an expression to check.

            if the MRO() with a period of 20 returns -1, this means that no occurence was found. If you are checking that slow[0] is <= slow[1] with a period of 20 and no occurrence was found, this means that every value for this series was higher on the current bar than the previous bar.

            slow[0] would be the current bar, slow[1] would be the previous bar. Can you clarify why you are using [0] and [1] as bars ago indexes? Is this because the script is running with Calculate OnEachTick and TickReplay with FirstTickOfBar being true in OnBarUpdate()?

            Regarding an error, what is the full error message you have received?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi i am trying to make sure that last time price bounced from baseEMA was more then 10 bars. Is this a right logic?
              (CountIf(() => ((Low[1] < baseEMA[1]) && (Close[1] > baseEMA[1])), 10) < 0)

              Comment


                #8
                Hello tkaboris,

                I recommend you print this to the output window to understand if the logic is how you are wanting.

                if (CurrentBar < 11)
                return;

                for (int index = 0; index < 10; index++)
                {
                int indexOffset = index + 1;
                Print(string.Format("{0} | Low[{1}]: {2} < baseEMA[{1}]: {3} && Close[{1}]: {4} > baseEMA[{1}]: {3}", Time[index], indexOffset, Low[indexOffset], baseEMA[indexOffset], Close[indexOffset] ));
                }

                Below is a link to a forum post that demonstrates how to use Print() to understand behavior.


                What constitutes a "bounce"? What specific logic?

                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  bounce is simple logic for longs
                  (Low[1] <= baseEMA[1]) && (Close[1] > baseEMA[1])

                  Comment


                    #10
                    Hello tkaboris,

                    If this is the code you are wanting I recommend testing it and reviewing the output.

                    Provide the rules of the logic you would like in words, if you would like assistance generating the code.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      ​HI i am looking for help to go with count if or MRO.
                      My regular entry is on number 2, but I need to code that if previously there was bounce from 21EMA within 6 bars, then i dont take my trade.
                      Currently this syntax is not right.. how can i do this?

                      && (CountIf(() => !((Close[1] < Open[1] && Close[2]>Open[2] && High[1] > EMA(21)[1] && Close[1] < EMA(21)[1])), 5) )
                      Click image for larger version

Name:	image.png
Views:	91
Size:	74.7 KB
ID:	1264682

                      Comment


                        #12
                        Hello tkaboris,

                        What are the specific rules for a "bounce"?

                        Do you mean the current price was less than the EMA 10 bars ago and is greater than the EMA on the current bar?
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          yes that what the logic is

                          Comment


                            #14
                            Hello tkaboris,

                            "the current price was less than the EMA 10 bars ago and is greater than the EMA on the current bar"

                            if (Close[0] < EME(21)[10] && Close[0] > EMA(21)[0])

                            'the close price of 10 bars ago was less than the EMA 10 bars ago and is greater than the EMA on the current bar'

                            if (Close[10] < EMA(21)[10] && Close[0] > EMA(21)[0])
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Thank you but can i also ask a slightly different logic using MRO ? So i can learn how to use it
                              How can i code that
                              Most recent occurence of
                              - bear bar
                              - high of bar above 21 ema and close below 21 ema
                              - to be more then 5 bars ago at least.


                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by tradebot, Yesterday, 01:25 PM
                              3 responses
                              16 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by manueldecastro, Yesterday, 10:26 AM
                              5 responses
                              24 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by memonic, 05-01-2024, 01:23 PM
                              5 responses
                              31 views
                              0 likes
                              Last Post memonic
                              by memonic
                               
                              Started by dcriador, Yesterday, 10:45 AM
                              2 responses
                              18 views
                              0 likes
                              Last Post dcriador  
                              Started by SnailHorn, Yesterday, 10:49 PM
                              1 response
                              10 views
                              0 likes
                              Last Post SnailHorn  
                              Working...
                              X