You mentioned "up" bars and "down" bars but you did not include a definition. There are two definitions available:
- Up Bar: Close of current bar is greater than close of previous bar
- Up Bar: Close of current bar is greater than open of current bar
Since you did not specify one or the other I have selected option 1 for this solution. Please note the candles on Thinkorswim are colored based on option 2. Which means on Thinkorswim, a bar that closes higher than the previous can still be colored red if it closes below the open.
For this solution I have included user inputs to allow full flexibilty for the end user (except that you cannot select between option 1 or 2 as listed above). You can select the number of consecutive bars, the number of total bars to include in the patter and the direction of those consecutive bars, either up or down.
input consecutiveBars = 2;
input totalBars = 5;
input direction = {up, default down};
def consecutiveUpDown = if direction == direction.up then Sum(close > close[1], consecutiveBars) >= consecutiveBars else
Sum(close < close[1], consecutiveBars) >= consecutiveBars;
plot scan = Sum(consecutiveUpDown, totalBars) > 0;