Pine Script v5 · Strategy
Kaufman's Efficiency Ratio Strategy [KL]
A Pine Script v5 strategy with no technical-analysis calculation. It has 12 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. 15 settings; defaults include Alert MSG for buying (Long position) Long entered, Alert MSG for closing (Long position) Long closed and Alert MSG for selling (Short position) Short 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 Kaufman's Efficiency Ratio Strategy [KL] repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Kaufman's Efficiency Ratio Strategy [KL] do on BTCUSDT 1D with default settings?
- +1.14%
- From 26 Jan 2024 to 21 Oct 2026, over 3 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- What alerts does Kaufman's Efficiency Ratio Strategy [KL] have?
- 4
- LONG_MSG_EXIT; SHORT_MSG_EXIT; LONG_MSG_ENTER; SHORT_MSG_ENTER.
- Does Kaufman's Efficiency Ratio Strategy [KL] run in Pine Script v6?
- Yes
- Same 3 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. The v6 script can be downloaded below.
- Does Kaufman's Efficiency Ratio Strategy [KL] use a stop loss?
- Yes
- strategy.exit sets a stop on line 103, 105.
BTCUSDT · 1D · default settings
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +1.14%
- $11,430,600.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 3
- 3 won · 0 lost
- Profitable
- 100.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 0.02%
- −$157,599.00
One run with the author's default settings, $1,000,000,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
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
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 |
|---|---|---|---|
| Alerts | |||
| Alert MSG for buying (Long position) | Long entered | string | alert |
| Alert MSG for closing (Long position) | Long closed | string | alert |
| Enter into short positions | false | bool | entry_signal_short |
| Alert MSG for selling (Short position) | Short entered | string | alert |
| Alert MSG for covering (Short position) | Short covered | string | alert |
| Time horizon of backtests | |||
| Backtest Start Time | timestamp("01 Apr 2020 13:30 +0000") | time | within_timeframe |
| Define backtest end-timeIf 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 |
| Main Signal: Kaufman's Eff. Ratio | |||
| Lookback period | 10 | int | ER |
| Order size | |||
| Allocation (%) of portfolio into this securityAssume no rebalancing | 5 | int · min 0 · max 100 | initial_order_size |
| Stop loss | |||
| ATR Multiplier for trailing stoploss | 2.0 | float | SL_buffer |
| Length of ATR for trailing stoploss | 14 | int | stop_loss_price_long stop_loss_price_short risk_amt |
| Profit taking | |||
| First level profitRelative to risk. Example: entry at $10 and inital stop loss at $9. Taking first level profit at 1R means taking profits at $11 | 1 | float | close LONG close SHORT line 73 line 83 |
| Second level profitRelative to risk. Example: entry at $10 and inital stop loss at $9. Taking second level profit at 2R means taking profits at $12 | 2 | float | close LONG close SHORT line 76 line 86 |
| Third level profitRelative to risk. Example: entry at $10 and inital stop loss at $9. Taking third level profit at 3R means taking profits at $13 | 3 | float | close LONG close SHORT line 79 line 89 |
What it calculates
No technical-analysis calculation: the script works on prices and settings directly.
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 | tp_count_long == 0 and close >= strategy.position_avg_price + (FIRST_LVL_PROFIT * risk_amt) | On | 74 |
| Close LONG | tp_count_long == 1 and close >= strategy.position_avg_price + (SECOND_LVL_PROFIT * risk_amt) | On | 77 |
| Close LONG | tp_count_long == 2 and close >= strategy.position_avg_price + (THIRD_LVL_PROFIT * risk_amt) | On | 80 |
| Close SHORT | tp_count_short == 0 and close <= strategy.position_avg_price + (-FIRST_LVL_PROFIT * risk_amt) | On | 84 |
| Close SHORT | tp_count_short == 1 and close <= strategy.position_avg_price + (-SECOND_LVL_PROFIT * risk_amt) | On | 87 |
| Close SHORT | tp_count_short == 2 and close <= strategy.position_avg_price + (-THIRD_LVL_PROFIT * risk_amt) | On | 90 |
| Close LONG | pos_opened_long and TSL_source_long <= stop_loss_price_long | On | 96 |
| Close SHORT | pos_opened_short and (TSL_source_short >= stop_loss_price_short) | On | 100 |
| Exit LONG stop | pos_opened_long | On | 103 |
| Exit SHORT stop | pos_opened_short | On | 105 |
| Open LONG long | within_timeframe and entry_signal_long | On | 111 |
| Open SHORT short | within_timeframe and entry_signal_short | On | 117 |
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 | 95 |
| (no title) | alert | SHORT_MSG_EXIT | 99 |
| (no title) | alert | LONG_MSG_ENTER | 109 |
| (no title) | alert | SHORT_MSG_ENTER | 116 |
Does it repaint?
No
No repainting patterns in the source.
- 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 | +1.1% | +102.5% | 3 | 100% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.3% | +5.5% | 3 | 100% | — | 1.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +1.8% | +9.8% | 3 | 100% | — | 0.7% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.3% | +8.4% | 3 | 100% | — | 1.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +1.9% | +16.7% | 3 | 100% | — | 0.7% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.5% | +21.5% | 3 | 100% | — | 1.8% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +2.5% | +148.3% | 3 | 100% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.3% | +17.1% | 3 | 100% | — | 0.6% | 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 | 0 | — | $0.00 |
| Sideways market | 3 | 100% | $11,430,600.00 |
| Falling market | 0 | — | $0.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
0.0%
The 95th percentile of the worst drawdown when the same 3 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 0.0% to 0.0%; the tick is what actually happened, 0.0%. Order luck alone moves it this much.
All 3 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 6 Feb 2024 42,708.70 | 25 Feb 2024 51,568.20 | 19 | $2,932,500.00 |
| long | 6 Feb 2024 42,708.70 | 26 Feb 2024 51,728.80 | 20 | $2,985,670.00 |
| long | 6 Feb 2024 42,708.70 | 17 Mar 2024 65,300.60 | 40 | $5,512,430.00 |
Similar scripts
No other indicator or strategy here shares enough of its calculations.
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- 1 setting uses the untyped input() (line 14). It still compiles in v6; input.int, input.float or input.source state the type.
- No v6-only features. v6 is not needed to run this script.
Same 3 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 14: input() → input.bool()
⋯var string LONG_MSG_EXIT = input.string("Long closed", title="Alert MSG for closing (Long position)", group=GROUP_ALERT)-USE_SHORT = input(false, title="Enter into short positions", group=GROUP_ALERT)+USE_SHORT = input.bool(false, title="Enter into short positions", group=GROUP_ALERT)ENUM_SHORT = "SHORT" ⋯Does it run in Pine Script v6?
Yes
Same 3 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.
⋯// © DojiEmoji (kevinhhl)-//@version=5+//@version=6strategy("[KL] Eff. Ratio Strategy", overlay=true, pyramiding=1, initial_capital=1000000000)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 49 ms | With a workaround 1,000 bars in 94 ms | Not measured |
| PineTS | Not tested | Not tested | 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.
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=5strategy("[KL] Eff. Ratio Strategy", overlay=true, pyramiding=1, initial_capital=1000000000)var string GROUP_ALERT = "Alerts"var string GROUP_SL = "Stop loss"var string GROUP_ORDER = "Order size"var string GROUP_TP = "Profit taking"var string GROUP_HORIZON = "Time horizon of backtests"var string GROUP_ER = "Main Signal: Kaufman's Eff. Ratio"var string ENUM_LONG = "LONG"var string LONG_MSG_ENTER = input.string("Long entered", title="Alert MSG for buying (Long position)", group=GROUP_ALERT)var string LONG_MSG_EXIT = input.string("Long closed", title="Alert MSG for closing (Long position)", group=GROUP_ALERT)USE_SHORT = input(false, title="Enter into short positions", group=GROUP_ALERT)ENUM_SHORT = "SHORT" SHORT_MSG_ENTER = input.string("Short entered", title="Alert MSG for selling (Short position)")SHORT_MSG_EXIT = input.string("Short covered", title="Alert MSG for covering (Short position)")backtest_timeframe_start = input.time(defval=timestamp("01 Apr 2020 13:30 +0000"), title="Backtest Start Time", group=GROUP_HORIZON)USE_ENDTIME = input.bool(false, title="Define backtest end-time", group=GROUP_HORIZON, tooltip="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)", group=GROUP_HORIZON)within_timeframe = time >= backtest_timeframe_start and (time <= backtest_timeframe_end or not USE_ENDTIME)// Kaufman"s efficiency ratio {er_lookbackperiod = input.int(10, title="Lookback period", group=GROUP_ER)kaufman_ER(src, len) => total_abs_chng = float(0) // [B] for i = 1 to len by 1 total_abs_chng := total_abs_chng + math.abs(src[i - 1] - src[i]) total_abs_chng net_chng = src - src[len] // [A] er = net_chng / total_abs_chng * 100 // = ([A]/[B]) * 100 erER = kaufman_ER(close, er_lookbackperiod)// Removed: Confirmation of ER being low/med/high; through backtesting, noticed it's better to simply use "entry_signal_long = ER > 0"// ER_bar = ta.sma(math.abs(ER), er_lookbackperiod)// ER_sigma = ta.stdev(math.abs(ER), er_lookbackperiod)// ER_std_err = ER_sigma / math.pow(er_lookbackperiod, 0.5)// ER_sigma_mult = input.float(1.0, "stdev multiplier", tooltip="For determining low/medium/high ER.", group=GROUP_ER)// dynamic_low = ER_bar - ER_sigma * ER_sigma_mult * ER_std_err// dynamic_high = ER_bar + ER_sigma * ER_sigma_mult * ER_std_err// ER_is_low = math.abs(ER) <= dynamic_low// ER_is_mid = math.abs(ER) > dynamic_low and math.abs(ER) < dynamic_high// ER_is_high = math.abs(ER) >= dynamic_high// } end of block: Kaufman"s efficiency ratio// Signals for entryentry_signal_long = ER > 0entry_signal_short = ER < 0 and ta.tr > ta.atr(14) and volume > ta.sma(volume, 5) and USE_SHORT// Portfolio mgtpcnt_alloc = input.int(5, title="Allocation (%) of portfolio into this security", tooltip="Assume no rebalancing", minval=0, maxval=100, group=GROUP_ORDER) / 100// Trailing stop loss ("TSL") {tsl_multi = input.float(2.0, title="ATR Multiplier for trailing stoploss", group=GROUP_SL)SL_buffer = ta.atr(input.int(14, title="Length of ATR for trailing stoploss", group=GROUP_SL)) * tsl_multiTSL_source_long = lowTSL_source_short = highvar stop_loss_price_long = float(0)var stop_loss_price_short = float(9999999999)var pos_opened_long = falsevar pos_opened_short = falsestop_loss_price_long := pos_opened_long ? math.max(stop_loss_price_long, TSL_source_long - SL_buffer) : TSL_source_long - SL_bufferstop_loss_price_short := pos_opened_short ? math.min(stop_loss_price_short, TSL_source_short + SL_buffer) : TSL_source_short + SL_buffer// } end of block: Trailing stop loss// Profit takingvar int tp_count_long = 0 // counts how many levels of profits had been taken during holding period; resets when position size becomes zerovar int tp_count_short = 0var float initial_order_size = 0 // to be divided by 3, then math.floor'ed to determine size of disposalFIRST_LVL_PROFIT = input.float(1, title="First level profit", tooltip="Relative to risk. Example: entry at $10 and inital stop loss at $9. Taking first level profit at 1R means taking profits at $11", group=GROUP_TP)SECOND_LVL_PROFIT = input.float(2, title="Second level profit", tooltip="Relative to risk. Example: entry at $10 and inital stop loss at $9. Taking second level profit at 2R means taking profits at $12", group=GROUP_TP)THIRD_LVL_PROFIT = input.float(3, title="Third level profit", tooltip="Relative to risk. Example: entry at $10 and inital stop loss at $9. Taking third level profit at 3R means taking profits at $13", group=GROUP_TP)// MAIN: {var float initial_entry_p = float(0) // initial entry pricevar float risk_amt = float(0) // initial entry price minus initial stop loss price// (1) Taking profits at user defined target levels relative to risked amount (i.e 1R, 2R, 3R)if not entry_signal_long if tp_count_long == 0 and close >= strategy.position_avg_price + (FIRST_LVL_PROFIT * risk_amt) strategy.close(ENUM_LONG, comment="TP lvl1", qty=math.floor(initial_order_size / 3)) tp_count_long := tp_count_long + 1 else if tp_count_long == 1 and close >= strategy.position_avg_price + (SECOND_LVL_PROFIT * risk_amt) strategy.close(ENUM_LONG, comment="TP lvl2", qty=math.floor(initial_order_size / 3)) tp_count_long := tp_count_long + 1 else if tp_count_long == 2 and close >= strategy.position_avg_price + (THIRD_LVL_PROFIT * risk_amt) strategy.close(ENUM_LONG, comment="TP lvl3", qty=math.floor(initial_order_size / 3)) tp_count_long := tp_count_long + 1if not entry_signal_short if tp_count_short == 0 and close <= strategy.position_avg_price + (-FIRST_LVL_PROFIT * risk_amt) strategy.close(ENUM_SHORT, comment="TP lvl1", qty=math.floor(initial_order_size / 3)) tp_count_short := tp_count_short + 1 else if tp_count_short == 1 and close <= strategy.position_avg_price + (-SECOND_LVL_PROFIT * risk_amt) strategy.close(ENUM_SHORT, comment="TP lvl2", qty=math.floor(initial_order_size / 3)) tp_count_short := tp_count_short + 1 else if tp_count_short == 2 and close <= strategy.position_avg_price + (-THIRD_LVL_PROFIT * risk_amt) strategy.close(ENUM_SHORT, comment="TP lvl3", qty=math.floor(initial_order_size / 3)) tp_count_short := tp_count_short + 1// (2) States update: pos_opened_long, pos_opened_shortif pos_opened_long and TSL_source_long <= stop_loss_price_long 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")// (3) Update the stoploss to latest trailing amt.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")// (4) INITIAL ENTRY:if within_timeframe and entry_signal_long pos_opened_long := true alert(LONG_MSG_ENTER, alert.freq_once_per_bar) initial_order_size := math.floor(pcnt_alloc * strategy.equity / close) strategy.entry(ENUM_LONG, strategy.long, comment="long", qty=initial_order_size) risk_amt := SL_buffer // for taking profits at 1R, 2R, and 3R initial_entry_p := close // ""if within_timeframe and entry_signal_short pos_opened_short := true alert(SHORT_MSG_ENTER, alert.freq_once_per_bar) strategy.entry(ENUM_SHORT, strategy.short, comment="short")// Plotting: TSL_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))// CLEAN UP: Setting variables back to default values once no longer in useif ta.change(strategy.position_size) and strategy.position_size == 0 risk_amt := float(0) initial_entry_p := float(0) initial_order_size := 0 pos_opened_long := false pos_opened_short := falseif not pos_opened_long stop_loss_price_long := float(0) tp_count_long := 0if not pos_opened_short stop_loss_price_short := float(9999999999) tp_count_short := 0// } end of MAIN block