Pine Script v4 · Strategy
Williams Fractals Strategy
A Pine Script v4 strategy built on EMA (close, 20) and RMA or Change (max(change(rsi_src), 0), 14). It has 5 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 commits 1000 in cash and pays no commission. 25 settings; defaults include Theme dark, Fractal Periods 2 and EMA Length A 20.
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 Williams Fractals Strategy repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Williams Fractals Strategy do on BTCUSDT 1D with default settings?
- Not measured
- It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings, so there is no result to report. Its code limits trading to a date window, which can leave this period out. It closed no trades on the other markets either. A backtest, not investment advice.
- Can Williams Fractals Strategy be converted to Pine Script v5?
- Not tested
- The steps are shown below, but neither version placed a trade on any market tested, so there was nothing to compare and no download is offered.
- Does Williams Fractals Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 140, 142.
- Does Williams Fractals Strategy run in Zeal Terminal?
- Yes
- It compiles in 42 ms and runs 1,000 bars in 144 ms, in the browser.
BTCUSDT · 1D · default settings
- long label
- short label
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- 0.00%
- $0.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 0
- 0 won · 0 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 0.00%
- $0.00
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 commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
1 setting has no label
In the settings dialog theme (line 7) 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 long =, which v5 renamed line 119
long = on line 119, 121. v5 writes it as direction = strategy.long or strategy.short.
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 ema → ta.ema, rma → ta.rma, max → math.max, change → ta.change, min → math.min.
v4 → v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 4: //@version=4 → //@version=5
- Line 7: input(type = input.string) → input.string()
- Line 19: input(type = input.integer) → input.int()
- Line 57: input(type = input.integer) → input.int()
- Line 58: ema → ta.ema
- Line 62: input(type = input.integer) → input.int()
22 more edits
- Line 63: ema → ta.ema
- Line 68: input(type = input.integer) → input.int()
- Line 69: ema → ta.ema
- Line 74: input(type = input.source) → input.source()
- Line 75: rma → ta.rma
- Line 75: max → math.max
- Line 75: change → ta.change
- Line 76: rma → ta.rma
- Line 76: min → math.min
- Line 76: change → ta.change
- Line 85: input(type = input.integer) → input.int()
- Line 87: input(type = input.integer) → input.int()
- Line 89: input(type = input.integer) → input.int()
- Line 91: input(type = input.integer) → input.int()
- Line 93: input(type = input.integer) → input.int()
- Line 95: input(type = input.integer) → input.int()
- Line 103: input(type = input.float) → input.float()
- Line 105: input(type = input.float) → input.float()
- Line 119: long = true → direction = strategy.long
- Line 121: long = false → direction = strategy.short
- Line 124: input(type = input.float) → input.float()
- Line 126: input(type = input.float) → input.float()
⋯// v 1.1-//@version=4+//@version=5strategy("Williams Fractals Strategy by ȼhąţhµяąɲǥą", overlay=true, default_qty_type=strategy.cash, default_qty_value=1000, currency='USD')// *************Appearance*************-theme = input(type=input.string, defval="dark", options=["light","dark"], group="Appearance")+theme = input.string(defval="dark", options=["light","dark"], group="Appearance")show_fractals = input(false, "Show Fractals", group="Appearance")⋯// Define "n" as the number of periods and keep a minimum value of 2 for error handling.-n = input(title="Fractal Periods", defval=2, minval=2, type=input.integer, group="Williams Fractals")+n = input.int(title="Fractal Periods", defval=2, minval=2, group="Williams Fractals")// UpFractal⋯src_a = input(close, title="EMA Source A", group="EMA")-offset_a = input(title="EMA Offset A", type=input.integer, defval=0, minval=-500, maxval=500, group="EMA")+offset_a = input.int(title="EMA Offset A", defval=0, minval=-500, maxval=500, group="EMA")-out_a = ema(src_a, len_a)+out_a = ta.ema(src_a, len_a)plot(show_ema ? out_a : na, title="EMA A", color=color_green, offset=offset_a)⋯src_b = input(close, title="EMA Source B", group="EMA")-offset_b = input(title="EMA Offset B", type=input.integer, defval=0, minval=-500, maxval=500, group="EMA")+offset_b = input.int(title="EMA Offset B", defval=0, minval=-500, maxval=500, group="EMA")-out_b = ema(src_b, len_b)+out_b = ta.ema(src_b, len_b)ema_b_color = (theme == "dark") ? color_yellow : color_orange⋯src_c = input(close, title="EMA Source C", group="EMA")-offset_c = input(title="EMA Offset C", type=input.integer, defval=0, minval=-500, maxval=500, group="EMA")+offset_c = input.int(title="EMA Offset C", defval=0, minval=-500, maxval=500, group="EMA")-out_c = ema(src_c, len_c)+out_c = ta.ema(src_c, len_c)ema_c_color = (theme == "dark") ? color_white : color_blue⋯rsi_len = input(14, minval=1, title="RSI Length", group="RSI")-rsi_src = input(close, "RSI Source", type = input.source, group="RSI")+rsi_src = input.source(close, "RSI Source", group="RSI")-up = rma(max(change(rsi_src), 0), rsi_len)+up = ta.rma(math.max(ta.change(rsi_src), 0), rsi_len)-down = rma(-min(change(rsi_src), 0), rsi_len)+down = ta.rma(-math.min(ta.change(rsi_src), 0), rsi_len)rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))⋯// Make input options that configure backtest date range-startDate = input(title="Start Date", type=input.integer,+startDate = input.int(title="Start Date", defval=1, minval=1, maxval=31, group="Orders")-startMonth = input(title="Start Month", type=input.integer,+startMonth = input.int(title="Start Month", defval=1, minval=1, maxval=12, group="Orders")-startYear = input(title="Start Year", type=input.integer,+startYear = input.int(title="Start Year", defval=2018, minval=1800, maxval=2100, group="Orders")-endDate = input(title="End Date", type=input.integer,+endDate = input.int(title="End Date", defval=1, minval=1, maxval=31, group="Orders")-endMonth = input(title="End Month", type=input.integer,+endMonth = input.int(title="End Month", defval=12, minval=1, maxval=12, group="Orders")-endYear = input(title="End Year", type=input.integer,+endYear = input.int(title="End Year", defval=2022, minval=1800, maxval=2100, group="Orders")⋯// Make inputs that set the take profit % (optional)-longProfitPerc = input(title="Long Take Profit (%)",- type=input.float, minval=0.0, step=0.1, defval=0.5, group="Orders") * 0.01+longProfitPerc = input.float(title="Long Take Profit (%)", minval=0.0, step=0.1, defval=0.5, group="Orders") * 0.01-shortProfitPerc = input(title="Short Take Profit (%)",- type=input.float, minval=0.0, step=0.1, defval=0.5, group="Orders") * 0.01+shortProfitPerc = input.float(title="Short Take Profit (%)", minval=0.0, step=0.1, defval=0.5, group="Orders") * 0.01// Figure out take profit price⋯if (inDateRange and long and strategy.opentrades == 0)- strategy.entry(id="Long", long=true)+ strategy.entry(id="Long", direction = strategy.long)if (inDateRange and short and strategy.opentrades == 0)- strategy.entry(id="Short", long=false)+ strategy.entry(id="Short", direction = strategy.short) // Set stop loss level with input options (optional)-longLossPerc = input(title="Long Stop Loss (%)",- type=input.float, minval=0.0, step=0.1, defval=3.1, group="Orders") * 0.01+longLossPerc = input.float(title="Long Stop Loss (%)", minval=0.0, step=0.1, defval=3.1, group="Orders") * 0.01-shortLossPerc = input(title="Short Stop Loss (%)",- type=input.float, minval=0.0, step=0.1, defval=3.1, group="Orders") * 0.01+shortLossPerc = input.float(title="Short Stop Loss (%)", minval=0.0, step=0.1, defval=3.1, group="Orders") * 0.01// Determine stop loss price⋯Clean v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 8: input() → input.bool()
- Line 9: input() → input.bool()
- Line 55: input() → input.int()
- Line 56: input() → input.source()
- Line 60: input() → input.int()
- Line 61: input() → input.source()
3 more edits
- Line 66: input() → input.int()
- Line 67: input() → input.source()
- Line 73: input() → input.int()
⋯theme = input.string(defval="dark", options=["light","dark"], group="Appearance")-show_fractals = input(false, "Show Fractals", group="Appearance")+show_fractals = input.bool(false, "Show Fractals", group="Appearance")-show_ema = input(false, "Show EMAs", group="Appearance")+show_ema = input.bool(false, "Show EMAs", group="Appearance")// *************colors*************⋯// *************EMA*************-len_a = input(20, minval=1, title="EMA Length A", group="EMA")+len_a = input.int(20, minval=1, title="EMA Length A", group="EMA")-src_a = input(close, title="EMA Source A", group="EMA")+src_a = input.source(close, title="EMA Source A", group="EMA")offset_a = input.int(title="EMA Offset A", defval=0, minval=-500, maxval=500, group="EMA")⋯plot(show_ema ? out_a : na, title="EMA A", color=color_green, offset=offset_a)-len_b = input(50, minval=1, title="EMA Length B", group="EMA")+len_b = input.int(50, minval=1, title="EMA Length B", group="EMA")-src_b = input(close, title="EMA Source B", group="EMA")+src_b = input.source(close, title="EMA Source B", group="EMA")offset_b = input.int(title="EMA Offset B", defval=0, minval=-500, maxval=500, group="EMA")⋯plot(show_ema ? out_b : na, title="EMA B", color=ema_b_color, offset=offset_b)-len_c = input(100, minval=1, title="EMA Length C", group="EMA")+len_c = input.int(100, minval=1, title="EMA Length C", group="EMA")-src_c = input(close, title="EMA Source C", group="EMA")+src_c = input.source(close, title="EMA Source C", group="EMA")offset_c = input.int(title="EMA Offset C", defval=0, minval=-500, maxval=500, group="EMA")⋯// *************RSI*************-rsi_len = input(14, minval=1, title="RSI Length", group="RSI")+rsi_len = input.int(14, minval=1, title="RSI Length", group="RSI")rsi_src = input.source(close, "RSI Source", group="RSI")⋯Does it run in Pine Script v6?
Not tested
Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// v 1.1-//@version=5+//@version=6strategy("Williams Fractals Strategy by ȼhąţhµяąɲǥą", overlay=true, default_qty_type=strategy.cash, default_qty_value=1000, currency='USD')⋯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 |
|---|---|---|---|
| Appearance | |||
| Theme (no label in the script) | dark | string · options ["light","dark"] | ema_b_color ema_c_color |
| Show Fractals | false | bool | plotshape |
| Show EMAs | false | bool | EMA A EMA B EMA C |
| Williams Fractals | |||
| Fractal Periods | 2 | int (written input.integer) · min 2 | upflagDownFrontier upflagUpFrontier0 upflagUpFrontier1 upflagUpFrontier2 upflagUpFrontier3 upflagUpFrontier4 downflagDownFrontier downflagUpFrontier0 downflagUpFrontier1 downflagUpFrontier2 downflagUpFrontier3 downflagUpFrontier4 line 27 line 43 plotshape |
| EMA | |||
| EMA Length A | 20 | int · min 1 | out_a |
| EMA Source A | close | source | out_a |
| EMA Offset A | 0 | int (written input.integer) · min -500 · max 500 | EMA A |
| EMA Length B | 50 | int · min 1 | out_b |
| EMA Source B | close | source | out_b |
| EMA Offset B | 0 | int (written input.integer) · min -500 · max 500 | EMA B |
| EMA Length C | 100 | int · min 1 | out_c |
| EMA Source C | close | source | out_c |
| EMA Offset C | 0 | int (written input.integer) · min -500 · max 500 | EMA C |
| RSI | |||
| RSI Length | 14 | int · min 1 | up down |
| RSI Source | close | source | up down |
| Orders | |||
| Start Date | 1 | int (written input.integer) · min 1 · max 31 | inDateRange |
| Start Month | 1 | int (written input.integer) · min 1 · max 12 | inDateRange |
| Start Year | 2018 | int (written input.integer) · min 1800 · max 2100 | inDateRange |
| End Date | 1 | int (written input.integer) · min 1 · max 31 | inDateRange |
| End Month | 12 | int (written input.integer) · min 1 · max 12 | inDateRange |
| End Year | 2022 | int (written input.integer) · min 1800 · max 2100 | inDateRange |
| Long Take Profit (%) | 0.5 | float · min 0.0 · step 0.1 | longExitPrice |
| Short Take Profit (%) | 0.5 | float · min 0.0 · step 0.1 | shortExitPrice |
| Long Stop Loss (%) | 3.1 | float · min 0.0 · step 0.1 | longStopPrice |
| Short Stop Loss (%) | 3.1 | float · min 0.0 · step 0.1 | shortStopPrice |
What it calculates
- EMA (
ema,ta.emain v5)EMA Source A (close), EMA Length A (20)out_a · line 58 - EMA (
ema,ta.emain v5)EMA Source B (close), EMA Length B (50)out_b · line 63 - EMA (
ema,ta.emain v5)EMA Source C (close), EMA Length C (100)out_c · line 69 - RMA / Change (
rma,ta.rmain v5)max(change(rsi_src), 0), RSI Length (14)up · line 75 - RMA / Change (
rma,ta.rmain v5)-min(change(rsi_src), 0), RSI Length (14)down · line 76
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.
The script has 68 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long long | (inDateRange and long and strategy.opentrades == 0) | On | 119 |
| Open Short short | (inDateRange and short and strategy.opentrades == 0) | On | 121 |
| Exit ExL stop and limit | (strategy.position_size > 0) | On | 140 |
| Exit ExS stop and limit | (strategy.position_size < 0) | On | 142 |
| Close | (not inDateRange) | On | 145 |
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.
- 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 | 0.0% | +102.5% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | 0.0% | +5.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | 0.0% | +9.8% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | 0.0% | +8.4% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | 0.0% | +16.7% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | 0.0% | +21.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | 0.0% | +148.3% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | 0.0% | +17.1% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Settings heatmap
Two settings that feed the entry signal, moved around their defaults. The best in-sample pair (EMA Length A 10, EMA Length B 25) made 0.0% before 26 Dec 2025 and 0.0% after it, against 0.0% for the defaults. 0% of the 64 combinations made money in-sample and 0% out-of-sample.
| EMA Length B ↓ · EMA Length A → | 10 | 12 | 14 | 17 | 20 | 24 | 28 | 34 |
|---|---|---|---|---|---|---|---|---|
| 25 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 30 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 35 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 43 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 50 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 60 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 70 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 85 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| EMA Length B ↓ · EMA Length A → | 10 | 12 | 14 | 17 | 20 | 24 | 28 | 34 |
|---|---|---|---|---|---|---|---|---|
| 25 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 30 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 35 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 43 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 50 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 60 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 70 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 85 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
Net P&L of trades opened in each period, as a share of starting capital. marks the defaults and the best in-sample cell. Hover a cell for its trade count. Backtests are simulations on historical data and are not investment advice.
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 |
|---|---|---|---|---|---|
| Williams Fractals Strategy this page | — | v4 strategy | 0.0% | 0 | 0.0% |
| DCA After Downtrend v2 (by BHD_Trade_Bot) by BHD_Trade_Bot | ema rma | v5 strategy | No trades on this market | ||
| Strategy Myth-Busting #10 - InsideBar+EMA - [MYN] by myncrypto | ema rma | v5 strategy | No trades on this market | ||
| Optimized Keltner Channels SL-TP Strategy for BTC by chanu_lev10k | ema rma | v4 strategy | −19.4% | 36 | 70.0% |
| Take Profit On Trend v2 (by BHD_Trade_Bot) by BHD_Trade_Bot | ema rma | v5 strategy | +9.4% | 6 | 24.0% |
| Daytrade strategy RSI CCI EMA 4-8 by SoftKill21 | ema rma | v4 strategy | +0.0% | 26 | 0.0% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 42 ms | With a workaround 1,000 bars in 144 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true, long = false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 119, 121) so the engine trades the way the script says.
“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/// © B_L_A_C_K_S_C_O_R_P_I_O_N// v 1.1//@version=4strategy("Williams Fractals Strategy by ȼhąţhµяąɲǥą", overlay=true, default_qty_type=strategy.cash, default_qty_value=1000, currency='USD')// *************Appearance*************theme = input(type=input.string, defval="dark", options=["light","dark"], group="Appearance")show_fractals = input(false, "Show Fractals", group="Appearance")show_ema = input(false, "Show EMAs", group="Appearance")// *************colors*************color_green = color.greencolor_red = color.redcolor_yellow = color.yellowcolor_orange = color.orangecolor_blue = color.bluecolor_white = color.white// *************WF*************// Define "n" as the number of periods and keep a minimum value of 2 for error handling.n = input(title="Fractal Periods", defval=2, minval=2, type=input.integer, group="Williams Fractals")// UpFractalbool upflagDownFrontier = truebool upflagUpFrontier0 = truebool upflagUpFrontier1 = truebool upflagUpFrontier2 = truebool upflagUpFrontier3 = truebool upflagUpFrontier4 = truefor i = 1 to n upflagDownFrontier := upflagDownFrontier and (high[n-i] < high[n]) upflagUpFrontier0 := upflagUpFrontier0 and (high[n+i] < high[n]) upflagUpFrontier1 := upflagUpFrontier1 and (high[n+1] <= high[n] and high[n+i + 1] < high[n]) upflagUpFrontier2 := upflagUpFrontier2 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+i + 2] < high[n]) upflagUpFrontier3 := upflagUpFrontier3 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+i + 3] < high[n]) upflagUpFrontier4 := upflagUpFrontier4 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+4] <= high[n] and high[n+i + 4] < high[n])flagUpFrontier = upflagUpFrontier0 or upflagUpFrontier1 or upflagUpFrontier2 or upflagUpFrontier3 or upflagUpFrontier4upFractal = (upflagDownFrontier and flagUpFrontier)// downFractalbool downflagDownFrontier = truebool downflagUpFrontier0 = truebool downflagUpFrontier1 = truebool downflagUpFrontier2 = truebool downflagUpFrontier3 = truebool downflagUpFrontier4 = truefor i = 1 to n downflagDownFrontier := downflagDownFrontier and (low[n-i] > low[n]) downflagUpFrontier0 := downflagUpFrontier0 and (low[n+i] > low[n]) downflagUpFrontier1 := downflagUpFrontier1 and (low[n+1] >= low[n] and low[n+i + 1] > low[n]) downflagUpFrontier2 := downflagUpFrontier2 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+i + 2] > low[n]) downflagUpFrontier3 := downflagUpFrontier3 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+i + 3] > low[n]) downflagUpFrontier4 := downflagUpFrontier4 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+4] >= low[n] and low[n+i + 4] > low[n])flagDownFrontier = downflagUpFrontier0 or downflagUpFrontier1 or downflagUpFrontier2 or downflagUpFrontier3 or downflagUpFrontier4downFractal = (downflagDownFrontier and flagDownFrontier)plotshape(downFractal and show_fractals, style=shape.triangleup, location=location.belowbar, offset=-n, color=color_green)plotshape(upFractal and show_fractals, style=shape.triangledown, location=location.abovebar, offset=-n, color=color_red)// *************EMA*************len_a = input(20, minval=1, title="EMA Length A", group="EMA")src_a = input(close, title="EMA Source A", group="EMA")offset_a = input(title="EMA Offset A", type=input.integer, defval=0, minval=-500, maxval=500, group="EMA")out_a = ema(src_a, len_a)plot(show_ema ? out_a : na, title="EMA A", color=color_green, offset=offset_a)len_b = input(50, minval=1, title="EMA Length B", group="EMA")src_b = input(close, title="EMA Source B", group="EMA")offset_b = input(title="EMA Offset B", type=input.integer, defval=0, minval=-500, maxval=500, group="EMA")out_b = ema(src_b, len_b)ema_b_color = (theme == "dark") ? color_yellow : color_orangeplot(show_ema ? out_b : na, title="EMA B", color=ema_b_color, offset=offset_b)len_c = input(100, minval=1, title="EMA Length C", group="EMA")src_c = input(close, title="EMA Source C", group="EMA")offset_c = input(title="EMA Offset C", type=input.integer, defval=0, minval=-500, maxval=500, group="EMA")out_c = ema(src_c, len_c)ema_c_color = (theme == "dark") ? color_white : color_blueplot(show_ema ? out_c : na, title="EMA C", color=ema_c_color, offset=offset_c)// *************RSI*************rsi_len = input(14, minval=1, title="RSI Length", group="RSI")rsi_src = input(close, "RSI Source", type = input.source, group="RSI")up = rma(max(change(rsi_src), 0), rsi_len)down = rma(-min(change(rsi_src), 0), rsi_len)rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))// *************Calculation*************long = (out_a > out_b) and (out_a > out_c) and downFractal and low[2] > out_c and rsi[2] < rsishort = (out_a < out_b) and (out_a < out_c) and upFractal and high[2] < out_c and rsi[2] > rsiplotshape(long, style=shape.labelup, color=color_green, location=location.belowbar, title="long label", text= "L", textcolor=color_white)plotshape(short, style=shape.labeldown, color=color_red, location=location.abovebar, title="short label", text= "S", textcolor=color_white)// *************End of Signals calculation*************// Make input options that configure backtest date rangestartDate = input(title="Start Date", type=input.integer, defval=1, minval=1, maxval=31, group="Orders")startMonth = input(title="Start Month", type=input.integer, defval=1, minval=1, maxval=12, group="Orders")startYear = input(title="Start Year", type=input.integer, defval=2018, minval=1800, maxval=2100, group="Orders")endDate = input(title="End Date", type=input.integer, defval=1, minval=1, maxval=31, group="Orders")endMonth = input(title="End Month", type=input.integer, defval=12, minval=1, maxval=12, group="Orders")endYear = input(title="End Year", type=input.integer, defval=2022, minval=1800, maxval=2100, group="Orders")// Look if the close time of the current bar// falls inside the date rangeinDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))// Make inputs that set the take profit % (optional)longProfitPerc = input(title="Long Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=0.5, group="Orders") * 0.01shortProfitPerc = input(title="Short Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=0.5, group="Orders") * 0.01// Figure out take profit pricelongExitPrice = strategy.position_avg_price * (1 + longProfitPerc)shortExitPrice = strategy.position_avg_price * (1 - shortProfitPerc)// Plot take profit values for confirmationplot(series=(strategy.position_size > 0) ? longExitPrice : na, color=color_green, style=plot.style_circles, linewidth=1, title="Long Take Profit")plot(series=(strategy.position_size < 0) ? shortExitPrice : na, color=color_green, style=plot.style_circles, linewidth=1, title="Short Take Profit")// Submit entry ordersif (inDateRange and long and strategy.opentrades == 0) strategy.entry(id="Long", long=true)if (inDateRange and short and strategy.opentrades == 0) strategy.entry(id="Short", long=false) // Set stop loss level with input options (optional)longLossPerc = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=3.1, group="Orders") * 0.01shortLossPerc = input(title="Short Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=3.1, group="Orders") * 0.01// Determine stop loss pricelongStopPrice = strategy.position_avg_price * (1 - longLossPerc)shortStopPrice = strategy.position_avg_price * (1 + shortLossPerc)// Plot stop loss values for confirmationplot(series=(strategy.position_size > 0) ? longStopPrice : na, color=color_red, style=plot.style_cross, linewidth=1, title="Long Stop Loss")plot(series=(strategy.position_size < 0) ? shortStopPrice : na, color=color_red, style=plot.style_cross, linewidth=1, title="Short Stop Loss")// Submit exit orders based on calculated stop loss priceif (strategy.position_size > 0) strategy.exit(id="ExL",limit=longExitPrice, stop=longStopPrice)if (strategy.position_size < 0) strategy.exit(id="ExS", limit=shortExitPrice, stop=shortStopPrice)// Exit open market position when date range endsif (not inDateRange) strategy.close_all()