StochRSI#
Description#
StochRSI (Chande & Kroll, 1994) applies the Stochastic oscillator formula to an RSI series rather than to price. It is a "rate of change of momentum" indicator: more responsive than RSI alone and useful for spotting RSI turning points.
1-input, 2-output (FunctorBase<_, 1, 2>).
Setting it up#
Configuration |
Constructor |
TA-Lib equivalent |
|---|---|---|
Fast StochRSI (TA-Lib's |
|
|
Slow StochRSI -- add smoothing on K |
|
not in TA-Lib; common in pandas-ta |
smooth_k=1 is the identity SMA, collapsing %K = raw_K (the "fast" form). smooth_k >= 2 gives the slow form.
Parameters#
rsi_period(int, default14): RSI lookback. Uses Wilder smoothing internally.stoch_period(int, default14): rolling min/max window applied to the RSI series.smooth_k(int, default1): SMA period applied toraw_K.d(int, default3): SMA period applied to%Kto produce%D.
Warmup: both outputs are NaN until %D is valid, at sample index rsi_period + stoch_period + smooth_k + d - 3 (TA-Lib's convention -- gate both K and D together). For defaults that is index 29.
Range-zero: when the RSI window is flat (max == min), raw_K is undefined; we return 0.
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 StochRSI
np.random.seed(0)
close = 100*np.exp(np.cumsum(np.random.normal(0, 0.01, size=300)))
out = StochRSI(14, 14, 1, 3)(close)
k = out[:, 0]
d = out[:, 1]
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=close, name="close"), row=1, col=1)
fig.add_trace(go.Scatter(y=k, name="%K", line=dict(color="red")), row=2, col=1)
fig.add_trace(go.Scatter(y=d, name="%D", line=dict(color="orange")), row=2, col=1)
fig.update_layout(title="Price with Stochastic RSI (StochRSI)",
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="%K / %D (0-100)", row=2, col=1)
fig.show()
Implementation Details#
Composition: an internal RollingRSI(rsi_period, method="wilder"), two detail::MonotonicDeque (for rolling RSI min / max), and two detail::RollingMean (for the smooth_k and d SMAs). Amortised O(1) per step.
Reference#
Matches talib.STOCHRSI(close, timeperiod=N, fastk_period=K, fastd_period=D) bit-exactly (to ~1e-13 in tests/test_third_party_alignment.py).