Stock Indicators for Python provides financial market technical indicators from historical price quotes.
Moving averages and trend-following indicators that smooth price data to identify market direction and trend strength. These indicators are essential for trend analysis and are often used as the basis for other technical indicators.
Calculates the arithmetic mean of prices over a specified lookback period. The most basic and widely used trend indicator.
def get_sma(quotes: Iterable[Quote], lookback_periods: int,
candle_part: CandlePart = CandlePart.CLOSE):
"""
Simple Moving Average (SMA) is the average of price over a lookback window.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods in the lookback window
candle_part (CandlePart): Selected OHLCV part (defaults to CLOSE)
Returns:
SMAResults[SMAResult]: Collection of SMA results
"""SMAResult Properties:
date (datetime): Result datesma (Optional[Decimal]): Simple moving average valueEnhanced SMA calculation with statistical analysis including Mean Absolute Deviation (MAD), Mean Squared Error (MSE), and Mean Absolute Percentage Error (MAPE).
def get_sma_analysis(quotes: Iterable[Quote], lookback_periods: int):
"""
Simple Moving Average with analysis metrics.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods in the lookback window
Returns:
SMAAnalysisResults[SMAAnalysisResult]: Collection of SMA analysis results
"""SMAAnalysisResult Properties:
date (datetime): Result datesma (Optional[Decimal]): Simple moving average valuemad (Optional[Decimal]): Mean Absolute Deviationmse (Optional[Decimal]): Mean Squared Errormape (Optional[Decimal]): Mean Absolute Percentage ErrorGives more weight to recent prices, making it more responsive to price changes than SMA.
def get_ema(quotes: Iterable[Quote], lookback_periods: int):
"""
Exponential Moving Average (EMA) gives more weight to recent prices.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods in the lookback window
Returns:
EMAResults[EMAResult]: Collection of EMA results
"""EMAResult Properties:
date (datetime): Result dateema (Optional[Decimal]): Exponential moving average valueLinear weighted moving average where the most recent price has the highest weight, decreasing linearly for older prices.
def get_wma(quotes: Iterable[Quote], lookback_periods: int):
"""
Weighted Moving Average (WMA) applies linear weights to price data.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods in the lookback window
Returns:
WMAResults[WMAResult]: Collection of WMA results
"""WMAResult Properties:
date (datetime): Result datewma (Optional[Decimal]): Weighted moving average valueFast-responding moving average that reduces lag while maintaining smoothness by using weighted calculations with square roots.
def get_hma(quotes: Iterable[Quote], lookback_periods: int):
"""
Hull Moving Average (HMA) reduces lag while maintaining smoothness.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods in the lookback window
Returns:
HMAResults[HMAResult]: Collection of HMA results
"""HMAResult Properties:
date (datetime): Result datehma (Optional[Decimal]): Hull moving average valueApplies exponential smoothing twice to reduce lag while maintaining smoothness.
def get_dema(quotes: Iterable[Quote], lookback_periods: int):
"""
Double Exponential Moving Average (DEMA) applies exponential smoothing twice.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods in the lookback window
Returns:
DEMAResults[DEMAResult]: Collection of DEMA results
"""DEMAResult Properties:
date (datetime): Result datedema (Optional[Decimal]): Double exponential moving average valueApplies exponential smoothing three times for even smoother results with reduced lag.
def get_tema(quotes: Iterable[Quote], lookback_periods: int):
"""
Triple Exponential Moving Average (TEMA) applies exponential smoothing three times.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods in the lookback window
Returns:
TEMAResults[TEMAResult]: Collection of TEMA results
"""TEMAResult Properties:
date (datetime): Result datetema (Optional[Decimal]): Triple exponential moving average valueTim Tillson's T3 moving average designed to be smoother than traditional moving averages while reducing lag.
def get_t3(quotes: Iterable[Quote], lookback_periods: int = 5, volume_factor: float = 0.7):
"""
T3 Moving Average designed to be smoother with reduced lag.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods in the lookback window (defaults to 5)
volume_factor (float): Volume factor for smoothing (defaults to 0.7)
Returns:
T3Results[T3Result]: Collection of T3 results
"""T3Result Properties:
date (datetime): Result datet3 (Optional[Decimal]): T3 moving average valueAdaptive moving average that adjusts its smoothing based on market volatility and noise.
def get_kama(quotes: Iterable[Quote], er_periods: int = 10, fast_periods: int = 2, slow_periods: int = 30):
"""
Kaufman's Adaptive Moving Average (KAMA) adapts to market conditions.
Args:
quotes (Iterable[Quote]): Historical price quotes
er_periods (int): Efficiency Ratio periods (defaults to 10)
fast_periods (int): Fast EMA constant periods (defaults to 2)
slow_periods (int): Slow EMA constant periods (defaults to 30)
Returns:
KAMAResults[KAMAResult]: Collection of KAMA results
"""KAMAResult Properties:
date (datetime): Result datekama (Optional[Decimal]): KAMA valueefficiency_ratio (Optional[float]): Efficiency ratioAdaptive moving average based on Hilbert Transform using MESA algorithms to adapt to market cycles.
def get_mama(quotes: Iterable[Quote], fast_limit: float = 0.5, slow_limit: float = 0.05):
"""
MESA Adaptive Moving Average (MAMA) using Hilbert Transform.
Args:
quotes (Iterable[Quote]): Historical price quotes
fast_limit (float): Fast limit parameter (defaults to 0.5)
slow_limit (float): Slow limit parameter (defaults to 0.05)
Returns:
MAMAResults[MAMAResult]: Collection of MAMA results
"""MAMAResult Properties:
date (datetime): Result datemama (Optional[Decimal]): MAMA valuefama (Optional[Decimal]): Following Adaptive Moving Average (FAMA) valueMoving average designed to have less lag and be more responsive while reducing noise.
def get_alma(quotes: Iterable[Quote], lookback_periods: int = 9, offset: float = 0.85, sigma: float = 6.0):
"""
Arnaud Legoux Moving Average (ALMA) with reduced lag and noise.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods in the lookback window (defaults to 9)
offset (float): Offset parameter (defaults to 0.85)
sigma (float): Sigma parameter for smoothing (defaults to 6.0)
Returns:
ALMAResults[ALMAResult]: Collection of ALMA results
"""ALMAResult Properties:
date (datetime): Result datealma (Optional[Decimal]): ALMA valueLinear regression-based moving average that uses the endpoint of linear regression as the moving average value.
def get_epma(quotes: Iterable[Quote], lookback_periods: int):
"""
Endpoint Moving Average (EPMA) based on linear regression endpoint.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods in the lookback window
Returns:
EPMAResults[EPMAResult]: Collection of EPMA results
"""EPMAResult Properties:
date (datetime): Result dateepma (Optional[Decimal]): EPMA valueAlso known as Modified Moving Average (MMA), applies exponential smoothing with a smoothing factor.
def get_smma(quotes: Iterable[Quote], lookback_periods: int):
"""
Smoothed Moving Average (SMMA) using exponential smoothing.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods in the lookback window
Returns:
SMMAResults[SMMAResult]: Collection of SMMA results
"""SMMAResult Properties:
date (datetime): Result datesmma (Optional[Decimal]): SMMA valueMoving average weighted by trading volume, giving more importance to periods with higher volume.
def get_vwma(quotes: Iterable[Quote], lookback_periods: int):
"""
Volume Weighted Moving Average (VWMA) weighted by trading volume.
Args:
quotes (Iterable[Quote]): Historical price quotes
lookback_periods (int): Number of periods in the lookback window
Returns:
VWMAResults[VWMAResult]: Collection of VWMA results
"""VWMAResult Properties:
date (datetime): Result datevwma (Optional[Decimal]): VWMA valuefrom stock_indicators.indicators import get_sma, get_ema, get_hma
from stock_indicators.indicators.common import Quote
# Calculate different moving averages for comparison
sma_20 = get_sma(quotes, lookback_periods=20)
ema_20 = get_ema(quotes, lookback_periods=20)
hma_20 = get_hma(quotes, lookback_periods=20)
# Compare the latest values
latest_sma = sma_20[-1].sma
latest_ema = ema_20[-1].ema
latest_hma = hma_20[-1].hma
print(f"SMA: {latest_sma}, EMA: {latest_ema}, HMA: {latest_hma}")from stock_indicators.indicators import get_kama
# KAMA adapts to market volatility
kama_results = get_kama(quotes, er_periods=14, fast_periods=2, slow_periods=30)
# Analyze efficiency ratio to understand market conditions
for result in kama_results[-10:]: # Last 10 periods
if result.efficiency_ratio is not None:
market_condition = "Trending" if result.efficiency_ratio > 0.3 else "Sideways"
print(f"Date: {result.date}, KAMA: {result.kama}, Market: {market_condition}")from stock_indicators.indicators import get_vwma, get_sma
# Compare volume-weighted vs regular moving average
vwma_20 = get_vwma(quotes, lookback_periods=20)
sma_20 = get_sma(quotes, lookback_periods=20)
# Identify periods where volume emphasis makes a difference
for i in range(len(vwma_20)):
if vwma_20[i].vwma and sma_20[i].sma:
diff_pct = abs(vwma_20[i].vwma - sma_20[i].sma) / sma_20[i].sma * 100
if diff_pct > 1.0: # More than 1% difference
print(f"Date: {vwma_20[i].date}, VWMA: {vwma_20[i].vwma}, SMA: {sma_20[i].sma}, Diff: {diff_pct:.2f}%")Install with Tessl CLI
npx tessl i tessl/pypi-stock-indicators