Skip to content

Technical Indicators API Reference

Trend Indicators

financelib.trading.algo_trade.sma.sma(data: pd.Series, period: int = 20) -> pd.Series

Calculate the Simple Moving Average.

SMA is the unweighted mean of the previous N data points.

Parameters:

Name Type Description Default
data Series

Price series.

required
period int

Number of periods to average (default: 20).

20

Returns:

Type Description
Series

SMA values as a pandas Series.

Source code in financelib/trading/algo_trade/sma.py
def sma(data: pd.Series, period: int = 20) -> pd.Series:
    """Calculate the Simple Moving Average.

    SMA is the unweighted mean of the previous N data points.

    Args:
        data: Price series.
        period: Number of periods to average (default: 20).

    Returns:
        SMA values as a pandas Series.
    """
    return pd.Series(data).rolling(window=period).mean()

financelib.trading.algo_trade.ema.ema(data: pd.Series, period: int = 20) -> pd.Series

Calculate the Exponential Moving Average.

EMA gives more weight to recent prices, making it more responsive to new information than SMA.

Parameters:

Name Type Description Default
data Series

Price series.

required
period int

Number of periods for EMA span (default: 20).

20

Returns:

Type Description
Series

EMA values as a pandas Series.

Source code in financelib/trading/algo_trade/ema.py
def ema(data: pd.Series, period: int = 20) -> pd.Series:
    """Calculate the Exponential Moving Average.

    EMA gives more weight to recent prices, making it more
    responsive to new information than SMA.

    Args:
        data: Price series.
        period: Number of periods for EMA span (default: 20).

    Returns:
        EMA values as a pandas Series.
    """
    return pd.Series(data).ewm(span=period).mean()

financelib.trading.algo_trade.macd.macd(data: pd.Series, fast_period: int = 12, slow_period: int = 26, signal_period: int = 9) -> Tuple[pd.Series, pd.Series]

Calculate MACD (Moving Average Convergence Divergence).

MACD shows the relationship between two EMAs of prices. A signal line crossover indicates potential buy/sell signals.

Parameters:

Name Type Description Default
data Series

Price series (typically close prices).

required
fast_period int

Fast EMA period (default: 12).

12
slow_period int

Slow EMA period (default: 26).

26
signal_period int

Signal line EMA period (default: 9).

9

Returns:

Type Description
Tuple[Series, Series]

Tuple of (macd_line, signal_line) as pandas Series.

Source code in financelib/trading/algo_trade/macd.py
def macd(
    data: pd.Series,
    fast_period: int = 12,
    slow_period: int = 26,
    signal_period: int = 9,
) -> Tuple[pd.Series, pd.Series]:
    """Calculate MACD (Moving Average Convergence Divergence).

    MACD shows the relationship between two EMAs of prices.
    A signal line crossover indicates potential buy/sell signals.

    Args:
        data: Price series (typically close prices).
        fast_period: Fast EMA period (default: 12).
        slow_period: Slow EMA period (default: 26).
        signal_period: Signal line EMA period (default: 9).

    Returns:
        Tuple of (macd_line, signal_line) as pandas Series.
    """
    fast_ema = pd.Series(data).ewm(span=fast_period).mean()
    slow_ema = pd.Series(data).ewm(span=slow_period).mean()
    macd_line = fast_ema - slow_ema
    signal_line = macd_line.ewm(span=signal_period).mean()
    return macd_line, signal_line

financelib.trading.algo_trade.adx.adx(high: pd.Series, low: pd.Series, close: pd.Series, period: int = 14) -> pd.Series

Calculate the Average Directional Index.

ADX measures the strength of a trend regardless of direction. Values above 25 indicate a strong trend.

Parameters:

Name Type Description Default
high Series

High price series.

required
low Series

Low price series.

required
close Series

Close price series.

required
period int

Lookback period (default: 14).

14

Returns:

Type Description
Series

ADX values as a pandas Series.

Source code in financelib/trading/algo_trade/adx.py
def adx(
    high: pd.Series, low: pd.Series, close: pd.Series, period: int = 14
) -> pd.Series:
    """Calculate the Average Directional Index.

    ADX measures the strength of a trend regardless of direction.
    Values above 25 indicate a strong trend.

    Args:
        high: High price series.
        low: Low price series.
        close: Close price series.
        period: Lookback period (default: 14).

    Returns:
        ADX values as a pandas Series.
    """
    plus_dm = high.diff()
    minus_dm = low.diff()
    tr = pd.DataFrame({
        "HL": high - low,
        "HPC": abs(high - close.shift(1)),
        "LPC": abs(low - close.shift(1)),
    }).max(axis=1)

    atr_val = tr.rolling(period).mean()
    plus_di = 100 * (plus_dm.rolling(period).mean() / atr_val)
    minus_di = 100 * (minus_dm.rolling(period).mean() / atr_val)
    dx = 100 * abs(plus_di - minus_di) / (plus_di + minus_di)
    return dx.rolling(period).mean()

financelib.trading.algo_trade.aroon.aroon(high: pd.Series, low: pd.Series, period: int = 25) -> Tuple[pd.Series, pd.Series]

Calculate the Aroon Indicator.

Aroon identifies whether a security is trending and how strong the trend is. Aroon Up above 70 = strong uptrend, Aroon Down above 70 = strong downtrend.

Parameters:

Name Type Description Default
high Series

High price series.

required
low Series

Low price series.

required
period int

Lookback period (default: 25).

25

Returns:

Type Description
Tuple[Series, Series]

Tuple of (aroon_up, aroon_down) as pandas Series (0-100 range).

Source code in financelib/trading/algo_trade/aroon.py
def aroon(
    high: pd.Series, low: pd.Series, period: int = 25
) -> Tuple[pd.Series, pd.Series]:
    """Calculate the Aroon Indicator.

    Aroon identifies whether a security is trending and how strong
    the trend is. Aroon Up above 70 = strong uptrend,
    Aroon Down above 70 = strong downtrend.

    Args:
        high: High price series.
        low: Low price series.
        period: Lookback period (default: 25).

    Returns:
        Tuple of (aroon_up, aroon_down) as pandas Series (0-100 range).
    """
    aroon_up = 100 * (
        period - high.rolling(period + 1).apply(lambda x: x.argmax())
    ) / period
    aroon_down = 100 * (
        period - low.rolling(period + 1).apply(lambda x: x.argmin())
    ) / period
    return aroon_up, aroon_down

Momentum Indicators

financelib.trading.algo_trade.rsi.rsi(data: pd.Series, period: int = 14) -> pd.Series

Calculate the Relative Strength Index.

RSI measures the speed and magnitude of price changes to evaluate overbought (>70) or oversold (<30) conditions.

Parameters:

Name Type Description Default
data Series

Price series (typically close prices).

required
period int

Lookback period (default: 14).

14

Returns:

Type Description
Series

RSI values as a pandas Series (0-100 range).

Source code in financelib/trading/algo_trade/rsi.py
def rsi(data: pd.Series, period: int = 14) -> pd.Series:
    """Calculate the Relative Strength Index.

    RSI measures the speed and magnitude of price changes to evaluate
    overbought (>70) or oversold (<30) conditions.

    Args:
        data: Price series (typically close prices).
        period: Lookback period (default: 14).

    Returns:
        RSI values as a pandas Series (0-100 range).
    """
    delta = pd.Series(data).diff()
    gain = (delta.where(delta > 0, 0)).rolling(window=period).mean()
    loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean()
    rs = gain / loss
    return 100 - (100 / (1 + rs))

financelib.trading.algo_trade.stochastic.stochastic(high: pd.Series, low: pd.Series, close: pd.Series, k_period: int = 14, d_period: int = 3) -> Tuple[pd.Series, pd.Series]

Calculate the Stochastic Oscillator.

Compares a closing price to a range of prices over a period. %K above 80 = overbought, below 20 = oversold.

Parameters:

Name Type Description Default
high Series

High price series.

required
low Series

Low price series.

required
close Series

Close price series.

required
k_period int

%K lookback period (default: 14).

14
d_period int

%D smoothing period (default: 3).

3

Returns:

Type Description
Tuple[Series, Series]

Tuple of (%K, %D) as pandas Series.

Source code in financelib/trading/algo_trade/stochastic.py
def stochastic(
    high: pd.Series,
    low: pd.Series,
    close: pd.Series,
    k_period: int = 14,
    d_period: int = 3,
) -> Tuple[pd.Series, pd.Series]:
    """Calculate the Stochastic Oscillator.

    Compares a closing price to a range of prices over a period.
    %K above 80 = overbought, below 20 = oversold.

    Args:
        high: High price series.
        low: Low price series.
        close: Close price series.
        k_period: %K lookback period (default: 14).
        d_period: %D smoothing period (default: 3).

    Returns:
        Tuple of (%K, %D) as pandas Series.
    """
    lowest_low = low.rolling(k_period).min()
    highest_high = high.rolling(k_period).max()
    k = 100 * (close - lowest_low) / (highest_high - lowest_low)
    d = k.rolling(d_period).mean()
    return k, d

financelib.trading.algo_trade.williams_r.williams_r(high: pd.Series, low: pd.Series, close: pd.Series, period: int = 14) -> pd.Series

Calculate Williams %R.

Williams %R is a momentum indicator that measures overbought and oversold levels. Values range from -100 to 0. Above -20 = overbought, below -80 = oversold.

Parameters:

Name Type Description Default
high Series

High price series.

required
low Series

Low price series.

required
close Series

Close price series.

required
period int

Lookback period (default: 14).

14

Returns:

Type Description
Series

Williams %R values as a pandas Series (-100 to 0 range).

Source code in financelib/trading/algo_trade/williams_r.py
def williams_r(
    high: pd.Series, low: pd.Series, close: pd.Series, period: int = 14
) -> pd.Series:
    """Calculate Williams %R.

    Williams %R is a momentum indicator that measures overbought
    and oversold levels. Values range from -100 to 0.
    Above -20 = overbought, below -80 = oversold.

    Args:
        high: High price series.
        low: Low price series.
        close: Close price series.
        period: Lookback period (default: 14).

    Returns:
        Williams %R values as a pandas Series (-100 to 0 range).
    """
    highest_high = high.rolling(period).max()
    lowest_low = low.rolling(period).min()
    return -100 * (highest_high - close) / (highest_high - lowest_low)

financelib.trading.algo_trade.cci.cci(high: pd.Series, low: pd.Series, close: pd.Series, period: int = 20) -> pd.Series

Calculate the Commodity Channel Index.

CCI measures the current price relative to an average price level over a given period. Values above +100 may indicate overbought, below -100 may indicate oversold.

Parameters:

Name Type Description Default
high Series

High price series.

required
low Series

Low price series.

required
close Series

Close price series.

required
period int

Lookback period (default: 20).

20

Returns:

Type Description
Series

CCI values as a pandas Series.

Source code in financelib/trading/algo_trade/cci.py
def cci(
    high: pd.Series, low: pd.Series, close: pd.Series, period: int = 20
) -> pd.Series:
    """Calculate the Commodity Channel Index.

    CCI measures the current price relative to an average price level
    over a given period. Values above +100 may indicate overbought,
    below -100 may indicate oversold.

    Args:
        high: High price series.
        low: Low price series.
        close: Close price series.
        period: Lookback period (default: 20).

    Returns:
        CCI values as a pandas Series.
    """
    tp = (high + low + close) / 3
    sma_tp = tp.rolling(period).mean()
    mad = tp.rolling(period).apply(lambda x: (x - x.mean()).abs().mean())
    return (tp - sma_tp) / (0.015 * mad)

Volatility Indicators

financelib.trading.algo_trade.bollinger_bands.bollinger_bands(data: pd.Series, period: int = 20, std_dev: int = 2) -> Tuple[pd.Series, pd.Series, pd.Series]

Calculate Bollinger Bands.

Bollinger Bands consist of a middle SMA band with upper and lower bands at a specified number of standard deviations.

Parameters:

Name Type Description Default
data Series

Price series (typically close prices).

required
period int

SMA lookback period (default: 20).

20
std_dev int

Number of standard deviations for bands (default: 2).

2

Returns:

Type Description
Tuple[Series, Series, Series]

Tuple of (upper_band, middle_band, lower_band) as pandas Series.

Source code in financelib/trading/algo_trade/bollinger_bands.py
def bollinger_bands(
    data: pd.Series, period: int = 20, std_dev: int = 2
) -> Tuple[pd.Series, pd.Series, pd.Series]:
    """Calculate Bollinger Bands.

    Bollinger Bands consist of a middle SMA band with upper and lower
    bands at a specified number of standard deviations.

    Args:
        data: Price series (typically close prices).
        period: SMA lookback period (default: 20).
        std_dev: Number of standard deviations for bands (default: 2).

    Returns:
        Tuple of (upper_band, middle_band, lower_band) as pandas Series.
    """
    series = pd.Series(data)
    middle_band = series.rolling(window=period).mean()
    std = series.rolling(window=period).std()
    upper_band = middle_band + (std * std_dev)
    lower_band = middle_band - (std * std_dev)
    return upper_band, middle_band, lower_band

financelib.trading.algo_trade.atr.atr(high: pd.Series, low: pd.Series, close: pd.Series, period: int = 14) -> pd.Series

Calculate the Average True Range.

ATR measures market volatility by decomposing the entire range of a price for a given period.

Parameters:

Name Type Description Default
high Series

High price series.

required
low Series

Low price series.

required
close Series

Close price series.

required
period int

Lookback period (default: 14).

14

Returns:

Type Description
Series

ATR values as a pandas Series.

Source code in financelib/trading/algo_trade/atr.py
def atr(
    high: pd.Series, low: pd.Series, close: pd.Series, period: int = 14
) -> pd.Series:
    """Calculate the Average True Range.

    ATR measures market volatility by decomposing the entire range
    of a price for a given period.

    Args:
        high: High price series.
        low: Low price series.
        close: Close price series.
        period: Lookback period (default: 14).

    Returns:
        ATR values as a pandas Series.
    """
    tr = pd.DataFrame({
        "HL": high - low,
        "HPC": abs(high - close.shift(1)),
        "LPC": abs(low - close.shift(1)),
    }).max(axis=1)
    return tr.rolling(period).mean()