Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator to count bars?

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

    #16
    Originally posted by kabott View Post
    and how do i use the value?

    this wont compile

    if(CountIf(delegate {return Close[0] > Open[0];}, 50));

    UpCandles =UpCandles +1;

    else

    UpCandles = 0;


    [ATTACH]21281[/ATTACH]
    You may be overthinking your solution. CountIf does the counting if a certain condition is met. You want the number of candles where the close is greater than the open, so all you need is:
    Code:
    int NumBullishCandle = CountIf(delegate {return Close[0] > Open[0];}, 50);

    Comment


      #17
      Thank you very much Koganam!!

      i had this working but couldn't compare both values


      DrawTextFixed("Up",CountIf(delegate {return Close[0] >Open[0];}, periods).ToString("N0"), TextPosition.TopRight ,Color.White,new Font ("Arial", 20), Color.Black, Color.Green, 10);

      DrawTextFixed("Dn",CountIf(delegate {return Close[0] < Open[0];}, periods).ToString("N0"), TextPosition.BottomRight ,Color.White,new Font ("Arial", 20), Color.Black, Color.Red, 10);


      now i am, thank you man!!

      Comment


        #18
        Originally posted by NinjaTrader_RyanM1 View Post
        You could custom code a counter, but using built-in method CountIf() should work well for this:
        http://www.ninjatrader.com/support/h...t7/countif.htm
        Hello Ryan, how would you print the count?

        For example I have this code
        PHP Code:
        if (CountIf(() => High[0] - Low[0] >= 25*TickSize, 5) > 0)
        {
             Print("# of Bar Ranges >= 25 Ticks : " + ?);
        } 
        

        What do i substitute the"?'" with?

        It's supposed to means, if over the previous 5 Bars there is any (>0) bar whose range is greater or equal 25 ticks, then print the number of bars that meet that condition.

        For illustration example, the Countif() would return 2 (for Bar[2] and Bar[3] below)

        High[0] - Low[0] (Bar[0]) = 7 ticks

        High[0] - Low[0] (Bar[1]) = 12 Ticks

        High[0] - Low[0] (Bar[2) = 31 ticks

        High[0] - Low[0] (Bar[3]) = 29 Ticks

        High[0] - Low[0] (Bar[4]) = 21 Ticks

        Thanks!



        I've found a way with the Draw.FixedText method to draw it on the chart without variable declaration
        PHP Code:
        protected override void OnBarUpdate()
        {
             if(CurrentBar < 5)
             {
                  return;
             }
        
             if (CountIf(() => High[0] - Low[0] >= 25*TickSize, 5) > 0)
             {
                 Draw.TextFixed(this, "myTextFixed", CountIf(() => High[0] - Open[0] >= 25*TickSize, 5).ToString(), TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Blue, Brushes.Transparent, 0);
             }
        } 
        

        but for the print it needs variable declaration

        PHP Code:
        namespace NinjaTrader.NinjaScript.Indicators
        {
             public class up25 : Indicator
             {
                  private string up25;
             ... 
        
        PHP Code:
        if (CountIf(() => High[0] - Low[0] >= 25*TickSize, 5) > 0)
        {
             up25 = CountIf(() => High[0] - Open[0] >= 25*TickSize, 5).ToString();
             Print("up25 : " + up25);
        } 
        

        With output
        PHP Code:
        up25 : 2
        up25 : 3
        up25 : 3
        up25 : 4
        up25 : 4 
        
        Last edited by PaulMohn; 02-22-2022, 12:44 PM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        62 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        134 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        75 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        45 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        50 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X