Skip to content

mplchart.expressions

Polars expression factories.

Each factory returns a native polars.Expr computing the indicator over an OHLCV frame: prices.select(SMA(50)). In charts, expressions are passed to chart.plot(...) directly (auto-plotted) or bound to a renderer primitive with Line(RSI(14)) or RSI(14) @ Line(). Both binding forms defer calculation until plotting. These are the polars-pipeline counterparts of mplchart.indicators.


wrap_expression

wrap_expression(func)

Decorator for expression factory functions.

Allows the first positional argument to be a pl.Expr, which is then passed as the src keyword argument. This enables both calling styles:

SMA(20, pl.col("close"))   # positional expr
SMA(20)                    # defaults to CLOSE
SMA(20, src=pl.col("open"))

Aliases the resulting pl.Expr with the lowercase function name (e.g. "sma" or "macd"). Call .alias(...) on the returned expression when multiple instances need distinct names. Multi-output factories return a single pl.struct(...) Expr; the view's eval unnests it into a DataFrame at evaluation time.

OPEN

Column expression for the open price.

HIGH

Column expression for the high price.

LOW

Column expression for the low price.

CLOSE

Column expression for the close price. The default source of single-series factories.

VOLUME

Column expression for the volume series.

SMA

SMA(period: int = 20, *, src: pl.Expr = CLOSE)

Simple Moving Average

EMA

EMA(period: int = 20, *, src: pl.Expr = CLOSE)

Exponential Moving Average

RMA

RMA(period: int = 14, *, src: pl.Expr = CLOSE)

Rolling Moving Average (RSI style)

WMA

WMA(period: int = 20, *, src: pl.Expr = CLOSE)

Weighted Moving Average

HMA

HMA(period: int = 20, *, src: pl.Expr = CLOSE)

Hull Moving Average

DEMA

DEMA(period: int = 20, *, src: pl.Expr = CLOSE)

Double Exponential Moving Average

TEMA

TEMA(period: int = 20, *, src: pl.Expr = CLOSE)

Triple Exponential Moving Average

ROC

ROC(period: int = 1, *, src: pl.Expr = CLOSE)

Rate of Change (percent)

MOM

MOM(period: int = 1, *, src: pl.Expr = CLOSE)

Momentum

DPO

DPO(period: int = 20, *, src: pl.Expr = CLOSE)

Detrended Price Oscillator.

Computes (src.shift(displacement) - SMA(period, src=src)).shift(-displacement), where displacement = period // 2 + 1. This centers the oscillator on the displaced price bar to isolate price cycles. Values use later bars relative to their plotted date; the final displacement bars are missing because their moving-average windows are not yet available.

Arguments:

  • period (int): Moving-average window in bars. Must be positive. Defaults to 20.
  • src (Expr): Source expression. Defaults to the close price.

RSI

RSI(period: int = 14, *, src: pl.Expr = CLOSE)

Relative Strength Index

PPO

PPO(n1: int = 12, n2: int = 26, n3: int = 9, *, src: pl.Expr = CLOSE)

Price Percentage Oscillator — struct of (ppo, pposignal, ppohist)

MACD

MACD(n1: int = 12, n2: int = 26, n3: int = 9, *, src: pl.Expr = CLOSE)

Moving Average Convergence Divergence — struct of (macd, macdsignal, macdhist)

MACDV

MACDV(
    n1: int = 12,
    n2: int = 26,
    n3: int = 9,
    *,
    high: pl.Expr = HIGH,
    low: pl.Expr = LOW,
    close: pl.Expr = CLOSE,
)

MACD Volatility-Normalized — struct of (macd, macdsignal, macdhist)

BOP

BOP(
    period: int = 14,
    *,
    open: pl.Expr = OPEN,
    high: pl.Expr = HIGH,
    low: pl.Expr = LOW,
    close: pl.Expr = CLOSE,
)

Balance of Power

CMF

CMF(
    period: int = 20,
    *,
    high: pl.Expr = HIGH,
    low: pl.Expr = LOW,
    close: pl.Expr = CLOSE,
    volume: pl.Expr = VOLUME,
)

Chaikin Money Flow

MFI

MFI(
    period: int = 14,
    *,
    high: pl.Expr = HIGH,
    low: pl.Expr = LOW,
    close: pl.Expr = CLOSE,
    volume: pl.Expr = VOLUME,
)

Money Flow Index

STOCH

STOCH(
    period: int = 14,
    fastn: int = 3,
    slown: int = 3,
    *,
    high: pl.Expr = HIGH,
    low: pl.Expr = LOW,
    src: pl.Expr = CLOSE,
)

Stochastic Oscillator — struct of (slowk, slowd)

TRANGE

TRANGE(*, high: pl.Expr = HIGH, low: pl.Expr = LOW, close: pl.Expr = CLOSE)

True Range

ATR

ATR(
    period: int = 14,
    *,
    high: pl.Expr = HIGH,
    low: pl.Expr = LOW,
    close: pl.Expr = CLOSE,
)

Average True Range

BBP

BBP(period: int = 20, nbdev: float = 2.0, *, src: pl.Expr = CLOSE)

Bollinger Bands Percent (%B)

BBW

BBW(period: int = 20, nbdev: float = 2.0, *, src: pl.Expr = CLOSE)

Bollinger Bands Width

NATR

NATR(
    period: int = 14,
    *,
    high: pl.Expr = HIGH,
    low: pl.Expr = LOW,
    close: pl.Expr = CLOSE,
)

Normalized Average True Range (percent)

BBANDS

BBANDS(period: int = 20, nbdev: float = 2.0, *, src: pl.Expr = CLOSE)

Bollinger Bands — struct of (upperband, middleband, lowerband)

DONCHIAN

DONCHIAN(period: int = 20, *, high: pl.Expr = HIGH, low: pl.Expr = LOW)

Donchian Channel — struct of (upperband, middleband, lowerband)

KELTNER

KELTNER(
    period: int = 20,
    nbatr: float = 2.0,
    *,
    high: pl.Expr = HIGH,
    low: pl.Expr = LOW,
    close: pl.Expr = CLOSE,
)

Keltner Channel — struct of (upperband, middleband, lowerband)

DMI

DMI(
    period: int = 14,
    *,
    high: pl.Expr = HIGH,
    low: pl.Expr = LOW,
    close: pl.Expr = CLOSE,
)

Directional Movement Index — struct of (adx, pdi, ndi)

ADX

ADX(
    period: int = 14,
    *,
    high: pl.Expr = HIGH,
    low: pl.Expr = LOW,
    close: pl.Expr = CLOSE,
)

Average Directional Index

AVGPRICE

AVGPRICE(
    *,
    open: pl.Expr = OPEN,
    high: pl.Expr = HIGH,
    low: pl.Expr = LOW,
    close: pl.Expr = CLOSE,
)

Average Price (OHLC/4)

MEDPRICE

MEDPRICE(*, high: pl.Expr = HIGH, low: pl.Expr = LOW)

Median Price (HL/2)

TYPPRICE

TYPPRICE(*, high: pl.Expr = HIGH, low: pl.Expr = LOW, close: pl.Expr = CLOSE)

Typical Price (HLC/3)

WCLPRICE

WCLPRICE(*, high: pl.Expr = HIGH, low: pl.Expr = LOW, close: pl.Expr = CLOSE)

Weighted Close Price (HLCC/4)