How to count bar

How to count bar (candle) like barIdx in tradingview?

Say

Count Candle pairs (Consecutive Red+Green) when close (0) > SMA 20

Or Say

Count consecutive 3 candle set ( Green+Green+Green) when close (0) > SMA 20