The example below checks only the active chart. Bullish requires a bearish prior body, a bullish current body that covers it, and a strictly larger current body. It also requires the close above an EMA and volume above a prior-volume average. Bearish uses the opposite body and EMA comparisons. This is an educational continuation-filter variant; it is not TradingView’s built-in reversal detector, a hammer detector or a geometric pattern engine.
Closed-bar body-engulfing events with an EMA and prior-volume filterpinescript
//@version=6
indicator("Closed-bar body engulfing example [Pineify]", overlay=true)
int trendLength = input.int(50, "Trend EMA length", minval=1, maxval=500)
int volumeLength = input.int(20, "Prior volume average length", minval=1, maxval=500)
float trendEMA = ta.ema(close, trendLength)
float priorVolumeAverage = ta.sma(volume[1], volumeLength)
float body = math.abs(close - open)
float previousBody = math.abs(close[1] - open[1])
bool ready = bar_index >= math.max(trendLength, volumeLength) and not na(priorVolumeAverage) and priorVolumeAverage > 0 and not na(volume) and not na(trendEMA)
bool volumeAboveAverage = ready and volume > priorVolumeAverage
bool bullishBody = close[1] < open[1] and close > open and open <= close[1] and close >= open[1] and body > previousBody
bool bearishBody = close[1] > open[1] and close < open and open >= close[1] and close <= open[1] and body > previousBody
bool bullishEvent = barstate.isconfirmed and volumeAboveAverage and bullishBody and close > trendEMA
bool bearishEvent = barstate.isconfirmed and volumeAboveAverage and bearishBody and close < trendEMA
plot(trendEMA, title="Trend EMA", color=color.blue)
plotshape(bullishEvent, title="Bullish body engulfing", style=shape.labelup, location=location.belowbar, color=color.green, text="BULL", textcolor=color.white, size=size.tiny)
plotshape(bearishEvent, title="Bearish body engulfing", style=shape.labeldown, location=location.abovebar, color=color.red, text="BEAR", textcolor=color.white, size=size.tiny)
alertcondition(bullishEvent, title="Bullish body engulfing", message="Closed-bar bullish body engulfing: {{ticker}} {{interval}}, close {{close}}. Research event, not an order.")
alertcondition(bearishEvent, title="Bearish body engulfing", message="Closed-bar bearish body engulfing: {{ticker}} {{interval}}, close {{close}}. Research event, not an order.")