Zeal Terminal Open in Zeal Terminal

Pine Script v5 · Strategy

Bollinger Bands + EMA 9

A Pine Script v5 strategy built on SMA or Stdev (close, 20) and EMA (close, 9). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade is one contract and pays no commission. 2 settings; defaults include Max Risk (%) 1 and Risk Type %.

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 Bollinger Bands + EMA 9 repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 9 of 9 trades.
How did Bollinger Bands + EMA 9 do on BTCUSDT 1D with default settings?
+68.67%
From 26 Jan 2024 to 21 Oct 2026, over 40 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Does Bollinger Bands + EMA 9 run in Pine Script v6?
No
The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 40 before, 3 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
Does Bollinger Bands + EMA 9 use a stop loss?
Yes
strategy.exit sets a stop on line 45, 47.
Does Bollinger Bands + EMA 9 run in Zeal Terminal?
Yes
It compiles in 31 ms and runs 1,000 bars in 58 ms, in the browser.

BTCUSDT · 1D · default settings

  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Winning trade
  • Losing trade
  • Equity
  • Buy and hold
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (2)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$1,687$2,025Equity from $1,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots.
Net profit
+68.67%
$686.71
Buy and hold
+102.5%
same period
Closed trades
40
21 won · 19 lost
Profitable
52.5%
of closed trades
Profit factor
1.34
gross profit ÷ gross loss
Max drawdown
55.42%
−$727.62

One run with the author's default settings, $1,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 commission

    The backtest assumes free trading. Results on any real exchange will be lower.

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

SettingDefaultType and rangeFeeds
Risk Management
Max Risk (%)1floatrisk
Risk Type%string · options ["%", "$"]risk

What it calculates

  • SMA / Stdevclose, 20bb_upper · line 10
  • SMA / Stdevclose, 20bb_lower · line 11
  • EMAclose, 9ema · line 12

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.

closecloseprice datahighhighprice datalowlowprice dataopenopenprice datariskTypeRisk Typesetting · %max_risk_percentageMax Risk (%)setting · 1bb_upperSMA / Stdev→ bb_upperbb_lowerSMA / Stdev→ bb_loweremaEMA→ emastopstopvalueentryentryvaluetptpvaluestop_sizestop_sizevalueorder_sizeorder_sizevaluebull_candlebull_candleconditionbear_candlebear_candleconditionriskriskconditionentry:LONGOpen LONGstrategy.entryentry:SHORTOpen SHORTstrategy.entryexit:LONGexit LONGstrategy.exitexit:SHORTexit SHORTstrategy.exitplot:49plotplotplot:50plotplotplot:51plotplotplot:53plotplotplot:54plotplotplot:55plotplotfill:56Fillfill × 4fill:57Fillfill × 4plot:58plotplotplot:59plotplotplot:60plotplotfill:61Fillfill × 4fill:62Fillfill × 4

Order rules

OrderFires whenBy defaultLine
Open LONG longclose[1] < bb_lower and bull_candle and strategy.position_size == 0On36
Open SHORT shortclose[1] > bb_upper and bear_candle and strategy.position_size == 0On43
Exit LONG stop and limitstrategy.position_size > 0On45
Exit SHORT stop and limitstrategy.position_size < 0On47

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, and the live replay matched history.

9/9trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+68.7%+102.5%4053%1.3455.4%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−82.1%+5.5%4854%0.75123.7%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−17.9%+9.8%3946%0.9163.1%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−125.0%+8.4%4549%0.62140.0%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−164.5%+16.7%5044%0.53163.3%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h−235.8%+21.5%4943%0.54189.9%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+116.6%+148.3%4562%1.7233.8%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+158.5%+17.1%4464%1.7346.0%8 May 2026 – 21 Oct 2026

Backtests are simulations on historical data and are not investment advice.

The trades

By market direction

At entry, last 90 barsTradesWonP&L
Rising market1747%−$68.79
Sideways market1560%$710.26
Falling market850%$45.23

Rising and falling mean the market moved more than 15% over the 90 bars before entry.

How bad could the drawdown be?

66.5%

The 95th percentile of the worst drawdown when the same 40 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 36.0% to 66.5%; the tick is what actually happened, 51.1%. Order luck alone moves it this much.

0%76%
All 40 trades
SideEntryExitBarsP&L
short29 Feb 2024 61,131.004 Mar 2024 64,000.004−$104.69
short5 Mar 2024 63,724.008 Mar 2024 68,499.003−$107.49
long14 Apr 2024 65,661.8017 Apr 2024 60,660.603−$92.38
long18 Apr 2024 63,470.1020 Apr 2024 64,828.702$33.69
long2 May 2024 59,060.603 May 2024 62,882.001$145.91
short16 May 2024 65,235.2017 May 2024 67,024.001−$150.70
short21 May 2024 70,148.3023 May 2024 67,026.302$232.80
long25 Jun 2024 61,806.001 Jul 2024 63,608.706$50.04
long6 Jul 2024 58,230.1013 Jul 2024 59,690.807$28.28
long6 Aug 2024 56,022.008 Aug 2024 60,190.002$51.92
short30 Oct 2024 72,344.701 Nov 2024 69,517.602$225.58
short12 Nov 2024 87,952.0013 Nov 2024 90,375.201−$156.26
short14 Nov 2024 87,325.6019 Nov 2024 93,265.605−$106.80
short18 Dec 2024 100,204.0019 Dec 2024 97,461.901$36.39
short7 Jan 2025 96,954.608 Jan 2025 95,060.601$36.23
short19 Jan 2025 101,332.0021 Jan 2025 104,989.002−$103.61
long3 Feb 2025 101,329.006 Feb 2025 96,150.003−$94.89
long27 Feb 2025 84,708.602 Mar 2025 90,739.103$238.77
long7 Apr 2025 79,163.208 Apr 2025 76,322.401−$137.79
long9 Apr 2025 82,615.2011 Apr 2025 81,220.702−$20.19
short9 May 2025 102,972.0010 May 2025 104,810.001−$158.34
short11 May 2025 104,118.0018 May 2025 104,985.007−$100.83
long6 Jun 2025 104,288.007 Jun 2025 105,552.001$31.06
short12 Jul 2025 117,420.0013 Jul 2025 119,087.001−$116.36
long3 Aug 2025 114,209.006 Aug 2025 115,579.003$60.93
short14 Aug 2025 118,295.0015 Aug 2025 117,342.001$18.54
long26 Aug 2025 111,763.0029 Aug 2025 109,274.003−$97.77
long26 Sep 2025 109,643.0029 Sep 2025 112,415.003$271.38
long5 Nov 2025 103,885.0011 Nov 2025 107,263.006$65.11
long15 Nov 2025 95,596.2017 Nov 2025 94,012.402−$98.34
long16 Dec 2025 87,863.4018 Dec 2025 85,146.602−$96.91
short6 Jan 2026 93,748.008 Jan 2026 90,979.902$268.83
short15 Jan 2026 95,604.8019 Jan 2026 93,432.104$95.94
long2 Feb 2026 78,738.604 Feb 2026 75,700.002−$96.14
long6 Feb 2026 70,580.3017 Mar 2026 75,315.8039$50.79
short17 Mar 2026 73,909.4018 Mar 2026 71,246.501$269.96
long26 Jun 2026 60,097.301 Jul 2026 58,115.005−$96.70
short22 Jul 2026 66,114.5024 Jul 2026 65,063.202$126.50
short22 Aug 2026 77,074.9024 Aug 2026 79,500.002−$103.15
short22 Sep 2026 86,208.608 Oct 2026 81,672.0016$387.42

Similar scripts

Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.

ScriptSharesTypeNetTradesMax drawdown
Bollinger Bands + EMA 9 this page—v5 strategy+68.7%4055.4%
Volume Weighted Bollinger Bands Strategy by Ankit_1618ema sma stdevv4 strategy+10.0%1527.5%
BB21_MA200_Strategy by mohaneeema sma stdevv4 strategyNo trades on this market
Super Breakout day trading by beststockalertstdev sma emav4 strategyNo trades on this market
Super Auto Breakout Day Trade Volatile stocks by beststockalertstdev sma emav4 strategy−6.1%5129.1%
VFI strategy [based on VFI indicator published by UTS] by mohaneesma stdev emav4 strategy+181.4%8154.2%

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

Verified

Nothing to change.

Does it run in Pine Script v6?

No

The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 40 before, 3 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.

⋯// -D4--//@version=5+//@version=6strategy("Bollinger Bands + EMA 9", overlay = true, initial_capital = 1000, process_orders_on_close=true, default_qty_type = strategy.fixed)⋯

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 31 msYes 1,000 bars in 58 msNot measured
PineTSNot testedNot testedNot measured

“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/// © D499// -D4-//@version=5strategy("Bollinger Bands + EMA 9", overlay = true, initial_capital = 1000, process_orders_on_close=true, default_qty_type = strategy.fixed)// Risk management inputsmax_risk_percentage = input.float(1, "Max Risk (%)", 0.01, 99, 0.01, group = "Risk Management")riskType = input.string("%", title="Risk Type", options = ["%", "$"], group = "Risk Management")// Calculate Indicatorsbb_upper = ta.sma(close, 20) + 2 * ta.stdev(close, 20)bb_lower = ta.sma(close, 20) - 2 * ta.stdev(close, 20)ema = ta.ema(close, 9)// Define bull_candle and bear_candle candlesbull_candle = close > openbear_candle = close < open// Check if entry signal is flashed and calculate order sizebarsSinceLastEntry() =>    strategy.opentrades > 0 ? bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1) : napercent2money(price, percent) =>    price * percent / 100 * syminfo.pointvaluecalcPositionSize(entry, stop_size) =>    risk = if riskType == "% from equity"        strategy.equity * max_risk_percentage / 100    else        max_risk_percentage    risk / percent2money(entry, stop_size)stop = 0.0entry = 0.0tp = 0.0if close[1] < bb_lower and bull_candle and strategy.position_size == 0     stop := low[1]    entry := close    tp := ema    stop_size = (entry - stop) / stop    order_size = calcPositionSize(close, stop_size)    strategy.entry("LONG", strategy.long, math.abs(order_size))if close[1] > bb_upper and bear_candle and strategy.position_size == 0    stop := high[1]    entry := close    tp := ema    stop_size = (stop - entry) / stop    order_size = calcPositionSize(close, stop_size)    strategy.entry("SHORT", strategy.short, math.abs(order_size))if strategy.position_size > 0    strategy.exit("LONG", limit = ema[barsSinceLastEntry()], stop = low[barsSinceLastEntry()+1], comment_profit = "✅", comment_loss = "❌")if strategy.position_size < 0     strategy.exit("SHORT", limit = ema[barsSinceLastEntry()], stop = high[barsSinceLastEntry()+1], comment_profit = "✅", comment_loss = "❌")// Plot Indicatorsplot(bb_upper, color=color.red)plot(bb_lower, color=color.green)plot(ema, color = color.white)// Plot trade visualizationlongEntryPlot = plot(strategy.position_size > 0  ? strategy.position_avg_price : na, color=color.new(color.white, 85), style=plot.style_linebr)longTargetPlot = plot(strategy.position_size > 0  ? ema[barsSinceLastEntry()] : na, color=color.new(color.green, 85), style=plot.style_linebr)longStopPlot = plot(strategy.position_size > 0  ? low[barsSinceLastEntry()+1] : na, color=color.new(color.red, 85), style=plot.style_linebr)fill(longEntryPlot, longTargetPlot, color=color.new(color.green, 85), editable=true)fill(longEntryPlot, longStopPlot, color=color.new(color.red, 85), editable=true)shortEntryPlot = plot(strategy.position_size < 0  ? strategy.position_avg_price : na, color=color.new(color.white, 85), style=plot.style_linebr)shortTargetPlot = plot(strategy.position_size < 0  ? ema[barsSinceLastEntry()] : na, color=color.new(color.green, 85), style=plot.style_linebr)shortStopPlot = plot(strategy.position_size < 0  ? high[barsSinceLastEntry()+1] : na, color=color.new(color.red, 85), style=plot.style_linebr)fill(shortEntryPlot, shortTargetPlot, color=color.new(color.green, 85), editable=true)fill(shortEntryPlot, shortStopPlot, color=color.new(color.red, 85), editable=true)