I need to detect when candle 3 or 4 come inside box (pic below). A box forms simply when a green candle closes above high of previous red candle - or vise versa.

So after candle 4 closes, it will check if candle 3 or 4 has touched the box, if it has, the box will not be printed, but if there are no touches, then the box will print.
*** Edit: This is the code I have now. Trying to first do red box. It is probably all over the place as the code is chop and changed. Also how would I add the candle count as an input setting? Thanks
indicator('candle pattern', overlay=true, max_lines_count=500)
len = input.int(2, "Box Length")
greenCandle = (close > open)
redCandle = (close < open)
f_is_within(val, top) => (val <= top)
if close < low[1] and greenCandle[1]
redBox=box.new(left=bar_index[1], top=math.max(high[1], high[1]),
right=bar_index + len, bottom=math.min(low[1], low[1]),
bgcolor=color.new(color.red, 50), border_color=color.new(color.red, 80), border_width=1),
box_top = box.get_top(redBox),
f_is_within(low, box_top)
// if close > high[1] and redCandle[1]
// greenBox=box.new(left=bar_index[1], top=math.max(low[1], low[1]),
// right=bar_index + len, bottom=math.min(high[1], high[1]),
// bgcolor=color.new(color.green, 50), border_color=color.new(color.green, 80), border_width=1),