Logo
OFFLINEPIXEL
/ pinescript-strategies / pine-script-engulfing

$ 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.

500+ Clients Helped
100% Satisfaction
Live Trading Ready
⚠️
Trading financial markets carries risk. All content (PineScript code, indicators, strategies) on this website is for educational purposes only. Past performance is not indicative of future results. By using any code or information from this site, you agree that you are solely responsible for your trading decisions. The author disclaims all liability for any losses incurred. To gain from experts experiences, You can always try our Invite Only Scripts
01

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.
02

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.

03

Key Characteristic

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.

04

Basic Engulfing Pattern Detection in Pine Script

pine-script@terminal
//@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
isBullishEngulfing = isPrevBearish and (close > open) and (close > prevHigh) and (open < prevLow)

// Define Bearish Engulfing criteria
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!")
$ ✓ Compiled successfully
05

Advanced Engulfing Strategies

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)

// 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])

// Trend Confirmation
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
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
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
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)
06

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.
07

Warning

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.

08

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.
09

Conclusion

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.

Enhance Your Trading

Get a high-performance Pine Script analysis tool for actionable market insights, designed for traders on the move.

This strategy runs in live mode on TradingView, helping you identify potential opportunities.

Get Pine Script Strategy