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

Relative Strength Index [Wikipedia](https://en.wikipedia.org/wiki/Relative_strength_index).

Gain and loss averages use Wilder's smoothing (an RMA seeded with the
SMA of the first `period` changes), matching TA-Lib. `nil` is returned
when there is not enough data or the window has no movement (RSI is
undefined). See `TAlib` for the input conventions.

# `average_gain`

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

Wilder-smoothed average gain over the `period` newest price changes.

## Parameters

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

## Examples

    iex> TAlib.Indicators.RSI.average_gain([1, 2, 3, 4, 5, 6], 3) |> Float.round(4)
    1.0

# `average_loss`

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

Wilder-smoothed average loss over the `period` newest price changes.

## Parameters

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

## Examples

    iex> TAlib.Indicators.RSI.average_loss([6, 1, 4, 2, 5, 3], 3) |> Float.round(4)
    1.7037

# `gain`

```elixir
@spec gain([number()]) :: number()
```

Sum of gains across consecutive prices, oldest first.

## Examples

    iex> TAlib.Indicators.RSI.gain([0, 10, 20, 30])
    30

# `init`

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

Warm up streaming RSI state from `history`.

Returns `{state, values}` with `values` the full RSI series of
`history` (identical to `rsi_list/2`), and `state` ready to continue
it one price at a time in O(1) via `next/2`. History needs more than
`period` prices (the first `period` changes form the seed).

## Examples

    iex> {state, values} = TAlib.Indicators.RSI.init([1, 2, 3, 4, 5], 3)
    iex> values
    [nil, nil, nil, 100.0, 100.0]
    iex> {_state, 100.0} = TAlib.Indicators.RSI.next(state, 6)

# `loss`

```elixir
@spec loss([number()]) :: number()
```

Sum of losses across consecutive prices, oldest first.

## Examples

    iex> TAlib.Indicators.RSI.loss([40, 30, 20, 10])
    30

# `next`

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

Advance streaming RSI state by one price, in O(1).

    iex> {state, _} = TAlib.Indicators.RSI.init([1, 2, 3, 4, 5], 3)
    iex> {_state, value} = TAlib.Indicators.RSI.next(state, 6)
    iex> value
    100.0

# `rsi`

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

Calculate RSI over the `period` newest price changes, with Wilder's
smoothing (matching TA-Lib).

## Parameters

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

## Examples

    iex> TAlib.Indicators.RSI.rsi([1, 2, 3, 4, 5, 6], 3)
    100.0

    iex> TAlib.Indicators.RSI.rsi([6, 1, 4, 2, 5, 3], 3) |> Float.round(4)
    39.4737

# `rsi_list`

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

Calculate RSI for each point of a price series in a single pass
(Wilder-smoothed gain/loss averages).

Returns a list aligned with `data`: the first `period` entries are `nil`,
and every later entry is the RSI of the window ending at that point.

## Parameters

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

## Examples

    iex> TAlib.Indicators.RSI.rsi_list([1, 2, 3, 4, 5], 3)
    [nil, nil, nil, 100.0, 100.0]

---

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