//@version=4
study(‘Pin Bar Indicator Alert’, overlay=true)
bullishPinBar = 1
bearishPinBar = -1
xor (a, b) =>
(a and not b) or (not a and b)
isLongBar(bodyLength, tailLength) =>
(tailLength > 3 * bodyLength)
isBullishBar(oneThird) =>
bullishBody = close > open
float bodyLength = (open > close) ? high – close : high – open
float tailLength = (open > close) ? close – low : open – low
is_pin_bar = (open >= oneThird) and (close >= oneThird)
longTail = isLongBar(bodyLength, tailLength) ? true : close < open
is_pin_bar and (longTail or bullishBody)
isBearishBar(oneThird) =>
bearishBody = close < open
float bodyLength = (open > close) ? high – close : high – open
float tailLength = (open > close) ? close – low : open – low
is_pin_bar = (open <= oneThird) and (close <= oneThird)
longTail = isLongBar(bodyLength, tailLength) ? true : close < open
is_pin_bar and (longTail or bearishBody)
isPinBar() =>
range = high – low
oneThird = range / 3
oneThirdBullish = high – oneThird
oneThirdBearish = low + oneThird
bullishPinBar = isBullishBar(oneThirdBullish)
bearishPinBar = isBearishBar(oneThirdBearish)
result = xor(bullishPinBar, bearishPinBar) ? 1 : 0
(result and bearishPinBar) ? -1 : result
isPinBarMade = isPinBar() == 1 or isPinBar() == -1 ? true : false
alertcondition(isPinBarMade, title=’Pin Bar’, message=’Pin Bar has made!’)
barcolor(isPinBar() == bullishPinBar ? color.green : na)
barcolor(isPinBar() == bearishPinBar ? color.red : na)
// When is bullish pin bar paint green
plotshape(isPinBar() == bullishPinBar, style=shape.triangleup,
location=location.abovebar, color=color.green)
// When is bearish pin bar paint red
plotshape(isPinBar() == bearishPinBar, style=shape.triangledown,
location=location.belowbar, color=color.red)