Pine Script v5 · Strategy
Fractal Breakout Strategy [KL]
A Pine Script v5 strategy built on SMA (volume, rel_vol_len), ATR (5) and Stdev (ta.tr, 5). It has 5 order rules, 1 entry among them, and trades long only with the default settings. Exits use a stop loss. Each trade commits 5% of equity and pays no commission. 10 settings; defaults include ATR Multiplier for trailing stop loss 2.0, Length of ATR for trailing stop loss 14 and First level profit 1.
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 Fractal Breakout Strategy [KL] repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 11 of 11 trades.
- How did Fractal Breakout Strategy [KL] do on BTCUSDT 1D with default settings?
- +1.86%
- From 26 Jan 2024 to 21 Oct 2026, over 36 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- What alerts does Fractal Breakout Strategy [KL] have?
- 1
- str.tostring(prefix) + str.tostring(msg) + str.tostring(suffix).
- Does Fractal Breakout Strategy [KL] run in Pine Script v6?
- Yes
- Same 36 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 Fractal Breakout Strategy [KL] use a stop loss?
- Yes
- strategy.exit sets a stop on line 71.
BTCUSDT · 1D · default settings
- Plot
- fractal_resistance
- fractal_support
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +1.86%
- $18,628,700.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 36
- 26 won · 10 lost
- Profitable
- 72.2%
- of closed trades
- Profit factor
- 1.65
- gross profit ÷ gross loss
- Max drawdown
- 0.78%
- −$7,839,950.00
One run with the author's default settings, $1,000,000,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.
-
Likely bug
ta.atr(14) ignores the settings line 90
Line 90 hard-codes 14 where line 54 uses “Length of ATR (fast) for diversion test”. Changing those settings moves one band but not this one.
ta.atr(14)ta.atr(len_fast) -
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 |
|---|---|---|---|
| Backtest Start Time | timestamp("01 Apr 2000 13:30 +0000") | time | within_timeframe |
| Confidence IntervalCritical values of 1.645, 1.96, 2.58, for CI=90%/95%/99%, respectively; Under 2-tailed test to compare atr_fast vs atr_slow. Null hypothesis (H0) is that both should equal. Based on the computed test statistic value, if absolute value of it is +/- critical value, then H0 will be rejected. | 95% | string · options ["90%","95%","99%"] | critical_value |
| Stop loss | |||
| ATR Multiplier for trailing stop loss | 2.0 | float | ATR_TSL |
| Length of ATR for trailing stop lossInitial risk amount = atr(this length) x multiplier | 14 | int | stop_loss_price risk_amt |
| Order size and Profit taking | |||
| Take profit and different levels | true | bool | line 78 |
| 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 line 85 |
| 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 line 82 |
| 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 line 79 |
| Entry | |||
| Length of ATR (fast) for diversion test | 5 | int | atr_fast std_error |
| Length of ATR (slow) for diversion testThis needs to be larger than Fast | 50 | int | test_stat |
What it calculates
- SMAvolume, rel_vol_len
rel_vol · line 38 - ATRLength of ATR (fast) for diversion test (5)
atr_fast · line 54 - Stdevta.tr, Length of ATR (fast) for diversion test (5)
std_error · line 57 - ATR14
_atr · line 90
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 | strategy.position_size > 0 | On | 71 |
| Open Long long | within_timeframe and entry_signal_long and strategy.position_size == 0 | On | 76 |
| Close Long | close >= initial_entry_p + THIRD_LVL_PROFIT * risk_amt and TP_taken_count == 2 | On | 80 |
| Close Long | close >= initial_entry_p + SECOND_LVL_PROFIT * risk_amt and TP_taken_count == 1 | On | 83 |
| Close Long | close >= initial_entry_p + FIRST_LVL_PROFIT * risk_amt and TP_taken_count == 0 | On | 86 |
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 | str.tostring(prefix) + str.tostring(msg) + str.tostring(suffix) | 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 | +1.9% | +102.5% | 36 | 72% | 1.65 | 0.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.1% | +5.5% | 34 | 59% | 1.12 | 0.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +2.1% | +9.8% | 36 | 67% | 1.42 | 2.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.4% | +8.4% | 30 | 57% | 1.36 | 0.6% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +2.1% | +16.7% | 40 | 75% | 1.36 | 2.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.1% | +21.5% | 38 | 63% | 1.05 | 0.6% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.7% | +148.3% | 39 | 69% | 1.21 | 1.5% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.4% | +17.1% | 39 | 51% | 0.73 | 0.7% | 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 | 56% | −$3,601,450.00 |
| Sideways market | 16 | 63% | $1,580,490.00 |
| Falling market | 11 | 100% | $20,649,700.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
1.5%
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, 0.6% to 1.5%; the tick is what actually happened, 0.8%. Order luck alone moves it this much.
All 36 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 29 Apr 2024 63,118.60 | 1 May 2024 56,848.20 | 2 | −$4,967,200.00 |
| long | 9 May 2024 61,193.00 | 21 May 2024 71,446.60 | 12 | $2,778,720.00 |
| long | 9 May 2024 61,193.00 | 14 Jun 2024 65,931.20 | 36 | $2,568,230.00 |
| long | 18 Jun 2024 66,504.30 | 24 Jun 2024 61,142.80 | 6 | −$4,032,500.00 |
| long | 30 Jun 2024 60,986.70 | 4 Jul 2024 58,655.50 | 4 | −$1,904,260.00 |
| long | 17 Sep 2024 58,214.00 | 20 Sep 2024 62,948.00 | 3 | $1,344,460.00 |
| long | 17 Sep 2024 58,214.00 | 1 Oct 2024 61,451.30 | 14 | $1,845,660.00 |
| long | 7 Oct 2024 62,819.90 | 16 Oct 2024 67,074.10 | 9 | $1,123,120.00 |
| long | 7 Oct 2024 62,819.90 | 30 Oct 2024 72,736.40 | 23 | $1,745,300.00 |
| long | 7 Oct 2024 62,819.90 | 4 Nov 2024 67,264.60 | 28 | $1,573,620.00 |
| long | 6 Jan 2025 98,363.60 | 13 Jan 2025 90,970.30 | 7 | −$3,765,970.00 |
| long | 15 Feb 2025 97,500.50 | 25 Feb 2025 90,024.10 | 10 | −$3,827,540.00 |
| long | 17 Apr 2025 84,030.40 | 23 Apr 2025 93,443.00 | 6 | $1,854,280.00 |
| long | 17 Apr 2025 84,030.40 | 9 May 2025 103,262.00 | 22 | $2,519,290.00 |
| long | 17 Apr 2025 84,030.40 | 21 May 2025 106,850.00 | 34 | $1,985,310.00 |
| long | 17 Apr 2025 84,030.40 | 31 May 2025 103,154.00 | 44 | $3,379,900.00 |
| long | 2 Jun 2025 105,643.00 | 20 Jun 2025 102,491.00 | 18 | −$1,497,970.00 |
| long | 29 Jun 2025 107,297.00 | 11 Jul 2025 116,010.00 | 12 | $1,350,550.00 |
| long | 29 Jun 2025 107,297.00 | 14 Jul 2025 119,087.00 | 15 | $1,226,150.00 |
| long | 29 Jun 2025 107,297.00 | 1 Aug 2025 113,273.00 | 33 | $1,244,720.00 |
| long | 7 Aug 2025 114,992.00 | 14 Aug 2025 123,306.00 | 7 | $1,205,550.00 |
| long | 7 Aug 2025 114,992.00 | 19 Aug 2025 113,284.00 | 12 | −$499,940.00 |
| long | 13 Sep 2025 116,029.00 | 24 Sep 2025 111,314.00 | 11 | −$2,046,550.00 |
| long | 25 Dec 2025 87,669.40 | 14 Jan 2026 95,414.00 | 20 | $1,479,210.00 |
| long | 25 Dec 2025 87,669.40 | 20 Jan 2026 90,315.90 | 26 | $1,011,710.00 |
| long | 20 Feb 2026 67,003.70 | 17 Mar 2026 74,884.70 | 25 | $1,970,240.00 |
| long | 20 Feb 2026 67,003.70 | 22 Mar 2026 67,453.30 | 30 | $225,680.00 |
| long | 2 Apr 2026 68,113.90 | 14 Apr 2026 74,418.00 | 12 | $1,557,110.00 |
| long | 2 Apr 2026 68,113.90 | 5 May 2026 79,861.00 | 33 | $1,926,520.00 |
| long | 2 Apr 2026 68,113.90 | 18 May 2026 76,410.10 | 46 | $2,740,440.00 |
| long | 20 May 2026 76,834.40 | 28 May 2026 72,995.10 | 8 | −$2,538,690.00 |
| long | 17 Jun 2026 65,675.00 | 24 Jun 2026 60,849.60 | 7 | −$3,723,610.00 |
| long | 12 Jul 2026 63,819.00 | 20 Aug 2026 69,334.80 | 39 | $1,450,650.00 |
| long | 12 Jul 2026 63,819.00 | 21 Aug 2026 73,027.00 | 40 | $1,620,610.00 |
| long | 12 Jul 2026 63,819.00 | 22 Aug 2026 78,338.00 | 41 | $1,698,730.00 |
| long | 12 Jul 2026 63,819.00 | 8 Oct 2026 80,857.60 | 88 | $4,007,210.00 |
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 |
|---|---|---|---|---|---|
| Fractal Breakout Strategy [KL] this page | — | v5 strategy | +1.9% | 36 | 0.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 | ||
| [KL] Double Bollinger Bands Strategy (for Crypto-FOREX) by DojiEmoji | sma stdev atr | v4 strategy | −42.2% | 42 | 56.8% |
| Mean Reversion Strategy v2 [KL] by DojiEmoji | atr stdev sma | v5 strategy | +1.7% | 52 | 1.5% |
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.
- 5 settings use the untyped input() (line 12, 13, 30, 53, 55). 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 36 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 12: input() → input.float()
- Line 13: input() → input.int()
- Line 30: input() → input.bool()
- Line 53: input() → input.int()
- Line 55: input() → input.int()
⋯// TSL: calculate the stop loss price. {-_multiple = input(2.0, title="ATR Multiplier for trailing stop loss", group=GROUP_TSL)+_multiple = input.float(2.0, title="ATR Multiplier for trailing stop loss", group=GROUP_TSL)-ATR_TSL = ta.atr(input(14, title="Length of ATR for trailing stop loss", group=GROUP_TSL, tooltip="Initial risk amount = atr(this length) x multiplier")) * _multiple+ATR_TSL = ta.atr(input.int(14, title="Length of ATR for trailing stop loss", group=GROUP_TSL, tooltip="Initial risk amount = atr(this length) x multiplier")) * _multipleTSL_source = low⋯// Profit taking {-var bool tp_mode = input(true, title="Take profit and different levels", group=GROUP_ORDER)+var bool tp_mode = input.bool(true, title="Take profit and different levels", group=GROUP_ORDER)var float FIRST_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_ORDER)⋯// Ha : atr_fast not equals to atr_slow; implies atr_fast is either too low or too high-len_fast = input(5,title="Length of ATR (fast) for diversion test", group=GROUP_ENTRY)+len_fast = input.int(5,title="Length of ATR (fast) for diversion test", group=GROUP_ENTRY)atr_fast = ta.atr(len_fast)-atr_slow = ta.atr(input(50,title="Length of ATR (slow) for diversion test", group=GROUP_ENTRY, tooltip="This needs to be larger than Fast"))+atr_slow = ta.atr(input.int(50,title="Length of ATR (slow) for diversion test", group=GROUP_ENTRY, tooltip="This needs to be larger than Fast"))// Calculate test statistic (test_stat)⋯Does it run in Pine Script v6?
Yes
Same 36 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-//@version=5+//@version=6strategy("Fractal Strat [KL] ", overlay=true, pyramiding=1, initial_capital=1000000000, default_qty_type=strategy.percent_of_equity, default_qty_value=5)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 42 ms | Yes 1,000 bars in 60 ms | Not measured |
| PineTS | Not tested | Not tested | 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.
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=5strategy("Fractal Strat [KL] ", overlay=true, pyramiding=1, initial_capital=1000000000, default_qty_type=strategy.percent_of_equity, default_qty_value=5)var string ENUM_LONG = "Long"var string GROUP_ENTRY = "Entry"var string GROUP_TSL = "Stop loss"var string GROUP_ORDER = "Order size and Profit taking"backtest_timeframe_start = input.time(defval=timestamp("01 Apr 2000 13:30 +0000"), title="Backtest Start Time")within_timeframe = time >= backtest_timeframe_start// TSL: calculate the stop loss price. {_multiple = input(2.0, title="ATR Multiplier for trailing stop loss", group=GROUP_TSL)ATR_TSL = ta.atr(input(14, title="Length of ATR for trailing stop loss", group=GROUP_TSL, tooltip="Initial risk amount = atr(this length) x multiplier")) * _multipleTSL_source = lowTSL_line_color = color.greenTSL_transp = 100var stop_loss_price = float(0)var float initial_entry_p = float(0)var float risk_amt = float(0)var float initial_order_size = float(0)if strategy.position_size == 0 or not within_timeframe TSL_line_color := color.black stop_loss_price := TSL_source - ATR_TSLelse if strategy.position_size > 0 stop_loss_price := math.max(stop_loss_price, TSL_source - ATR_TSL) TSL_transp := 0plot(stop_loss_price, color=color.new(TSL_line_color, TSL_transp))// } end of "TSL" block// Profit taking {var bool tp_mode = input(true, title="Take profit and different levels", group=GROUP_ORDER)var float FIRST_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_ORDER)var float 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_ORDER)var float 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_ORDER)// }// Fractals {// Modified from synapticEx's implementation: https://www.tradingview.com/script/cDCNneRP-Fractal-Support-Resistance-Fixed-Volume-2/rel_vol_len = 6 // Relative volume is used; the middle candle has to have volume above the average (say sma over prior 6 bars)rel_vol = ta.sma(volume, rel_vol_len)_up = high[3]>high[4] and high[4]>high[5] and high[2]<high[3] and high[1]<high[2] and volume[3]>rel_vol[3]_down = low[3]<low[4] and low[4]<low[5] and low[2]>low[3] and low[1]>low[2] and volume[3]>rel_vol[3]fractal_resistance = high[3], fractal_support = low[3] // initializefractal_resistance := _up ? high[3] : fractal_resistance[1]fractal_support := _down ? low[3] : fractal_support[1]plot(fractal_resistance, "fractal_resistance", color=color.new(color.red,50), linewidth=2, style=plot.style_cross, offset =-3, join=false)plot(fractal_support, "fractal_support", color=color.new(color.lime,50), linewidth=2, style=plot.style_cross, offset=-3, join=false)// }// ATR diversion test {// Hypothesis testing (2-tailed)://// Null hypothesis (H0) and Alternative hypothesis (Ha):// H0 : atr_fast equals atr_slow// Ha : atr_fast not equals to atr_slow; implies atr_fast is either too low or too highlen_fast = input(5,title="Length of ATR (fast) for diversion test", group=GROUP_ENTRY)atr_fast = ta.atr(len_fast)atr_slow = ta.atr(input(50,title="Length of ATR (slow) for diversion test", group=GROUP_ENTRY, tooltip="This needs to be larger than Fast"))// Calculate test statistic (test_stat)std_error = ta.stdev(ta.tr, len_fast) / math.pow(len_fast, 0.5)test_stat = (atr_fast - atr_slow) / std_error// Compare test_stat against critical value defined by user in settings//critical_value = input.float(1.645,title="Critical value", tooltip="Strategy uses 2-tailed test to compare atr_fast vs atr_slow. Null hypothesis (H0) is that both should equal. Based on the computed test statistic value, if absolute value of it is +/- this critical value, then H0 will be rejected.", group=GROUP_ENTRY)conf_interval = input.string(title="Confidence Interval", defval="95%", options=["90%","95%","99%"], tooltip="Critical values of 1.645, 1.96, 2.58, for CI=90%/95%/99%, respectively; Under 2-tailed test to compare atr_fast vs atr_slow. Null hypothesis (H0) is that both should equal. Based on the computed test statistic value, if absolute value of it is +/- critical value, then H0 will be rejected.")critical_value = conf_interval == "90%" ? 1.645 : conf_interval == "95%" ? 1.96 : 2.58reject_H0_lefttail = test_stat < -critical_valuereject_H0_righttail = test_stat > critical_value// } end of "ATR diversion test" block// Entry Signalsentry_signal_long = close >= fractal_support and reject_H0_lefttail// MAIN {// Update the stop limit if strategy holds a position.if strategy.position_size > 0 strategy.exit(ENUM_LONG, comment="SL", stop=stop_loss_price)// Entryif within_timeframe and entry_signal_long and strategy.position_size == 0 initial_entry_p := close risk_amt := ATR_TSL strategy.entry(ENUM_LONG, strategy.long)var int TP_taken_count = 0if tp_mode and close > strategy.position_avg_price if close >= initial_entry_p + THIRD_LVL_PROFIT * risk_amt and TP_taken_count == 2 strategy.close(ENUM_LONG, comment="TP Lvl3", qty=math.floor(strategy.position_size / 3)) TP_taken_count := TP_taken_count + 1 else if close >= initial_entry_p + SECOND_LVL_PROFIT * risk_amt and TP_taken_count == 1 strategy.close(ENUM_LONG, comment="TP Lvl2", qty=math.floor(strategy.position_size / 3)) TP_taken_count := TP_taken_count + 1 else if close >= initial_entry_p + FIRST_LVL_PROFIT * risk_amt and TP_taken_count == 0 strategy.close(ENUM_LONG, comment="TP Lvl1", qty=math.floor(strategy.position_size / 3)) TP_taken_count := TP_taken_count + 1 // Alerts_atr = ta.atr(14)alert_helper(msg) => prefix = "[" + syminfo.root + "] " suffix = "(P=" + str.tostring(close, "#.##") + "; atr=" + str.tostring(_atr, "#.##") + ")" alert(str.tostring(prefix) + str.tostring(msg) + str.tostring(suffix), alert.freq_once_per_bar)if strategy.position_size > 0 and ta.change(strategy.position_size) if strategy.position_size > strategy.position_size[1] alert_helper("BUY") else if strategy.position_size < strategy.position_size[1] alert_helper("SELL")// Clean up - set the variables back to default values once no longer in useif ta.change(strategy.position_size) and strategy.position_size == 0 TP_taken_count := 0 initial_entry_p := float(0) risk_amt := float(0) initial_order_size := float(0) stop_loss_price := float(0)// } end of MAIN block