'x number of consecutive bars or candles

I have multiple conditions that need to be consecutively met for entering a short position. I already coded my 1st and 3rd conditions. Once the 1st condition is met, we search for the 2nd condition, and then it should continue with the 3rd condition.

However, the 2nd condition involves x numbers of consecutive bars (Green or Red Candles, it doesn't matter) that are closed between the upper and lower bollinger bands before the 3rd condition begins. I think I should use either while or for with to and by with a break, or ta.barssince.

Please see below the code. I would be greatful if anybody could provide me the code for the 2nd condition. Thank you for your help!

//@version=5
indicator(title="x numbers of consecutive bars until condition is met", overlay=true)



length = input.int(20, minval=1)
src = input(close, title='Source')
mult = input.float(2.0, minval=0.001, maxval=50, title='StdDev')
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
offset = input.int(0, 'Offset', minval=-500, maxval=500)
plot(basis, 'Basis', color=color.new(#FF6D00, 0), offset=offset)
p1 = plot(upper, 'Upper', color=color.new(#2962FF, 0), offset=offset)
p2 = plot(lower, 'Lower', color=color.new(#2962FF, 0), offset=offset)
fill(p1, p2, title='Background', color=color.rgb(33, 150, 243, 95))




// 1st condition: Red Candle crossunders the upper bollinger band

RedCandle = close <= open
GreenCandle = close >= open

FirstCondition = RedCandle and ta.crossunder(close, upper)


plotshape(FirstCondition, text='1st', title='Red Candle crossunders upper band', style=shape.labeldown, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), location=location.abovebar)

// 2nd Condition: there should be x numbers of consecutive bars closed (Green or Red Candles, it doesn't matter)
// between the upper and lower bollinger bands before the 3rd condition begins

// here, should I use while with break or for with break, or ta.barssince()? and how? 


// 3rd Condition:
    // Red Candle crossunders the lower bollinger band
    // Green Candle crossovers the lower bollinger band
    // then another Green Candle is closed between upper and lower bollinger bands


ThirdCondition_1 = RedCandle[2] and ta.crossunder(close[2], lower[2])
ThirdCondition_2 = GreenCandle[1] and ta.crossover(close[1], lower[1])
ThirdCondition_3 = GreenCandle and close > lower and close < upper
ThirdCondition = ThirdCondition_1 and ThirdCondition_2 and ThirdCondition_3


plotshape(ThirdCondition, text='3rd', title='Red Candle crossunders lower, then 2 Green Candles', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.belowbar)


// 1st, 2nd and 3rd Condition must be consecutively true for a short entry

// FirstCondition
// SecondCondition
// ThirdCondition

// ShortEntry = FirstCondition and SecondCondition and ThirdCondition

// plotshape(ShortEntry, text='SE', title='Short Entry', style=shape.arrowdown, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), location=location.abovebar, size=size.large)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source