Indicator · Pine Script v5 · On the chart

Candlestick Pattern - Engulfing

by harryguiacorn· code credited to FxCloudTrader

This indicator identifies bullish and bearish engulfing candlestick patterns and plots them directly on the price chart.

View on GitHub

GitHub stars219on the repository
Pine versionv5runs as is
Lines of code72
Settings7inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago8 May 2025

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

72 lines of Pine Script v5 from CandlestickEngulfing.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

CandlestickEngulfing.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © FxCloudTrader

//@version=5
indicator(title="Candlestick Pattern - Engulfing", shorttitle="Engulfing", overlay=true, precision = 0)
bool enableEngulfing = input.bool(true, '1. Enable Engulfing signal')

// Options for setting the minimum requirement of candlestick body to range percentage.
// It's less important for the left formation to have a fat body.
// However, for the right formation, namely the engulfing candle, 
// we want to see higher percentage of body to price range and small wicks
min_body_to_range_pct_lt   = input.int(0, 'Minimum body % for left bar',   minval=0, maxval=100, step=5, tooltip = 'Minimum body to price range percentage for the left formation')
min_body_to_range_pct_rt  = input.int(0, 'Minimum body % for right bar',  minval=0, maxval=100, step=5, tooltip = 'Minimum body to price range percentage for the right formation')

// Only show engulfing signal if the body of the engulfing candle is larger than 1 Average True Range(ATR)
// The idea is we prefer engulfing candles that show dominance and mean business.
filter_by_atr = input.bool(false, "Only display superior engulfing signals", "Only display engulfing candles whose body range is larger than 1 ATR")

display_body_to_range_label = input.bool(false, "Display body to price range percentage")
display_body_range_label = input.bool(false, "Display body of price range")

setting_engulfing_bar_close_beyond_prev_max = input.bool(true, "Engulfing Candle Closes Above/Below Prior Bar's High/Low")

getRange(x, y) => math.abs(x - y)

atr = ta.atr(14)

engulfing_cond_dn = open >= math.max(close[1],open[1])  and 
                     close <= math.min(close[1],open[1]) and 
                     open > close and 
                     getRange(open[1], close[1])/getRange(high[1], low[1]) * 100> min_body_to_range_pct_lt and
                     getRange(open, close)/getRange(high, low) * 100> min_body_to_range_pct_rt
con_dn = engulfing_cond_dn
if filter_by_atr and engulfing_cond_dn
    con_dn := getRange(open, close)>atr?true:false
engulfing_bar_close_below_prev_max = close < low[1]
if setting_engulfing_bar_close_beyond_prev_max and engulfing_cond_dn
    con_dn := engulfing_bar_close_below_prev_max ? true:false
if setting_engulfing_bar_close_beyond_prev_max and engulfing_cond_dn and filter_by_atr
    con_dn := engulfing_bar_close_below_prev_max ? true:false

engulfing_cond_up = open <= math.min(close[1],open[1]) and 
                     close >= math.max(close[1],open[1]) and 
                     open < close and 
                     getRange(open[1], close[1])/getRange(high[1], low[1]) * 100> min_body_to_range_pct_lt and
                     getRange(open, close)/getRange(high, low) * 100> min_body_to_range_pct_rt

con_up = engulfing_cond_up
if filter_by_atr and engulfing_cond_up
    con_up := getRange(open, close)>atr?true:false
engulfing_bar_close_above_prev_max = close > high[1]
if setting_engulfing_bar_close_beyond_prev_max and engulfing_cond_up
    con_up := engulfing_bar_close_above_prev_max ? true:false
if setting_engulfing_bar_close_beyond_prev_max and engulfing_cond_up and filter_by_atr
    con_up := engulfing_bar_close_above_prev_max ? true:false

// Display engulfing body percentage in comparison with its range (high and low)
if display_body_to_range_label and (engulfing_cond_dn or engulfing_cond_up)
    label.new(bar_index, engulfing_cond_dn?high:low, 
                 str.tostring(math.ceil(getRange(open, close)/getRange(high, low) * 100)) + "%", 
                 yloc = engulfing_cond_dn?yloc.abovebar:yloc.belowbar, 
                 style = engulfing_cond_dn?label.style_label_down:label.style_label_up)

// Display engulfing body in points
if display_body_range_label and (engulfing_cond_dn or engulfing_cond_up)
    label.new(bar_index, engulfing_cond_dn?high:low, 
                 "body:" + str.tostring(getRange(open, close)) + "\natr:" + str.tostring(atr, format.mintick), 
                 yloc = engulfing_cond_dn?yloc.abovebar:yloc.belowbar, 
                 style = engulfing_cond_dn?label.style_label_down:label.style_label_up)

plotshape(con_dn, "Bearish Engulfing Circle", shape.square, location.abovebar, color = color.new(color.blue, 50), size = size.auto, display = enableEngulfing?display.all:display.data_window)
plotshape(con_up, "Bullish Engulfing Circle", shape.square, location.belowbar, color = color.new(color.blue, 50), size = size.auto, display = enableEngulfing?display.all:display.data_window)

Overview

Users can filter patterns by setting minimum body-to-range percentages for both the prior and current candles. Additional filters require the engulfing bar to close beyond the prior candle's high or low, or require the candle body to exceed 14-period ATR. Optional labels display candlestick body range and body-to-range percentages.

Details

Published by

harryguiacorn on GitHub

Code credit

FxCloudTrader, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Candlestick patterns

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.

Topics

bollingerbollinger-bandbollinger-bandsichimokuichimoku-cloudichimoku-indicatorpinepinescriptpinescript-guidespinescript-indicators

Settings

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

SettingTypeDefaultVariable
1. Enable Engulfing signalbooltrueenableEngulfing
Minimum body % for left barint0min_body_to_range_pct_lt
Minimum body % for right barint0min_body_to_range_pct_rt
Only display superior engulfing signalsboolfalsefilter_by_atr
Display body to price range percentageboolfalsedisplay_body_to_range_label
Display body of price rangeboolfalsedisplay_body_range_label
Engulfing Candle Closes Above/Below Prior Bar's High/Lowbooltruesetting_engulfing_bar_close_beyond_prev_max
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

948 stars

The Heikin-Ashi Chart Identifier is an indicator that checks if the current chart uses Heikin-Ashi candles. It tracks matched candles where open equals the average of previous open and close, and verifies no gaps between bars. When conditions match, it display

Indicator v4 Candlestick patterns
Updated 10 days ago · GPL-3.0 GitHub repository of Heikin-Ashi Chart Identifier
575 stars

The indicator plots ICT displacement candles on the price chart and can switch to retracement scanning. Displacement requires a move larger than a set share of the 14-bar average high-low range, lasting at least a chosen number of bars, with optional close, pr

Indicator v6 SMC / ICT / price actionCandlestick patterns
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Displacement Candles Indicator

All Candlestick Patterns

by shunjizhan
292 stars

All Candlestick Patterns detects and highlights a wide range of bullish, bearish, and neutral candlestick patterns directly on the price chart. It features optional trend filtering using either a 50-period simple moving average or a combination of 50-period an

Indicator v4 Candlestick patternsTrend
Updated 3 years ago · None stated GitHub repository of All Candlestick Patterns
285 stars

Mean Reversion Study is an indicator that identifies trade setups combining exponential moving averages, RSI extremes, and engulfing candlestick patterns. It highlights bullish setups when a bullish engulfing candle forms below the EMA while RSI is oversold, a

Indicator v4 Mean reversionCandlestick patterns
Updated 3 years ago · None stated GitHub repository of Mean Reversion Study

[RS]MTF Candle Sticks

by ricardosantos79
261 stars

This indicator overlays up to five higher-timeframe candlestick groups on the price chart. For each enabled timeframe it requests open, high, low, and close and can show up to five candles, offset by a spacing setting. The main inputs set the candle count, the

Indicator v4 Candlestick patternsSupport & resistance Repaints
Updated 4 years ago · None stated GitHub repository of [RS]MTF Candle Sticks
257 stars

This indicator detects a large set of classic candlestick patterns and plots colored labels on the price chart. Trend context uses a 50-period simple moving average, or the 50 and 200 averages stacked together, and can be switched off. Users filter bullish, be

Indicator v4 Candlestick patternsTrend
Updated 6 weeks ago · None stated GitHub repository of Snag - All Candlestick Patterns

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