Pine Script v4 · Strategy
[KL] Double Bollinger Bands Strategy (for Crypto-FOREX)
A Pine Script v4 strategy built on SMA (BOLL_src, BOLL_length), Stdev (BOLL_src, BOLL_length) and Stdev or SMA (close, 20). 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. 11 settings; defaults include Alert MSG for buying (Long position) Long +1 entered, Alert MSG for closing (Long position) Long -1 closed and Alert MSG for selling (Short position) Short -1 entered.
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 [KL] Double Bollinger Bands Strategy (for Crypto-FOREX) repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 13 of 13 trades.
- How did [KL] Double Bollinger Bands Strategy (for Crypto-FOREX) do on BTCUSDT 1D with default settings?
- −42.16%
- From 26 Jan 2024 to 21 Oct 2026, over 42 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can [KL] Double Bollinger Bands Strategy (for Crypto-FOREX) 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.
- What alerts does [KL] Double Bollinger Bands Strategy (for Crypto-FOREX) have?
- 4
- LONG_MSG_EXIT; SHORT_MSG_EXIT; LONG_MSG_ENTER; SHORT_MSG_ENTER.
- Does [KL] Double Bollinger Bands Strategy (for Crypto-FOREX) use a stop loss?
- Yes
- strategy.exit sets a stop on line 74, 76.
BTCUSDT · 1D · default settings
- Basis
- BOLL Upper1
- BOLL Lower1
- BOLL Upper2
- BOLL Lower2
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −42.16%
- −$42,160.40
- Buy and hold
- +102.5%
- same period
- Closed trades
- 42
- 19 won · 23 lost
- Profitable
- 45.2%
- of closed trades
- Profit factor
- 0.61
- gross profit ÷ gross loss
- Max drawdown
- 56.81%
- −$71,654.00
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A short 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.
-
Likely bug
sma(close, 20) ignores the settings line 33
Line 33 hard-codes 20 where line 36 uses “lookback period for drift calc.”. Changing those settings moves one band but not this one.
sma(close,20)sma(close, len_drift) -
Likely bug
sma(cv, 50) ignores the settings line 48
Line 48 hard-codes 50 where line 36 uses “lookback period for drift calc.”. Changing those settings moves one band but not this one.
sma(cv,50)sma(cv, len_drift) -
Likely bug
sma(atr14, 5) ignores the settings line 67
Line 67 hard-codes 5 where line 36 uses “lookback period for drift calc.”. Changing those settings moves one band but not this one.
sma(atr14,5)sma(atr14, len_drift) -
Likely bug
sma(atr14, 5) ignores the settings line 68
Line 68 hard-codes 5 where line 36 uses “lookback period for drift calc.”. Changing those settings moves one band but not this one.
sma(atr14,5)sma(atr14, len_drift) -
Likely bug
stdev(close, 20) ignores the settings line 33
Line 33 hard-codes 20 where line 36 uses “lookback period for drift calc.”. Changing those settings moves one band but not this one.
stdev(close,20)stdev(close, len_drift) -
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.
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 sma → ta.sma, stdev → ta.stdev, log → math.log, pow → math.pow, atr → ta.atr, max → math.max, min → math.min.
v4 → v5
VerifiedSame 42 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 6: input(type = input.string) → input.string()
- Line 7: input(type = input.string) → input.string()
- Line 9: input(type = input.string) → input.string()
- Line 10: input(type = input.string) → input.string()
- Line 12: input(type = input.time) → input.time()
19 more edits
- Line 14: input(type = input.time) → input.time()
- Line 18: sma → ta.sma
- Line 19: stdev → ta.stdev
- Line 33: stdev → ta.stdev
- Line 33: sma → ta.sma
- Line 35: log → math.log
- Line 36: sma → ta.sma
- Line 36: pow → math.pow
- Line 36: stdev → ta.stdev
- Line 39: atr → ta.atr
- Line 39: input(type = input.float) → input.float()
- Line 45: max → math.max
- Line 46: min → math.min
- Line 48: sma → ta.sma
- Line 53: max → math.max
- Line 55: min → math.min
- Line 66: atr → ta.atr
- Line 67: sma → ta.sma
- Line 68: sma → ta.sma
⋯// © DojiEmoji (kevinhhl)-//@version=4+//@version=5strategy("[KL] Double BB Strategy",overlay=true,pyramiding=1)ENUM_LONG = "LONG"-LONG_MSG_ENTER = input("Long +1 entered", type=input.string, title="Alert MSG for buying (Long position)")+LONG_MSG_ENTER = input.string("Long +1 entered", title="Alert MSG for buying (Long position)")-LONG_MSG_EXIT = input("Long -1 closed", type=input.string, title="Alert MSG for closing (Long position)")+LONG_MSG_EXIT = input.string("Long -1 closed", title="Alert MSG for closing (Long position)")ENUM_SHORT = "SHORT"-SHORT_MSG_ENTER = input("Short -1 entered", type=input.string, title="Alert MSG for selling (Short position)")+SHORT_MSG_ENTER = input.string("Short -1 entered", title="Alert MSG for selling (Short position)")-SHORT_MSG_EXIT = input("Short +1 covered", type=input.string, title="Alert MSG for covering (Short position)")+SHORT_MSG_EXIT = input.string("Short +1 covered", title="Alert MSG for covering (Short position)")// Timeframe {-backtest_timeframe_start = input(defval = timestamp("01 Apr 2020 13:30 +0000"), title = "Backtest Start Time", type = input.time)+backtest_timeframe_start = input.time(defval = timestamp("01 Apr 2020 13:30 +0000"), title = "Backtest Start Time")USE_ENDTIME = input(false,title="Define backtest end-time (If false, will test up to most recent candle)")-backtest_timeframe_end = input(defval = timestamp("19 Apr 2021 19:30 +0000"), title = "Backtest End Time (if checked above)", type = input.time)+backtest_timeframe_end = input.time(defval = timestamp("19 Apr 2021 19:30 +0000"), title = "Backtest End Time (if checked above)")within_timeframe = time >= backtest_timeframe_start and (time <= backtest_timeframe_end or not USE_ENDTIME)⋯// Bollinger bands {-BOLL_length = 20, BOLL_src = close, SMA20 = sma(BOLL_src, BOLL_length)+BOLL_length = 20, BOLL_src = close, SMA20 = ta.sma(BOLL_src, BOLL_length)-BOLL_sDEV = stdev(BOLL_src, BOLL_length)+BOLL_sDEV = ta.stdev(BOLL_src, BOLL_length)BOLL_upper1 = SMA20 + BOLL_sDEV, BOLL_lower1 = SMA20 - BOLL_sDEV⋯// Quantative methods - used for setting stop losses and entry/exit signals {-cv = stdev(close,20)/sma(close,20)+cv = ta.stdev(close,20)/ta.sma(close,20)len_drift = input(14,title="lookback period for drift calc.")-percentage_chng = log(close/close[1])+percentage_chng = math.log(close/close[1])-drift = sma(percentage_chng, len_drift) - pow(stdev(percentage_chng, len_drift),2) * 0.5+drift = ta.sma(percentage_chng, len_drift) - math.pow(ta.stdev(percentage_chng, len_drift),2) * 0.5// } Quant methods// Trailing stop loss {-ATR_X2_TSL = atr(input(14,title="Length of ATR for trailing stop loss")) * input(2.0,title="ATR Multiplier for trailing stop loss",type=input.float)+ATR_X2_TSL = ta.atr(input(14,title="Length of ATR for trailing stop loss")) * input.float(2.0,title="ATR Multiplier for trailing stop loss")TSL_source_long = low, TSL_source_short = high⋯var pos_opened_short = false // to be updated in MAIN loop-stop_loss_price_long := pos_opened_long ? max(stop_loss_price_long, TSL_source_long - ATR_X2_TSL) : TSL_source_long - ATR_X2_TSL+stop_loss_price_long := pos_opened_long ? math.max(stop_loss_price_long, TSL_source_long - ATR_X2_TSL) : TSL_source_long - ATR_X2_TSL-stop_loss_price_short := pos_opened_short ? min(stop_loss_price_short, TSL_source_short + ATR_X2_TSL) : TSL_source_short + ATR_X2_TSL+stop_loss_price_short := pos_opened_short ? math.min(stop_loss_price_short, TSL_source_short + ATR_X2_TSL) : TSL_source_short + ATR_X2_TSL// tighten TSL-is_consol = cv < sma(cv,50)+is_consol = cv < ta.sma(cv,50)TIGHTEN_TSL = input(true, title="Tighten stop loss when price moves sharply in favorable direction")⋯ if pos_opened_long and cv > cv[1]- stop_loss_price_long := max(stop_loss_price_long * (1+percentage_chng*0.5), stop_loss_price_long[1])+ stop_loss_price_long := math.max(stop_loss_price_long * (1+percentage_chng*0.5), stop_loss_price_long[1]) if pos_opened_short and cv > cv[1]- stop_loss_price_short := min(stop_loss_price_long * (1+percentage_chng*0.5), stop_loss_price_short[1])+ stop_loss_price_short := math.min(stop_loss_price_long * (1+percentage_chng*0.5), stop_loss_price_short[1])// Plot TSL⋯above_neutral = close > BOLL_upper1 and close < BOLL_upper2-atr14 = atr(14)+atr14 = ta.atr(14)-entry_signal_long = is_consol and close > BOLL_upper1 and atr14 > sma(atr14,5) and drift > 0+entry_signal_long = is_consol and close > BOLL_upper1 and atr14 > ta.sma(atr14,5) and drift > 0-entry_signal_short = is_consol and close < BOLL_lower1 and atr14 > sma(atr14,5) and drift < 0+entry_signal_short = is_consol and close < BOLL_lower1 and atr14 > ta.sma(atr14,5) and drift < 0// } Signals for entry⋯Same 42 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 13: input() → input.bool()
- Line 34: input() → input.int()
- Line 39: input() → input.int()
- Line 49: input() → input.bool()
⋯backtest_timeframe_start = input.time(defval = timestamp("01 Apr 2020 13:30 +0000"), title = "Backtest Start Time")-USE_ENDTIME = input(false,title="Define backtest end-time (If false, will test up to most recent candle)")+USE_ENDTIME = input.bool(false,title="Define backtest end-time (If false, will test up to most recent candle)")backtest_timeframe_end = input.time(defval = timestamp("19 Apr 2021 19:30 +0000"), title = "Backtest End Time (if checked above)")⋯cv = ta.stdev(close,20)/ta.sma(close,20)-len_drift = input(14,title="lookback period for drift calc.")+len_drift = input.int(14,title="lookback period for drift calc.")percentage_chng = math.log(close/close[1])⋯// Trailing stop loss {-ATR_X2_TSL = ta.atr(input(14,title="Length of ATR for trailing stop loss")) * input.float(2.0,title="ATR Multiplier for trailing stop loss")+ATR_X2_TSL = ta.atr(input.int(14,title="Length of ATR for trailing stop loss")) * input.float(2.0,title="ATR Multiplier for trailing stop loss")TSL_source_long = low, TSL_source_short = high⋯is_consol = cv < ta.sma(cv,50)-TIGHTEN_TSL = input(true, title="Tighten stop loss when price moves sharply in favorable direction")+TIGHTEN_TSL = input.bool(true, title="Tighten stop loss when price moves sharply in favorable direction")if TIGHTEN_TSL⋯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: 42 before, 40 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.
⋯// © DojiEmoji (kevinhhl)-//@version=5+//@version=6strategy("[KL] Double BB Strategy",overlay=true,pyramiding=1)⋯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 |
|---|---|---|---|
| Alert MSG for buying (Long position) | Long +1 entered | string | alert |
| Alert MSG for closing (Long position) | Long -1 closed | string | alert |
| Alert MSG for selling (Short position) | Short -1 entered | string | alert |
| Alert MSG for covering (Short position) | Short +1 covered | string | alert |
| Backtest Start Time | timestamp("01 Apr 2020 13:30 +0000") | time | within_timeframe |
| Define backtest end-time (If false, will test up to most recent candle) | false | bool | within_timeframe |
| Backtest End Time (if checked above) | timestamp("19 Apr 2021 19:30 +0000") | time | within_timeframe |
| lookback period for drift calc. | 14 | int | drift |
| Length of ATR for trailing stop loss | 14 | int | stop_loss_price_long stop_loss_price_short |
| ATR Multiplier for trailing stop loss | 2.0 | float | stop_loss_price_long stop_loss_price_short |
| Tighten stop loss when price moves sharply in favorable direction | true | bool | line 50 |
What it calculates
- SMA (
sma,ta.smain v5)BOLL_src, BOLL_lengthSMA20 · line 18 - Stdev (
stdev,ta.stdevin v5)BOLL_src, BOLL_lengthBOLL_sDEV · line 19 - Stdev / SMA (
stdev,ta.stdevin v5)close, 20cv · line 33 - SMA / Stdev (
sma,ta.smain v5)percentage_chng, lookback period for drift calc. (14)drift · line 36 - ATR (
atr,ta.atrin v5)14atr14 · line 66
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 |
|---|---|---|---|
| Exit LONG stop | pos_opened_long | On | 74 |
| Exit SHORT stop | pos_opened_short | On | 76 |
| Close LONG | pos_opened_long and (TSL_source_long <= stop_loss_price_long or end_of_rally and not is_consol) | On | 82 |
| Close SHORT | pos_opened_short and (TSL_source_short >= stop_loss_price_short) | On | 86 |
| Open LONG long | entry_signal_long and close > stop_loss_price_long | On | 91 |
| Open SHORT short | entry_signal_short | On | 95 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Alerts
| Alert | Call | Message | Line |
|---|---|---|---|
| (no title) | alert | LONG_MSG_EXIT | 81 |
| (no title) | alert | SHORT_MSG_EXIT | 85 |
| (no title) | alert | LONG_MSG_ENTER | 90 |
| (no title) | alert | SHORT_MSG_ENTER | 94 |
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 | −42.2% | +102.5% | 42 | 45% | 0.61 | 56.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −11.0% | +5.5% | 45 | 47% | 0.65 | 14.2% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +1.5% | +9.8% | 39 | 44% | 1.35 | 1.7% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.5% | +8.4% | 41 | 34% | 0.53 | 0.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.2% | +16.7% | 36 | 67% | 2.23 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 44 | 52% | 1.06 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.0% | +148.3% | 31 | 55% | 1.02 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.1% | +17.1% | 32 | 44% | 1.63 | 0.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 18 | 50% | −$5,232.52 |
| Sideways market | 14 | 50% | −$6,542.03 |
| Falling market | 10 | 30% | −$30,385.90 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
57.5%
The 95th percentile of the worst drawdown when the same 42 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 42.2% to 57.5%; the tick is what actually happened, 56.2%. Order luck alone moves it this much.
All 42 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 13 Apr 2024 67,116.50 | 15 May 2024 65,956.80 | 32 | $1,159.74 |
| long | 16 May 2024 66,206.50 | 27 May 2024 68,507.70 | 11 | $2,301.16 |
| short | 14 Jun 2024 66,773.00 | 23 Jun 2024 64,262.00 | 9 | $2,511.00 |
| short | 3 Aug 2024 61,498.30 | 6 Aug 2024 54,018.80 | 3 | $7,479.52 |
| long | 24 Aug 2024 64,037.20 | 27 Aug 2024 58,361.60 | 3 | −$5,675.62 |
| short | 6 Sep 2024 56,180.00 | 9 Sep 2024 54,869.90 | 3 | $1,310.05 |
| long | 18 Sep 2024 60,314.00 | 1 Oct 2024 61,451.30 | 13 | $1,137.28 |
| short | 11 Oct 2024 60,326.40 | 14 Oct 2024 65,339.60 | 3 | −$5,013.15 |
| long | 15 Oct 2024 66,084.00 | 23 Oct 2024 67,426.00 | 8 | $1,342.01 |
| long | 31 Oct 2024 72,344.80 | 4 Nov 2024 67,264.60 | 4 | −$5,080.12 |
| long | 7 Nov 2024 75,572.00 | 19 Dec 2024 97,438.70 | 42 | $21,866.70 |
| short | 10 Jan 2025 92,552.50 | 16 Jan 2025 100,497.00 | 6 | −$7,944.86 |
| long | 16 Jan 2025 100,497.00 | 27 Jan 2025 102,620.00 | 11 | $2,122.66 |
| short | 5 Feb 2025 97,763.10 | 10 Feb 2025 96,462.80 | 5 | $1,300.39 |
| short | 25 Feb 2025 91,552.90 | 27 Feb 2025 84,250.10 | 2 | $7,302.79 |
| short | 3 Apr 2025 82,516.30 | 22 Apr 2025 88,506.80 | 19 | −$5,990.50 |
| long | 26 May 2025 109,004.00 | 5 Jun 2025 102,688.00 | 10 | −$6,316.04 |
| short | 6 Jun 2025 101,509.00 | 10 Jun 2025 110,263.00 | 4 | −$8,754.33 |
| long | 10 Jun 2025 110,263.00 | 20 Jun 2025 102,491.00 | 10 | −$7,771.78 |
| short | 21 Jun 2025 103,298.00 | 2 Jul 2025 109,550.00 | 11 | −$6,252.03 |
| long | 3 Jul 2025 108,850.00 | 1 Aug 2025 115,764.00 | 29 | $6,914.48 |
| short | 1 Aug 2025 115,764.00 | 3 Aug 2025 114,313.00 | 2 | $1,451.21 |
| long | 13 Aug 2025 120,134.00 | 19 Aug 2025 113,284.00 | 6 | −$6,850.10 |
| short | 22 Aug 2025 112,500.00 | 27 Aug 2025 111,763.00 | 5 | $736.78 |
| short | 26 Sep 2025 108,994.00 | 29 Sep 2025 114,301.00 | 3 | −$5,306.60 |
| long | 2 Oct 2025 118,595.00 | 10 Oct 2025 119,208.00 | 8 | $612.62 |
| short | 5 Nov 2025 101,497.00 | 9 Nov 2025 102,313.00 | 4 | −$815.72 |
| long | 4 Dec 2025 93,429.90 | 18 Dec 2025 86,243.20 | 14 | −$7,186.72 |
| short | 18 Dec 2025 86,243.20 | 19 Dec 2025 85,516.40 | 1 | $726.82 |
| long | 7 Jan 2026 93,748.00 | 19 Jan 2026 93,673.10 | 12 | −$74.83 |
| short | 28 Feb 2026 65,872.10 | 5 Mar 2026 72,666.80 | 5 | −$6,794.68 |
| long | 5 Mar 2026 72,666.80 | 22 Mar 2026 67,453.30 | 17 | −$5,213.45 |
| short | 28 Mar 2026 66,407.30 | 7 Apr 2026 72,451.40 | 10 | −$6,044.14 |
| long | 8 Apr 2026 71,924.20 | 13 Apr 2026 70,741.60 | 5 | −$1,182.66 |
| long | 8 May 2026 80,006.00 | 18 May 2026 76,410.10 | 10 | −$3,595.85 |
| short | 24 May 2026 76,752.00 | 29 May 2026 73,617.50 | 5 | $3,134.48 |
| short | 25 Jun 2026 61,078.00 | 6 Jul 2026 64,694.70 | 11 | −$3,616.71 |
| long | 15 Jul 2026 65,044.00 | 1 Aug 2026 62,887.90 | 17 | −$2,156.11 |
| short | 1 Aug 2026 62,887.90 | 2 Aug 2026 62,823.70 | 1 | $64.23 |
| short | 12 Sep 2026 77,225.70 | 13 Sep 2026 77,278.70 | 1 | −$53.02 |
| short | 16 Sep 2026 75,644.50 | 17 Sep 2026 76,206.00 | 1 | −$561.52 |
| long | 19 Sep 2026 80,883.90 | 29 Sep 2026 83,500.00 | 10 | $2,616.15 |
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 |
|---|---|---|---|---|---|
| [KL] Double Bollinger Bands Strategy (for Crypto-FOREX) this page | — | v4 strategy | −42.2% | 42 | 56.8% |
| MCL-YG Pair Trading Strategy by shark792 | sma stdev atr | v5 strategy | No trades on this market | ||
| [KL] Relative Volume + ATR Strategy by DojiEmoji | atr sma stdev | v4 strategy | −32.7% | 10 | 33.3% |
| [KL] Mean Reversion (ATR) Strategy by DojiEmoji | atr sma stdev | v5 strategy | No trades on this market | ||
| Fractal Breakout Strategy [KL] by DojiEmoji | sma atr stdev | v5 strategy | +1.9% | 36 | 0.8% |
| Mean Reversion Strategy v2 [KL] by DojiEmoji | atr stdev sma | v5 strategy | +1.7% | 52 | 1.5% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 30 ms | With a workaround 1,000 bars in 75 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently merges fill() calls that share a title, so it would draw 1 of this script's 4 fills; this page gives each call its own title to draw all of them. The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“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/// © DojiEmoji (kevinhhl)//@version=4strategy("[KL] Double BB Strategy",overlay=true,pyramiding=1)ENUM_LONG = "LONG"LONG_MSG_ENTER = input("Long +1 entered", type=input.string, title="Alert MSG for buying (Long position)")LONG_MSG_EXIT = input("Long -1 closed", type=input.string, title="Alert MSG for closing (Long position)")ENUM_SHORT = "SHORT"SHORT_MSG_ENTER = input("Short -1 entered", type=input.string, title="Alert MSG for selling (Short position)")SHORT_MSG_EXIT = input("Short +1 covered", type=input.string, title="Alert MSG for covering (Short position)")// Timeframe {backtest_timeframe_start = input(defval = timestamp("01 Apr 2020 13:30 +0000"), title = "Backtest Start Time", type = input.time)USE_ENDTIME = input(false,title="Define backtest end-time (If false, will test up to most recent candle)")backtest_timeframe_end = input(defval = timestamp("19 Apr 2021 19:30 +0000"), title = "Backtest End Time (if checked above)", type = input.time)within_timeframe = time >= backtest_timeframe_start and (time <= backtest_timeframe_end or not USE_ENDTIME)// } Timeframe// Bollinger bands {BOLL_length = 20, BOLL_src = close, SMA20 = sma(BOLL_src, BOLL_length)BOLL_sDEV = stdev(BOLL_src, BOLL_length)BOLL_upper1 = SMA20 + BOLL_sDEV, BOLL_lower1 = SMA20 - BOLL_sDEVBOLL_upper2 = SMA20 + BOLL_sDEV*2, BOLL_lower2 = SMA20 - BOLL_sDEV*2SMA_20_plot = plot(SMA20, "Basis", color=#872323, offset = 0)BOLL_upper1_plot = plot(BOLL_upper1, "BOLL Upper1", color=color.new(color.navy, 50), offset = 0)BOLL_lower1_plot = plot(BOLL_lower1, "BOLL Lower1", color=color.new(color.navy, 50), offset = 0)BOLL_upper2_plot = plot(BOLL_upper2, "BOLL Upper2", color=color.new(color.navy, 50), offset = 0)BOLL_lower2_plot = plot(BOLL_lower2, "BOLL Lower2", color=color.new(color.navy, 50), offset = 0)fill(BOLL_upper2_plot, BOLL_upper1_plot, title = "Background", color=color.new(#198787, 85))fill(BOLL_upper1_plot, SMA_20_plot, title = "Background", color=color.new(#198787, 75))fill(SMA_20_plot, BOLL_lower1_plot, title = "Background", color=color.new(#198787, 75))fill(BOLL_lower1_plot, BOLL_lower2_plot, title = "Background", color=color.new(#198787, 85))// { Bollinger bands // Quantative methods - used for setting stop losses and entry/exit signals {cv = stdev(close,20)/sma(close,20)len_drift = input(14,title="lookback period for drift calc.")percentage_chng = log(close/close[1])drift = sma(percentage_chng, len_drift) - pow(stdev(percentage_chng, len_drift),2) * 0.5// } Quant methods// Trailing stop loss {ATR_X2_TSL = atr(input(14,title="Length of ATR for trailing stop loss")) * input(2.0,title="ATR Multiplier for trailing stop loss",type=input.float)TSL_source_long = low, TSL_source_short = highvar stop_loss_price_long = float(0)var stop_loss_price_short = float(2147483647)var pos_opened_long = false // to be updated in MAIN loopvar pos_opened_short = false // to be updated in MAIN loopstop_loss_price_long := pos_opened_long ? max(stop_loss_price_long, TSL_source_long - ATR_X2_TSL) : TSL_source_long - ATR_X2_TSLstop_loss_price_short := pos_opened_short ? min(stop_loss_price_short, TSL_source_short + ATR_X2_TSL) : TSL_source_short + ATR_X2_TSL// tighten TSLis_consol = cv < sma(cv,50)TIGHTEN_TSL = input(true, title="Tighten stop loss when price moves sharply in favorable direction")if TIGHTEN_TSL if not is_consol if pos_opened_long and cv > cv[1] stop_loss_price_long := max(stop_loss_price_long * (1+percentage_chng*0.5), stop_loss_price_long[1]) if pos_opened_short and cv > cv[1] stop_loss_price_short := min(stop_loss_price_long * (1+percentage_chng*0.5), stop_loss_price_short[1])// Plot TSLTSL_transp_long = pos_opened_long and within_timeframe ? 0 : 100TSL_transp_short = pos_opened_short and within_timeframe ? 0 : 100plot(stop_loss_price_long, color=color.new(color.green, TSL_transp_long))plot(stop_loss_price_short, color=color.new(color.red, TSL_transp_short))// } Trailing stop loss// Signals for entry {is_neutral = close < BOLL_upper1 and close > BOLL_lower1below_neutral = close < BOLL_lower1 and close > BOLL_lower2above_neutral = close > BOLL_upper1 and close < BOLL_upper2atr14 = atr(14)entry_signal_long = is_consol and close > BOLL_upper1 and atr14 > sma(atr14,5) and drift > 0entry_signal_short = is_consol and close < BOLL_lower1 and atr14 > sma(atr14,5) and drift < 0// } Signals for entry// MAIN:if within_timeframe // EXIT :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: if pos_opened_long strategy.exit(ENUM_LONG, stop=stop_loss_price_long, comment="SL") if pos_opened_short strategy.exit(ENUM_SHORT, stop=stop_loss_price_short, comment="SL") end_of_rally = close < BOLL_upper1 and strategy.position_avg_price > stop_loss_price_long and low < low[1] if pos_opened_long and (TSL_source_long <= stop_loss_price_long or end_of_rally and not is_consol) pos_opened_long := false alert(LONG_MSG_EXIT, alert.freq_once_per_bar) strategy.close(ENUM_LONG, comment=close < strategy.position_avg_price ? "stop loss" : "take profit") if pos_opened_short and (TSL_source_short >= stop_loss_price_short) pos_opened_short := false alert(SHORT_MSG_EXIT, alert.freq_once_per_bar) strategy.close(ENUM_SHORT, comment=close > strategy.position_avg_price ? "stop loss" : "take profit") // ENTRY ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: if entry_signal_long and close > stop_loss_price_long pos_opened_long := true alert(LONG_MSG_ENTER, alert.freq_once_per_bar) strategy.entry(ENUM_LONG, strategy.long, comment="long") if entry_signal_short pos_opened_short := true alert(SHORT_MSG_ENTER, alert.freq_once_per_bar) strategy.entry(ENUM_SHORT, strategy.short, comment="short")// CLEAN UP:if not pos_opened_long stop_loss_price_long := float(0)if not pos_opened_short stop_loss_price_short := float(2147483647)