Pine Script v4 · Strategy
Optimized Keltner Channels SL-TP Strategy for BTC
A Pine Script v4 strategy built on SMA (source, 9), EMA (source, 9) and RMA or TR or ATR (true, 9). It has 6 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. 8 settings; defaults include Length 9, Multiplier 1.0 and Source close.
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 Optimized Keltner Channels SL-TP Strategy for BTC 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 Optimized Keltner Channels SL-TP Strategy for BTC do on BTCUSDT 1D with default settings?
- −19.44%
- From 26 Jan 2024 to 21 Oct 2026, over 36 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Optimized Keltner Channels SL-TP Strategy for BTC be converted to Pine Script v5?
- No
- The automatic step from v4 to v5 gives a script that does not run in v5 (Pine Script v5 Error at @L25:C11: No value assigned to the `transp` parameter in color.new()), so no v5 download is offered; the edits are shown below.
- Does Optimized Keltner Channels SL-TP Strategy for BTC use a stop loss?
- Yes
- strategy.exit sets a stop on line 48, 49.
- Does Optimized Keltner Channels SL-TP Strategy for BTC run in Zeal Terminal?
- Yes
- It compiles in 16 ms and runs 1,000 bars in 57 ms, in the browser.
BTCUSDT · 1D · default settings
- Upper
- Basis
- Lower
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −19.44%
- −$19,440.70
- Buy and hold
- +102.5%
- same period
- Closed trades
- 36
- 11 won · 25 lost
- Profitable
- 30.6%
- of closed trades
- Profit factor
- 0.86
- gross profit ÷ gross loss
- Max drawdown
- 69.98%
- −$78,946.40
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.
-
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 5) 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.
-
Note
Uses transp =, which v5 removed line 25
transp = on line 25. 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, ema → ta.ema, rma → ta.rma, tr → ta.tr, atr → ta.atr, crossover → ta.crossover, crossunder → ta.crossunder.
v4 → v5
DiffersThe v5 version does not run in Zeal Terminal's engine: Pine Script v5 Error at @L25:C11: No value assigned to the `transp` parameter in color.new()
- Line 3: //@version=4 → //@version=5
- Line 11: input(type = input.float) → input.float()
- Line 12: input(type = input.float) → input.float()
- Line 14: sma → ta.sma
- Line 15: ema → ta.ema
- Line 18: rma → ta.rma
6 more edits
- Line 18: tr → ta.tr
- Line 18: atr → ta.atr
- Line 18: rma → ta.rma
- Line 25: transp = 95 → color.new(#0094FF, 95)
- Line 26: crossover → ta.crossover
- Line 27: crossunder → ta.crossunder
⋯// © chanu_lev10k-//@version=4+//@version=5strategy(title="Optimized Keltner Channels SL/TP Strategy for BTC", overlay=true)⋯atrlength = input(19, "ATR Length")-sl = input(defval=20, minval=0, type=input.float, step=0.1, title="Stop Loss (%)")+sl = input.float(defval=20, minval=0, step=0.1, title="Stop Loss (%)")-tp = input(defval=20.3, minval=0, type=input.float, step=0.1, title="Take Profit (%)")+tp = input.float(defval=20.3, minval=0, step=0.1, title="Take Profit (%)")esma(source, length)=>- s = sma(source, length)+ s = ta.sma(source, length)- e = ema(source, length)+ e = ta.ema(source, length) exp ? e : sma = esma(src, length)-rangema = BandsStyle == "True Range" ? rma(tr(true), length) : BandsStyle == "Average True Range" ? atr(atrlength) : rma(high - low, length)+rangema = BandsStyle == "True Range" ? ta.rma(ta.tr(true), length) : BandsStyle == "Average True Range" ? ta.atr(atrlength) : ta.rma(high - low, length)upper = ma + rangema * mult⋯l = plot(lower, color=color.red, title="Lower")-fill(u, l, color.new(color=#0094FF, transp=95), title="Background")+fill(u, l, color.new(color=color.new(#0094FF, 95)), title="Background")-crossUpper = crossover(src, upper)+crossUpper = ta.crossover(src, upper)-crossLower = crossunder(src, lower)+crossLower = ta.crossunder(src, lower)bprice = 0.0⋯Clean v5
Not verifiedThe v5 script does not run in Zeal Terminal's engine (Pine Script v5 Error at @L25:C11: No value assigned to the `transp` parameter in color.new()), so this step could not be checked.
- Line 5: input() → input.int()
- Line 6: input() → input.float()
- Line 7: input() → input.source()
- Line 8: input() → input.bool()
- Line 9: input() → input.string()
- Line 10: input() → input.int()
⋯strategy(title="Optimized Keltner Channels SL/TP Strategy for BTC", overlay=true)-length = input(9, minval=1)+length = input.int(9, minval=1)-mult = input(1.0, "Multiplier")+mult = input.float(1.0, "Multiplier")-src = input(close, title="Source")+src = input.source(close, title="Source")-exp = input(true, "Use Exponential MA")+exp = input.bool(true, "Use Exponential MA")-BandsStyle = input("Average True Range", options = ["Average True Range", "True Range", "Range"], title="Bands Style")+BandsStyle = input.string("Average True Range", options = ["Average True Range", "True Range", "Range"], title="Bands Style")-atrlength = input(19, "ATR Length")+atrlength = input.int(19, "ATR Length")sl = input.float(defval=20, minval=0, step=0.1, title="Stop Loss (%)")⋯Does it run in Pine Script v6?
Not tested
The v5 script does not run in Zeal Terminal's engine (Pine Script v5 Error at @L25:C11: No value assigned to the `transp` parameter in color.new()), so this step could not be checked. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © chanu_lev10k-//@version=5+//@version=6strategy(title="Optimized Keltner Channels SL/TP Strategy for BTC", 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 |
|---|---|---|---|
| Length (no label in the script) | 9 | int · min 1 | s e ma rangema |
| Multiplier | 1.0 | float | upper lower |
| Source | close | source | ma crossUpper crossLower cancelBcond cancelScond |
| Use Exponential MA | true | bool | line 16 |
| Bands Style | Average True Range | string · options ["Average True Range", "True Range", "Range"] | rangema |
| ATR Length | 19 | int | rangema |
| Stop Loss (%) | 20 | float · min 0 · step 0.1 | exit Long exit exit Short exit |
| Take Profit (%) | 20.3 | float · min 0 · step 0.1 | exit Long exit exit Short exit |
What it calculates
- SMA (
sma,ta.smain v5)source, Length (9)s · line 14 - EMA (
ema,ta.emain v5)source, Length (9)e · line 15 - RMA / TR / ATR (
rma,ta.rmain v5)tr(true), Length (9) · chosen by Bands Stylerangema · line 18 - Crossover (
crossover,ta.crossoverin v5)Source (close), uppercrossUpper · line 26 - Crossunder (
crossunder,ta.crossunderin v5)Source (close), lowercrossLower · line 27
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 KltChLE | (cancelBcond) | On | 41 |
| Open KltChLE long | (crossUpper) | On | 43 |
| cancel KltChSE | (cancelScond) | On | 45 |
| Open KltChSE short | (crossLower) | On | 47 |
| Exit Long exit stop and limit | every bar | On | 48 |
| Exit Short exit stop and limit | every bar | 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 | −19.4% | +102.5% | 36 | 31% | 0.86 | 70.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −5.3% | +5.5% | 34 | 29% | 0.89 | 14.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −1.0% | +9.8% | 46 | 39% | 0.90 | 4.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.1% | +8.4% | 29 | 31% | 1.05 | 0.6% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 48 | 42% | 1.01 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.0% | +21.5% | 34 | 29% | 0.78 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.0% | +148.3% | 35 | 40% | 1.01 | 0.6% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 28 | 29% | 1.09 | 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 | 9 | 22% | −$12,119.70 |
| Sideways market | 19 | 37% | $12,439.40 |
| Falling market | 8 | 25% | −$19,760.40 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
68.1%
The 95th percentile of the worst drawdown when the same 36 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 39.6% to 68.1%; the tick is what actually happened, 60.4%. Order luck alone moves it this much.
All 36 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 27 Feb 2024 54,476.50 | 4 Mar 2024 67,288.60 | 6 | $12,812.10 |
| long | 7 Mar 2024 66,074.10 | 17 Mar 2024 65,300.60 | 10 | −$773.43 |
| short | 17 Mar 2024 65,300.60 | 16 May 2024 66,206.50 | 60 | −$905.89 |
| long | 16 May 2024 66,206.50 | 25 Jun 2024 60,293.30 | 40 | −$5,913.22 |
| short | 25 Jun 2024 60,293.30 | 16 Jul 2024 64,724.20 | 21 | −$4,430.86 |
| long | 16 Jul 2024 64,724.20 | 3 Aug 2024 61,498.30 | 18 | −$3,225.83 |
| short | 3 Aug 2024 61,498.30 | 5 Aug 2024 49,691.60 | 2 | $11,806.70 |
| long | 24 Aug 2024 64,037.30 | 6 Sep 2024 52,801.30 | 13 | −$11,236.00 |
| short | 7 Sep 2024 53,963.00 | 14 Sep 2024 60,498.00 | 7 | −$6,535.05 |
| long | 14 Sep 2024 60,498.00 | 2 Oct 2024 60,804.90 | 18 | $306.91 |
| short | 2 Oct 2024 60,804.90 | 15 Oct 2024 66,084.00 | 13 | −$5,279.08 |
| long | 15 Oct 2024 66,084.00 | 11 Nov 2024 82,399.10 | 27 | $16,315.10 |
| long | 20 Nov 2024 92,310.80 | 25 Feb 2025 91,552.90 | 97 | −$757.93 |
| short | 25 Feb 2025 91,552.90 | 3 Mar 2025 94,270.00 | 6 | −$2,717.14 |
| long | 3 Mar 2025 94,270.00 | 10 Mar 2025 80,734.40 | 7 | −$13,535.60 |
| short | 10 Mar 2025 80,734.40 | 23 Apr 2025 93,443.00 | 44 | −$12,708.60 |
| long | 23 Apr 2025 93,443.00 | 6 Jun 2025 101,509.00 | 44 | $8,065.67 |
| short | 6 Jun 2025 101,509.00 | 10 Jun 2025 110,263.00 | 4 | −$8,754.36 |
| long | 10 Jun 2025 110,263.00 | 2 Aug 2025 113,298.00 | 53 | $3,034.89 |
| short | 2 Aug 2025 113,298.00 | 11 Aug 2025 119,294.00 | 9 | −$5,996.35 |
| long | 11 Aug 2025 119,294.00 | 20 Aug 2025 112,873.00 | 9 | −$6,421.34 |
| short | 20 Aug 2025 112,873.00 | 12 Sep 2025 115,483.00 | 23 | −$2,609.77 |
| long | 12 Sep 2025 115,483.00 | 24 Sep 2025 111,999.00 | 12 | −$3,483.91 |
| short | 24 Sep 2025 111,999.00 | 2 Oct 2025 118,595.00 | 8 | −$6,596.21 |
| long | 2 Oct 2025 118,595.00 | 11 Oct 2025 112,774.00 | 9 | −$5,820.51 |
| short | 11 Oct 2025 112,774.00 | 16 Nov 2025 93,368.40 | 36 | $19,406.00 |
| short | 20 Nov 2025 91,554.90 | 6 Jan 2026 93,859.70 | 47 | −$2,304.77 |
| long | 6 Jan 2026 93,859.70 | 21 Jan 2026 88,427.60 | 15 | −$5,432.07 |
| short | 21 Jan 2026 88,427.60 | 4 Feb 2026 73,046.30 | 14 | $15,381.40 |
| long | 5 Mar 2026 72,666.80 | 28 Mar 2026 66,407.30 | 23 | −$6,259.51 |
| short | 28 Mar 2026 66,407.30 | 8 Apr 2026 71,924.20 | 11 | −$5,516.96 |
| long | 8 Apr 2026 71,924.20 | 23 May 2026 75,539.50 | 45 | $3,615.26 |
| short | 23 May 2026 75,539.50 | 4 Jun 2026 62,518.50 | 12 | $13,021.00 |
| short | 10 Jun 2026 61,730.00 | 20 Aug 2026 69,334.80 | 71 | −$7,604.81 |
| long | 20 Aug 2026 69,334.80 | 21 Sep 2026 85,813.90 | 32 | $16,479.10 |
| long | 22 Sep 2026 86,620.00 | 9 Oct 2026 81,754.40 | 17 | −$4,865.57 |
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 |
|---|---|---|---|---|---|
| Optimized Keltner Channels SL-TP Strategy for BTC this page | — | v4 strategy | −19.4% | 36 | 70.0% |
| Strategy - Trend Chaser - PSe by ElvinKennedyHernandezLatayan | tr atr rma ema sma | v5 strategy | No trades on this market | ||
| ATR with EOM and VORTEX by SoftKill21 | atr ema sma | v4 strategy | +66.2% | 22 | 29.9% |
| Scalping Trading System bot Crypto and Stocks by exlux99 | sma ema tr | v5 strategy | +6.5% | 5 | 1.0% |
| cATRpillar Strategy by Obi-wanClintobe | atr sma ema | v4 strategy | +1.02827e+24% | 27 | 88,097.2% |
| Optimized Zhaocaijinbao strategy by fzj20020403 | sma ema atr | v5 strategy | +15.6% | 12 | 9.8% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 16 ms | Yes 1,000 bars in 57 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/// © chanu_lev10k//@version=4strategy(title="Optimized Keltner Channels SL/TP Strategy for BTC", overlay=true)length = input(9, minval=1)mult = input(1.0, "Multiplier")src = input(close, title="Source")exp = input(true, "Use Exponential MA")BandsStyle = input("Average True Range", options = ["Average True Range", "True Range", "Range"], title="Bands Style")atrlength = input(19, "ATR Length")sl = input(defval=20, minval=0, type=input.float, step=0.1, title="Stop Loss (%)")tp = input(defval=20.3, minval=0, type=input.float, step=0.1, title="Take Profit (%)")esma(source, length)=> s = sma(source, length) e = ema(source, length) exp ? e : sma = esma(src, length)rangema = BandsStyle == "True Range" ? rma(tr(true), length) : BandsStyle == "Average True Range" ? atr(atrlength) : rma(high - low, length)upper = ma + rangema * multlower = ma - rangema * multc = color.blueu = plot(upper, color=color.green, title="Upper")plot(ma, color=#0094FF, title="Basis")l = plot(lower, color=color.red, title="Lower")fill(u, l, color.new(color=#0094FF, transp=95), title="Background")crossUpper = crossover(src, upper)crossLower = crossunder(src, lower)bprice = 0.0bprice := crossUpper ? close+syminfo.mintick : nz(bprice[1])sprice = 0.0sprice := crossLower ? close-syminfo.mintick : nz(sprice[1])crossBcond = falsecrossBcond := crossUpper ? true : na(crossBcond[1]) ? false : crossBcond[1]crossScond = falsecrossScond := crossLower ? true : na(crossScond[1]) ? false : crossScond[1]cancelBcond = crossBcond and (src < ma or high >= bprice )cancelScond = crossScond and (src > ma or low <= sprice )if (cancelBcond) strategy.cancel("KltChLE")if (crossUpper) strategy.entry("KltChLE", strategy.long, stop=bprice, comment="Long")if (cancelScond) strategy.cancel("KltChSE")if (crossLower) strategy.entry("KltChSE", strategy.short, stop=sprice, comment="Short")strategy.exit("Long exit", "KltChLE", profit = close * tp * 0.01 / syminfo.mintick, loss = close * sl * 0.01 / syminfo.mintick)strategy.exit("Short exit", "KltChSE", profit = close * tp * 0.01 / syminfo.mintick, loss = close * sl * 0.01 / syminfo.mintick)plot(bprice, color=color.green)plot(sprice, color=color.red)