SuperTrend Engulfing Strategy

Author: ChaoZhang, Date: 2023-12-08 15:40:26



The SuperTrend Engulfing strategy is a trend following strategy that combines Average True Range (ATR), SuperTrend indicator and engulfing patterns to identify trend direction and find good risk-reward ratio entry opportunities when engulfing patterns confirm the trend.

Strategy Logic

The strategy first uses ATR and SuperTrend indicator to determine the CURRENT market trend direction. Specifically, a downtrend is defined when price is below the upper band, and an uptrend when price is above the lower band.

At the same time, the strategy also judges whether the K-line forms an engulfing pattern. According to the code logic, in an uptrend, if the previous bar’s closing price is higher than the current bar’s opening price, while the current bar’s closing price is lower than the opening price, a bullish engulfing will be triggered. In a downtrend, if the previous bar’s closing price is lower than the current bar’s opening price, while the current bar’s closing price is higher than the opening price, a bearish engulfing will be triggered.

When the engulfing pattern is consistent with the trend direction, a trading signal will be generated. In addition, the strategy will also calculate the stop loss and take profit levels based on the engulfing pattern. After entering the market, if the price touches the stop loss or take profit level, the current position will be exited.

Advantage Analysis

The strategy combines the advantages of trend following and pattern recognition to identify reversal signals in trending markets, thus catching larger moves at turning points. Also, the stop loss mechanism can effectively control the risk of losses.

Risk Analysis

The biggest risk of this strategy is that engulfing patterns may be fake breaks, thus generating wrong signals. In addition, stop loss and take profit settings could also be too arbitrary, failing to achieve balanced profits and losses. It is recommended to optimize parameter combinations and adjust stop loss and take profit levels appropriately.

Optimization Directions

Consider optimizing ATR parameters in real time for better capturing changes in market volatility. Also, researching other indicators to identify trends can further improve the stability of the strategy. From the perspective of stop loss and take profit, dynamic trailing is also a feasible optimization direction.


The SuperTrend Engulfing strategy integrates the advantages of trend following and pattern recognition, using engulfing patterns as reversal signals. It can obtain higher returns at market turning points. But the strategy also has certain risks of fake signals. Further testing and optimization are needed to control risks.

start: 2023-11-07 00:00:00
end: 2023-12-07 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]

// This source code is subject to the terms of the Mozilla Public License 2.0 at
// © Armanhammer

strategy("Engulfing with Trend", overlay=true)

Periods ="ATR Period", defval=10)
src = input(hl2, title="Source")
Multiplier = input.float(title="ATR Multiplier", step=0.1, defval=3.0)
changeATR= input.bool(title="Change ATR Calculation Method ?", defval=true)
showsignals = input.bool(title="Show Buy/Sell Signals ?", defval=true)
highlighting = input.bool(title="Highlighter On/Off ?", defval=true)

atr2 = ta.sma(src, Periods)
atr= changeATR ? ta.atr(Periods) : atr2

up1 = nz(up[1],up)
up := close[1] > up1 ? math.max(up,up1) : up
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn

var trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend

upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2,
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal and showsignals ? up : na, title="Buy", style=shape.labelup, location=location.absolute,, 0), text="Buy")
//plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute,, size=size.tiny,, transp=0)
//plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny,, textcolor=color.white, transp=0)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2,
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal and showsignals ? dn : na, title="Sell", style=shape.labeldown, location=location.absolute,, 0), text="Sell")
//plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute,, size=size.tiny,, transp=0)
//plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny,, textcolor=color.white, transp=0)
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = highlighting and trend == 1 ?, 0) : na
shortFillColor = highlighting and trend == -1 ?, 0) : na
fill(upPlot, dnPlot, color=longFillColor)
fill(dnPlot, upPlot, color=shortFillColor)
alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")
changeCond = trend != trend[1]
alertcondition(changeCond, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")

// Define Downtrend and Uptrend conditions
downtrend = trend == -1
uptrend = trend == 1

// Engulfing
boringThreshold = input.float(25, title="Boring Candle Threshold (%)", minval=1, maxval=100)
engulfingThreshold = input.float(50, title="Engulfing Candle Threshold (%)", minval=1, maxval=100)
stopLevel =, title="Stop Level (Pips)", minval=1)

// Boring Candle (Inside Bar) and Engulfing Candlestick Conditions
isBoringCandle = math.abs(open[1] - close[1]) * 100 / math.abs(high[1] - low[1]) <= boringThreshold
isEngulfingCandle = math.abs(open - close) * 100 / math.abs(high - low) <= engulfingThreshold

// Bullish and Bearish Engulfing Conditions
bullEngulfing = uptrend and close[1] < open[1] and close > open[1] and not isBoringCandle and not isEngulfingCandle
bearEngulfing = downtrend and close[1] > open[1] and close < open[1] and not isBoringCandle and not isEngulfingCandle

// Stop Loss, Take Profit, and Entry Price Calculation
bullStop = close + (stopLevel * syminfo.mintick)
bearStop = close - (stopLevel * syminfo.mintick)
bullSL = low 
bearSL = high
bullTP = bullStop + (bullStop - low)
bearTP = bearStop - (high - bearStop)

// Entry Conditions
enterLong = bullEngulfing and uptrend
enterShort = bearEngulfing and downtrend

// Exit Conditions
exitLong = ta.crossover(close, bullTP) or ta.crossover(close, bullSL)
exitShort = ta.crossover(close, bearTP) or ta.crossover(close, bearSL)

// Check if exit conditions are met by the next candle
exitLongNextCandle = exitLong and (ta.crossover(close[1], bullTP[1]) or ta.crossover(close[1], bullSL[1]))
exitShortNextCandle = exitShort and (ta.crossover(close[1], bearTP[1]) or ta.crossover(close[1], bearSL[1]))

// Strategy Execution
if enterLong
    strategy.entry("Buy", strategy.long)

if enterShort
    strategy.entry("Sell", strategy.short)

// Exit Conditions for Long (Buy) Positions
if bullEngulfing and not na(bullTP) and not na(bullSL)
    strategy.exit("Exit Long", from_entry="Buy", stop=bullSL, limit=bullTP)

// Exit Conditions for Short (Sell) Positions
if bearEngulfing and not na(bearTP) and not na(bearSL)
    strategy.exit("Exit Short", from_entry="Sell", stop=bearSL, limit=bearTP)

// Plot Shapes and Labels
plotshape(series=bullEngulfing, style=shape.triangleup, location=location.abovebar,
plotshape(series=bearEngulfing, style=shape.triangledown, location=location.abovebar,

// Determine OP, SL, and TP
plot(series=bullEngulfing ? bullStop : na, title="Bullish Engulfing stop",, linewidth=3, style=plot.style_linebr)
plot(series=bearEngulfing ? bearStop : na, title="Bearish Engulfing stop",, linewidth=3, style=plot.style_linebr)
plot(series=bullEngulfing ? bullSL : na, title="Bullish Engulfing SL",, linewidth=3, style=plot.style_linebr)
plot(series=bearEngulfing ? bearSL : na, title="Bearish Engulfing SL",, linewidth=3, style=plot.style_linebr)
plot(series=bullEngulfing ? bullTP : na, title="Bullish Engulfing TP",, linewidth=3, style=plot.style_linebr)
plot(series=bearEngulfing ? bearTP : na, title="Bearish Engulfing TP",, linewidth=3, style=plot.style_linebr)

// Create labels if the condition for bullEngulfing or bearEngulfing is met
//if bullEngulfing
   //, y=bullSL, text="SL: " + str.tostring(bullSL),, textcolor=color.white, style=label.style_labelup, size=size.tiny)

//if bearEngulfing
   //, y=bearSL, text="SL: " + str.tostring(bearSL),, textcolor=color.white, style=label.style_labeldown, size=size.tiny)

//if bullEngulfing
  //, y=bullTP, text="TP: " + str.tostring(bullTP),, textcolor=color.white, style=label.style_labeldown, size=size.tiny)

//if bearEngulfing
  //, y=bearTP, text="TP: " + str.tostring(bearTP),, textcolor=color.white, style=label.style_labelup, size=size.tiny)