CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-stock-indicators

Stock Indicators for Python provides financial market technical indicators from historical price quotes.

Overview
Eval results
Files

trend-indicators.mddocs/

Trend Indicators

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.

Capabilities

Simple Moving Average (SMA)

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 date
  • sma (Optional[Decimal]): Simple moving average value

SMA Analysis

Enhanced 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 date
  • sma (Optional[Decimal]): Simple moving average value
  • mad (Optional[Decimal]): Mean Absolute Deviation
  • mse (Optional[Decimal]): Mean Squared Error
  • mape (Optional[Decimal]): Mean Absolute Percentage Error

Exponential Moving Average (EMA)

Gives 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 date
  • ema (Optional[Decimal]): Exponential moving average value

Weighted Moving Average (WMA)

Linear 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 date
  • wma (Optional[Decimal]): Weighted moving average value

Hull Moving Average (HMA)

Fast-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 date
  • hma (Optional[Decimal]): Hull moving average value

Double Exponential Moving Average (DEMA)

Applies 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 date
  • dema (Optional[Decimal]): Double exponential moving average value

Triple Exponential Moving Average (TEMA)

Applies 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 date
  • tema (Optional[Decimal]): Triple exponential moving average value

T3 Moving Average

Tim 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 date
  • t3 (Optional[Decimal]): T3 moving average value

Kaufman's Adaptive Moving Average (KAMA)

Adaptive 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 date
  • kama (Optional[Decimal]): KAMA value
  • efficiency_ratio (Optional[float]): Efficiency ratio

MESA Adaptive Moving Average (MAMA)

Adaptive 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 date
  • mama (Optional[Decimal]): MAMA value
  • fama (Optional[Decimal]): Following Adaptive Moving Average (FAMA) value

Arnaud Legoux Moving Average (ALMA)

Moving 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 date
  • alma (Optional[Decimal]): ALMA value

Endpoint Moving Average (EPMA)

Linear 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 date
  • epma (Optional[Decimal]): EPMA value

Smoothed Moving Average (SMMA)

Also 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 date
  • smma (Optional[Decimal]): SMMA value

Volume Weighted Moving Average (VWMA)

Moving 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 date
  • vwma (Optional[Decimal]): VWMA value

Usage Examples

Basic Moving Average Comparison

from 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}")

Adaptive Moving Average with Market Conditions

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}")

Volume-Weighted Analysis

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

docs

core-types.md

index.md

momentum-indicators.md

overlay-indicators.md

specialized-indicators.md

trend-indicators.md

volatility-indicators.md

volume-indicators.md

tile.json