Williams %R 14 Pullback — Pine Script v5 strategy
This script tries to buy short pullbacks within an uptrend. It computes Williams %R 14 and buys only if the close is above the 100 EMA; it sells when %R reaches the high zone or the trend breaks.
Full code (free to copy)
//@version=5
strategy("Williams %R Trend Pullback", overlay=false, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1)
wprLen = input.int(14, "WPR Length", minval=5, maxval=35)
trendLen = input.int(100, "Trend EMA", minval=40, maxval=200)
hh = ta.highest(high, wprLen)
ll = ta.lowest(low, wprLen)
wpr = -100.0 * (hh - close) / math.max(hh - ll, 0.0001)
trend = ta.ema(close, trendLen)
if close > trend and ta.crossover(wpr, -80)
strategy.entry("Long", strategy.long)
if ta.crossover(wpr, -20) or close < trend
strategy.close("Long")
plot(wpr, "Williams %R", color=color.orange)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 | 13 | -0.53 % | -23.25 % | No evidence of an edge, for now |
| 4h | 2026-03-12 → 2026-10-17 | 20 | -7.93 % | 15.83 % | No evidence of an edge, for now |
| 1h | 2026-08-05 → 2026-10-17 | 22 | -8.30 % | 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
%R is computed by hand: wpr = -100 × (hh - close) / max(hh - ll, 0.0001), with hh and ll the highest high and lowest low of the last 14 bars. trend = ta.ema(close, trendLen) with trendLen = 100.
The entry requires close > trend and ta.crossover(wpr, -80): %R leaves the low zone. The exit happens on ta.crossover(wpr, -20) or if close < trend.
Parameters in the code
| Parameter | Value | Role |
|---|---|---|
wprLen | 14 | Input "WPR Length" created with input.int, from 5 to 35. |
trendLen | 100 | Input "Trend EMA" created with input.int, from 40 to 200. |
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 target per trade is short (from the low zone to the high zone): fees take a noticeable share.
- At the end of a trend, a pullback can be the start of a decline; the exit below the 100 EMA then comes late.
- The 0.0001 floor avoids a division by zero but has no economic meaning.
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: "No evidence of an edge, for now" (13 trades, -0.53% on the sealed period); 4h: "No evidence of an edge, for now" (20 trades, -7.93% on the sealed period); 1h: "No evidence of an edge, for now" (22 trades, -8.30% on the sealed period). No timeframe reaches "Survives". These verdicts concern past data and say nothing certain about the future.
Why compute %R by hand rather than with ta.wpr?
The result follows the classic definition. Writing it by hand makes the computation readable and avoids depending on a function that some Pine readers do not recognise.
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.