There's probably a better way to do it but I would use a for loop to count the times the condition was true in the past 20 bars , then check to see if it was true 20 times. Something like this.
Code: Select all
// Count candles above Boolinger Bands Basis
barsAboveBBbasis = 0
for i = 0 to 19
if close[i] > BollingerBandBasis[i]
barsAboveBBbasis := barsAboveBBbasis + 1
bgcolor(barsAboveBBbasis == 20 ? color.green : na , transp=80, title="Past 20 Bars Above BBbasis")
You could also check for the opposite condition using barssince:
Code: Select all
barsAboveBBbasis = barssince(close <= BoolingerBandsBasis)
bgcolor(barsAboveBBbasis >= 20 ? color.green : na , transp=80, title="Past 20 Bars Above BBbasis")
You could also use sum to accomplish this
Code: Select all
barAboveBBbasis = close > BollingerBandBasis
barsAboveBBbasis = sum(barAboveBBbasis ? 1 : 0, 20) == 20
bgcolor(barsAboveBBbasis ? color.green : na , transp=80, title="Past 20 Bars Above BBbasis")