Pine Script v4 · Strategy
Backtest Custom HiLo Strategy
A Pine Script v4 strategy built on EMA or SMA (high, 34), Highest (high, 34) and Lowest (low, 34). It has 5 order rules, 4 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 100.0% of equity and pays no commission. 15 settings; defaults include Start Day 2, Start Month 1 and Start Year 2020.
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 Backtest Custom HiLo 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 Backtest Custom HiLo 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 Backtest Custom HiLo 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 Backtest Custom HiLo Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Backtest Custom HiLo Strategy run in Zeal Terminal?
- Yes
- It compiles in 21 ms and runs 1,000 bars in 76 ms, in the browser.
BTCUSDT · 1D · default settings
- HiLo
- Buy
- Sell
- 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 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.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
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 66
transp = on line 66, 67, 68. v5 folds the transparency into the colour with color.new(c, transp).
-
Note
Uses long =, which v5 renamed line 90
long = on line 90, 92, 94, 96. 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, sma → ta.sma, highest → ta.highest, lowest → ta.lowest, sign → math.sign, crossover → ta.crossover, crossunder → ta.crossunder.
v4 → v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 3: //@version=4 → //@version=5
- Line 12: input(type = input.integer) → input.int()
- Line 14: input(type = input.integer) → input.int()
- Line 16: input(type = input.integer) → input.int()
- Line 19: input(type = input.integer) → input.int()
- Line 21: input(type = input.integer) → input.int()
20 more edits
- Line 23: input(type = input.integer) → input.int()
- Line 26: input(type = input.integer) → input.int()
- Line 28: input(type = input.integer) → input.int()
- Line 42: ema → ta.ema
- Line 42: sma → ta.sma
- Line 43: ema → ta.ema
- Line 43: sma → ta.sma
- Line 44: highest → ta.highest
- Line 45: lowest → ta.lowest
- Line 53: sign → math.sign
- Line 54: sign → math.sign
- Line 57: crossover → ta.crossover
- Line 58: crossunder → ta.crossunder
- Line 66: transp = 0 → color.new(hlColor, 0)
- Line 67: transp = 0 → color.new(color.green, 0)
- Line 68: transp = 0 → color.new(color.maroon, 0)
- Line 90: long = true → direction = strategy.long
- Line 92: long = false → direction = strategy.short
- Line 94: long = true → direction = strategy.long
- Line 96: long = false → direction = strategy.short
⋯// © luyikaibr-//@version=4+//@version=5strategy("Backtest Custom HiLo Strategy", "BTCHiLoS", overlay=true, precision=2,⋯// Configure backtest start date with inputs-startDay = input(title="Start Day", type=input.integer,+startDay = input.int(title="Start Day", defval=2, minval=1, maxval=31)-startMonth = input(title="Start Month", type=input.integer,+startMonth = input.int(title="Start Month", defval=1, minval=1, maxval=12)-startYear = input(title="Start Year", type=input.integer,+startYear = input.int(title="Start Year", defval=2020, minval=1800, maxval=2100)// Set backtest end date with inputs-endDay = input(title="End Day", type=input.integer,+endDay = input.int(title="End Day", defval=31, minval=1, maxval=31)-endMonth = input(title="End Month", type=input.integer,+endMonth = input.int(title="End Month", defval=10, minval=1, maxval=12)-endYear = input(title="End Year", type=input.integer,+endYear = input.int(title="End Year", defval=2020, minval=1800, maxval=2100) -endHour = input(title="End Hour", type=input.integer,+endHour = input.int(title="End Hour", defval=17, minval=0, maxval=23)-endMin = input(title="End Minute", type=input.integer,+endMin = input.int(title="End Minute", defval=0, minval=0, maxval=59)⋯opBoth = (opType == "Both")-hima = (useEMA ? ema(high, len) : sma(high, len))+hima = (useEMA ? ta.ema(high, len) : ta.sma(high, len))-loma = (useEMA ? ema(low, len) : sma(low, len))+loma = (useEMA ? ta.ema(low, len) : ta.sma(low, len))-hihi = (simpleHiLo ? na: highest(high, len))+hihi = (simpleHiLo ? na: ta.highest(high, len))-lolo = (simpleHiLo ? na: lowest(low, len))+lolo = (simpleHiLo ? na: ta.lowest(low, len))hilo = close⋯hlColor := (close < loma[offset] ? color.red : (close > hima[offset] ? color.green : hlColor[1]))-buyArith = sign(close - hima[offset])+buyArith = math.sign(close - hima[offset])-sellArith = sign(close - loma[offset])+sellArith = math.sign(close - loma[offset])buyCond = (close > hima[offset])sellCond = (close < loma[offset])-buy = crossover(buyArith, 0.5)+buy = ta.crossover(buyArith, 0.5)-sell = crossunder(sellArith, -0.5)+sell = ta.crossunder(sellArith, -0.5)buyState = false⋯ -plot(hilo, color=hlColor, linewidth=2, style=plot.style_cross, transp=0, title="HiLo")+plot(hilo, color=color.new(hlColor, 0), linewidth=2, style=plot.style_cross, title="HiLo")-plotshape(enterBuyState, title = "Buy", text = 'Buy', style = shape.labelup, location = location.belowbar, color= color.green,textcolor = color.white, transp = 0, size = size.tiny)+plotshape(enterBuyState, title = "Buy", text = 'Buy', style = shape.labelup, location = location.belowbar, color= color.new(color.green, 0),textcolor = color.white, size = size.tiny)-plotshape(enterSellState, title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color= color.maroon,textcolor = color.white, transp = 0, size = size.tiny)+plotshape(enterSellState, title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color= color.new(color.maroon, 0),textcolor = color.white, size = size.tiny)// See if this bar's time happened on/after start date⋯if (atStartDate and beforeEndDate and buyCond)- strategy.entry(id="EL", long=true)+ strategy.entry(id="EL", direction = strategy.long)if (atStartDate and beforeEndDate and sellCond)- strategy.entry(id="ES", long=false)+ strategy.entry(id="ES", direction = strategy.short)if (afterStartDate and beforeEndDate and enterBuyState)- strategy.entry(id="EL", long=true)+ strategy.entry(id="EL", direction = strategy.long)if (afterStartDate and beforeEndDate and enterSellState)- strategy.entry(id="ES", long=false)+ strategy.entry(id="ES", direction = strategy.short) ⋯Clean v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 30: input() → input.bool()
- Line 31: input() → input.bool()
- Line 32: input() → input.int()
- Line 33: input() → input.int()
- Line 34: input() → input.string()
- Line 36: input() → input.string()
- Line 38: input() → input.string()
⋯ defval=0, minval=0, maxval=59)-startAtOrigin = input(false, title="Start at Origin")+startAtOrigin = input.bool(false, title="Start at Origin")-endNow = input(false, title="End Now")+endNow = input.bool(false, title="End Now")-len = input(34, title="Length", minval=0)+len = input.int(34, title="Length", minval=0)-offset = input(0, title="Offset", minval=0)+offset = input.int(0, title="Offset", minval=0)-hiloType = input(title="Type", defval="HiLo", options=["HiLo", "HiLo Activator"])+hiloType = input.string(title="Type", defval="HiLo", options=["HiLo", "HiLo Activator"])simpleHiLo = (hiloType == "HiLo")-maType = input(title="MA Type", defval="SMA", options=["SMA", "EMA"])+maType = input.string(title="MA Type", defval="SMA", options=["SMA", "EMA"])useEMA = (maType == "EMA")-opType = input(title="Operation Type", defval="Long", options=["Long", "Short", "Both"])+opType = input.string(title="Operation Type", defval="Long", options=["Long", "Short", "Both"])opShort = (opType != "Long")⋯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.
⋯// © luyikaibr-//@version=5+//@version=6strategy("Backtest Custom HiLo Strategy", "BTCHiLoS", overlay=true, precision=2,⋯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 |
|---|---|---|---|
| Start Day | 2 | int (written input.integer) · min 1 · max 31 | startDate |
| Start Month | 1 | int (written input.integer) · min 1 · max 12 | startDate |
| Start Year | 2020 | int (written input.integer) · min 1800 · max 2100 | startDate |
| End Day | 31 | int (written input.integer) · min 1 · max 31 | endTS |
| End Month | 10 | int (written input.integer) · min 1 · max 12 | endTS |
| End Year | 2020 | int (written input.integer) · min 1800 · max 2100 | endTS |
| End Hour | 17 | int (written input.integer) · min 0 · max 23 | endTS |
| End Minute | 0 | int (written input.integer) · min 0 · max 59 | endTS |
| Start at Origin | false | bool | afterStartDate atStartDate |
| End Now | false | bool | endTS |
| Length | 34 | int · min 0 | hima loma hihi lolo |
| Offset | 0 | int · min 0 | hilo hlColor buyArith sellArith buyCond sellCond |
| Type | HiLo | string · options ["HiLo", "HiLo Activator"] | simpleHiLo |
| MA Type | SMA | string · options ["SMA", "EMA"] | useEMA |
| Operation Type | Long | string · options ["Long", "Short", "Both"] | opShort opLong opBoth |
What it calculates
- EMA / SMA (
ema,ta.emain v5)high, Length (34)hima · line 42 - EMA / SMA (
ema,ta.emain v5)low, Length (34)loma · line 43 - Highest (
highest,ta.highestin v5)high, Length (34)hihi · line 44 - Lowest (
lowest,ta.lowestin v5)low, Length (34)lolo · line 45 - Crossover (
crossover,ta.crossoverin v5)buyArith, 0.5buy · line 57 - Crossunder (
crossunder,ta.crossunderin v5)sellArith, -0.5sell · line 58
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 |
|---|---|---|---|
| Open EL long | (atStartDate and beforeEndDate and buyCond) | On | 90 |
| Open ES short | (atStartDate and beforeEndDate and sellCond) | On | 92 |
| Open EL long | (afterStartDate and beforeEndDate and enterBuyState) | On | 94 |
| Open ES short | (afterStartDate and beforeEndDate and enterSellState) | On | 96 |
| Close | (not beforeEndDate and strategy.position_size != 0) | On | 100 |
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 timingprocess_orders_on_close = true: orders fill at the signal bar's close.
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.
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 |
|---|---|---|---|---|---|
| Backtest Custom HiLo Strategy this page | — | v4 strategy | 0.0% | 0 | 0.0% |
| Forex bot full strategy with risk management by exlux99 | lowest highest ema sma | v4 strategy | −26.4% | 11 | 38.9% |
| ATR + STC + MA by Romedius | ema lowest highest sma | v5 strategy | −1.2% | 15 | 3.3% |
| Bagheri IG Ether by Bagheri_IG | ema highest lowest sma | v4 strategy | +0.0% | 3 | 0.0% |
| Bagheri IG Ether v2 by Bagheri_IG | ema highest lowest sma | v4 strategy | −0.0% | 2 | 0.0% |
| Crunchster's Turtle and Trend System by Crunchster1 | lowest highest ema sma | v5 strategy | +18.0% | 31 | 16.7% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 21 ms | With a workaround 1,000 bars in 76 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 90, 92, 94, 96) 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/// © luyikaibr//@version=4strategy("Backtest Custom HiLo Strategy", "BTCHiLoS", overlay=true, precision=2, process_orders_on_close=true, pyramiding=1, close_entries_rule="FIFO", initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=100.0)// Configure backtest start date with inputsstartDay = input(title="Start Day", type=input.integer, defval=2, minval=1, maxval=31)startMonth = input(title="Start Month", type=input.integer, defval=1, minval=1, maxval=12)startYear = input(title="Start Year", type=input.integer, defval=2020, minval=1800, maxval=2100)// Set backtest end date with inputsendDay = input(title="End Day", type=input.integer, defval=31, minval=1, maxval=31)endMonth = input(title="End Month", type=input.integer, defval=10, minval=1, maxval=12)endYear = input(title="End Year", type=input.integer, defval=2020, minval=1800, maxval=2100) endHour = input(title="End Hour", type=input.integer, defval=17, minval=0, maxval=23)endMin = input(title="End Minute", type=input.integer, defval=0, minval=0, maxval=59)startAtOrigin = input(false, title="Start at Origin")endNow = input(false, title="End Now")len = input(34, title="Length", minval=0)offset = input(0, title="Offset", minval=0)hiloType = input(title="Type", defval="HiLo", options=["HiLo", "HiLo Activator"])simpleHiLo = (hiloType == "HiLo")maType = input(title="MA Type", defval="SMA", options=["SMA", "EMA"])useEMA = (maType == "EMA")opType = input(title="Operation Type", defval="Long", options=["Long", "Short", "Both"])opShort = (opType != "Long")opLong = (opType != "Short")opBoth = (opType == "Both")hima = (useEMA ? ema(high, len) : sma(high, len))loma = (useEMA ? ema(low, len) : sma(low, len))hihi = (simpleHiLo ? na: highest(high, len))lolo = (simpleHiLo ? na: lowest(low, len))hilo = closehilo := if simpleHiLo (close < loma[offset] ? hima : (close > hima[offset] ? loma : hilo[1]))else (close < loma[offset] ? hihi : (close > hima[offset] ? lolo : hilo[1]))hlColor = color.redhlColor := (close < loma[offset] ? color.red : (close > hima[offset] ? color.green : hlColor[1]))buyArith = sign(close - hima[offset])sellArith = sign(close - loma[offset])buyCond = (close > hima[offset])sellCond = (close < loma[offset])buy = crossover(buyArith, 0.5)sell = crossunder(sellArith, -0.5)buyState = falsebuyState := buy ? true : (sell ? false : buyState[1])sellState = falsesellState := buy ? false : (sell ? true : sellState[1])enterBuyState = buyState[0] and not buyState[1]enterSellState = sellState[0] and not sellState[1] plot(hilo, color=hlColor, linewidth=2, style=plot.style_cross, transp=0, title="HiLo")plotshape(enterBuyState, title = "Buy", text = 'Buy', style = shape.labelup, location = location.belowbar, color= color.green,textcolor = color.white, transp = 0, size = size.tiny)plotshape(enterSellState, title = "Sell", text = 'Sell', style = shape.labeldown, location = location.abovebar, color= color.maroon,textcolor = color.white, transp = 0, size = size.tiny)// See if this bar's time happened on/after start datestartDate = timestamp(syminfo.timezone, startYear, startMonth, startDay, 0, 0)afterStartDate = if startAtOrigin trueelse (time >= startDate)atStartDate = if startAtOrigin falseelse (time >= startDate and time < (startDate + time_close[1]-time_close[2])) // See if bar's close time is before end dateendTS = if endNow timenow - 2*(time_close[1]-time_close[2]) // 86400else timestamp(syminfo.timezone, endYear, endMonth, endDay, endHour, endMin, 0)beforeEndDate = (time < endTS)// Only submit enter long ordersstrategy.risk.allow_entry_in(opBoth ? strategy.direction.all : (opLong ? strategy.direction.long : strategy.direction.short))// Submit entry orders, but only on/after start dateif (atStartDate and beforeEndDate and buyCond) strategy.entry(id="EL", long=true)if (atStartDate and beforeEndDate and sellCond) strategy.entry(id="ES", long=false)if (afterStartDate and beforeEndDate and enterBuyState) strategy.entry(id="EL", long=true)if (afterStartDate and beforeEndDate and enterSellState) strategy.entry(id="ES", long=false) // if (barstate.islast or not beforeEndDate)if (not beforeEndDate and strategy.position_size != 0) strategy.close_all(comment="End")