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:
- Identifying potential trend reversals (top for bearish, bottom for bullish).
- Confirming existing trend strength or weakness.
- Generating entry and exit signals in trading strategies.
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.
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:
- Context is King: Always consider the larger trend. Bullish engulfing patterns are strongest after a downtrend, and bearish engulfing patterns are strongest after an uptrend.
- Volume Confirmation: Look for higher volume on the engulfing candle compared to the previous one, indicating stronger conviction behind the move.
- Support & Resistance: Engulfing patterns that occur at significant support or resistance levels tend to be more reliable.
- Timeframe: While visible on all timeframes, signals on higher timeframes (e.g., daily, weekly) generally carry more weight than those on lower timeframes.
- Confirmation from Other Indicators: Combine with momentum oscillators (RSI, Stochastic), trend indicators (Moving Averages, ADX), or volatility tools (Bollinger Bands) for higher probability trades.
Common Engulfing Pattern Pitfalls to Avoid
- Ignoring Trend: An engulfing pattern against the primary trend is usually a weaker signal.
- Small Candles: Patterns involving very small candles (dojis, spinning tops) might not carry enough conviction.
- Lack of Volume: A weak volume on the engulfing candle can indicate a lack of true buying/selling pressure.
- Over-reliance: Using engulfing patterns alone without other forms of analysis (e.g., support/resistance, market structure) can lead to whipsaws.
- Mistaking Gaps: Ensure the engulfing candle truly opens below/above the previous candle's close, not just engulfs the body due to a large gap.
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.