Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

Trend #2 - BB+EMA

A Pine Script v4 strategy built on EMA (close, 100), Stdev (close, 100) and SAR (start*0.01, increment*0.01). It has 6 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss. Each trade is one contract and pays no commission. 13 settings; defaults include Length 100, StdDev 1.5 and lot 1.

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 Trend #2 - BB+EMA repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 2 of 2 trades.
How did Trend #2 - BB+EMA do on BTCUSDT 1D with default settings?
−0.58%
From 26 Jan 2024 to 21 Oct 2026, over 14 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can Trend #2 - BB+EMA be converted to Pine Script v5?
Yes
The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
Does Trend #2 - BB+EMA use a stop loss?
Yes
strategy.exit sets a stop on line 47, 49.
Does Trend #2 - BB+EMA run in Zeal Terminal?
Yes
It compiles in 20 ms and runs 1,000 bars in 46 ms, in the browser.

BTCUSDT · 1D · default settings

  • Basis
  • Upper
  • Lower
  • EMA
  • EMA
  • 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 (13)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$99,974$202,527Equity from $100,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
−0.58%
−$578.46
Buy and hold
+102.5%
same period
Closed trades
14
5 won · 9 lost
Profitable
35.7%
of closed trades
Profit factor
0.91
gross profit ÷ gross loss
Max drawdown
3.94%
−$3,942.00

One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A long position was still open at the last bar. 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

    1 setting has no label

    In the settings dialog length (line 7) shows an empty name. The labels on this page come from the variable names.

  • Note

    No slippage

    Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.

Upgrade and export

Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.

  • Upgrading to v5 renames ema → ta.ema, stdev → ta.stdev, sar → ta.sar, max → math.max, min → math.min.

v4 → v5

Verified

Same 14 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 3: //@version=4 → //@version=5
  • Line 5: input(type = input.time) → input.time()
  • Line 6: input(type = input.time) → input.time()
  • Line 10: ema → ta.ema
  • Line 11: stdev → ta.stdev
  • Line 15: input(type = input.bool) → input.bool()
13 more edits
  • Line 18: input(type = input.bool) → input.bool()
  • Line 20: input(type = input.bool) → input.bool()
  • Line 27: ema → ta.ema
  • Line 28: sar → ta.sar
  • Line 29: sar → ta.sar
  • Line 41: max → math.max
  • Line 41: max → math.max
  • Line 45: min → math.min
  • Line 45: min → math.min
  • Line 47: max → math.max
  • Line 47: max → math.max
  • Line 49: min → math.min
  • Line 49: min → math.min
⋯// © zxcv55602-//@version=4+//@version=5strategy(shorttitle="Trend #2 - BB+EMA v4", title="Bollinger Bands", overlay=true)-date1 = input(title="Start Date", type=input.time, defval=timestamp("2000-01-01T00:00:00"))+date1 = input.time(title="Start Date", defval=timestamp("2000-01-01T00:00:00"))-date2 = input(title="Stop Date", type=input.time, defval=timestamp("2030-01-01T00:00:00"))+date2 = input.time(title="Stop Date", defval=timestamp("2030-01-01T00:00:00"))length = input(100, minval=1,step=10)⋯//-------------------basis = ema(close, length)+basis = ta.ema(close, length)-dev = mult * stdev(close, length)+dev = mult * ta.stdev(close, length)upper = basis + dev⋯//-------------------stopcon=input(title="stopcon/lot", type=input.bool, defval=true)+stopcon=input.bool(title="stopcon/lot", defval=true)lot1=input(title="lot",defval=1)stoploss=input(title="stoploss",defval=1000)-emacon=input(title="EMA", type=input.bool, defval=true)+emacon=input.bool(title="EMA", defval=true)ema_value=input(title="EMA value",defval=100, minval=2,step=1)-sarcon=input(title="SAR boost", type=input.bool, defval=true)+sarcon=input.bool(title="SAR boost", defval=true)start = input(0.1, minval=0, maxval=10, title="Start",step=0.1)⋯p2 = plot(lower, "Lower", color=color.new(color.blue,30))-ema1=ema(close,ema_value)+ema1=ta.ema(close,ema_value)-sarUp = sar(start*0.01, increment*0.01, maximum*0.1)+sarUp = ta.sar(start*0.01, increment*0.01, maximum*0.1)-sarDown = sar(start*0.01, increment*0.01, maximum*0.1)+sarDown = ta.sar(start*0.01, increment*0.01, maximum*0.1)var ema2=0.0⋯        lot_L=stoploss/(close-basis)-        strategy.entry("long",strategy.long,qty=stopcon?lot_L:lot1,stop=emacon?sarcon?max(basis,ema1,sarUp):max(basis,ema1):basis)+        strategy.entry("long",strategy.long,qty=stopcon?lot_L:lot1,stop=emacon?sarcon?math.max(basis,ema1,sarUp):math.max(basis,ema1):basis)    if ema1>lower and close<lower⋯        lot_S=stoploss/(basis-close)-        strategy.entry("short",strategy.short,qty=stopcon?lot_S:lot1,stop=emacon?sarcon?min(basis,ema1,sarDown):min(basis,ema1):basis)+        strategy.entry("short",strategy.short,qty=stopcon?lot_S:lot1,stop=emacon?sarcon?math.min(basis,ema1,sarDown):math.min(basis,ema1):basis)if strategy.position_size>0-    strategy.exit("long",from_entry="long",stop=emacon?sarcon?max(basis,ema1,sarUp):max(basis,ema1):basis,comment="close_L")+    strategy.exit("long",from_entry="long",stop=emacon?sarcon?math.max(basis,ema1,sarUp):math.max(basis,ema1):basis,comment="close_L")if strategy.position_size<0-    strategy.exit("short",from_entry="short",stop=emacon?sarcon?min(basis,ema1,sarDown):min(basis,ema1):basis,comment="close_S")+    strategy.exit("short",from_entry="short",stop=emacon?sarcon?math.min(basis,ema1,sarDown):math.min(basis,ema1):basis,comment="close_S")

Clean v5

VerifiedDownload the v5 script

Same 14 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 7: input() → input.int()
  • Line 8: input() → input.float()
  • Line 16: input() → input.int()
  • Line 17: input() → input.int()
  • Line 19: input() → input.int()
  • Line 21: input() → input.float()
  • Line 22: input() → input.float()
  • Line 23: input() → input.int()
⋯date2 = input.time(title="Stop Date", defval=timestamp("2030-01-01T00:00:00"))-length = input(100, minval=1,step=10)+length = input.int(100, minval=1,step=10)-mult = input(1.5,title="StdDev",step=0.1)+mult = input.float(1.5,title="StdDev",step=0.1)//------------------⋯stopcon=input.bool(title="stopcon/lot", defval=true)-lot1=input(title="lot",defval=1)+lot1=input.int(title="lot",defval=1)-stoploss=input(title="stoploss",defval=1000)+stoploss=input.int(title="stoploss",defval=1000)emacon=input.bool(title="EMA", defval=true)-ema_value=input(title="EMA value",defval=100, minval=2,step=1)+ema_value=input.int(title="EMA value",defval=100, minval=2,step=1)sarcon=input.bool(title="SAR boost", defval=true)-start = input(0.1, minval=0, maxval=10, title="Start",step=0.1)+start = input.float(0.1, minval=0, maxval=10, title="Start",step=0.1)-increment = input(0.3, minval=0, maxval=10, title="Step Setting" ,step=0.1)+increment = input.float(0.3, minval=0, maxval=10, title="Step Setting" ,step=0.1)-maximum = input(1, minval=1, maxval=10, title="Maximum Step",step=0.1)+maximum = input.int(1, minval=1, maxval=10, title="Maximum Step",step=0.1)plot(basis, "Basis", color=sarcon?na:color.new(#FF6D00,50))⋯

Does it run in Pine Script v6?

Yes

Same 14 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.

Download the v6 script

⋯// © zxcv55602-//@version=5+//@version=6strategy(shorttitle="Trend #2 - BB+EMA v4", title="Bollinger Bands", overlay=true)⋯

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
Start Datetimestamp("2000-01-01T00:00:00")timeline 37
Stop Datetimestamp("2030-01-01T00:00:00")timeline 37
Length (no label in the script)100int · min 1 · step 10basis dev
StdDev1.5float · step 0.1dev
stopcon/lottrueboolentry long entry short
lot1intentry long entry short
stoploss1000intlot_L lot_S
EMAtrueboolEMA entry long entry short exit long exit short
EMA value100int · min 2 · step 1ema1
SAR boosttrueboolBasis EMA entry long entry short exit long exit short
Start0.1float · min 0 · max 10 · step 0.1sarUp sarDown
Step Setting0.3float · min 0 · max 10 · step 0.1sarUp sarDown
Maximum Step1int · min 1 · max 10 · step 0.1sarUp sarDown

What it calculates

  • EMA (ema, ta.ema in v5)close, Length (100)basis · line 10
  • Stdev (stdev, ta.stdev in v5)close, Length (100)dev · line 11
  • EMA (ema, ta.ema in v5)close, EMA value (100)ema1 · line 27
  • SAR (sar, ta.sar in v5)start*0.01, increment*0.01, maximum*0.1sarUp · line 28
  • SAR (sar, ta.sar in v5)start*0.01, increment*0.01, maximum*0.1sarDown · line 29

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 datalengthLengthsetting · 100multStdDevsetting · 1.5ema_valueEMA valuesetting · 100high · low · closehigh · low · closeprice datastartStartsetting · 0.1incrementStep Settingsetting · 0.3maximumMaximum Stepsetting · 1stoplossstoplosssetting · 1000basisEMA→ basisdevStdev→ devema1EMA→ ema1sarUpSAR→ sarUpsarDownSAR→ sarDownupper, lowerupper · lower2 valuesema2ema2valuelot_L, lot_Slot_L · lot_S2 valuescancel:shortcancel shortstrategy.cancelentry:longOpen longstrategy.entrycancel:longcancel longstrategy.cancelentry:shortOpen shortstrategy.entryexit:longexit longstrategy.exitexit:shortexit shortstrategy.exitplot:24Basisplotplot:25Upperplotplot:26Lowerplotplot:35EMAplotplot:36EMAplot

Order rules

OrderFires whenBy defaultLine
cancel shortema1<upper and close>upperOn39
Open long longema1<upper and close>upperOn41
cancel longema1>lower and close<lowerOn43
Open short shortema1>lower and close<lowerOn45
Exit long stopstrategy.position_size>0On47
Exit short stopstrategy.position_size<0On49

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.

2/2trades 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 timingOrders fill at the next bar's open, after the signal bar has closed.

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−0.6%+102.5%1436%0.913.9%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+1.5%+5.5%1233%1.262.9%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+5.3%+9.8%978%8.520.8%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+3.8%+8.4%1145%2.072.7%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+0.7%+16.7%1164%1.192.6%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+17.0%+21.5%1050%5.892.4%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−2.5%+148.3%1540%0.665.1%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+2.7%+17.1%1242%1.702.9%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 market20%−$1,143.99
Sideways market944%$2,106.10
Falling market333%−$1,540.57

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

How bad could the drawdown be?

6.1%

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

0%7%
All 14 trades
SideEntryExitBarsP&L
long21 May 2024 71,446.6018 Jun 2024 64,334.7028−$700.54
short5 Jul 2024 57,050.0015 Jul 2024 62,633.2010−$841.98
short6 Aug 2024 54,018.8023 Aug 2024 62,145.2017−$873.54
short7 Sep 2024 53,963.0017 Sep 2024 60,754.9010−$911.89
long18 Oct 2024 68,595.3020 Dec 2024 94,305.4063$4,733.54
short27 Feb 2025 84,250.102 Mar 2025 93,279.803−$946.65
short4 Mar 2025 86,221.2021 Apr 2025 87,382.4048−$167.36
long9 May 2025 103,262.0020 Jun 2025 103,371.0042$8.10
long12 Jul 2025 117,528.0024 Aug 2025 110,772.0043−$443.46
long2 Oct 2025 118,595.0010 Oct 2025 113,529.008−$765.71
short18 Oct 2025 106,432.0021 Oct 2025 112,958.003−$962.17
short5 Nov 2025 101,497.005 Jan 2026 92,087.8061$886.81
short1 Feb 2026 78,741.1017 Mar 2026 75,484.1044$213.29
short3 Jun 2026 66,760.808 Aug 2026 64,923.2066$193.08

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
Trend #2 - BB+EMA this page—v4 strategy−0.6%143.9%
KAMA Strategy - Kaufman's Adaptive Moving Average by BenHampsonstdev ema sarv5 strategy+51.1%2034.6%
Escaping of Rate from Avarage By Mustafa OZVER Strategy by Mustafaozverstdev emav4 strategy+122.0%9679.1%
Bollinger Band with Fib Golden Ratio (0.618) by mohaneestdev emav4 strategyNo trades on this market
Volume Weighted Bollinger Bands Strategy by Ankit_1618ema stdevv4 strategy+10.0%1527.5%
BB21_MA200_Strategy by mohaneeema stdevv4 strategyNo trades on this market

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 20 msYes 1,000 bars in 46 msNot measured
PineTSNo v5 and v6 only—Not 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. PineTS runs Pine Script v5 and v6 only.

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/// © zxcv55602//@version=4strategy(shorttitle="Trend #2 - BB+EMA v4", title="Bollinger Bands", overlay=true)date1 = input(title="Start Date", type=input.time, defval=timestamp("2000-01-01T00:00:00"))date2 = input(title="Stop Date", type=input.time, defval=timestamp("2030-01-01T00:00:00"))length = input(100, minval=1,step=10)mult = input(1.5,title="StdDev",step=0.1)//------------------basis = ema(close, length)dev = mult * stdev(close, length)upper = basis + devlower = basis - dev//------------------stopcon=input(title="stopcon/lot", type=input.bool, defval=true)lot1=input(title="lot",defval=1)stoploss=input(title="stoploss",defval=1000)emacon=input(title="EMA", type=input.bool, defval=true)ema_value=input(title="EMA value",defval=100, minval=2,step=1)sarcon=input(title="SAR boost", type=input.bool, defval=true)start = input(0.1, minval=0, maxval=10, title="Start",step=0.1)increment = input(0.3, minval=0, maxval=10, title="Step Setting" ,step=0.1)maximum = input(1, minval=1, maxval=10, title="Maximum Step",step=0.1)plot(basis, "Basis", color=sarcon?na:color.new(#FF6D00,50))p1 = plot(upper, "Upper", color=color.new(color.blue,30))p2 = plot(lower, "Lower", color=color.new(color.blue,30))ema1=ema(close,ema_value)sarUp = sar(start*0.01, increment*0.01, maximum*0.1)sarDown = sar(start*0.01, increment*0.01, maximum*0.1)var ema2=0.0if strategy.position_size>0    ema2:=ema1>sarUp?ema1:(close<sarUp?na:sarUp)else    ema2:=ema1>sarDown?(close>sarDown?na:sarDown):ema1plot(sarcon ? ema2 : na, title="EMA", color=color.red,style=plot.style_stepline)plot(emacon?ema1:na, title="EMA", color=sarcon?na:color.blue)if time >= date1 and time <= date2    if ema1<upper and close>upper        strategy.cancel("short")        lot_L=stoploss/(close-basis)        strategy.entry("long",strategy.long,qty=stopcon?lot_L:lot1,stop=emacon?sarcon?max(basis,ema1,sarUp):max(basis,ema1):basis)    if ema1>lower and close<lower        strategy.cancel("long")        lot_S=stoploss/(basis-close)        strategy.entry("short",strategy.short,qty=stopcon?lot_S:lot1,stop=emacon?sarcon?min(basis,ema1,sarDown):min(basis,ema1):basis)if strategy.position_size>0    strategy.exit("long",from_entry="long",stop=emacon?sarcon?max(basis,ema1,sarUp):max(basis,ema1):basis,comment="close_L")if strategy.position_size<0    strategy.exit("short",from_entry="short",stop=emacon?sarcon?min(basis,ema1,sarDown):min(basis,ema1):basis,comment="close_S")