Indicator · Pine Script v5 · On the chart

Engulfing Patterns

by g-moe· code credited to goodgains

This indicator identifies price action reversal setups by combining candlestick patterns with Exponential Moving Average (EMA) trend filters.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code92
Settings10inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago27 May 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

92 lines of Pine Script v5 from Tradingview/engulfing-patterns-v2.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

engulfing-patterns-v2.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains


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

// @keywords: engulfing-patterns, ema-cloud, pin-bar, hammer-candle, bullish-engulfing, bearish-engulfing, buy-signal, sell-signal, ema-cross, wick-percentage, body-percentage, pattern-detection, shape-markers, bar-coloring, ema-filter, candle-patterns, reversal-signals, wait-between-signals, touch-ema, price-action

// Rules for the Indicator: 
// To give a sell signal, the following all rules must be true and for buy same

// Sell signal rules:
// -If 20EMA is above 10 EMA
// -If the price crosses the 20EMA or at least touch 20EMA
// - if the candle close below 20EMA 
// - the candle that touched 20EMA is a pin bar, hammer, or a bearish engulfer.

// Rules for the Indicator: 
// buy signal rules:
// -If 20EMA is below 10 EMA
// -If the price crosses the 20EMA or at least touch 20EMA
// - if the candle close above 20EMA 
// - the candle that touched 20EMA is a pin bar, hammer, or a bullish engulfer.


//EMAs
emalength_1 = input.int(10, title = 'EMA Length 1')
emalength_2 = input.int(20, title = 'EMA Length 2')

ema_1 = ta.ema(close, emalength_1)
ema_2 = ta.ema(close, emalength_2)

emaplot_1 = plot(ema_1, title = 'EMA 1', color = color.orange)
emaplot_2 = plot(ema_2, title = 'EMA 2', color = color.yellow)

fill(emaplot_1, emaplot_2, color = ema_1 >= ema_2 ? color.new(color.teal, 80) : color.new(color.red, 80), title = 'EMA Cloud')


//TYPES OF CANDLES

candle_size = high - low
upper_wick_size = high - math.max(open,close)
lower_wick_size = math.min(open,close) - low
body_size = math.max(open,close) - math.min(open,close)

upper_wick_pct = upper_wick_size / candle_size * 100
lower_wick_pct = lower_wick_size / candle_size * 100
body_size_pct = body_size / candle_size * 100



//PIN BAR
use_pin_bars = input.bool(true, title = 'Use Pin Bars', group = 'Pin Bar Candles')
pin_bar_wick = input.float(40.0, title = 'Pin Bar Wick Size %', group = 'Pin Bar Candles', minval = 1.00, maxval = 99.0)
pin_bar_body = input.float(20.0, title = 'Pin Bar Body Size %', group = 'Pin Bar Candles', minval = 1.00, maxval = 99.0)


bullish_pin_bar = use_pin_bars and (lower_wick_pct >= pin_bar_wick) and (body_size_pct <= pin_bar_body)
bearish_pin_bar = use_pin_bars and (upper_wick_pct >= pin_bar_wick) and (body_size_pct <= pin_bar_body)


//HAMMER
use_hammers = input.bool(true, title = 'Use Hammers', group = 'Hammer Candles')
hammer_must_close = input.float(60.0, title = 'Hammer Body Area %', group = 'Hammer Candles', minval = 1.00, maxval = 99.0)

bullish_hammer = use_hammers and (math.min(close,open) >= (hammer_must_close / 100 * candle_size + low))
bearish_hammer = use_hammers and (math.max(close,open) <= (high - hammer_must_close / 100 * candle_size))


//ENGULFING
use_engulfing = input.bool(true, title = 'Use Engulfing', group = 'Engulfing Candles')

bullishEngulfing = use_engulfing and (open <= close[1]) and (open < open[1]) and (close > open[1])
bearishEngulfing = use_engulfing and (open >= close[1]) and (open > open[1]) and (close < open[1])


//PLOT SPECIAL CANDLES
plotshape(bullish_pin_bar or bullish_hammer or bullishEngulfing ? low : na, location = location.absolute, color = color.teal, style = shape.xcross)
plotshape(bearish_pin_bar or bearish_hammer or bearishEngulfing ? high : na ,location = location.absolute, color = color.red, style = shape.xcross)


//SIGNALS
color_candles = input.bool(false, title = 'Color Candles Based on MAs', group = 'Strategy Settings')
wait_between_signals = input.int(1, title = 'Bars Between Signals', group = 'Strategy Settings')
buy_signal = (ema_2 < ema_1) and (close > ema_2) and (high >= ema_2 and low <= ema_2) and (bullish_pin_bar or bullish_hammer or bullishEngulfing) ? 1 : 0 
sell_signal = (ema_2 > ema_1) and (close < ema_2) and (high >= ema_2 and low <= ema_2) and (bearish_pin_bar or bearish_hammer or bearishEngulfing) ? 1 : 0

plotshape(math.sum(buy_signal[1],wait_between_signals ) == 0  and buy_signal ? true : false, location = location.belowbar, color = color.teal, style = shape.labelup, textcolor = color.white, text= 'BUY')
plotshape(math.sum(sell_signal[1],wait_between_signals ) == 0  and sell_signal ? true : false, location = location.abovebar, color = color.red, style = shape.labeldown, textcolor = color.white, text= 'SELL')

barcolor(color_candles and ema_1 >= ema_2 ? color.teal : color_candles ? color.red : na )

Overview

Engulfing Patterns calculates two customizable EMAs (default 10 and 20 periods) and fills the area between them to form an EMA cloud. It detects bullish and bearish pin bars, hammers, and engulfing candles based on user-defined wick and body size percentages. Buy and sell signals trigger directly on the price chart when a valid candlestick pattern interacts with or crosses the 20 EMA in alignment with the broader moving average trend.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Candlestick patterns, Moving averages, Trend

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
EMA Length 1int10emalength_1
EMA Length 2int20emalength_2
Use Pin Barsbooltrueuse_pin_bars
Pin Bar Wick Size %float40.0pin_bar_wick
Pin Bar Body Size %float20.0pin_bar_body
Use Hammersbooltrueuse_hammers
Hammer Body Area %float60.0hammer_must_close
Use Engulfingbooltrueuse_engulfing
Color Candles Based on MAsboolfalsecolor_candles
Bars Between Signalsint1wait_between_signals
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

55 stars

This indicator overlays on the price chart and provides trend and momentum analysis. It calculates and plots three exponential moving averages with fixed periods of 20, 50, and 200 using yellow, orange, and blue colors respectively. The script also computes th

Indicator v6 TrendOscillators & momentum
Updated 6 months ago · Apache-2.0 GitHub repository of Trend & Momentum Analysis

Hanging Man - Bearish

by TWODS-CAPITAL
37 stars

This overlay indicator marks a bearish hanging man candlestick on the price chart. It looks for a small real body sitting in the upper part of the range, a lower shadow at least twice the body, and no upper shadow, and only while an uptrend filter is active. T

Indicator v5 Candlestick patternsTrend
Updated 3 years ago · MIT GitHub repository of Hanging Man - Bearish
29 stars

Zen EMA Pullback Patterns identifies pullback formations relative to an Exponential Moving Average on the price chart. It detects 3-bar pullbacks consisting of three consecutive bearish bars above the EMA or bullish bars below it, as well as 4-bar micro channe

Indicator v6 Moving averagesCandlestick patterns
Updated 2 weeks ago · MIT GitHub repository of Zen EMA Pullback Patterns
15 stars

This indicator plots Bollinger Bands and smoothed HAMA candles on the price chart. It builds each candle from separate moving averages of open, high, low and close, using selectable EMA, SMA or WMA lengths. A simple moving average forms the band basis, and an

Indicator v5 Candlestick patternsMoving averages
Updated 4 months ago · Apache-2.0 GitHub repository of NSDT HAMA Candles with Bollinger Bands

Calculates adaptive arithmetic candles by blending price data with Kaufman Efficiency Ratio smoothing and a linear regression anchor. It measures trend health using a zero-to-hundred Quality Score derived from market efficiency, regression slope, and normalize

Indicator v6 TrendCandlestick patterns
Updated yesterday · MIT GitHub repository of Adaptive Arithmetic Candles + Quality Score [WavesUnchained]

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers