title: Start counting bars number when the condition is met and add the exceptions to the count as well
body: <p>When the conditions are met, I want to count the bars number, but also add exceptions.
  For example, I want to count 11 days in a row to see if there is 9 days up during this period, and if one day(let's say today) is down but the close price are less or equal to 1% than yesterday (means yesterday is up day, not previous up day), then count this day is a up day too.</p>
  <p>For example, let's say day1 is up day, here is 11days in a row, and there is 3 days down (day3, day5, day6), but day3's close price are less or equal to 1% than yesterday (means day2 and day2 is up day), and day2 is up day, so day3 is a up day too, which mean is it will met the condition this time.(9 days up in 11days)</p>
  <pre><code>day1  day2  day3   day4  day5   day6   day7   day8  day9  day10  day11
  10    11    10.5   10.8    9     8      12     17    18     19    20
  up    up    down    up    down  down    up     up    up     up    up
  </code></pre>
  <p>And in a case like this, the condition is not valid, day3' close price are less or equal to 1% than yesterday(day2), but yesterday(day2) are not up day, which mean the condition are not met this time.(8 days up in 11days this time)</p>
  <pre><code>day1  day2  day3   day4  day5   day6  day7  day8  day9  day10  day11
  10     9     8.5   10.8    8     9     12    17    18    19     20
  up    down   down   up    down   up    up    up    up    up     up
  </code></pre>
  <p>This is my code:</p>
  <pre><code>//@version=5
  
  indicator(&quot;Green bars&quot;, overlay = true, max_labels_count = 500)
  
  barsBack = input.int(11)
  
  var float[] green_bar_array = array.new_float()
  
  if close &gt; close[1] or ((close/close[1])-1) * 100 &lt;= 1
      array.push(green_bar_array, close)
  
  if array.size(green_bar_array) &gt; barsBack
      array.shift(green_bar_array)
  
  bool updaygreen = false
  int green_count = 0
  
  if array.size(green_bar_array) == barsBack
      for i = 1 to barsBack - 1
          if array.get(green_bar_array, i) &gt;= array.get(green_bar_array, i - 1)
              green_count += 1
      if green_count &gt;= 9
          updaygreen := true
  
  plotshape(updaygreen, location=location.belowbar, color=#00b543, style=shape.circle, size=size.tiny, transp=55)
  </code></pre>
  <p>But the result are not correct... what am I missing?
  Please help me, thanks!</p>
  <p>EDIT:</p>
  <pre><code>//@version=5
  indicator(&quot;Count Bar Color&quot;, overlay = true, max_labels_count = 500)
  
  var int     barsBack        = input.int(11, 'Number of bars lookback', 0)
  
  var int[]   greenBars       = array.new_int(barsBack, 0)
  var int[]   redBars         = array.new_int(barsBack, 0)
  var int     greenBarCount   = 0
  var int     redBarCount     = 0
  
  direction = ta.change(close) // red or green ?
  
  if barstate.isconfirmed
      if direction &gt; 0
          array.push(greenBars, 1) 
          array.push(redBars,   0)
      else if direction &lt; 0 
          array.push(greenBars, 0) 
          array.push(redBars,   1)
  
      if direction != 0 
          array.shift(greenBars) 
          array.shift(redBars)
  
      greenBarCount := array.sum(greenBars)
      redBarCount   := array.sum(redBars)
  
  upday = greenBarCount &gt;= 9
  
  plotshape(upday, location=location.belowbar, color=#00b543, style=shape.circle, size=size.tiny, transp=55)
  </code></pre>
  <p>Changed my code to this now, but still don't know how to add &quot;close price are less or equal to 1% than yesterday(yesterday is up day, not previous up day)&quot; into it...</p>