Stock Indicators for Python provides financial market technical indicators from historical price quotes.
Oscillators that measure the rate of price change and momentum to identify overbought/oversold conditions, trend strength, and potential reversal points. These indicators typically oscillate between fixed boundaries and are essential for timing market entries and exits.
Measures the strength of winning versus losing streaks on a scale of 0 to 100, commonly used to identify overbought (>70) and oversold (<30) conditions.
def get_rsi(quotes: Iterable[Quote], lookback_periods: int = 14):
"""
Relative Strength Index (RSI) measures strength of winning/losing streak
over N lookback periods on a scale of 0 to 100.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods in the lookback window (defaults to 14)
Returns:
RSIResults[RSIResult]: Collection of RSI results
"""RSIResult Properties:
date (datetime): Result datersi (Optional[Decimal]): RSI value (0-100)Momentum indicator comparing closing price to its price range over a lookback period, producing %K and %D lines with optional %J extension for KDJ analysis.
def get_stoch(quotes: Iterable[Quote], lookback_periods: int = 14, signal_periods: int = 3,
smooth_periods: int = 3, k_factor: float = 3, d_factor: float = 2,
ma_type: MAType = MAType.SMA):
"""
Stochastic Oscillator with KDJ indexes - momentum indicator on a scale of 0 to 100.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods for the Oscillator (defaults to 14)
signal_periods (int): Smoothing period for the %D signal line (defaults to 3)
smooth_periods (int): Smoothing period for the %K Oscillator (defaults to 3, use 1 for Fast)
k_factor (float): K factor for %J calculation (defaults to 3)
d_factor (float): D factor for %J calculation (defaults to 2)
ma_type (MAType): Moving average type for smoothing (defaults to SMA)
Returns:
STOCHResults[STOCHResult]: Collection of Stochastic results
"""STOCHResult Properties:
date (datetime): Result dateoscillator (Optional[Decimal]): %K oscillator value (0-100)signal (Optional[Decimal]): %D signal line value (0-100)percent_j (Optional[Decimal]): %J extension value for KDJ analysisCombines RSI and Stochastic oscillator concepts, applying stochastic calculation to RSI values for enhanced sensitivity.
def get_stoch_rsi(quotes: Iterable[Quote], rsi_periods: int = 14, stoch_periods: int = 14,
signal_periods: int = 3, smooth_periods: int = 1):
"""
Stochastic RSI applies stochastic calculation to RSI values for enhanced sensitivity.
Args:
quotes (Iterable[Quote]): Historical price quotes
rsi_periods (int): Number of periods for RSI calculation (defaults to 14)
stoch_periods (int): Number of periods for Stochastic calculation (defaults to 14)
signal_periods (int): Smoothing period for signal line (defaults to 3)
smooth_periods (int): Smoothing period for oscillator (defaults to 1)
Returns:
StochRSIResults[StochRSIResult]: Collection of Stochastic RSI results
"""StochRSIResult Properties:
date (datetime): Result datestoch_rsi (Optional[Decimal]): Stochastic RSI value (0-100)percent_k (Optional[Decimal]): %K line valuepercent_d (Optional[Decimal]): %D signal line valueShows the relationship between two moving averages with signal line and histogram for trend and momentum analysis.
def get_macd(quotes: Iterable[Quote], fast_periods: int = 12, slow_periods: int = 26,
signal_periods: int = 9, candle_part: CandlePart = CandlePart.CLOSE):
"""
Moving Average Convergence/Divergence (MACD) - oscillator view of two
converging/diverging exponential moving averages.
Args:
quotes (Iterable[Quote]): Historical price quotes
fast_periods (int): Number of periods in the Fast EMA (defaults to 12)
slow_periods (int): Number of periods in the Slow EMA (defaults to 26)
signal_periods (int): Number of periods for the Signal moving average (defaults to 9)
candle_part (CandlePart): Selected OHLCV part (defaults to CLOSE)
Returns:
MACDResults[MACDResult]: Collection of MACD results
"""MACDResult Properties:
date (datetime): Result datemacd (Optional[Decimal]): MACD line (fast EMA - slow EMA)signal (Optional[Decimal]): Signal line (EMA of MACD)histogram (Optional[Decimal]): MACD histogram (MACD - Signal)fast_ema (Optional[Decimal]): Fast EMA valueslow_ema (Optional[Decimal]): Slow EMA valueMulti-component momentum oscillator combining RSI, streak analysis, and rate of change for enhanced overbought/oversold signals.
def get_connors_rsi(quotes: Iterable[Quote], rsi_periods: int = 3, streak_periods: int = 2,
rank_periods: int = 100):
"""
Connors RSI - multi-component momentum oscillator combining RSI, streak, and rank analysis.
Args:
quotes (Iterable[Quote]): Historical price quotes
rsi_periods (int): RSI lookback periods (defaults to 3)
streak_periods (int): Consecutive up/down periods for streak RSI (defaults to 2)
rank_periods (int): Periods for percentile rank of rate of change (defaults to 100)
Returns:
ConnorsRSIResults[ConnorsRSIResult]: Collection of Connors RSI results
"""ConnorsRSIResult Properties:
date (datetime): Result datersi_close (Optional[Decimal]): RSI of closing pricesrsi_streak (Optional[Decimal]): RSI of streak valuespercent_rank (Optional[Decimal]): Percentile rank componentconnors_rsi (Optional[Decimal]): Combined Connors RSI value (0-100)Measures the percentage change in price over a specified number of periods.
def get_roc(quotes: Iterable[Quote], lookback_periods: int):
"""
Rate of Change (ROC) measures percentage change in price over lookback periods.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods for rate calculation
Returns:
ROCResults[ROCResult]: Collection of ROC results
"""ROCResult Properties:
date (datetime): Result dateroc (Optional[Decimal]): Rate of change as percentageroc_sma (Optional[Decimal]): Simple moving average of ROCROC with upper and lower bands based on standard deviation for volatility context.
def get_roc_with_band(quotes: Iterable[Quote], lookback_periods: int, ema_periods: int,
std_dev_periods: int):
"""
Rate of Change with Bands - ROC with volatility bands.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods for ROC calculation
ema_periods (int): EMA periods for center line
std_dev_periods (int): Standard deviation periods for bands
Returns:
ROCWBResults[ROCWBResult]: Collection of ROC with Bands results
"""ROCWBResult Properties:
date (datetime): Result dateroc (Optional[Decimal]): Rate of change valueupper_band (Optional[Decimal]): Upper volatility bandlower_band (Optional[Decimal]): Lower volatility bandBill Williams' Awesome Oscillator using the difference between 5-period and 34-period simple moving averages of midpoint prices.
def get_awesome(quotes: Iterable[Quote], fast_periods: int = 5, slow_periods: int = 34):
"""
Awesome Oscillator - difference between fast and slow SMAs of midpoint prices.
Args:
quotes (Iterable[Quote]): Historical price quotes
fast_periods (int): Fast SMA periods (defaults to 5)
slow_periods (int): Slow SMA periods (defaults to 34)
Returns:
AwesomeResults[AwesomeResult]: Collection of Awesome Oscillator results
"""AwesomeResult Properties:
date (datetime): Result dateoscillator (Optional[Decimal]): Awesome Oscillator valueMomentum oscillator that identifies cyclical trends and overbought/oversold conditions.
def get_cci(quotes: Iterable[Quote], lookback_periods: int = 20):
"""
Commodity Channel Index (CCI) - momentum oscillator for cyclical trends.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods for calculation (defaults to 20)
Returns:
CCIResults[CCIResult]: Collection of CCI results
"""CCIResult Properties:
date (datetime): Result datecci (Optional[Decimal]): CCI value (typically between -100 and +100)Momentum oscillator that uses both up and down price movements in its calculation, similar to RSI but with different scaling.
def get_cmo(quotes: Iterable[Quote], lookback_periods: int = 14):
"""
Chande Momentum Oscillator (CMO) - momentum indicator using up/down movements.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods for calculation (defaults to 14)
Returns:
CMOResults[CMOResult]: Collection of CMO results
"""CMOResult Properties:
date (datetime): Result datecmo (Optional[Decimal]): CMO value (-100 to +100)Momentum indicator that measures overbought/oversold levels, similar to Stochastic but with inverted scale.
def get_williams_r(quotes: Iterable[Quote], lookback_periods: int = 14):
"""
Williams %R - momentum indicator measuring overbought/oversold levels.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods for calculation (defaults to 14)
Returns:
WilliamsResults[WilliamsResult]: Collection of Williams %R results
"""WilliamsResult Properties:
date (datetime): Result datewilliams_r (Optional[Decimal]): Williams %R value (-100 to 0)Momentum oscillator that double-smooths price rate of change for cleaner signals.
def get_pmo(quotes: Iterable[Quote], time_periods: int = 35, smoothing_periods: int = 20,
signal_periods: int = 10):
"""
Price Momentum Oscillator (PMO) - double-smoothed rate of change oscillator.
Args:
quotes (Iterable[Quote]): Historical price quotes
time_periods (int): Time periods for ROC calculation (defaults to 35)
smoothing_periods (int): Smoothing periods for first EMA (defaults to 20)
signal_periods (int): Signal line EMA periods (defaults to 10)
Returns:
PMOResults[PMOResult]: Collection of PMO results
"""PMOResult Properties:
date (datetime): Result datepmo (Optional[Decimal]): PMO oscillator valuesignal (Optional[Decimal]): PMO signal lineMulti-timeframe momentum oscillator that combines short, medium, and long-term price momentum.
def get_ultimate(quotes: Iterable[Quote], short_periods: int = 7, middle_periods: int = 14,
long_periods: int = 28, short_weight: float = 4, middle_weight: float = 2,
long_weight: float = 1):
"""
Ultimate Oscillator - multi-timeframe momentum combining short, medium, and long periods.
Args:
quotes (Iterable[Quote]): Historical price quotes
short_periods (int): Short timeframe periods (defaults to 7)
middle_periods (int): Middle timeframe periods (defaults to 14)
long_periods (int): Long timeframe periods (defaults to 28)
short_weight (float): Weight for short timeframe (defaults to 4)
middle_weight (float): Weight for middle timeframe (defaults to 2)
long_weight (float): Weight for long timeframe (defaults to 1)
Returns:
UltimateResults[UltimateResult]: Collection of Ultimate Oscillator results
"""UltimateResult Properties:
date (datetime): Result dateultimate (Optional[Decimal]): Ultimate Oscillator value (0-100)Double-smoothed momentum oscillator that reduces noise while maintaining sensitivity to price changes.
def get_tsi(quotes: Iterable[Quote], lookback_periods: int = 25, smooth_periods: int = 13,
signal_periods: int = 7):
"""
True Strength Index (TSI) - double-smoothed momentum oscillator.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Lookback periods for momentum (defaults to 25)
smooth_periods (int): First smoothing periods (defaults to 13)
signal_periods (int): Signal line smoothing periods (defaults to 7)
Returns:
TSIResults[TSIResult]: Collection of TSI results
"""TSIResult Properties:
date (datetime): Result datetsi (Optional[Decimal]): TSI oscillator valuesignal (Optional[Decimal]): TSI signal lineTriple exponentially smoothed moving average oscillator that filters out short-term price movements.
def get_trix(quotes: Iterable[Quote], lookback_periods: int = 14, signal_periods: int = 9):
"""
TRIX - triple exponentially smoothed moving average oscillator.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods for TRIX calculation (defaults to 14)
signal_periods (int): Signal line EMA periods (defaults to 9)
Returns:
TRIXResults[TRIXResult]: Collection of TRIX results
"""TRIXResult Properties:
date (datetime): Result dateema1 (Optional[Decimal]): First EMA valueema2 (Optional[Decimal]): Second EMA valueema3 (Optional[Decimal]): Third EMA valuetrix (Optional[Decimal]): TRIX oscillator value (as percentage)signal (Optional[Decimal]): TRIX signal lineRemoves trend from price data to focus on shorter-term cycles by comparing price to a displaced moving average.
def get_dpo(quotes: Iterable[Quote], lookback_periods: int):
"""
Detrended Price Oscillator (DPO) - removes trend to focus on cycles.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods for moving average calculation
Returns:
DPOResults[DPOResult]: Collection of DPO results
"""DPOResult Properties:
date (datetime): Result datesma (Optional[Decimal]): Simple moving average valuedpo (Optional[Decimal]): Detrended Price Oscillator valuefrom stock_indicators.indicators import get_rsi
# Calculate RSI with standard 14-period setting
rsi_results = get_rsi(quotes, lookback_periods=14)
# Identify overbought/oversold conditions
for result in rsi_results:
if result.rsi is not None:
if result.rsi > 70:
print(f"{result.date}: RSI {result.rsi:.2f} - Overbought")
elif result.rsi < 30:
print(f"{result.date}: RSI {result.rsi:.2f} - Oversold")from stock_indicators.indicators import get_macd
# Calculate MACD with standard settings
macd_results = get_macd(quotes, fast_periods=12, slow_periods=26, signal_periods=9)
# Look for MACD crossovers
previous_result = None
for result in macd_results:
if result.macd is not None and result.signal is not None and previous_result:
# Bullish crossover: MACD crosses above signal
if (previous_result.macd <= previous_result.signal and
result.macd > result.signal):
print(f"{result.date}: Bullish MACD crossover")
# Bearish crossover: MACD crosses below signal
elif (previous_result.macd >= previous_result.signal and
result.macd < result.signal):
print(f"{result.date}: Bearish MACD crossover")
previous_result = resultfrom stock_indicators.indicators import get_rsi, get_stoch, get_cci
# Calculate multiple momentum indicators
rsi_results = get_rsi(quotes, lookback_periods=14)
stoch_results = get_stoch(quotes, lookback_periods=14)
cci_results = get_cci(quotes, lookback_periods=20)
# Look for momentum confirmation across indicators
for i in range(len(rsi_results)):
rsi_val = rsi_results[i].rsi
stoch_val = stoch_results[i].oscillator
cci_val = cci_results[i].cci
if rsi_val and stoch_val and cci_val:
# Check for oversold confirmation
oversold_signals = 0
if rsi_val < 30: oversold_signals += 1
if stoch_val < 20: oversold_signals += 1
if cci_val < -100: oversold_signals += 1
if oversold_signals >= 2:
print(f"{rsi_results[i].date}: Oversold confirmation - {oversold_signals}/3 indicators")Install with Tessl CLI
npx tessl i tessl/pypi-stock-indicators