Skip to content

RSI Indicator

rsi

RSI (Relative Strength Index) calculation module.

Public API

compute_rsi(close: pd.Series, period: int = 14) -> pd.Series

compute_rsi(close, period=14)

Compute Relative Strength Index (RSI).

RSI = 100 - (100 / (1 + RS)) where RS = Average Gain / Average Loss.

Parameters:

Name Type Description Default
close Series

Series of closing prices.

required
period int

Lookback period length (must be >=1, default 14).

14

Returns:

Type Description
Series

Series of RSI values in range [0,100]. NaN values where insufficient history.

Raises:

Type Description
ValueError

If period < 1.

Source code in src/stockcharts/indicators/rsi.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def compute_rsi(close: pd.Series, period: int = 14) -> pd.Series:
    """Compute Relative Strength Index (RSI).

    RSI = 100 - (100 / (1 + RS)) where RS = Average Gain / Average Loss.

    Args:
        close: Series of closing prices.
        period: Lookback period length (must be >=1, default 14).

    Returns:
        Series of RSI values in range [0,100]. NaN values where insufficient history.

    Raises:
        ValueError: If ``period`` < 1.
    """
    if period < 1:
        raise ValueError("period must be >= 1")

    if len(close) < period + 1:
        # Not enough data to seed RSI calculation: return NaNs aligned to index
        return pd.Series([pd.NA] * len(close), index=close.index, dtype="Float64")

    # Price changes
    delta = close.diff()

    # Gains & losses separated
    gain = delta.where(delta > 0, 0.0)
    loss = -delta.where(delta < 0, 0.0)

    # EWMA averages (EWM chosen for smoother adaptation)
    avg_gain = gain.ewm(com=period - 1, min_periods=period).mean()
    avg_loss = loss.ewm(com=period - 1, min_periods=period).mean()

    rs = avg_gain / avg_loss
    rsi = 100 - (100 / (1 + rs))
    return rsi.astype(float)