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

Count an event in the last X days

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

    Count an event in the last X days

    Hey guys,

    I am just coming back to NT from a while away and was hoping someone could help me with some code... If this is not the best place to ask, please let me know

    What I am wanting to achieve, is to set a variable say 'percentaboveMA' (which I can then use as part of a strategy or to plot as a indicator, which will display the percentage of times that the low of the day is above a SMA for the last X periods (say 40 periods).

    In Metastock the formula is: Sum( L>Mov(C,20,S), 40)*100/120

    I imagine I may have to use a count and loop, or does anyone know if there is a built in NT function i can use?

    Thanks a lot & cheers,
    Shane

    #2
    Hi guys, sorry I should have clarified, where I said 'last X days', i was meaning the last X bars, as this was for a daily chart...
    Cheers
    Shane

    Comment


      #3
      Hello Shane,

      Thank you for your note.

      You will want to look at using CountIf() which will count the number of occurrences of the condition -
      http://www.ninjatrader.com/support/h...ml?countif.htm

      Let me know if I can be of further assistance.
      Cal H.NinjaTrader Customer Service

      Comment


        #4
        I am looking to add the NbarsDown indicator into a condition of a strategy.

        IF Close is less than 3 consecutive down bars within the last 10 bars.

        && CountIf(delegate {return Close[0] < NBarsDown(3, true, false, false)[0];},10)
        I am however getting an error https://gyazo.com/a501c37437f5736cbc5c1cf809dbd4c3

        Comment


          #5
          I got the error to go away by adding a == 3 at the end like in the CountIf() example, but does this do what I need it to? The NbarsDown returns a 1 if the conditions are met...

          && CountIf(delegate {return Close[0] < NBarsDown(3, true, false, false)[0];},10) == 1
          Last edited by brucelevy; 11-07-2015, 10:33 PM.

          Comment


            #6
            Hello brucelevy,

            Thank you for writing in. The code you provided is likely not doing what you are looking for. CountIf is simply going to return the number of occurrences. You need more than this for what you are trying to accomplish.
            I would recommend writing your own method to test for this. For example:
            Code:
            private bool closeIsLessThan3ConsecDownBarsOverLast10Bars(double closeVal)
            {
              for(int i = 1; i < 10; i++) //Test 10 bars back from the last bar. This loop will make 'i' go from 1 to 9. At 9, the 11th bar (i + 2) is checked, resulting in 11-1 (starting value of i) total bars checked (10)
              {
                int downBar = NBarsDown(3, true, false, false)[i], downBar2 = NBarsDown(3, true, false, false)[i+ 1], downBar3 = NBarsDown(3, true, false, false)[i + 2];
                if(downBar == 1 && downBar2 == 1 && downBar3 == 1)
                {
                  if(closeVal < Close[i] && closeVal < Close[i + 1] && closeVal < Close[i + 2])
                  {
                    return true;
                  }
                }
              }
            }
            return false;
            }
            and then use:
            Code:
            && closeIsLessThan3ConsecDownBarsOverLast10Bars(Close[0])
            instead of:
            Code:
            && CountIf(delegate {return Close[0] < NBarsDown(3, true, false, false)[0];},10) == 1
            I may be misunderstanding what the NBarsDown indicator actually does. If it returns a value of 1 when the bar requested + the prior 2 bars are down bars, then my code would look like this instead:
            Code:
            private bool closeIsLessThan3ConsecDownBarsOverLast10Bars(double closeVal)
            {
              for(int i = 1; i < 10; i++) //Test 10 bars back from the last bar. This loop will make 'i' go from 1 to 9. At 9, the 11th bar (i + 2) is checked, resulting in 11-1 (starting value of i) total bars checked (10)
              {
                int downCondition = NBarsDown(3, true, false, false)[i];
                if(downCondition == 1)
                {
                  if(closeVal < Close[i] && closeVal < Close[i + 1] && closeVal < Close[i + 2])
                  {
                    return true;
                  }
                }
              }
            }
            return false;
            }
            Please let me know if I may be of further assistance.
            Last edited by NinjaTrader_MichaelM; 11-08-2015, 04:33 PM.
            Michael M.NinjaTrader Quality Assurance

            Comment


              #7
              So is this code saying that 3 nbars down pattern occured within the last 10 bars?

              && CountIf(delegate {return 1 == NBarsDown(3, true, false, false)[0];},10) != 0)

              Comment


                #8
                Hello brucelevy,

                I apologize I may have misinterpreted what the indicator you are referring to does, as I am not familiar with it. I have updated my post below.

                If the NBarsDown indicator returns a 1 if there are 3 consecutive down bars, then the code you provided would do the following:

                if over the last 10 bars, one or more occurrence(s) of 3 consecutive down bars took place, return true.

                Otherwise if the indicator returns 1 if a single bar is a down bar, then the code you provided would do the following:

                if over the last 10 bars, one or more down bar(s) exist(s), return true.

                Neither of these however will check if over the last 10 bars, 3 consecutive down bars exist and that the close is less than all three values of the consecutive down bars. This is what the code I provided below will do.
                Michael M.NinjaTrader Quality Assurance

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by mmckinnm, Today, 01:34 PM
                0 responses
                2 views
                0 likes
                Last Post mmckinnm  
                Started by f.saeidi, Today, 01:32 PM
                1 response
                2 views
                0 likes
                Last Post NinjaTrader_Erick  
                Started by traderqz, Today, 12:06 AM
                9 responses
                16 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by kevinenergy, 02-17-2023, 12:42 PM
                117 responses
                2,766 views
                1 like
                Last Post jculp
                by jculp
                 
                Started by Mongo, Today, 11:05 AM
                5 responses
                15 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Working...
                X