Pivot High/Low Breakout — Pine Script v5 strategy
This script finds swing highs and lows confirmed with 5 bars on each side, then buys when the close breaks above the last confirmed swing high. It sells when the close drops below the last confirmed swing low.
Full code (free to copy)
//@version=5
strategy("Pivot Structure Breakout 5/5", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1)
leftBars = input.int(5, "Pivot Left Bars", minval=2, maxval=15)
rightBars = input.int(5, "Pivot Right Bars", minval=2, maxval=15)
ph = ta.pivothigh(high, leftBars, rightBars)
pl = ta.pivotlow(low, leftBars, rightBars)
var float lastHigh = na
var float lastLow = na
if not na(ph)
lastHigh := ph
if not na(pl)
lastLow := pl
if not na(lastHigh) and ta.crossover(close, lastHigh)
strategy.entry("Long", strategy.long)
if not na(lastLow) and ta.crossunder(close, lastLow)
strategy.close("Long")
plot(lastHigh, "Confirmed Swing High", color=color.teal)
plot(lastLow, "Confirmed Swing Low", color=color.red)Judge this code on BTCUSDT 1D →Free, in your browser, on the same candles as the table below.
Online judge verdict on BTCUSDT (measured 29 November 2026)
| Timeframe | Sealed period | Trades | Return | Buy & hold | Verdict |
|---|---|---|---|---|---|
| 1d | 2024-12-17 → 2026-10-16 | 9 | -6.46 % | -23.25 % | Not enough trades to judge yet |
| 4h | 2026-03-12 → 2026-10-17 | 14 | 8.98 % | 15.83 % | No evidence of an edge yet: 2 of 4 steps passed |
| 1h | 2026-08-05 → 2026-10-17 | 25 | 6.23 % | 27.14 % | No evidence of an edge, for now |
Past data, 10 bps per fill, last 20% of the bars sealed. Windows: full history on 1d, last 3 years on 4h, last year on 1h. Research, not advice: no result is promised.
This reading has not been compared with a real TradingView run.
Measured on 29 November 2026 with the online judge’s Pine v5 engine (sa-pine-vm 0.2.0), on Binance BTCUSDT candles up to 16/10/2026 on 1D and up to 17/10/2026 on 4h and 1h. Differences with TradingView’s Strategy Tester remain possible. Binance data, CC BY-NC-SA 4.0.
Open this code in the free judge (1 click) →
Replay each verdict yourself, in your browser: 1d · 4h · 1h. The script is filled in; nothing runs until you click “Judge”.
How the script works
ph = ta.pivothigh(high, leftBars, rightBars) and pl = ta.pivotlow(low, leftBars, rightBars), 5 and 5 by default. A pivot is known only 5 bars after the high or low: the code stores it in lastHigh or lastLow when it is confirmed.
ta.crossover(close, lastHigh) opens the position, ta.crossunder(close, lastLow) closes it. Both levels are plotted on the chart.
Parameters in the code
| Parameter | Value | Role |
|---|---|---|
leftBars | 5 | Input "Pivot Left Bars" created with input.int, from 2 to 15. |
rightBars | 5 | Input "Pivot Right Bars" created with input.int, from 2 to 15. |
initial_capital | 10000 | Starting capital declared in strategy(). |
default_qty_type | percent_of_equity, 100 | Each entry commits 100% of available equity. |
commission_value | 0.1 (%) | 0.10% commission per order declared in the script; the judge applies it (10 bps per fill). |
process_orders_on_close | not set | Orders fill at the open of the next bar, the Pine default. |
Known limits of this rule
- The 5-bar confirmation delay lags the levels: the high being broken may already be old.
- The last confirmed low can sit far below price: the loss per trade is not bounded in advance.
- On a chart, the pivot is drawn at the bar of the high; a visual reading that forgets the delay looks too favourable.
Before you trust it: test it on data it never saw
A backtest in TradingView's Strategy Tester shows one past path with the settings you picked. Strategy Arena Lab (free, Windows and Linux) replays this Pine on your PC, compares its trades one by one with TradingView's for supported Pine, counts the variants you try, then judges the rule on a period kept sealed during the search: survives, no evidence of an edge or inconclusive.
FAQ
What does the online judge say about this script?
On BTCUSDT, with 10 bps fees per fill, measured on 29 November 2026: 1D: "Not enough trades to judge yet" (9 trades, -6.46% on the sealed period); 4h: "No evidence of an edge yet: 2 of 4 steps passed" (14 trades, 8.98% on the sealed period); 1h: "No evidence of an edge, for now" (25 trades, 6.23% on the sealed period). No timeframe reaches "Survives". These verdicts concern past data and say nothing certain about the future.
Do pivots repaint?
ta.pivothigh and ta.pivotlow return a value only once the right-hand bars are finished; the code uses them at that moment, so without looking ahead. The visual offset on the chart differs from the moment the rule can act.
Can I change the script's settings?
Yes, the settings are inputs you can edit in TradingView. Each setting you try is one more trial: a setting chosen after seeing the sealed period is no longer a test on unseen data. The judge and the Lab count trials for this reason.
Other free Pine Script v5 strategies
More TradingView paths
Educational code on past data. No investment advice; no result is promised.