ta.wpr in MQL5: iWPR and the Williams %R formula
Short answer
Use iWPR(symbol, timeframe, length): it computes exactly Williams %R, from 0 at the top of the range to -100 at the bottom. The two differ only on a window whose high equals its low, where iWPR keeps the previous value.
ta.wpr(length) is Williams %R: minus 100 times the distance of the close from the highest high of the last length bars, divided by the window's range, so the value runs from 0 (close at the top of the range) to -100 (close at the bottom). MetaTrader 5's iWPR(symbol, timeframe, length) computes exactly this, with the same inputs and no applied-price choice, so the two are interchangeable except on a window whose high equals its low.
What ta.wpr computes
-100 * (highest(high, length) - close) / (highest(high, length) - lowest(low, length)). Unlike most ta.* functions it takes no source: it always reads the chart's high, low and close, which is why a Williams %R of another series has to be written as ta.stoch arithmetic instead (%R = ta.stoch(...) - 100). The window covers the current bar and the length - 1 before it, so the first value appears on bar index length - 1 and is na earlier. When the window is flat the denominator is zero and the result is na. The current bar's high and low are part of the window, so on the realtime bar the value changes with every tick that extends the bar.
The MQL5 equivalent
//@version=6
indicator("WPR ref")
w = ta.wpr(14)
plot(w, "%R")The indicator below writes the formula out so the na behaviour is yours to control. Arrays are oldest-first, so the window is i - length + 1 .. i.
//+------------------------------------------------------------------+
//| Williams %R matching ta.wpr(length) |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_label1 "%R"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_minimum -100
#property indicator_maximum 0
#property indicator_level1 -80
#property indicator_level2 -20
input int InpLength = 14; // Length
double WprBuf[];
int OnInit()
{
SetIndexBuffer(0, WprBuf, INDICATOR_DATA);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
return INIT_SUCCEEDED;
}
int OnCalculate(const int rates_total, const int prev_calculated,
const datetime &time[], const double &open[],
const double &high[], const double &low[], const double &close[],
const long &tick_volume[], const long &volume[], const int &spread[])
{
int start = prev_calculated > 1 ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
{
if(i < InpLength - 1) { WprBuf[i] = EMPTY_VALUE; continue; }
double hh = high[i], ll = low[i];
for(int k = 1; k < InpLength; k++)
{
if(high[i - k] > hh) hh = high[i - k];
if(low[i - k] < ll) ll = low[i - k];
}
// flat window: Pine's division by zero is na
WprBuf[i] = hh == ll ? EMPTY_VALUE : -100.0 * (hh - close[i]) / (hh - ll);
}
return rates_total;
}Or use the built-in handle and CopyBuffer buffer 0:
// iWPR(symbol, timeframe, length): same formula on the bar high/low/close.
// On a flat window it repeats the previous value where ta.wpr gives na.
int wprHandle = iWPR(_Symbol, _Period, 14);Where the two differ
| Aspect | Pine ta.wpr | MQL5 iWPR |
|---|---|---|
| Formula | -100 * (HH - close) / (HH - LL) | Identical |
| Inputs | length only; high/low/close fixed | length only; high/low/close fixed |
| Flat window | na | Previous bar's value repeated |
| Warm-up | na for length - 1 bars | 0 written, hidden from the plot |
| Range | -100..0 | -100..0 |
| Access | Direct | Handle plus CopyBuffer |
The flat-window rule is the only semantic difference, and it only matters on illiquid symbols or very low timeframes where several consecutive bars have the same high and low. After such a stretch Pine shows a gap in the line (or whatever nz() the script applies), MetaTrader shows a horizontal segment. A condition such as ta.wpr(14) < -80 is false on an na bar in Pine and may be true on the repeated value in MT5, so a strategy can take one extra signal there.
What the Stratlate converter emits
The converter does not use iWPR. It emits the formula inline: -100.0 * pine_div(pine_highest(high, bar, len) - close[bar], pine_highest(high, bar, len) - pine_lowest(low, bar, len)), where pine_highest and pine_lowest return na while the window is short and pine_div returns na for a zero denominator, so the flat-window behaviour is Pine's. The expression appears twice with pine_highest evaluated per occurrence, which is correct but repeats the window scan; on a long chart you may want to hoist it by hand if you edit the output. The mapping is reported as exact and the test script converts CLEAN. Compile and backtest the result.
Pitfalls
- Dropping the minus sign. The line runs 0..100 and every overbought test (
< -80) is inverted. - Taking the highest close instead of the highest high. The oscillator becomes a stochastic of closes and reaches 0 and -100 far more often.
- Using
length - 1bars because "the current bar is extra". The window islengthbars including the current one; off by one, every extreme is reached a bar early. - Guarding the flat window with 0 or -50. Pine shows
na; a numeric filler produces a signal where TradingView has none. - Reading
iWPRbeforeBarsCalculatedequalsrates_total: the first ticks after attaching the indicator get a short copy and the plot is blank until the next tick.
FAQ
Is iWPR identical to ta.wpr?
Yes on every bar whose window has a range: both compute -100 times the distance of the close from the highest high divided by the highest-high minus lowest-low range over length bars. On a window where high equals low Pine returns na and MetaTrader repeats the previous value.
How do I compute Williams %R of a custom source in MQL5?
Write the stochastic formula on your own buffer: -100 * (highest(src) - src) / (highest(src) - lowest(src)) over the window. Pine's ta.wpr cannot do that either; it reads the bar high, low and close only.
What is the relation between Williams %R and the stochastic %K?
%R equals the raw %K minus 100: ta.stoch(close, high, low, len) - 100. Both measure where the close sits in the recent range; %R is written on a -100..0 scale and %K on 0..100.
Convert the whole script
Paste a Pine Script indicator or strategy and get MQL5 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.
Open the converter