Pin Bar
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//

//@version=4

//Pin Bar at the edge of BB
//Conditions to check
// 1. It must be a Pin Bar
// 2. Price Above/Below 200 SMA
// 3. Volume must be above 20 SMA
// 4. Low/High of the Pin Bar must cut across the BB upper/lower band
// 5. The very next candle closes above/below the high/low of the pin bar candle


study(title="Pin Bar", overlay=true)

var bool isBullishPinBar = false
var bool isBearishPinBar = false
var bool isBullishBar = false
var bool isBearishBar = false
var bool isSizeOK = false
var bool isUptrend = false
var bool isDowntrend = false
var bool isVolume = false
var bool isLongEntry = false
var bool isShortEntry = false


closeRange = input(title="Closing Range", type=input.integer, minval=1, maxval=40,  defval=30)

//Check conditons for a Pin Bar 
// 1. Body:Size=1:3 
// 2. Close of the bar should be in the "closeRange" range of high/low. Recommended between 20-35% range 
// 3. Size of the bar should be greater than ATR

//Check if it is a Bullish or Bearish bar, based on the difference between high/low and close 
if abs(high[1] - close[1]) < abs(low[1] - close[1])
    isBullishBar := true
if abs(high[1] - close[1]) > abs(low[1] - close[1])
    isBearishBar := true

//Capture bar details
barBody = abs(close[1] - open[1])
barSize = abs(high[1] - low[1])

//Check ATR value
//atrValue = atr(14)

//Check the overall size of the bar against ATR and size:wick ratio 
if (barSize / barBody) >= 3
    isSizeOK := true

//Check if it is a Pin Bar    
if isBullishBar and isSizeOK and ((high[1] - close[1]) < ((closeRange * barSize) / 100))
    isBullishPinBar := true

if isBearishBar and isSizeOK and ((close[1] - low[1]) < ((closeRange * barSize) / 100))
    isBearishPinBar := true

//Check id the price is unptrend or downtrend. Use 200 SMA
mvgAvg200 = sma(close, 200)
if close > mvgAvg200
    isUptrend := true
    
if close < mvgAvg200
    isDowntrend := true

//Check if there is a volume spike i.e. Volume above 20SMA
if volume[1] > sma(volume[1], 20)
    isVolume := true
else
    isVolume := false


//Captute BB details
[mBB, uBB, lBB] = bb(close, 20, 2)

//Check if there is a trade
if isBullishPinBar and isUptrend and isVolume and (low[1] < lBB and close[1] > lBB) and (close > high[1])
    isLongEntry := true

if isBearishPinBar and isDowntrend and isVolume and (high[1] > uBB and close[1] < uBB) and (close < low[1])
    isShortEntry := true


//Print signal on the chart
plotshape(isLongEntry, title="Buy", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(isShortEntry, title="Sell", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)


isBullishPinBar := false
isBearishPinBar := false
isBullishBar := false
isBearishBar := false
isSizeOK := false
isUptrend := false
isDowntrend := false
isVolume := false
isLongEntry := false
isShortEntry := false









