//@version=5
indicator(title="High/Low & Candle Patterns", overlay=true, max_labels_count=500)
// Input for swing length
length = input(21)
// Style
swinghCss = input(color.red, 'Swing High', group = 'Style')
swinglCss = input(color.teal, 'Swing Low', group = 'Style')
// Swing High/Low Calculation
var float phy = na
var float ply = na
ph = ta.pivothigh(length, length)
pl = ta.pivotlow(length, length)
// Candlestick Pattern Detection
d = math.abs(close - open)
hammer = pl and math.min(open, close) - low > d and high - math.max(close, open) < d
ihammer = pl and high - math.max(close, open) > d and math.min(close, open) - low < d
bulleng = close > open and close[1] < open[1] and close > open[1] and open < close[1]
hanging = ph and math.min(close, open) - low > d and high - math.max(open, close) < d
shooting = ph and high - math.max(open, close) > d and math.min(close, open) - low < d
beareng = close < open and close[1] > open[1] and close < open[1] and open > close[1]
// Labeling Swing Points and Patterns
n = bar_index
if ph
H = ph > phy ? 'HH' : 'LH'
label.new(n[length], ph, H, color=color(na), style=label.style_label_down, textcolor=swinghCss)
phy := ph
if pl
L = pl < ply ? 'LL' : 'HL'
label.new(n[length], pl, L, color=color(na), style=label.style_label_up, textcolor=swinglCss)
ply := pl