Pinbar by BirdCoin
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BirdCoin11

//@version=5
indicator('Pinbar by BirdCoin', overlay=true)

candle = math.abs(high - low)
body = math.abs(open - close)
upshadow = open>close?(high-open):(high-close)
downshadow = open>close?(close-low):(open-low)

minspread = input.float(defval = 0.15, title = "Min Spread, %", minval = 0.01, maxval = 100, step=0.01, tooltip="Percentage of Min Spread compared to Price", group="Pinbar Condition")
maxspread = input.float(defval = 1.5, title = "Max Spread, %", minval = 0.01, maxval = 100, step=0.01, tooltip="Percentage of Max Spread compared to Price", group="Pinbar Condition")
longwick = input.float(defval = 50, title = "Min Long Wick, %", minval = 25, maxval = 100, step=1, tooltip="Percent longer than the total candle", group="Pinbar Condition")
shortwick = input.float(defval = 20, title = "Max Short Wick, %", minval = 0, maxval = 50, step=1, tooltip="Percent shorter than the total candle", group="Pinbar Condition")
realbody = input.float(defval = 33, title = "Max Body, %", minval = 0, maxval = 50, step=1, tooltip="Percent shorter than the total candle", group="Pinbar Condition")
minvol = input.float(defval = 500, title = "Min Volume", minval = 1, step=1, group="Pinbar Condition")
hunt = input.int(defval = 1, title = "Hunt?", minval = 0, step=1, tooltip = "How many previous candles were hunted?", group="Pinbar Condition")
hunt1 = hunt > 0 ? hunt : 1

upperbound = input.float(defval = 0, title = "Upper Bound", minval = 0, tooltip = "Alert when pinbar appear in range", group="Alert in Range")
lowerbound = input.float(defval = 0, title = "Lower Bound", minval = 0, tooltip = "Alert when pinbar appear in range", group="Alert in Range")

//Pinbar condition check?
l1 = candle > close * minspread / 100
l2 = candle < close * maxspread / 100
l3 = high >  ta.highest (high,hunt1)[1]
l4 = downshadow < candle * shortwick / 100
l5 = upshadow > candle * longwick / 100
l6 = volume > minvol
l7 = close < open
l8 = body < candle * realbody / 100

//bearish pin bar
bearishpin = hunt > 0 ? l1 and l2 and l3 and l4 and l5 and l6 and l7 and l8 : l1 and l2 and l4 and l5 and l6 and l7 and l8
plotshape(bearishpin, title = "Pinbar Down", style=shape.triangledown, color=color.new(color.red, 0), size=size.tiny, location=location.abovebar)


//Pinbar condition check?
h1 = candle > close * minspread / 100
h2 = candle < close * maxspread / 100
h3 = low < ta.lowest (low,hunt1)[1]
h4 = downshadow > candle * longwick / 100
h5 = upshadow < candle * shortwick / 100
h6 = volume > minvol
h7 = close > open
h8 = body < candle * realbody / 100

//bullish pin bar
bullishpin = hunt > 0 ? h1 and h2 and h3 and h4 and h5 and h6 and h7 and h8 : h1 and h2 and h4 and h5 and h6 and h7 and h8
plotshape(bullishpin, title = "Pinbar Up", style=shape.triangleup, color=color.new(color.green, 0), size=size.tiny, location=location.belowbar)

//pin bar in range
inrange_up_alert = high[1] >= lowerbound and low[1] <= upperbound and bullishpin
inrange_down_alert = high[1] >= lowerbound and low[1] <= upperbound and bearishpin

//================================= Alerts ===================================
    
NewPinbar = bullishpin[1] or bearishpin[1]

alertcondition(bullishpin, title='Pinbar Up',    message='Pinbar Up')
alertcondition(bearishpin, title='Pinbar Down',    message='Pinbar Down')
alertcondition(bullishpin, title='New Pinbar',    message='New Pinbar')
alertcondition(inrange_up_alert, title='Pinbar Up in Range',    message='Pinbar Up in Range')
alertcondition(inrange_down_alert, title='Pinbar Down in Range',    message='Pinbar Down in Range')


