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
- 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
VerifiedSame 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")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.
⋯// © 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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Start Date | timestamp("2000-01-01T00:00:00") | time | line 37 |
| Stop Date | timestamp("2030-01-01T00:00:00") | time | line 37 |
| Length (no label in the script) | 100 | int · min 1 · step 10 | basis dev |
| StdDev | 1.5 | float · step 0.1 | dev |
| stopcon/lot | true | bool | entry long entry short |
| lot | 1 | int | entry long entry short |
| stoploss | 1000 | int | lot_L lot_S |
| EMA | true | bool | EMA entry long entry short exit long exit short |
| EMA value | 100 | int · min 2 · step 1 | ema1 |
| SAR boost | true | bool | Basis EMA entry long entry short exit long exit short |
| Start | 0.1 | float · min 0 · max 10 · step 0.1 | sarUp sarDown |
| Step Setting | 0.3 | float · min 0 · max 10 · step 0.1 | sarUp sarDown |
| Maximum Step | 1 | int · min 1 · max 10 · step 0.1 | sarUp sarDown |
What it calculates
- EMA (
ema,ta.emain v5)close, Length (100)basis · line 10 - Stdev (
stdev,ta.stdevin v5)close, Length (100)dev · line 11 - EMA (
ema,ta.emain v5)close, EMA value (100)ema1 · line 27 - SAR (
sar,ta.sarin v5)start*0.01, increment*0.01, maximum*0.1sarUp · line 28 - SAR (
sar,ta.sarin 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| cancel short | ema1<upper and close>upper | On | 39 |
| Open long long | ema1<upper and close>upper | On | 41 |
| cancel long | ema1>lower and close<lower | On | 43 |
| Open short short | ema1>lower and close<lower | On | 45 |
| Exit long stop | strategy.position_size>0 | On | 47 |
| Exit short stop | strategy.position_size<0 | On | 49 |
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.
- 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.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | −0.6% | +102.5% | 14 | 36% | 0.91 | 3.9% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +1.5% | +5.5% | 12 | 33% | 1.26 | 2.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +5.3% | +9.8% | 9 | 78% | 8.52 | 0.8% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +3.8% | +8.4% | 11 | 45% | 2.07 | 2.7% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.7% | +16.7% | 11 | 64% | 1.19 | 2.6% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +17.0% | +21.5% | 10 | 50% | 5.89 | 2.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −2.5% | +148.3% | 15 | 40% | 0.66 | 5.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +2.7% | +17.1% | 12 | 42% | 1.70 | 2.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 2 | 0% | −$1,143.99 |
| Sideways market | 9 | 44% | $2,106.10 |
| Falling market | 3 | 33% | −$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.
All 14 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 21 May 2024 71,446.60 | 18 Jun 2024 64,334.70 | 28 | −$700.54 |
| short | 5 Jul 2024 57,050.00 | 15 Jul 2024 62,633.20 | 10 | −$841.98 |
| short | 6 Aug 2024 54,018.80 | 23 Aug 2024 62,145.20 | 17 | −$873.54 |
| short | 7 Sep 2024 53,963.00 | 17 Sep 2024 60,754.90 | 10 | −$911.89 |
| long | 18 Oct 2024 68,595.30 | 20 Dec 2024 94,305.40 | 63 | $4,733.54 |
| short | 27 Feb 2025 84,250.10 | 2 Mar 2025 93,279.80 | 3 | −$946.65 |
| short | 4 Mar 2025 86,221.20 | 21 Apr 2025 87,382.40 | 48 | −$167.36 |
| long | 9 May 2025 103,262.00 | 20 Jun 2025 103,371.00 | 42 | $8.10 |
| long | 12 Jul 2025 117,528.00 | 24 Aug 2025 110,772.00 | 43 | −$443.46 |
| long | 2 Oct 2025 118,595.00 | 10 Oct 2025 113,529.00 | 8 | −$765.71 |
| short | 18 Oct 2025 106,432.00 | 21 Oct 2025 112,958.00 | 3 | −$962.17 |
| short | 5 Nov 2025 101,497.00 | 5 Jan 2026 92,087.80 | 61 | $886.81 |
| short | 1 Feb 2026 78,741.10 | 17 Mar 2026 75,484.10 | 44 | $213.29 |
| short | 3 Jun 2026 66,760.80 | 8 Aug 2026 64,923.20 | 66 | $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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Trend #2 - BB+EMA this page | — | v4 strategy | −0.6% | 14 | 3.9% |
| KAMA Strategy - Kaufman's Adaptive Moving Average by BenHampson | stdev ema sar | v5 strategy | +51.1% | 20 | 34.6% |
| Escaping of Rate from Avarage By Mustafa OZVER Strategy by Mustafaozver | stdev ema | v4 strategy | +122.0% | 96 | 79.1% |
| Bollinger Band with Fib Golden Ratio (0.618) by mohanee | stdev ema | v4 strategy | No trades on this market | ||
| Volume Weighted Bollinger Bands Strategy by Ankit_1618 | ema stdev | v4 strategy | +10.0% | 15 | 27.5% |
| BB21_MA200_Strategy by mohanee | ema stdev | v4 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 20 ms | Yes 1,000 bars in 46 ms | Not measured |
| PineTS | No 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")