Pine Script v4 · Strategy
[KL] Relative Volume + ATR Strategy
A Pine Script v4 strategy built on ATR (14), SMA (ATR_volat, 14) and Stdev (ATR_volat, 14). 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. 10 settings; defaults include Length of ATR to determine volatility 14, Length of ATR for trailing stop loss 14 and ATR Multiplier for trailing stop loss 2.0.
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] Relative Volume + ATR Strategy 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 [KL] Relative Volume + ATR Strategy do on BTCUSDT 1D with default settings?
- −32.70%
- From 26 Jan 2024 to 21 Oct 2026, over 10 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can [KL] Relative Volume + ATR 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] Relative Volume + ATR Strategy have?
- 1
- tostring(prefix) + tostring(msg) + tostring(suffix).
- Does [KL] Relative Volume + ATR Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
BTCUSDT · 1D · default settings
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −32.70%
- −$32,695.30
- Buy and hold
- +102.5%
- same period
- Closed trades
- 10
- 2 won · 8 lost
- Profitable
- 20.0%
- of closed trades
- Profit factor
- 0.28
- gross profit ÷ gross loss
- Max drawdown
- 33.26%
- −$33,263.20
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. 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.
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 atr → ta.atr, sma → ta.sma, stdev → ta.stdev, max → math.max, log → math.log, pow → math.pow, tostring → str.tostring.
v4 → v5
VerifiedSame 10 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 7: input(type = input.time) → input.time()
- Line 9: input(type = input.time) → input.time()
- Line 13: atr → ta.atr
- Line 14: sma → ta.sma
- Line 15: stdev → ta.stdev
15 more edits
- Line 18: atr → ta.atr
- Line 18: input(type = input.float) → input.float()
- Line 26: max → math.max
- Line 31: sma → ta.sma
- Line 32: input(type = input.float) → input.float()
- Line 37: log → math.log
- Line 38: sma → ta.sma
- Line 38: pow → math.pow
- Line 38: stdev → ta.stdev
- Line 43: tostring → str.tostring
- Line 43: tostring → str.tostring
- Line 44: tostring → str.tostring
- Line 44: tostring → str.tostring
- Line 44: tostring → str.tostring
- Line 48: tostring → str.tostring
⋯// © DojiEmoji (kevinhhl)-//@version=4+//@version=5strategy("[KL] Relative Volume + ATR 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("01 May 2021 19:30 +0000"), title = "Backtest End Time (if checked above)", type = input.time)+backtest_timeframe_end = input.time(defval = timestamp("01 May 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)⋯len_volat = input(14,title="Length of ATR to determine volatility")-ATR_volat = atr(len_volat)+ATR_volat = ta.atr(len_volat)-avg_ATR_volat = sma(ATR_volat, len_volat)+avg_ATR_volat = ta.sma(ATR_volat, len_volat)-std_ATR_volat = stdev(ATR_volat, len_volat)+std_ATR_volat = ta.stdev(ATR_volat, len_volat)// }// 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 = low⋯else if strategy.position_size > 0- stop_loss_price := max(stop_loss_price, TSL_source - ATR_X2_TSL)+ stop_loss_price := math.max(stop_loss_price, TSL_source - ATR_X2_TSL) TSL_transp := 0⋯// Signals for entry {-_avg_vol = sma(volume,input(20, title="SMA(volume) length (for relative comparison)"))+_avg_vol = ta.sma(volume,input(20, title="SMA(volume) length (for relative comparison)"))-_relative_vol = _avg_vol * input(1.5,title="Multiple of avg vol to consider relative volume as being high",type=input.float)+_relative_vol = _avg_vol * input.float(1.5,title="Multiple of avg vol to consider relative volume as being high")entry_signal1 = volume > _relative_vol⋯len_drift = input(50,title="Length of Drift")-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.5entry_signal3 = drift > drift[1] or not USE_DRIFT⋯ prefix = "[" + syminfo.root + "] "- suffix = "(P=" + tostring(close) + "; atr=" + tostring(ATR_volat) + ")"+ suffix = "(P=" + str.tostring(close) + "; atr=" + str.tostring(ATR_volat) + ")"- alert(tostring(prefix) + tostring(msg) + tostring(suffix), alert.freq_once_per_bar)+ alert(str.tostring(prefix) + str.tostring(msg) + str.tostring(suffix), alert.freq_once_per_bar)// MAIN:⋯ if strategy.position_size > 0 and strategy.position_size[1] > 0 and (stop_loss_price/stop_loss_price[1]-1) > 0.005- alert_per_bar("TSL raised to " + tostring(stop_loss_price))+ alert_per_bar("TSL raised to " + str.tostring(stop_loss_price)) exit_msg = close <= strategy.position_avg_price ? "stop loss" : "take profit"⋯Same 10 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 8: input() → input.bool()
- Line 12: input() → input.int()
- Line 18: input() → input.int()
- Line 31: input() → input.int()
- Line 35: input() → input.bool()
- Line 36: input() → input.int()
⋯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("01 May 2021 19:30 +0000"), title = "Backtest End Time (if checked above)")⋯// }-len_volat = input(14,title="Length of ATR to determine volatility")+len_volat = input.int(14,title="Length of ATR to determine volatility")ATR_volat = ta.atr(len_volat)⋯// 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 = low⋯// Signals for entry {-_avg_vol = ta.sma(volume,input(20, title="SMA(volume) length (for relative comparison)"))+_avg_vol = ta.sma(volume,input.int(20, title="SMA(volume) length (for relative comparison)"))_relative_vol = _avg_vol * input.float(1.5,title="Multiple of avg vol to consider relative volume as being high")⋯entry_signal2 = ATR_volat < avg_ATR_volat + std_ATR_volat and ATR_volat > avg_ATR_volat - std_ATR_volat-USE_DRIFT = input(true,title="Use drift to determine uptrend")+USE_DRIFT = input.bool(true,title="Use drift to determine uptrend")-len_drift = input(50,title="Length of Drift")+len_drift = input.int(50,title="Length of Drift")percentage_chng = math.log(close/close[1])⋯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: 10 before, 9 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] Relative Volume + ATR 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("01 May 2021 19:30 +0000") | time | within_timeframe |
| Length of ATR to determine volatility | 14 | int | ATR_volat avg_ATR_volat std_ATR_volat |
| Length of ATR for trailing stop loss | 14 | int | stop_loss_price |
| ATR Multiplier for trailing stop loss | 2.0 | float | stop_loss_price |
| SMA(volume) length (for relative comparison) | 20 | int | _relative_vol |
| Multiple of avg vol to consider relative volume as being high | 1.5 | float | entry_signal1 |
| Use drift to determine uptrend | true | bool | entry_signal3 |
| Length of Drift | 50 | int | drift |
What it calculates
- ATR (
atr,ta.atrin v5)Length of ATR to determine volatility (14)ATR_volat · line 13 - SMA (
sma,ta.smain v5)ATR_volat, Length of ATR to determine volatility (14)avg_ATR_volat · line 14 - Stdev (
stdev,ta.stdevin v5)ATR_volat, Length of ATR to determine volatility (14)std_ATR_volat · line 15 - SMA / Stdev (
sma,ta.smain v5)percentage_chng, Length of Drift (50)drift · line 38
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 |
|---|---|---|---|
| Close Long | strategy.position_size > 0 and TSL_source <= stop_loss_price | On | 52 |
| Open Long long | (strategy.position_size == 0 or (strategy.position_size > 0 and close > stop_loss_price)) and entry_signal1 and entry_signal2 and entry_signal3 | On | 56 |
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 | tostring(prefix) + tostring(msg) + tostring(suffix) | 44 |
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 | −32.7% | +102.5% | 10 | 20% | 0.28 | 33.3% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +11.3% | +5.5% | 14 | 36% | 2.14 | 5.1% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −2.1% | +9.8% | 13 | 15% | 0.41 | 2.7% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.1% | +8.4% | 18 | 22% | 1.10 | 0.3% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.1% | +16.7% | 6 | 33% | 0.15 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 14 | 43% | 2.34 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.1% | +148.3% | 10 | 60% | 1.26 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.1% | +17.1% | 18 | 17% | 0.71 | 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 | 4 | 0% | −$21,386.40 |
| Sideways market | 5 | 40% | −$6,582.34 |
| Falling market | 1 | 0% | −$4,726.63 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
41.5%
The 95th percentile of the worst drawdown when the same 10 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 32.7% to 41.5%; the tick is what actually happened, 32.7%. Order luck alone moves it this much.
All 10 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 21 May 2024 71,446.60 | 15 Jun 2024 66,044.00 | 25 | −$5,402.63 |
| long | 15 Oct 2024 66,084.00 | 5 Nov 2024 67,850.00 | 21 | $1,766.01 |
| long | 27 Feb 2025 84,250.10 | 11 Mar 2025 78,595.90 | 12 | −$5,654.23 |
| long | 5 Apr 2025 83,889.90 | 8 Apr 2025 79,163.20 | 3 | −$4,726.63 |
| long | 23 Apr 2025 93,443.00 | 1 Jun 2025 104,592.00 | 39 | $11,148.90 |
| long | 14 Jun 2025 106,067.00 | 23 Jun 2025 100,964.00 | 9 | −$5,102.72 |
| long | 26 Jul 2025 117,614.00 | 3 Aug 2025 112,546.00 | 8 | −$5,067.96 |
| long | 12 Aug 2025 118,686.00 | 20 Aug 2025 112,873.00 | 8 | −$5,813.05 |
| long | 14 Jan 2026 95,414.00 | 21 Jan 2026 88,427.70 | 7 | −$6,986.33 |
| long | 29 May 2026 73,617.50 | 3 Jun 2026 66,760.80 | 5 | −$6,856.68 |
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] Relative Volume + ATR Strategy this page | — | v4 strategy | −32.7% | 10 | 33.3% |
| MCL-YG Pair Trading Strategy by shark792 | sma stdev atr | v5 strategy | No trades on this market | ||
| [KL] Mean Reversion (ATR) Strategy by DojiEmoji | atr sma stdev | v5 strategy | No trades on this market | ||
| [KL] Double Bollinger Bands Strategy (for Crypto-FOREX) by DojiEmoji | sma stdev atr | v4 strategy | −42.2% | 42 | 56.8% |
| 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 26 ms | Yes 1,000 bars in 47 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/// © DojiEmoji (kevinhhl)//@version=4strategy("[KL] Relative Volume + ATR Strategy",overlay=true,pyramiding=1)ENUM_LONG = "Long"// 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("01 May 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)// }len_volat = input(14,title="Length of ATR to determine volatility")ATR_volat = atr(len_volat)avg_ATR_volat = sma(ATR_volat, len_volat)std_ATR_volat = stdev(ATR_volat, len_volat)// }// 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 = lowvar stop_loss_price = float(0)TSL_line_color = color.green, TSL_transp = 100if strategy.position_size == 0 or not within_timeframe TSL_line_color := color.black stop_loss_price := TSL_source - ATR_X2_TSL else if strategy.position_size > 0 stop_loss_price := max(stop_loss_price, TSL_source - ATR_X2_TSL) TSL_transp := 0plot(stop_loss_price, color=color.new(TSL_line_color, TSL_transp))// }// Signals for entry {_avg_vol = sma(volume,input(20, title="SMA(volume) length (for relative comparison)"))_relative_vol = _avg_vol * input(1.5,title="Multiple of avg vol to consider relative volume as being high",type=input.float)entry_signal1 = volume > _relative_volentry_signal2 = ATR_volat < avg_ATR_volat + std_ATR_volat and ATR_volat > avg_ATR_volat - std_ATR_volatUSE_DRIFT = input(true,title="Use drift to determine uptrend")len_drift = input(50,title="Length of Drift")percentage_chng = log(close/close[1])drift = sma(percentage_chng, len_drift) - pow(stdev(percentage_chng, len_drift),2)*0.5entry_signal3 = drift > drift[1] or not USE_DRIFT// }alert_per_bar(msg)=> prefix = "[" + syminfo.root + "] " suffix = "(P=" + tostring(close) + "; atr=" + tostring(ATR_volat) + ")" alert(tostring(prefix) + tostring(msg) + tostring(suffix), alert.freq_once_per_bar)// MAIN:if within_timeframe if strategy.position_size > 0 and strategy.position_size[1] > 0 and (stop_loss_price/stop_loss_price[1]-1) > 0.005 alert_per_bar("TSL raised to " + tostring(stop_loss_price)) exit_msg = close <= strategy.position_avg_price ? "stop loss" : "take profit" // EXIT: if strategy.position_size > 0 and TSL_source <= stop_loss_price strategy.close(ENUM_LONG, comment=exit_msg) // ENTRY: else if (strategy.position_size == 0 or (strategy.position_size > 0 and close > stop_loss_price)) and entry_signal1 and entry_signal2 and entry_signal3 entry_msg = strategy.position_size > 0 ? "adding" : "initial" strategy.entry(ENUM_LONG, strategy.long, comment=entry_msg)// CLEAN UP:if strategy.position_size == 0 stop_loss_price := float(0)