Extended Engulfing Candle
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MentalGap
//@version=5
indicator("Consecutive Engulfing Candle", overlay=true)

var int consecutiveDown = 0
var int consecutiveUp = 0
var float HH = na //HighestHigh of consecutive down candle
var float LL = na
var bool uDisplayed = false //trace BullishEngulfing label plot status
var bool dDisplayed = false

var bool isBullishEngulfing = false
var bool isBearishEngulfing = false


barup = close > close[1]
bardown = close < close[1]


if (bardown)
    consecutiveDown := consecutiveDown + 1
    if (na(HH) or barup[1])
        HH := high


if (barup)
    consecutiveUp := consecutiveUp + 1
    if (na(LL) or bardown[1])
        LL := low

// Check for Bullish Engulfing pattern
if barup and close > HH and not uDisplayed
    uDisplayed := true
    dDisplayed := false
    isBullishEngulfing := true
    consecutiveDown := 0
    HH := na
else
    isBullishEngulfing := false

//Bearish Engulfing
if bardown and close < LL and not dDisplayed
    dDisplayed := true
    uDisplayed := false
    isBearishEngulfing := true
    consecutiveUp := 0
    LL := na
else
    isBearishEngulfing := false

plotshape(series=isBullishEngulfing, title="Bullish Engulfing", location=location.belowbar, color=color.new(color.green, 100), textcolor= input.color(color.green, "BullsihEngulfing"),style=shape.labelup, text="U")
plotshape(series=isBearishEngulfing, title="Bearish Engulfing", location=location.abovebar, color=color.new(color.red, 100), textcolor= input.color(color.red, "BearishEngulfing"), style=shape.labeldown, text="D")
