# `TAlib.Indicators.WilliamsR`
[🔗](https://github.com/benyblack/talib/blob/main/lib/indicators/williams_r.ex#L1)

Williams %R [Wikipedia](https://en.wikipedia.org/wiki/Williams_%25R).

`%R = (Highest High - Close) / (Highest High - Lowest Low) * -100`

Ranges from -100 (close at the window low) to 0 (close at the window
high). See `TAlib` for the input conventions: prices are always given in
chronological order, oldest first, and the series is returned aligned
with the input. `nil` is returned when there is not enough data or the
window has no range (flat prices), since %R is then undefined.

# `init`

```elixir
@spec init([number()], pos_integer()) ::
  {TAlib.Indicators.WilliamsR.State.t(), [float() | nil]}
```

Warm up streaming Williams %R state from `history`.

Returns `{state, values}` with `values` the full %R series of
`history` (identical to `williams_r_list/2`), and `state` ready to
continue it one price at a time in O(1) via `next/2`. History needs
at least `period` prices.

## Examples

    iex> {state, values} = TAlib.Indicators.WilliamsR.init([5, 9, 3, 8, 2, 6], 3)
    iex> values |> Enum.map(fn x -> if is_nil(x), do: nil, else: Float.round(x, 4) end)
    [nil, nil, -100.0, -16.6667, -100.0, -33.3333]
    iex> {_state, -25.0} = TAlib.Indicators.WilliamsR.next(state, 5)

# `next`

```elixir
@spec next(TAlib.Indicators.WilliamsR.State.t(), number()) ::
  {TAlib.Indicators.WilliamsR.State.t(), float() | nil}
```

Advance streaming Williams %R state by one price, in O(1). A flat
window (no range) yields `nil`, like the batch series.

    iex> {state, _} = TAlib.Indicators.WilliamsR.init([5, 9, 3, 8, 2, 6], 3)
    iex> {_state, value} = TAlib.Indicators.WilliamsR.next(state, 5)
    iex> value
    -25.0

# `williams_r`

```elixir
@spec williams_r([number()], pos_integer()) :: float() | nil
```

Calculate the latest Williams %R.

## Parameters

  - prices: list of prices, oldest first.
  - period: lookback window, a positive integer (default 14).

## Examples

    iex> TAlib.Indicators.WilliamsR.williams_r([5, 9, 3, 8, 2, 6], 3) |> Float.round(4)
    -33.3333

# `williams_r_list`

```elixir
@spec williams_r_list([number()], pos_integer()) :: [float() | nil]
```

Calculate the full Williams %R series.

Returns a list aligned with `prices`: the first `period - 1` entries are
`nil`, and every later entry is %R of the window ending at that point.
Windows with no range produce `nil`.

## Parameters

  - prices: list of prices, oldest first.
  - period: lookback window, a positive integer (default 14).

## Examples

    iex> TAlib.Indicators.WilliamsR.williams_r_list([5, 9, 3, 8, 2, 6], 3) |> Enum.map(fn
    ...>   nil -> nil
    ...>   x -> Float.round(x, 4)
    ...> end)
    [nil, nil, -100.0, -16.6667, -100.0, -33.3333]

---

*Consult [api-reference.md](api-reference.md) for complete listing*
