RollingRSI#

Description#

Wilder's Relative Strength Index. Up- and down-moves are separately smoothed; the index is 100 - 100 / (1 + RS) where RS is the smoothed ratio of average up to average down moves:

\[\begin{split} \begin{aligned} \Delta[t] &= x[t] - x[t-1] \\ U[t] &= \max(\Delta, 0),\quad D[t] = \max(-\Delta, 0) \\ \overline{U}[t] &= \text{smooth}(U,\ \text{window\_size}),\quad \overline{D}[t] = \text{smooth}(D,\ \text{window\_size}) \\ \text{RS}[t] &= \overline{U}[t] / \overline{D}[t] \\ \text{RSI}[t] &= 100 - 100 / (1 + \text{RS}) \end{aligned} \end{split}\]

The smoothing depends on method:

  • 'wilder' (default) -- Wilder's smoothing \(\overline{X}[t] = \overline{X}[t-1] + (X[t] - \overline{X}[t-1]) / w\), with the SMA-of-the-first-w seed. Matches talib.RSI and pandas-ta-classic.rsi bit-exactly post-warmup.

  • 'cutler' -- plain rolling-mean smoothing. Cutler's variant. Useful if you specifically want the SMA-form (some legacy systems use it). Does not match TA-Lib.

Notes#

  • First valid output at sample index window_size.

  • Output is bounded in [0, 100].

  • The default was changed from 'cutler' to 'wilder' in a recent release to align with TA-Lib and pandas-ta-classic. Pass method='cutler' to recover the old behaviour.

NaN handling#

Policy: ignore. A NaN in any input at index t causes the function to skip that step: output at t is NaN and internal state is unchanged. Subsequent finite samples are processed as if step t had not occurred.

Examples#

Usage example#

import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from screamer import RollingRSI

np.random.seed(0)
price = 100*np.exp(np.cumsum(np.random.normal(0.0005, 0.02, size=300)))
rsi = RollingRSI(window_size=14)(price)

fig = make_subplots(rows=2, cols=1, shared_xaxes=True,
                    row_heights=[0.55, 0.45], vertical_spacing=0.08)
fig.add_trace(go.Scatter(y=price, name="price"), row=1, col=1)
fig.add_trace(go.Scatter(y=rsi, name="RollingRSI", line=dict(color="red")), row=2, col=1)
fig.update_layout(title="Price with RSI (RollingRSI)",
                  margin=dict(l=20, r=20, t=60, b=20),
                  legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1))
fig.update_yaxes(title_text="price", row=1, col=1)
fig.update_yaxes(title_text="RSI (0-100)", row=2, col=1)
fig.show()