• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


Identify number of closes in range x in the current day.

  • Filter
  • Time
  • Show
Clear All
new posts

    Identify number of closes in range x in the current day.


    I am having trouble trying to build a filter that looks like this, in plain English:

    "If two or more candles in THIS day CLOSE above x, then ..."

    Sorry if this is really easy, but I can't figure it out. At the moment, I'm using the filter below; it gives the same idea, but I would really like to get at least 2 candles CLOSING there.

    if (CurrentDayOHL().CurrentHigh[0] > x)

    x is some math with PriorDayOHLC, and I am using a 5-minute chart, if that is relevant.

    Thanks in advance for your time and help!

    Hello hddietrich,

    Thank you for your post and welcome to the NinjaTrader Support Forum!

    You can use the CountIf() method to see if the condition is true for at least two bars for the day.

    For example:
    if (CountIf(delegate {return CurrentDayOHL().CurrentHigh[0] > x;}, Bars.BarsSinceSession - 1) > 2)
    For information on CountIf() please visit the following link: http://www.ninjatrader.com/support/h...t7/countif.htm

    Please let me know if I may be of further assistance.
    Last edited by NinjaTrader_PatrickH; 01-14-2013, 12:55 PM.
    Patrick H.NinjaTrader Customer Service


      Awesome! Thanks Patrick, we're almost there.

      After some tests, I found out that the bottleneck of my code is now only "Bars.BarsSinceSession". I found that out because the code doesn't generate any trades with it. However, if I change it to some random integer, trades occur normally (the wrong trades, haha).

      For me to get BarsSinceSession to work, do I have to specify somewhere that I'm looking at 5-minute bars?

      PS: It's a simple instrument traded 8 hours per day, and NinjaTrader seems to recognize perfectly where sessions start and end with vertical lines.

      Also, I changed the CurrentHigh part - which was just what I was using to fix my leak - to simply Close[0], as I would like to know how many bars actually closed in that region (I don't know if that would matter (or even work)).

      I feel like I added too much irrelevant information. The main question is now bold and underlined. Thanks.
      Last edited by hddietrich; 01-14-2013, 12:48 PM.


        Hello hddietrich,

        Thank you for your response.

        Are you using multiple bars series in your code? (Example: Add(PeriodType.Minute, 5)

        Please post a snippet of your code that is using the Bars.BarsSinceSession so I may investigate this matter further.

        I look forward to your response.
        Patrick H.NinjaTrader Customer Service


          I tried inserting the Add(PeriodType.Minute, 5)), but it didn't work.

          Here is my initialize

          protected override void Initialize()
          			Add(PeriodType.Minute, 5);			
          			CalculateOnBarClose = true;
          And this is the precise condition that is causing the system not to generate any trades:

          if (CountIf(delegate {return Close[0] > (PriorDayOHLC().PriorHigh[0] + (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0])*ampfilter);}, Bars.BarsSinceSession - 1) > 2)
          The whole math with PriorDayOHLC and the parameter "ampfilter" is working in other parts of the code. I don't know what is makes the system generate 0 trades in over 2 years.

          The long and short parts of the strategy are separate, and I've applied this condition only to the long trades. If it were never satisfied, then I think the short trades would still happen normally, no? I am puzzled by this!

          PS: I don't know what you mean by "multiple bars series." I am only using one security and one time frame (5 minutes).


            I discovered the problem looking at the log. Ignore what I said about the BarsSinceSession.

            "Error on calling 'OnBarUpdate' method for strategy x: 'CountIf' can't be run on multi-series strategies."

            I don't know what is making my strategy multi-series; I don't need multiple time-frames or multiple instruments.

            Could you help me with this?


              Just a shot in the dark, but the Add() which you said you tried earlier... did you accidentally leave that in your code? If so, remove it and try again.


                Thanks, I did leave it on.

                With it removed, it now says in the log (still with 0 trades):

                "'period' parameter on 'CountIf' method must be greater/equal 0"

                Any clues on why it would think that whole thing is not greater than 0? It's just the priorHigh +10%*(priorHigh-PriorLow). Might it be the type of the object?


                  Hello hddietrich,

                  Thank you for your response.

                  You will need to make sure you have enough bars on your chart before accessing the values. Such as using the following:
                  if(CurrentBar < BarsRequired) return;
                  For information on making sure you have enough bars in the series you are accessing please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=3170

                  Please let me know if you have any questions.
                  Patrick H.NinjaTrader Customer Service


                    Thanks Patrick! It's all working now!

                    Just one more thing: is it too complicated to draw that math I did with PriorDayOHLC (mentioned in previous post)?

                    It would be like this: http://i.imgur.com/XJBEM.png

                    If it requires custom coding, new indicators or something else, don't worry about it. The visualizing has a low priority to me right now. Besides, you've done a lot!

                    Thanks for your prompt help!

                    Edit: The green lines are the filters, while the blue and red are the previous high and low.


                      Hello hddietrich,

                      Thank you for your response.

                      This would require custom programming custom plots. You can find an example of this for an indicator by going to the NinjaTrader Control Center > Tools > Edit NinjaScript > Indicator > CustomPlotSample > OK.

                      Please let me know if I may be of further assistance.
                      Patrick H.NinjaTrader Customer Service


                      Latest Posts


                      Topics Statistics Last Post
                      Started by shodson, 02-11-2020, 07:26 PM
                      3 responses
                      Last Post shodson
                      by shodson
                      Started by ksarin, Today, 04:31 PM
                      0 responses
                      Last Post ksarin
                      by ksarin
                      Started by ralett, Today, 03:39 PM
                      0 responses
                      Last Post ralett
                      by ralett
                      Started by koganam, Today, 03:17 PM
                      1 response
                      Last Post NinjaTrader_Kate  
                      Started by DTSSTS, Today, 02:46 PM
                      4 responses
                      Last Post DTSSTS
                      by DTSSTS