Pine Script v5 · Strategy
Buy&Sell Bullish Engulfing - The Quant Science
A Pine Script v5 strategy built on SMA (close, 50), EMA (C_Body, C_Len) and ATR (30). It has 3 order rules, 1 entry among them, and trades long only with the default settings. Exits use a profit target and no stop loss. Each trade commits 100% of equity and pays 0.07% commission. 11 settings; defaults include D: 1, M: 1 and Y: 2022.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does Buy&Sell Bullish Engulfing - The Quant Science repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Buy&Sell Bullish Engulfing - The Quant Science do on BTCUSDT 1D with default settings?
- Not measured
- It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings, so there is no result to report. Its code limits trading to a date window, which can leave this period out. It closed no trades on the other markets either. A backtest, not investment advice.
- Does Buy&Sell Bullish Engulfing - The Quant Science run in Pine Script v6?
- Not tested
- Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download.
- Does Buy&Sell Bullish Engulfing - The Quant Science use a stop loss?
- No
- Its exits set a profit target but no stop.
- Does Buy&Sell Bullish Engulfing - The Quant Science run in Zeal Terminal?
- Yes
- It compiles in 33 ms and runs 1,000 bars in 61 ms, in the browser.
BTCUSDT · 1D · default settings
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- 0.00%
- $0.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 0
- 0 won · 0 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 0.00%
- $0.00
One run with the author's default settings, $10,000 starting capital, commission as the script sets it. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
-
Check
No stop loss
strategy.exit is used without a stop, loss or trailing stop.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| START DATE BACKTESTING | |||
| D: D is Day, M is Month, Y is Year. | 1 | int · min 1 · max 31 | inDateRange |
| M: D is Day, M is Month, Y is Year. | 1 | int · min 1 · max 12 | inDateRange |
| Y: D is Day, M is Month, Y is Year. | 2022 | int · min 1800 · max 2100 | inDateRange |
| END DATE BACKTESTING | |||
| D: D is Day, M is Month, Y is Year. | 31 | int · min 1 · max 31 | inDateRange |
| M: D is Day, M is Month, Y is Year. | 12 | int · min 1 · max 12 | inDateRange |
| Y: D is Day, M is Month, Y is Year. | 2023 | int · min 1800 · max 2100 | inDateRange |
| TAKE PROFIT-STOP LOSS | |||
| Target profit (%): | 1 | float · min 0 · step 0.10 | c_exit |
| Stop Loss (%): | 1 | float · min 0 · step 0.10 | c_stopl |
| RISK MANAGEMENT | |||
| Orders size (%): Percentage size of each new order. With 'Reinvestment Profit' activate, the size will be calculate on the equity, with 'Reinvestment Profit' deactivate the size will be calculate on the initial capital. | 2 | float · min 0.10 · max 100 · step 0.10 | qty_order |
| BULLISH ENGULFING | |||
| Detect Trend Based On | trendRule1 | string · options [trendRule1, trendRule2, "No detection"] | line 45 line 49 |
| Label Color Bullish | color.rgb(43, 255, 0) | color | line 84 |
What it calculates
- SMAclose, 50
priceAvg · line 46 - SMAclose, 200
sma200 · line 50 - SMAclose, 50
sma50 · line 51 - EMAC_Body, C_Len
C_BodyAvg · line 62 - ATR30
patternLabelPosLow · line 77 - ATR30
patternLabelPosHigh · line 78
Signal flow
How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open LONG long | C_EngulfingBullish and strategy.opentrades==0 and inDateRange | On | 95 |
| Exit CLOSE-LONG limit | ta.crossover(strategy.equity, c_exit) | On | 97 |
| Exit CLOSE-LONG limit | ta.crossunder(strategy.equity, c_stopl) | On | 99 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
No
No repainting patterns in the source.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- ClearOrder timingprocess_orders_on_close = true: orders fill at the signal bar's close.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | 0.0% | +102.5% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | 0.0% | +5.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | 0.0% | +9.8% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | 0.0% | +8.4% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | 0.0% | +16.7% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | 0.0% | +21.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | 0.0% | +148.3% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | 0.0% | +17.1% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Similar scripts
Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Buy&Sell Bullish Engulfing - The Quant Science this page | — | v5 strategy | 0.0% | 0 | 0.0% |
| ATR with EOM and VORTEX by SoftKill21 | atr ema sma | v4 strategy | +66.2% | 22 | 29.9% |
| cATRpillar Strategy by Obi-wanClintobe | atr sma ema | v4 strategy | +1.02827e+24% | 27 | 88,097.2% |
| Optimized Zhaocaijinbao strategy by fzj20020403 | sma ema atr | v5 strategy | +15.6% | 12 | 9.8% |
| EMA & Supertrend by bhavikmota | ema sma atr | v4 strategy | +2,123.6% | 13 | 980.9% |
| Koala System EURUSD 15min by SoftKill21 | sma ema atr | v4 strategy | No trades on this market | ||
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- No v6-only features. v6 is not needed to run this script.
Clean v5
VerifiedNothing to change.
Does it run in Pine Script v6?
Not tested
Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯ -//@version=5+//@version=6strategy(⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 33 ms | With a workaround 1,000 bars in 61 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
Zeal Terminal's engine currently reads an order side written as direction = strategy.long as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 95) so the engine trades the way the script says.
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © thequantscience// ██████╗ ██╗ ██╗██╗ ██╗ ██╗███████╗██╗ ██╗ ███████╗███╗ ██╗ ██████╗ ██╗ ██╗██╗ ███████╗██╗███╗ ██╗ ██████╗ // ██╔══██╗██║ ██║██║ ██║ ██║██╔════╝██║ ██║ ██╔════╝████╗ ██║██╔════╝ ██║ ██║██║ ██╔════╝██║████╗ ██║██╔════╝ // ██████╔╝██║ ██║██║ ██║ ██║███████╗███████║ █████╗ ██╔██╗ ██║██║ ███╗██║ ██║██║ █████╗ ██║██╔██╗ ██║██║ ███╗// ██╔══██╗██║ ██║██║ ██║ ██║╚════██║██╔══██║ ██╔══╝ ██║╚██╗██║██║ ██║██║ ██║██║ ██╔══╝ ██║██║╚██╗██║██║ ██║// ██████╔╝╚██████╔╝███████╗███████╗██║███████║██║ ██║ ███████╗██║ ╚████║╚██████╔╝╚██████╔╝███████╗██║ ██║██║ ╚████║╚██████╔╝// ╚═════╝ ╚═════╝ ╚══════╝╚══════╝╚═╝╚══════╝╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝ ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ //@version=5strategy( "Buy&Sell Bullish Engulfing - The Quant Science", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, pyramiding = 1, currency = currency.EUR, initial_capital = 10000, commission_type = strategy.commission.percent, commission_value = 0.07, process_orders_on_close = true, close_entries_rule = "ANY" )startDate = input.int(title="D: ", defval=1, minval=1, maxval=31, inline = 'Start', group = "START DATE BACKTESTING", tooltip = "D is Day, M is Month, Y is Year.")startMonth = input.int(title="M: ", defval=1, minval=1, maxval=12, inline = 'Start', group = "START DATE BACKTESTING", tooltip = "D is Day, M is Month, Y is Year.")startYear = input.int(title="Y: ", defval=2022, minval=1800, maxval=2100, inline = 'Start', group = "START DATE BACKTESTING", tooltip = "D is Day, M is Month, Y is Year.")endDate = input.int(title="D: ", defval=31, minval=1, maxval=31, inline = 'End', group = "END DATE BACKTESTING", tooltip = "D is Day, M is Month, Y is Year.")endMonth = input.int(title="M: ", defval=12, minval=1, maxval=12, inline = 'End', group = "END DATE BACKTESTING", tooltip = "D is Day, M is Month, Y is Year.")endYear = input.int(title="Y: ", defval=2023, minval=1800, maxval=2100, inline = 'End', group = "END DATE BACKTESTING", tooltip = "D is Day, M is Month, Y is Year.")inDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))PROFIT = input.float(defval = 1, minval = 0, title = "Target profit (%): ", step = 0.10, group = "TAKE PROFIT-STOP LOSS")STOPLOSS = input.float(defval = 1, minval = 0, title = "Stop Loss (%): ", step = 0.10, group = "TAKE PROFIT-STOP LOSS")var float equity_trades = 0equity_trades := strategy.initial_capitalvar float equity = 0var float qty_order = 0t_ordersize = "Percentage size of each new order. With 'Reinvestment Profit' activate, the size will be calculate on the equity, with 'Reinvestment Profit' deactivate the size will be calculate on the initial capital."orders_size = input.float(defval = 2, title = "Orders size (%): ", minval = 0.10, step = 0.10, maxval = 100, group = "RISK MANAGEMENT", tooltip = t_ordersize)qty_order := ((equity_trades * orders_size) / 100 ) / close C_DownTrend = trueC_UpTrend = truevar trendRule1 = "SMA50"var trendRule2 = "SMA50, SMA200"var trendRule = input.string(trendRule1, "Detect Trend Based On", options=[trendRule1, trendRule2, "No detection"], group = "BULLISH ENGULFING")if trendRule == trendRule1 priceAvg = ta.sma(close, 50) C_DownTrend := close < priceAvg C_UpTrend := close > priceAvgif trendRule == trendRule2 sma200 = ta.sma(close, 200) sma50 = ta.sma(close, 50) C_DownTrend := close < sma50 and sma50 < sma200 C_UpTrend := close > sma50 and sma50 > sma200C_Len = 14C_ShadowPercent = 5.0 C_ShadowEqualsPercent = 100.0C_DojiBodyPercent = 5.0C_Factor = 2.0 C_BodyHi = math.max(close, open)C_BodyLo = math.min(close, open)C_Body = C_BodyHi - C_BodyLoC_BodyAvg = ta.ema(C_Body, C_Len)C_SmallBody = C_Body < C_BodyAvgC_LongBody = C_Body > C_BodyAvgC_UpShadow = high - C_BodyHiC_DnShadow = C_BodyLo - lowC_HasUpShadow = C_UpShadow > C_ShadowPercent / 100 * C_BodyC_HasDnShadow = C_DnShadow > C_ShadowPercent / 100 * C_BodyC_WhiteBody = open < closeC_BlackBody = open > closeC_Range = high-lowC_IsInsideBar = C_BodyHi[1] > C_BodyHi and C_BodyLo[1] < C_BodyLoC_BodyMiddle = C_Body / 2 + C_BodyLoC_ShadowEquals = C_UpShadow == C_DnShadow or (math.abs(C_UpShadow - C_DnShadow) / C_DnShadow * 100) < C_ShadowEqualsPercent and (math.abs(C_DnShadow - C_UpShadow) / C_UpShadow * 100) < C_ShadowEqualsPercentC_IsDojiBody = C_Range > 0 and C_Body <= C_Range * C_DojiBodyPercent / 100C_Doji = C_IsDojiBody and C_ShadowEqualspatternLabelPosLow = low - (ta.atr(30) * 0.6)patternLabelPosHigh = high + (ta.atr(30) * 0.6)label_color_bullish = input.color(color.rgb(43, 255, 0), title = "Label Color Bullish", group = "BULLISH ENGULFING")C_EngulfingBullishNumberOfCandles = 2C_EngulfingBullish = C_DownTrend and C_WhiteBody and C_LongBody and C_BlackBody[1] and C_SmallBody[1] and close >= open[1] and open <= close[1] and ( close > open[1] or open < close[1] )if C_EngulfingBullish var ttBullishEngulfing = "Engulfing\nAt the end of a given downward trend, there will most likely be a reversal pattern. To distinguish the first day, this candlestick pattern uses a small body, followed by a day where the candle body fully overtakes the body from the day before, and closes in the trend’s opposite direction. Although similar to the outside reversal chart pattern, it is not essential for this pattern to completely overtake the range (high to low), rather only the open and the close." label.new(bar_index, patternLabelPosLow, text="BE", style=label.style_label_up, color = label_color_bullish, textcolor=color.white, tooltip = ttBullishEngulfing)bgcolor(ta.highest(C_EngulfingBullish?1:0, C_EngulfingBullishNumberOfCandles)!=0 ? color.new(#21f321, 90) : na, offset=-(C_EngulfingBullishNumberOfCandles-1))var float c = 0var float o = 0var float c_exit = 0var float c_stopl = 0if C_EngulfingBullish and strategy.opentrades==0 and inDateRange c := strategy.equity o := close c_exit := c + (c * PROFIT / 100) c_stopl := c - (c * STOPLOSS / 100) strategy.entry(id = "LONG", direction = strategy.long, qty = qty_order, limit = o)if ta.crossover(strategy.equity, c_exit) strategy.exit(id = "CLOSE-LONG", from_entry = "LONG", limit = close)if ta.crossunder(strategy.equity, c_stopl) strategy.exit(id = "CLOSE-LONG", from_entry = "LONG", limit = close)