Pine Script: Master Engulfing Candlestick Patterns (Bullish & Bearish)

Unlock powerful reversal signals with this comprehensive guide to detecting and strategizing with Engulfing patterns in TradingView's Pine Script.

Posted: Expertise: Intermediate/Advanced

Why Engulfing Patterns Matter in Pine Script

The Engulfing candlestick pattern is one of the most powerful and widely recognized reversal signals in technical analysis. It consists of two candles, where the second candle's body completely "engulfs" the first candle's body. These patterns often indicate a strong shift in market sentiment, making them invaluable for:

Understanding Engulfing Candlestick Patterns

An engulfing pattern provides a strong visual clue about who's in control—buyers or sellers. The larger the engulfing candle relative to the previous one, and the larger its body, the stronger the signal. Importantly, the colors of the two candles must be opposite.

Bullish Engulfing Pattern

A small bearish candle is completely covered by a large bullish candle, often after a downtrend.

Bearish Engulfing Pattern

A small bullish candle is completely covered by a large bearish candle, often after an uptrend.

Key Characteristic: The engulfing candle's body (the solid part between open and close) must completely overlap the previous candle's body. Wicks (shadows) are not considered for the engulfing criteria, although wider engulfing wicks can add to conviction.

Basic Engulfing Pattern Detection in Pine Script

Here's how to create a basic indicator to detect both Bullish and Bearish Engulfing patterns in Pine Script v5:

//@version=5
indicator("Engulfing Pattern Detector", overlay=true)

// Get candle properties for current and previous bar
prevOpen = open[1]
prevClose = close[1]
prevHigh = high[1]
prevLow = low[1]

// Check if previous candle was bullish (green) or bearish (red)
isPrevBullish = prevClose > prevOpen
isPrevBearish = prevClose < prevOpen

// Define Bullish Engulfing criteria
// 1. Previous candle is bearish
// 2. Current candle is bullish
// 3. Current candle's body completely engulfs previous candle's body
isBullishEngulfing = isPrevBearish and (close > open) and (close > prevHigh) and (open < prevLow)

// Define Bearish Engulfing criteria
// 1. Previous candle is bullish
// 2. Current candle is bearish
// 3. Current candle's body completely engulfs previous candle's body
isBearishEngulfing = isPrevBullish and (close < open) and (open > prevHigh) and (close < prevLow)

// Plot signals on the chart
plotshape(isBullishEngulfing, title="Bullish Engulfing", location=location.belowbar, color=color.new(color.green, 0), style=shape.triangleup, size=size.small)
plotshape(isBearishEngulfing, title="Bearish Engulfing", location=location.abovebar, color=color.new(color.red, 0), style=shape.triangledown, size=size.small)

// Alert conditions (optional)
alertcondition(isBullishEngulfing, title="Bullish Engulfing Alert", message="Bullish Engulfing Pattern Detected!")
alertcondition(isBearishEngulfing, title="Bearish Engulfing Alert", message="Bearish Engulfing Pattern Detected!")

Advanced Engulfing Strategies

While basic detection is useful, combining engulfing patterns with other indicators provides stronger, more reliable signals.

1. Engulfing with Trend Confirmation (Using Moving Average)

//@version=5
strategy("Engulfing with Trend Confirmation", overlay=true)

// Input for Moving Average length
maLength = input(20, "MA Length")
ma = ta.ema(close, maLength) // Using EMA for trend detection

// Engulfing Pattern Detection (from basic example)
isPrevBullish = close[1] > open[1]
isPrevBearish = close[1] < open[1]
isBullishEngulfing = isPrevBearish and (close > open) and (close > high[1]) and (open < low[1])
isBearishEngulfing = isPrevBullish and (close < open) and (open > high[1]) and (close < low[1])

// Trend Confirmation:
// For Bullish Engulfing: Price should be below MA (downtrend)
// For Bearish Engulfing: Price should be above MA (uptrend)
isDowntrend = close < ma
isUptrend = close > ma

// Strategy Logic with Trend Confirmation
longSignal = isBullishEngulfing and isDowntrend
shortSignal = isBearishEngulfing and isUptrend

if (longSignal)
    strategy.entry("Long", strategy.long)
    
if (shortSignal)
    strategy.entry("Short", strategy.short)

// Plot MA
plot(ma, "Trend MA", color.blue)

// Plot confirmed signals
plotshape(longSignal, title="Confirmed Bullish Engulfing", location=location.belowbar, color=color.new(color.navy, 0), style=shape.arrowup, size=size.normal)
plotshape(shortSignal, title="Confirmed Bearish Engulfing", location=location.abovebar, color=color.new(color.fuchsia, 0), style=shape.arrowdown, size=size.normal)

2. Engulfing with Volume Confirmation

//@version=5
indicator("Engulfing with Volume Confirmation", overlay=true)

// Engulfing Pattern Detection (from basic example)
isPrevBullish = close[1] > open[1]
isPrevBearish = close[1] < open[1]
isBullishEngulfing = isPrevBearish and (close > open) and (close > high[1]) and (open < low[1])
isBearishEngulfing = isPrevBullish and (close < open) and (open > high[1]) and (close < low[1])

// Volume Confirmation:
// Engulfing candle's volume should be higher than previous candle's volume
isVolumeConfirmed = volume > volume[1]

// Combined signals
confirmedBullish = isBullishEngulfing and isVolumeConfirmed
confirmedBearish = isBearishEngulfing and isVolumeConfirmed

// Plot confirmed signals
barcolor(confirmedBullish ? color.new(color.teal, 60) : na)
barcolor(confirmedBearish ? color.new(color.maroon, 60) : na)

plotshape(confirmedBullish, title="Bullish Engulfing (Vol Confirmed)", location=location.belowbar, color=color.new(color.green, 0), style=shape.labelup, size=size.small, text="Bull Vol")
plotshape(confirmedBearish, title="Bearish Engulfing (Vol Confirmed)", location=location.abovebar, color=color.new(color.red, 0), style=shape.labeldown, size=size.small, text="Bear Vol")

// Optional: Plot volume for visual check
plot(volume, title="Volume", color=color.gray, style=plot.style_columns)

3. Engulfing with RSI Confirmation

//@version=5
indicator("Engulfing with RSI Confirmation", overlay=true)

// RSI Inputs
rsiLength = input(14, "RSI Length")
overbought = input(70, "RSI Overbought Level")
oversold = input(30, "RSI Oversold Level")

// Calculate RSI
rsiValue = ta.rsi(close, rsiLength)

// Engulfing Pattern Detection
isPrevBullish = close[1] > open[1]
isPrevBearish = close[1] < open[1]
isBullishEngulfing = isPrevBearish and (close > open) and (close > high[1]) and (open < low[1])
isBearishEngulfing = isPrevBullish and (close < open) and (open > high[1]) and (close < low[1])

// RSI Confirmation:
// Bullish Engulfing: RSI below oversold level (or rising from it)
// Bearish Engulfing: RSI above overbought level (or falling from it)
rsiBullishConfirm = rsiValue < oversold
rsiBearishConfirm = rsiValue > overbought

// Combined signals
signalBullish = isBullishEngulfing and rsiBullishConfirm
signalBearish = isBearishEngulfing and rsiBearishConfirm

// Plot signals
plotshape(signalBullish, title="Bullish Engulfing (RSI Confirmed)", location=location.belowbar, color=color.new(color.aqua, 0), style=shape.arrowup, size=size.normal)
plotshape(signalBearish, title="Bearish Engulfing (RSI Confirmed)", location=location.abovebar, color=color.new(color.orange, 0), style=shape.arrowdown, size=size.normal)

// Plot RSI in a separate pane
plot(rsiValue, "RSI", color.blue, linewidth=2, display=display.pane)
hline(overbought, "Overbought", color.red)
hline(oversold, "Oversold", color.green)
    

Optimizing Engulfing Pattern Performance

To maximize the effectiveness of Engulfing patterns in your Pine Script strategies:

Common Engulfing Pattern Pitfalls to Avoid

Warning: Not all engulfing patterns are strong reversal signals. Avoid trading them in choppy or ranging markets without additional confirmation, as they can lead to false signals.

Conclusion

The Engulfing candlestick pattern is a foundational element of price action analysis and a powerful tool in a trader's arsenal. When properly understood and combined with other technical indicators and market context, it can provide high-probability signals for trend reversals. By implementing these patterns in Pine Script, you can automate their detection, integrate them into sophisticated trading strategies, and enhance your ability to read the market's pulse, ultimately improving your trading decisions.