Hammers & Stars Candle [2022]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MINTYFRESH97

//@version=5
indicator("Hammers & Stars", overlay=true)

// Get user input
fibLevel    = input.float(title="Fib Level", defval=0.400)
colorFilter = input.bool(title="Color Filter", defval=false)
atrFilter   = input.float(title="ATR Filter", defval=0.1)

//swing high/low filter
swingHigh = high == ta.highest(high,10) or high[1] == ta.highest(high,10)
swingLow = low == ta.lowest(low,10) or low[1] ==ta.lowest(low,10)

//user input
var g_ema    = "EMA Filter"
emaLength    = input.int(title="EMA Length", defval=50, tooltip="Length of the EMA filter", group=g_ema)
useEmaFilter = input.bool(title="Use EMA Filter?", defval=false, tooltip="Turns on/off the EMA filter", group=g_ema)

// Get EMA filter
ema = ta.ema(close, emaLength)
emaFilterLong = not useEmaFilter or close > ema
emaFilterShort = not useEmaFilter or close < ema

// Check our ATR filter
atr = ta.atr(14)
candleSize = high - low
atrFilterCheck = candleSize >= atr * atrFilter

// Calculate fibonacci level for current candle
bullFib = (low - high) * fibLevel + high
bearFib = (high - low) * fibLevel + low

// Determine which price source closes or opens highest/lowest
lowestBody  = close < open ? close : open
highestBody = close > open ? close : open

// Determine if we have a valid hammer or shooting star candle
hammerCandle = lowestBody >= bullFib and (not colorFilter or close > open) and atrFilterCheck and swingLow and close and emaFilterLong 
starCandle   = highestBody <= bearFib and (not colorFilter or close < open) and atrFilterCheck and swingHigh and close and emaFilterShort

// Plot our candle patterns
barcolor(hammerCandle ? color.green:na)
barcolor(starCandle ? color.yellow :na)
plotshape(hammerCandle, style=shape.triangleup, size=size.tiny, text="H",color=color.green, location=location.belowbar, color=color.green)
plotshape(starCandle, style=shape.triangledown, size=size.tiny, text="S", color=color.yellow, location=location.abovebar, color=color.yellow)
alertcondition(hammerCandle, "Hammer", "hammerCandle detected for {{ticker}}")
alertcondition(starCandle, "Shooting Star", "starCandle detected for {{ticker}}")