Stochastic RSI Indicator
The Stochastic RSI (StochRSI) combines the Relative Strength Index with Stochastic oscillator mathematics to provide momentum and overbought/oversold signals on the 1-hour timeframe.
Overview
StochRSI is an oscillator that ranges from 0 to 100, measuring the level of RSI relative to its high-low range over a set period. It’s more sensitive than standard RSI, making it ideal for identifying momentum shifts.
How It Works
Calculation
- Calculate RSI over the specified period
- Apply Stochastic formula to RSI values:
StochRSI = (RSI - Lowest RSI) / (Highest RSI - Lowest RSI) × 100 - Smooth with moving averages for %K and %D lines
Implementation
# indicators/stochrsi.py
import pandas as pd
import pandas_ta as ta
def calculate_stochrsi(
df: pd.DataFrame,
length: int = 14,
stoch_length: int = 14,
smooth_k: int = 3,
smooth_d: int = 3
) -> pd.DataFrame:
"""
Calculate Stochastic RSI indicator.
Args:
df: DataFrame with OHLCV data
length: RSI period
stoch_length: Stochastic period
smooth_k: %K smoothing period
smooth_d: %D smoothing period
Returns:
DataFrame with StochRSI columns
"""
# Calculate StochRSI using pandas-ta
stochrsi = ta.stochrsi(
close=df['close'],
length=length,
rsi_length=length,
k=smooth_k,
d=smooth_d
)
# Add columns to dataframe
df['stochrsi_k'] = stochrsi[f'STOCHRSIk_{length}_{length}_{smooth_k}_{smooth_d}']
df['stochrsi_d'] = stochrsi[f'STOCHRSId_{length}_{length}_{smooth_k}_{smooth_d}']
return dfSignal Interpretation
Zones
| Zone | %K Range | Interpretation |
|---|---|---|
| Oversold | 0-20 | Potential buying opportunity |
| Neutral | 20-80 | Normal momentum |
| Overbought | 80-100 | Potential selling opportunity |
Crossover Signals
| Signal | Condition | Action |
|---|---|---|
| Bullish Cross | %K crosses above %D in oversold | Look for long entry |
| Bearish Cross | %K crosses below %D in overbought | Look for short entry |
Role in VRVP Strategy
StochRSI provides momentum timing for entries:
Long Entry Conditions
- %K crossing above oversold level (20), OR
- %K moving from oversold while below 60
Short Entry Conditions
- %K crossing below overbought level (80), OR
- %K moving from overbought while above 40
def check_stochrsi_signal(df: pd.DataFrame, config: StochRSIConfig) -> str:
"""Check StochRSI signal for entry."""
k = df['stochrsi_k'].iloc[-1]
k_prev = df['stochrsi_k'].iloc[-2]
# Bullish: crossing above oversold or recovering from oversold
if k_prev < config.oversold and k > config.oversold:
return "BULLISH_CROSS"
elif k < 60 and k > k_prev and k_prev < config.oversold:
return "BULLISH_MOMENTUM"
# Bearish: crossing below overbought or falling from overbought
if k_prev > config.overbought and k < config.overbought:
return "BEARISH_CROSS"
elif k > 40 and k < k_prev and k_prev > config.overbought:
return "BEARISH_MOMENTUM"
return "NEUTRAL"Configuration
@dataclass
class StochRSIConfig:
length: int = 14 # RSI period
stoch_length: int = 14 # Stochastic period
smooth_k: int = 3 # %K smoothing
smooth_d: int = 3 # %D smoothing
oversold: float = 20.0 # Oversold threshold
overbought: float = 80.0 # Overbought thresholdParameters
| Parameter | Default | Range | Description |
|---|---|---|---|
length | 14 | 7-21 | RSI calculation period |
stoch_length | 14 | 7-21 | Stochastic lookback |
smooth_k | 3 | 2-5 | %K smoothing |
smooth_d | 3 | 2-5 | %D smoothing |
oversold | 20 | 10-30 | Oversold threshold |
overbought | 80 | 70-90 | Overbought threshold |
Parameter Effects
Smoothing (K/D):
- Higher values → Smoother lines, fewer false signals
- Lower values → More responsive, more noise
Thresholds:
- Wider (10/90) → Fewer but stronger signals
- Narrower (30/70) → More frequent signals
Usage Example
from indicators.stochrsi import calculate_stochrsi
from config.settings import StochRSIConfig
# Load price data
df = load_ohlcv_data("EUR_USD", "1H")
# Configure indicator
config = StochRSIConfig(
length=14,
oversold=20.0,
overbought=80.0
)
# Calculate
df = calculate_stochrsi(
df,
length=config.length,
smooth_k=config.smooth_k,
smooth_d=config.smooth_d
)
# Check current state
k = df['stochrsi_k'].iloc[-1]
d = df['stochrsi_d'].iloc[-1]
print(f"StochRSI K: {k:.1f}, D: {d:.1f}")
if k < config.oversold:
print("Currently OVERSOLD - Watch for bullish reversal")
elif k > config.overbought:
print("Currently OVERBOUGHT - Watch for bearish reversal")Visual Representation
StochRSI Chart (1H)
100 ┬────────────────────────────────────────
│ ╭─╮ OVERBOUGHT
80 ├─────────────────────────────────────── ← Overbought Level
│ ╱ ╲
│ ╱ ╲
60 ├─────────────────╱───────╲─────────────
│ ╭──╮ ╱ ╲
40 ├────────╱────╲─╱───────────╲───────────
│ ╱ X ╲
│ ╱ ╱ ╲ ╲
20 ├─────╱──────────────────────────────── ← Oversold Level
│ ╱ OVERSOLD
0 ┴────────────────────────────────────────
│ │ │
Bullish Bullish Bearish
Cross Momentum CrossBest Practices
Tips for Using StochRSI:
- Wait for zone exit - Don’t trade just because indicator is oversold/overbought
- Confirm with trend - Only take signals in Supertrend direction
- Watch for divergence - Price making new highs while StochRSI isn’t
- Use with confluence - Combine with FVG and Volume Profile
Common Patterns
Bullish Divergence
Price makes lower low, StochRSI makes higher low → Potential reversal up
Bearish Divergence
Price makes higher high, StochRSI makes lower high → Potential reversal down
Related
- Supertrend - Trend direction
- Fair Value Gap - Price zones
- Volume Profile - Support/resistance
Last updated on