williams_r

Defined in fynance.features.ohlcv

williams_r(high, low=None, close=None, w=14)[source]

Williams %R over a trailing window, causal.

\(\%R_t = -100 \cdot (HH_t - C_t) / (HH_t - LL_t)\), where HH/LL are the rolling high/low of window w.

Parameters:
high, low, closearray-like, or high = OHLCV

High/Low/Close series, or a single OHLCV.

wint, optional

Look-back window. Default 14.

Returns:
numpy.ndarray

Williams %R, aligned with the input.

Raises:
ValueError

If w < 1, or if low / close are missing without an OHLCV first argument.

Notes

%R is bounded in [-100, 0] only when the bars are valid OHLC, i.e. each close lies within its window’s high/low range (\(LL_t \le C_t \le HH_t\)). The raw values are returned unclamped, so a close outside [LL_t, HH_t] — e.g. corrupt or mismatched series — yields values outside [-100, 0] (a close above the rolling high gives %R > 0, below the rolling low gives %R < -100). This is intentional: clamping would silently mask such data issues.

Examples

>>> import numpy as np
>>> h = np.array([10., 11., 12., 11.])
>>> low = np.array([9., 9., 10., 10.])
>>> c = np.array([9.5, 10.5, 11.5, 10.5])
>>> williams_r(h, low, c, w=2).round(2)
array([-50.  , -25.  , -16.67, -75.  ])