Pine Script v4 · Strategy
[KL] Bollinger Bands Consolidation Strategy
A Pine Script v4 strategy built on SMA (BOLL_src, BOLL_length), Stdev (BOLL_src, BOLL_length) and ATR (BOLL_length). It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 11 settings; defaults include No. of candles to lookback when determining ATR is decreasing 1, Source of TSL low and Reward ratio for exit (if selected above) 3.
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] Bollinger Bands Consolidation Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 6 of 6 trades.
- How did [KL] Bollinger Bands Consolidation Strategy do on BTCUSDT 1D with default settings?
- +39.11%
- From 26 Jan 2024 to 21 Oct 2026, over 33 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can [KL] Bollinger Bands Consolidation Strategy 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] Bollinger Bands Consolidation Strategy have?
- 3
- Stop loss limit raised; BOLL bands are squeezing; BOLL bands stopped squeezing.
- Does [KL] Bollinger Bands Consolidation Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
BTCUSDT · 1D · default settings
- Basis
- BOLL Upper
- BOLL Lower
- SMA20 + ATR_x2
- SMA20 - ATR_x2
- ATR_x2
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +39.11%
- $39,106.80
- Buy and hold
- +102.5%
- same period
- Closed trades
- 33
- 13 won · 20 lost
- Profitable
- 39.4%
- of closed trades
- Profit factor
- 1.44
- gross profit ÷ gross loss
- Max drawdown
- 19.64%
- −$26,739.80
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 stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
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.
-
Note
Uses an argument v6 removed
when = on line 135. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 16
transp = on line 16, 17, 18, 23, 24. v5 folds the transparency into the colour with color.new(c, transp).
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, atr → ta.atr, avg → math.avg, max → math.max, ema → ta.ema, abs → math.abs, rsi → ta.rsi.
- 1 strategy call uses the when = argument (line 135). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 33 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 4: //@version=4 → //@version=5
- Line 7: input(type = input.time) → input.time()
- Line 9: input(type = input.time) → input.time()
- Line 13: sma → ta.sma
- Line 13: stdev → ta.stdev
- Line 16: transp = 50 → color.new(color.navy, 50)
17 more edits
- Line 17: transp = 50 → color.new(color.navy, 50)
- Line 18: transp = 85 → color.new(#198787, 85)
- Line 21: atr → ta.atr
- Line 22: sma → ta.sma
- Line 23: transp = 50 → color.new(color.gray, 50)
- Line 24: transp = 50 → color.new(color.gray, 50)
- Line 43: avg → math.avg
- Line 49: max → math.max
- Line 59: ema → ta.ema
- Line 73: max → math.max
- Line 73: abs → math.abs
- Line 77: rsi → ta.rsi
- Line 77: rsi → ta.rsi
- Line 95: abs → math.abs
- Line 95: abs → math.abs
- Line 132: max → math.max
- Line 134: max → math.max
⋯// -//@version=4+//@version=5strategy("[KL] BB Consolidation Strategy",overlay=true,pyramiding=1)// Timeframe {-backtest_timeframe_start = input(defval = timestamp("01 Apr 2016 13:30 +0000"), title = "Backtest Start Time", type = input.time)+backtest_timeframe_start = input.time(defval = timestamp("01 Apr 2016 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 (sdv=2, len=20) {-BOLL_length = 20, BOLL_src = close, SMA20 = sma(BOLL_src, BOLL_length), BOLL_sDEV_x2 = 2 * stdev(BOLL_src, BOLL_length)+BOLL_length = 20, BOLL_src = close, SMA20 = ta.sma(BOLL_src, BOLL_length), BOLL_sDEV_x2 = 2 * ta.stdev(BOLL_src, BOLL_length)BOLL_upper = SMA20 + BOLL_sDEV_x2, BOLL_lower = SMA20 - BOLL_sDEV_x2plot(SMA20, "Basis", color=#872323, offset = 0)-BOLL_p1 = plot(BOLL_upper, "BOLL Upper", color=color.navy, offset = 0, transp=50)+BOLL_p1 = plot(BOLL_upper, "BOLL Upper", color=color.new(color.navy, 50), offset = 0)-BOLL_p2 = plot(BOLL_lower, "BOLL Lower", color=color.navy, offset = 0, transp=50)+BOLL_p2 = plot(BOLL_lower, "BOLL Lower", color=color.new(color.navy, 50), offset = 0)-fill(BOLL_p1, BOLL_p2, title = "Background", color=#198787, transp=85)+fill(BOLL_p1, BOLL_p2, title = "Background", color=color.new(#198787, 85))// }// Volatility Indicators {-ATR_x2 = atr(BOLL_length) * 2 // multiplier aligns with BOLL+ATR_x2 = ta.atr(BOLL_length) * 2 // multiplier aligns with BOLL-avg_atr = sma(ATR_x2, input(1,title="No. of candles to lookback when determining ATR is decreasing"))+avg_atr = ta.sma(ATR_x2, input(1,title="No. of candles to lookback when determining ATR is decreasing"))-plot(SMA20+ATR_x2, "SMA20 + ATR_x2", color=color.gray, offset = 0, transp=50)+plot(SMA20+ATR_x2, "SMA20 + ATR_x2", color=color.new(color.gray, 50), offset = 0)-plot(SMA20-ATR_x2, "SMA20 - ATR_x2", color=color.gray, offset = 0, transp=50)+plot(SMA20-ATR_x2, "SMA20 - ATR_x2", color=color.new(color.gray, 50), offset = 0)plotchar(ATR_x2, "ATR_x2", "", location = location.bottom)⋯ else if case == "middle of both"- result := avg(low, close)+ result := math.avg(low, close) ⋯else if strategy.position_size > 0- stop_loss_price := max(stop_loss_price, get_TSL_src() - trailing_SL_buffer)+ stop_loss_price := math.max(stop_loss_price, get_TSL_src() - trailing_SL_buffer)if strategy.position_size > 0 and stop_loss_price > stop_loss_price[1]⋯ alert("BOLL bands stopped squeezing", alert.freq_once_per_bar)-ema_trend = ema(close, 20)+ema_trend = ta.ema(close, 20)VERBOSE = input(true, title="Show text descriptions of buy/sell orders")⋯IGNORE_EXIT_CONSOL = input(true,title="Ignore sell signals during consolidation (unless hitting stop loss)")-bearish_candle1 = (close - open)/(high - low) < -0.5 and high > max(close[1],open[1]) and abs(open-close) > BOLL_sDEV_x2/2+bearish_candle1 = (close - open)/(high - low) < -0.5 and high > math.max(close[1],open[1]) and math.abs(open-close) > BOLL_sDEV_x2/2gapped_down = high < low[1] and close < open⋯reward_met = close >= (entry_price + (REWARD_RATIO_EXIT * risk_value))-rsi10 = rsi(close, 10), rsi14 = rsi(close, 14)+rsi10 = ta.rsi(close, 10), rsi14 = ta.rsi(close, 14)overbought = rsi14 > 70 and rsi10 > rsi14⋯ entry_msg := concat(entry_msg, "green")- else if abs(close-open)/abs(high-low) < 0.1 //"Doji"+ else if math.abs(close-open)/math.abs(high-low) < 0.1 //"Doji" conf_count := conf_count + 1⋯ if TIGHTEN_TSL_BEARISH and is_bearish- stop_loss_price := max(low-(low-stop_loss_price)*0.9,stop_loss_price)+ stop_loss_price := math.max(low-(low-stop_loss_price)*0.9,stop_loss_price) else if overbought and not EXIT_OVERBOUGHT- stop_loss_price := max(low-(low-stop_loss_price)*0.7,stop_loss_price)+ stop_loss_price := math.max(low-(low-stop_loss_price)*0.7,stop_loss_price) strategy.close("Long", when=bExit, comment=exit_msg)⋯Same 33 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 8: input() → input.bool()
- Line 22: input() → input.int()
- Line 31: input() → input.bool()
- Line 34: input() → input.string()
- Line 60: input() → input.bool()
- Line 71: input() → input.int()
4 more edits
- Line 72: input() → input.bool()
- Line 79: input() → input.bool()
- Line 80: input() → input.bool()
- Line 135: when = bExit → if block
⋯backtest_timeframe_start = input.time(defval = timestamp("01 Apr 2016 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)")⋯ATR_x2 = ta.atr(BOLL_length) * 2 // multiplier aligns with BOLL-avg_atr = ta.sma(ATR_x2, input(1,title="No. of candles to lookback when determining ATR is decreasing"))+avg_atr = ta.sma(ATR_x2, input.int(1,title="No. of candles to lookback when determining ATR is decreasing"))plot(SMA20+ATR_x2, "SMA20 + ATR_x2", color=color.new(color.gray, 50), offset = 0)⋯var risk_value = trailing_SL_buffer //declared separately; trailing_SL_buffer may be tightened subsequently-UPDATE_ATR_LEN = input(true, title="Update the size of stoploss after entry")+UPDATE_ATR_LEN = input.bool(true, title="Update the size of stoploss after entry")if UPDATE_ATR_LEN and strategy.position_size > 0 and close < open trailing_SL_buffer := ATR_x2-TSL_source = input(title="Source of TSL", defval="low", options=["close","low","middle of both"])+TSL_source = input.string(title="Source of TSL", defval="low", options=["close","low","middle of both"])get_TSL_src() =>⋯ema_trend = ta.ema(close, 20)-VERBOSE = input(true, title="Show text descriptions of buy/sell orders")+VERBOSE = input.bool(true, title="Show text descriptions of buy/sell orders")concat(a, b) =>⋯// Sell setup - Long position {-REWARD_RATIO_EXIT = input(3, title="Reward ratio for exit (if selected above)")+REWARD_RATIO_EXIT = input.int(3, title="Reward ratio for exit (if selected above)")-IGNORE_EXIT_CONSOL = input(true,title="Ignore sell signals during consolidation (unless hitting stop loss)")+IGNORE_EXIT_CONSOL = input.bool(true,title="Ignore sell signals during consolidation (unless hitting stop loss)")bearish_candle1 = (close - open)/(high - low) < -0.5 and high > math.max(close[1],open[1]) and math.abs(open-close) > BOLL_sDEV_x2/2⋯overbought = rsi14 > 70 and rsi10 > rsi14-EXIT_OVERBOUGHT = input(false,title="Exit position when overbought")+EXIT_OVERBOUGHT = input.bool(false,title="Exit position when overbought")-TIGHTEN_TSL_BEARISH = input(true,title="Tighten TSL instead of exiting when candle is bearish")+TIGHTEN_TSL_BEARISH = input.bool(true,title="Tighten TSL instead of exiting when candle is bearish")// } end of Sell setup - Long position⋯ stop_loss_price := math.max(low-(low-stop_loss_price)*0.7,stop_loss_price)- strategy.close("Long", when=bExit, comment=exit_msg)+ if bExit+ strategy.close("Long", comment=exit_msg)// plot TSL line⋯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: 33 before, 32 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.
⋯// -//@version=5+//@version=6strategy("[KL] BB Consolidation 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 |
|---|---|---|---|
| Backtest Start Time | timestamp("01 Apr 2016 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 |
| No. of candles to lookback when determining ATR is decreasing | 1 | int | line 90 |
| Update the size of stoploss after entry | true | bool | line 32 |
| Source of TSL | low | string · options ["close","low","middle of both"] | case |
| Show text descriptions of buy/sell orders | true | bool | line 63 |
| Reward ratio for exit (if selected above) | 3 | int | reward_met |
| Ignore sell signals during consolidation (unless hitting stop loss) | true | bool | line 120 |
| Exit position when overbought | false | bool | bExit line 133 |
| Tighten TSL instead of exiting when candle is bearish | true | bool | line 120 line 131 |
What it calculates
- SMA (
sma,ta.smain v5)BOLL_src, BOLL_lengthSMA20 · line 13 - Stdev (
stdev,ta.stdevin v5)BOLL_src, BOLL_lengthBOLL_sDEV_x2 · line 13 - ATR (
atr,ta.atrin v5)BOLL_lengthATR_x2 · line 21 - EMA (
ema,ta.emain v5)close, 20ema_trend · line 59 - RSI (
rsi,ta.rsiin v5)close, 10rsi10 · line 77 - RSI (
rsi,ta.rsiin v5)close, 14rsi14 · line 77
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 | is_squeezing and confirm_entry and strategy.position_size == 0 | On | 104 |
| Close Long | bExit | On | 135 |
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 | Stop loss limit raised | 51 |
| (no title) | alert | BOLL bands are squeezing | 56 |
| (no title) | alert | BOLL bands stopped squeezing | 58 |
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 | +39.1% | +102.5% | 33 | 39% | 1.44 | 19.6% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −9.3% | +5.5% | 41 | 34% | 0.72 | 17.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −1.4% | +9.8% | 36 | 25% | 0.78 | 3.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.2% | +8.4% | 34 | 35% | 0.86 | 0.5% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 33 | 45% | 0.93 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 32 | 31% | 1.26 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.0% | +148.3% | 28 | 36% | 0.99 | 0.3% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.1% | +17.1% | 37 | 35% | 0.78 | 0.1% | 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 | 14 | 36% | $10,482.10 |
| Sideways market | 13 | 46% | $38,732.10 |
| Falling market | 6 | 33% | −$10,107.40 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
35.8%
The 95th percentile of the worst drawdown when the same 33 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 16.3% to 35.8%; the tick is what actually happened, 18.6%. Order luck alone moves it this much.
All 33 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 21 Mar 2024 67,840.50 | 14 Apr 2024 63,924.50 | 24 | −$3,915.99 |
| long | 17 Apr 2024 63,793.40 | 2 May 2024 58,365.00 | 15 | −$5,428.43 |
| long | 3 May 2024 59,060.60 | 15 Jun 2024 66,044.00 | 43 | $6,983.39 |
| long | 16 Jun 2024 66,228.30 | 25 Jun 2024 60,293.30 | 9 | −$5,934.95 |
| long | 14 Jul 2024 59,204.00 | 3 Aug 2024 61,498.30 | 20 | $2,294.33 |
| long | 20 Aug 2024 59,438.50 | 28 Aug 2024 59,415.00 | 8 | −$23.50 |
| long | 30 Aug 2024 59,359.00 | 7 Sep 2024 53,963.00 | 8 | −$5,396.03 |
| long | 14 Sep 2024 60,498.00 | 2 Oct 2024 60,804.90 | 18 | $306.93 |
| long | 4 Oct 2024 60,752.70 | 4 Nov 2024 68,776.00 | 31 | $8,023.27 |
| long | 6 Nov 2024 69,372.00 | 12 Nov 2024 88,648.00 | 6 | $19,276.00 |
| long | 5 Dec 2024 98,587.30 | 20 Dec 2024 97,461.90 | 15 | −$1,125.46 |
| long | 24 Dec 2024 94,881.50 | 14 Jan 2025 94,536.10 | 21 | −$345.36 |
| long | 15 Jan 2025 96,560.90 | 4 Feb 2025 101,329.00 | 20 | $4,767.66 |
| long | 7 Feb 2025 96,554.40 | 26 Feb 2025 88,680.40 | 19 | −$7,873.96 |
| long | 15 Mar 2025 83,983.20 | 7 Apr 2025 78,430.00 | 23 | −$5,553.19 |
| long | 12 Apr 2025 83,423.80 | 22 May 2025 109,644.00 | 40 | $26,220.20 |
| long | 29 May 2025 107,782.00 | 6 Jun 2025 101,509.00 | 8 | −$6,273.09 |
| long | 8 Jun 2025 105,552.00 | 21 Jun 2025 103,298.00 | 13 | −$2,254.17 |
| long | 24 Jun 2025 105,334.00 | 16 Jul 2025 117,758.00 | 22 | $12,424.10 |
| long | 28 Jul 2025 119,416.00 | 3 Aug 2025 112,546.00 | 6 | −$6,869.21 |
| long | 5 Aug 2025 115,055.00 | 20 Aug 2025 112,873.00 | 15 | −$2,182.08 |
| long | 21 Aug 2025 114,271.00 | 30 Aug 2025 108,377.00 | 9 | −$5,893.83 |
| long | 8 Sep 2025 111,137.00 | 26 Sep 2025 108,994.00 | 18 | −$2,142.86 |
| long | 27 Sep 2025 109,643.00 | 7 Oct 2025 124,659.00 | 10 | $15,015.10 |
| long | 1 Nov 2025 109,608.00 | 5 Nov 2025 101,497.00 | 4 | −$8,110.78 |
| long | 4 Dec 2025 93,429.90 | 19 Dec 2025 85,516.40 | 15 | −$7,913.54 |
| long | 21 Dec 2025 88,360.90 | 21 Jan 2026 88,427.70 | 31 | $66.75 |
| long | 23 Jan 2026 89,559.70 | 30 Jan 2026 84,650.20 | 7 | −$4,909.52 |
| long | 3 Mar 2026 68,830.10 | 28 Mar 2026 66,407.30 | 25 | −$2,422.78 |
| long | 29 Mar 2026 66,377.00 | 19 May 2026 77,001.90 | 51 | $10,624.80 |
| long | 21 May 2026 77,552.20 | 29 May 2026 73,617.50 | 8 | −$3,934.72 |
| long | 28 Jun 2026 60,029.00 | 22 Aug 2026 78,338.00 | 55 | $18,309.00 |
| long | 9 Sep 2026 78,455.80 | 9 Oct 2026 81,754.50 | 30 | $3,298.66 |
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] Bollinger Bands Consolidation Strategy this page | — | v4 strategy | +39.1% | 33 | 19.6% |
| [KL] Bollinger bands + RSI Strategy by DojiEmoji | sma stdev atr ema rsi | v4 strategy | +28.2% | 30 | 14.7% |
| Full strategy AllinOne with risk management MACD RSI PSAR ATR MA by SoftKill21 | rsi sma stdev ema atr | v4 strategy | +2,985,110.0% | 3 | 2,350,820.0% |
| 3 x EMA + Stochastic RSI + ATR by tomimarson | ema rsi sma atr | v4 strategy | −42.3% | 18 | 44.8% |
| Take Profit On Trend (by BHD_Trade_Bot) by BHD_Trade_Bot | ema rsi stdev sma | v4 strategy | +54.8% | 9 | 13.1% |
| Atrend by OTS by prim722 | ema rsi sma atr | v4 strategy | +12.1% | 76 | 34.4% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 38 ms | With a workaround 1,000 bars in 78 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
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// //@version=4strategy("[KL] BB Consolidation Strategy",overlay=true,pyramiding=1)// Timeframe {backtest_timeframe_start = input(defval = timestamp("01 Apr 2016 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)// }// Bollinger bands (sdv=2, len=20) {BOLL_length = 20, BOLL_src = close, SMA20 = sma(BOLL_src, BOLL_length), BOLL_sDEV_x2 = 2 * stdev(BOLL_src, BOLL_length)BOLL_upper = SMA20 + BOLL_sDEV_x2, BOLL_lower = SMA20 - BOLL_sDEV_x2plot(SMA20, "Basis", color=#872323, offset = 0)BOLL_p1 = plot(BOLL_upper, "BOLL Upper", color=color.navy, offset = 0, transp=50)BOLL_p2 = plot(BOLL_lower, "BOLL Lower", color=color.navy, offset = 0, transp=50)fill(BOLL_p1, BOLL_p2, title = "Background", color=#198787, transp=85)// }// Volatility Indicators {ATR_x2 = atr(BOLL_length) * 2 // multiplier aligns with BOLLavg_atr = sma(ATR_x2, input(1,title="No. of candles to lookback when determining ATR is decreasing"))plot(SMA20+ATR_x2, "SMA20 + ATR_x2", color=color.gray, offset = 0, transp=50)plot(SMA20-ATR_x2, "SMA20 - ATR_x2", color=color.gray, offset = 0, transp=50)plotchar(ATR_x2, "ATR_x2", "", location = location.bottom)//}// Trailing stop loss {var entry_price = float(0), var stop_loss_price = float(0), trail_profit_line_color = color.greenvar trailing_SL_buffer = float(0)var risk_value = trailing_SL_buffer //declared separately; trailing_SL_buffer may be tightened subsequentlyUPDATE_ATR_LEN = input(true, title="Update the size of stoploss after entry")if UPDATE_ATR_LEN and strategy.position_size > 0 and close < open trailing_SL_buffer := ATR_x2TSL_source = input(title="Source of TSL", defval="low", options=["close","low","middle of both"])get_TSL_src() => result = float(-1) case = TSL_source if case == "close" result := close else if case == "low" result := low else if case == "middle of both" result := avg(low, close) if strategy.position_size == 0 or not within_timeframe trail_profit_line_color := color.black // make TSL line less visible stop_loss_price := get_TSL_src() - ATR_x2 // " else if strategy.position_size > 0 stop_loss_price := max(stop_loss_price, get_TSL_src() - trailing_SL_buffer)if strategy.position_size > 0 and stop_loss_price > stop_loss_price[1] alert("Stop loss limit raised", alert.freq_once_per_bar)// } end of Trailing stop loss//Buy setup - Long positions {is_squeezing = ATR_x2 > BOLL_sDEV_x2if is_squeezing and within_timeframe and not is_squeezing[1] alert("BOLL bands are squeezing", alert.freq_once_per_bar)else if not is_squeezing and within_timeframe and is_squeezing[1] alert("BOLL bands stopped squeezing", alert.freq_once_per_bar)ema_trend = ema(close, 20)VERBOSE = input(true, title="Show text descriptions of buy/sell orders")concat(a, b) => concat = " " if VERBOSE concat := a if a != "" concat := concat + ", " concat := concat + b concat// } end of Buy setup - Long positions// Sell setup - Long position {REWARD_RATIO_EXIT = input(3, title="Reward ratio for exit (if selected above)")IGNORE_EXIT_CONSOL = input(true,title="Ignore sell signals during consolidation (unless hitting stop loss)")bearish_candle1 = (close - open)/(high - low) < -0.5 and high > max(close[1],open[1]) and abs(open-close) > BOLL_sDEV_x2/2gapped_down = high < low[1] and close < openis_bearish = bearish_candle1 or gapped_downreward_met = close >= (entry_price + (REWARD_RATIO_EXIT * risk_value))rsi10 = rsi(close, 10), rsi14 = rsi(close, 14)overbought = rsi14 > 70 and rsi10 > rsi14EXIT_OVERBOUGHT = input(false,title="Exit position when overbought")TIGHTEN_TSL_BEARISH = input(true,title="Tighten TSL instead of exiting when candle is bearish")// } end of Sell setup - Long position// MAIN:if within_timeframe entry_msg = "" exit_msg = "" // ENTRY confirm_entry = false conf_count = 0 if avg_atr <= avg_atr[1] // "decreased volatility" conf_count := conf_count + 1 if open < close // "Green candlestick" conf_count := conf_count + 1 entry_msg := concat(entry_msg, "green") else if abs(close-open)/abs(high-low) < 0.1 //"Doji" conf_count := conf_count + 1 entry_msg := concat(entry_msg, "doji") if ema_trend > ema_trend[1] //"Upward EMA20" conf_count := conf_count + 1 entry_msg := concat(entry_msg, "+ve ema20") confirm_entry := conf_count >= 2 if is_squeezing and confirm_entry and strategy.position_size == 0 strategy.entry("Long",strategy.long, comment=entry_msg) entry_price := close trailing_SL_buffer := ATR_x2 risk_value := trailing_SL_buffer // may be adjusted during holding-period stop_loss_price := get_TSL_src() - trailing_SL_buffer // EXIT if strategy.position_size > 0 SL_hit = low < stop_loss_price bExit = false if close > entry_price and SL_hit exit_msg := concat(exit_msg, "profit [TSL]") bExit := true else if close <= entry_price and SL_hit exit_msg := concat(exit_msg, "stop loss") bExit := true else if (not IGNORE_EXIT_CONSOL or not is_squeezing) and is_bearish and not TIGHTEN_TSL_BEARISH exit_msg := concat(exit_msg, "bearish") bExit := true if reward_met exit_msg := concat(exit_msg, "reward met") bExit := true else if overbought exit_msg := concat(exit_msg, "overbought") bExit := EXIT_OVERBOUGHT if TIGHTEN_TSL_BEARISH and is_bearish stop_loss_price := max(low-(low-stop_loss_price)*0.9,stop_loss_price) else if overbought and not EXIT_OVERBOUGHT stop_loss_price := max(low-(low-stop_loss_price)*0.7,stop_loss_price) strategy.close("Long", when=bExit, comment=exit_msg)// plot TSL lineplot(stop_loss_price, color=trail_profit_line_color)// CLEAN UP:if strategy.position_size == 0 and not is_squeezing entry_price := 0 trailing_SL_buffer := float(0) stop_loss_price := float(0) risk_value := 0