PERFECT ENGULFING Candlestick Patterns by Anmol
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © anmolgoel056
//@version=4
study(title="Candlestick Patterns with Entry Signals", shorttitle="CPES by Anmol", overlay=true)

// User-defined inputs for label positions and visibility
var label_position = input(title="Label Position", type=input.string, options=["Above Candle", "Below Candle", "Above High", "Below Low"], defval="Above High")
var show_engulfing_labels = input(true, title="Show Engulfing Labels")
var show_IB_labels = input(true, title="Show IB Labels")
var show_zero_doji_labels = input(true, title="Show Zero Doji Labels")

// Bullish Engulfing
buE = (open < close[1]) and (close > open[1]) and (close > open) and (close[1] < open[1]) and (low < min(open, close)) and (high > max(open, close)) and (abs(close - open) / open > 0.00001) and high > high[1]

// Bearish Engulfing
beE = (open > close[1]) and (close < open[1]) and (close < open) and (close[1] > open[1]) and (low < min(open, close)) and (high > max(open, close)) and (abs(close - open) / open > 0.00001) and low < low[1]

// Inside Bar - Combined (IB)
IB =  ((open < close[1] and close > open[1] and close[1] > open[1] and close < open and high < high[1] and low > low[1] and abs(close[1] - open[1]) / close[1] > 0.00001) or 
       (open < open[1] and close[1] < open[1] and close > close[1] and close < open and high < high[1] and low > low[1] and abs(open[1] - close[1]) / open[1] > 0.00001) or 
       (open > open[1] and close < close[1] and close[1] > open[1] and close > open and high < high[1] and low > low[1] and abs(close[1] - open[1]) / close[1] > 0.00001) or 
       (open > close[1] and close < open[1] and close[1] < open[1] and close > open and high < high[1] and low > low[1] and abs(open[1] - close[1]) / open[1] > 0.00001))

// Zero Doji
zeroDoji = (open == close or abs(open - close) / open <= 0.000009) and (open > low or open > high or open < low or open < high)

// Plot text labels
var label labelBuE = na
var label labelBeE = na
var label labelIB = na
var label labelZD = na

// Calculate label y-position based on user input
buE_label_y = label_position == "Above Candle" ? high + syminfo.mintick : label_position == "Below Candle" ? low - syminfo.mintick : label_position == "Above High" ? high + syminfo.mintick : label_position == "Below Low" ? low - syminfo.mintick : na
beE_label_y = buE_label_y
IB_label_y = label_position == "Above Candle" ? high + syminfo.mintick : label_position == "Below Candle" ? low - syminfo.mintick : label_position == "Above High" ? high + syminfo.mintick : label_position == "Below Low" ? low - syminfo.mintick : na
zeroDoji_label_y = label_position == "Above Candle" ? high + syminfo.mintick : label_position == "Below Candle" ? low - syminfo.mintick : label_position == "Above High" ? high + syminfo.mintick : label_position == "Below Low" ? low - syminfo.mintick : na

// Plot labels based on visibility
if show_engulfing_labels and buE
    labelBuE := label.new(x=bar_index, y=buE_label_y, text="BuE", color=color.green, size=size.small)

if show_engulfing_labels and beE
    labelBeE := label.new(x=bar_index, y=beE_label_y, text="BeE", color=color.red, size=size.small)

if show_IB_labels and IB
    labelIB := label.new(x=bar_index, y=IB_label_y, text="IB", color=color.blue, size=size.small)

if show_zero_doji_labels and zeroDoji
    labelZD := label.new(x=bar_index, y=zeroDoji_label_y, text="ZD", color=color.orange, size=size.small)

// Clear labels if no pattern is detected
if na(buE)
    label.delete(labelBuE)

if na(beE)
    label.delete(labelBeE)

if na(IB)
    label.delete(labelIB)

if na(zeroDoji)
    label.delete(labelZD)
