Skip to main content

Module willr

Module willr 

Source
Expand description

§Williams %R

Oscillator on high/low/close over lookback (N):

%R = -100 * (HH − close) / (HH − LL)

where (HH = \max(high)) and (LL = \min(low)) over the last (N) bars. Range is typically [−100, 0]. Flat window ((HH = LL)): carry previous %R, else −50.

Default: period 14 (WillrParams::period_14).


§Trading perspective

RegionHabit (classic, not a rule)
%R > −20“Overbought” screen
%R < −80“Oversold” screen
Cross back from extremeMomentum resume / mean-reversion exit screen

§vs other oscillators

Williams %RStochasticRSI
Scale[−100, 0][0, 100][0, 100]
InputsH/L/C windowH/L/C + smoothCloses only
FeelFast, inverted Stoch-likeSmoothed %K/%DWilder smooth, slower

Rough map: raw Stoch %K ≈ 100 + %R (same HH/LL idea; sign/offset differ). Prefer Stochastic when you want %D signal line; %R when you want a single fast line.

§Pairs well with

  • ADX / DI — only fade %R extremes when ADX is low (range); avoid fading when ADX is high.
  • SMA/EMA trend filter — long setups only above rising MA, etc.
  • Volume (OBV/MFI) — confirm oversold bounce with rising money flow.
  • ATR stops — oscillator does not size risk; ATR does.

§Engineering

WillrParamswillr / WillrStatewillr_solution.
Batch uses WillrState end-to-end. HH/LL are amortized O(1) (sliding max/min).

§Word problem

Highs 12, lows 10, close 11 for three bars with (N=3). What is %R on bar 2?

Expect: (HH=12), (LL=10), %R = (-100 \times (12-11)/(12-10) = -50).

use finance_solution::stocks::ta::{willr, WillrParams};
let h = [12.0, 12.0, 12.0];
let l = [10.0, 10.0, 10.0];
let c = [11.0, 11.0, 11.0];
let s = willr(&h, &l, &c, WillrParams::new(3)).unwrap();
assert!((s.willr[2].unwrap() - (-50.0)).abs() < 1e-12);

Structs§

ValidatedWillr
Validated pack.
WillrParams
Williams %R lookback.
WillrSeries
WillrSolution
WillrState
Incremental Williams %R.

Functions§

willr
willr_solution
Examples