Skip to content

mintalib.expressions

Polars Expression Factory Methods

Functions in this module are polars expression factories, typically named after the indicator in upper case as in SMA, EMA, MACD.

This module is polars-only: factories build native polars expressions for use in select or with_columns contexts. For pandas, use mintalib.indicators or mintalib.functions.

The optional src keyword parameter allows overriding the default input column. For series-based indicators the default is CLOSE (i.e. pl.col("close")). For price-based indicators src is not applicable and should be left as None.

Multi-output indicators like MACD and BBANDS return a polars struct expression that can be unpacked with .unnest().


IntoExpr

IntoExpr: TypeAlias

Type alias for polars expressions accepted as inputs (pl.Expr, column name, or None).

CLOSE

Expression for the close price column.

OHLC

Expression for open, high, low, close columns as a struct.

ABS

ABS(*, src: polars.Expr | str | None = None) -> polars.Expr

Absolute Value

ADX

ADX(period: int = 14, *, src: polars.Expr | str | None = None) -> polars.Expr

Average Directional Index

Arguments: - period (int): time period, default 14

ALMA

ALMA(period: int = 9, offset: float = 0.85, sigma: float = 6.0, *, src: polars.Expr | str | None = None) -> polars.Expr

Arnaud Legoux Moving Average

ATR

ATR(period: int = 14, *, src: polars.Expr | str | None = None) -> polars.Expr

Average True Range

Arguments: - period (int): time period, default 14

AVGPRICE

AVGPRICE(*, src: polars.Expr | str | None = None) -> polars.Expr

Average Price

Value of (open + high + low + close) / 4

BBANDS

BBANDS(period: int = 20, nbdev: float = 2.0, *, src: polars.Expr | str | None = None) -> polars.Expr

Bollinger Bands

Arguments: - period (int): time period, default 20 - nbdev (float): bands width in number of standard deviations

BBP

BBP(period: int = 20, nbdev: float = 2.0, *, src: polars.Expr | str | None = None) -> polars.Expr

Bollinger Bands Percent (%B)

Arguments: - period (int): time period, default 20 - nbdev (float): bands width in number of standard deviations

BBW

BBW(period: int = 20, nbdev: float = 2.0, *, src: polars.Expr | str | None = None) -> polars.Expr

Bollinger Bands Width

Arguments: - period (int): time period, default 20 - nbdev (float): bands width in number of standard deviations

BOP

BOP(period: int = 20, *, src: polars.Expr | str | None = None) -> polars.Expr

Balance of Power

Arguments: - period (int): time period, default 20

CCI

CCI(period: int = 20, *, src: polars.Expr | str | None = None) -> polars.Expr

Commodity Channel Index

Arguments: - period (int): time period, default 20

CLAG

CLAG(period: int = 1, *, src: polars.Expr | str | None = None) -> polars.Expr

Confirmation Lag

Changes value only after a confirmation period

Arguments: - period (int): time period, default 1

CMF

CMF(period: int = 20, *, src: polars.Expr | str | None = None) -> polars.Expr

Chaikin Money Flow

Arguments: - period (int): time period, default 20

CROSSOVER

CROSSOVER(level: float = 0.0, *, src: polars.Expr | str | None = None) -> polars.Expr

Cross Over

Yields a value of 1 at the point where series crosses over level

Arguments: - level (float): level to cross, default 0.0

CROSSUNDER

CROSSUNDER(level: float = 0.0, *, src: polars.Expr | str | None = None) -> polars.Expr

Cross Under

Yields a value of 1 at the point where series crosses under level

Arguments: - level (float): level to cross, default 0.0

DEMA

DEMA(period: int, *, src: polars.Expr | str | None = None) -> polars.Expr

Double Exponential Moving Average

Arguments: - period (int): time period, required

DIFF

DIFF(period: int = 1, *, src: polars.Expr | str | None = None) -> polars.Expr

Difference

Difference between current value and the one offset by period

Arguments: - period (int): time period, default 1

DMI

DMI(period: int = 14, *, src: polars.Expr | str | None = None) -> polars.Expr

Directional Movement Indicator

Arguments: - period (int): time period, default 14

DONCHIAN

DONCHIAN(period: int = 20, *, src: polars.Expr | str | None = None) -> polars.Expr

Donchian Channel

Arguments: - period (int): time period, default 20

EMA

EMA(period: int, *, adjust: bool = False, src: polars.Expr | str | None = None) -> polars.Expr

Exponential Moving Average

Arguments: - period (int): time period, required - adjust (bool): whether to adjust weights, default False when true update ratio increases gradually (see formula)

Formula:

EMA is calculated as a recursive formula The standard formula is ema += alpha * (value - ema) with alpha = 2.0 / (period + 1.0) The adjusted formula is ema = num/div where num = value + rho * num, div = 1.0 + rho * div with rho = 1.0 - alpha

EXP

EXP(*, src: polars.Expr | str | None = None) -> polars.Expr

Exponential

FLAG

FLAG(*, src: polars.Expr | str | None = None) -> polars.Expr

Flag Value

Flag value of 1 for positive, 0 for zero or negative, and NaN otherwize

HMA

HMA(period: int, *, src: polars.Expr | str | None = None) -> polars.Expr

Hull Moving Average

Arguments: - period (int): time period, required

KAMA

KAMA(period: int = 10, fastn: int = 2, slown: int = 30, *, src: polars.Expr | str | None = None) -> polars.Expr

Kaufman Adaptive Moving Average

Arguments: - period (int): time period for efficiency ratio, default 10 - fastn (int): time period for fast moving average, default, 2 - slown (int): time period for slow moving average, default 30

KELTNER

KELTNER(period: int = 20, nbatr: float = 2.0, *, src: polars.Expr | str | None = None) -> polars.Expr

Keltner Channel

Arguments: - period (int): time period, default 20 - nbatr (float): channel width in number of atrs, default 2.0

KER

KER(period: int = 10, *, src: polars.Expr | str | None = None) -> polars.Expr

Kaufman Efficiency Ratio

Arguments: - period (int): time period, default 10

LAG

LAG(period: int, *, src: polars.Expr | str | None = None) -> polars.Expr

Lag Function

Arguments: - period (int): time period, required

LINREG

LINREG(period: int = 20, offset: int = 0, *, src: polars.Expr | str | None = None) -> polars.Expr

Linear Regression (least squares moving average)

Value of the regression line at the current bar, with offset projecting the line forward.

Arguments: - period (int): time period, default 20 - offset (int): forecast offset, default 0

LINREG_RMSE

LINREG_RMSE(period: int = 20, *, src: polars.Expr | str | None = None) -> polars.Expr

Linear Regression Root Mean Square Error

Arguments: - period (int): time period, default 20

LINREG_RVALUE

LINREG_RVALUE(period: int = 20, *, src: polars.Expr | str | None = None) -> polars.Expr

Linear Regression R-Value

Arguments: - period (int): time period, default 20

LINREG_SLOPE

LINREG_SLOPE(period: int = 20, *, src: polars.Expr | str | None = None) -> polars.Expr

Linear Regression Slope

Arguments: - period (int): time period, default 20

LOG

LOG(*, src: polars.Expr | str | None = None) -> polars.Expr

Logarithm

LROC

LROC(period: int = 1, *, src: polars.Expr | str | None = None) -> polars.Expr

Logarithmic Rate of Change

Equivalent to the difference of log values

Arguments: - period (int): time period, default 1 when negative the calculation is shifted back

MACD

MACD(n1: int = 12, n2: int = 26, n3: int = 9, *, src: polars.Expr | str | None = None) -> polars.Expr

Moving Average Convergence Divergence

Arguments: - n1 (int): short time period, default 12 - n2 (int): long time period, default 26 - n3 (int): signal time period, default 9

Outputs:

macd, macdsignal, macdhist

MACDV

MACDV(n1: int = 12, n2: int = 26, n3: int = 9, *, src: polars.Expr | str | None = None) -> polars.Expr

Moving Average Convergence Divergence - Volatility Normalized

Arguments: - n1 (int): short time period, default 12 - n2 (int): long time period, default 26 - n3 (int): signal time period, default 9

Outputs:

macdv, macdvsignal, macdvhist

MAD

MAD(period: int = 14, *, src: polars.Expr | str | None = None) -> polars.Expr

Rolling Mean Absolute Deviation

MAV

MAV(period: int = 20, *, ma_type: str = 'SMA', src: polars.Expr | str | None = None) -> polars.Expr

Generic Moving Average

Moving average computed according to ma_type

Arguments: - ma_type (str): one of 'SMA', 'EMA', 'WMA', 'HMA', 'DEMA', 'TEMA' defaults to 'SMA'

MAX

MAX(period: int, *, src: polars.Expr | str | None = None) -> polars.Expr

Rolling Maximum

MDI

MDI(period: int = 14, *, src: polars.Expr | str | None = None) -> polars.Expr

Minus Directional Index

Arguments: - period (int): time period, default 14

MEDPRICE

MEDPRICE(*, src: polars.Expr | str | None = None) -> polars.Expr

Median Price

Value of (high + low) / 2

MFI

MFI(period: int = 14, *, src: polars.Expr | str | None = None) -> polars.Expr

Money Flow Index

Arguments: - period (int): time period, default 14

MIN

MIN(period: int, *, src: polars.Expr | str | None = None) -> polars.Expr

Rolling Minimum

Arguments: - period (int): time period, required

NATR

NATR(period: int = 14, *, src: polars.Expr | str | None = None) -> polars.Expr

Normalized Average True Range

Arguments: - period (int): time period, default 14

PDI

PDI(period: int = 14, *, src: polars.Expr | str | None = None) -> polars.Expr

Plus Directional Index

Arguments: - period (int): time period, default 14

PPO

PPO(n1: int = 12, n2: int = 26, n3: int = 9, *, src: polars.Expr | str | None = None) -> polars.Expr

Price Percentage Oscillator

Arguments: - n1 (int): short time period, default 12 - n2 (int): long time period, default 26 - n3 (int): signal time period, default 9

Outputs:

ppo, pposignal, ppohist

PRICE

PRICE(item: str | None = None, *, src: polars.Expr | str | None = None) -> polars.Expr

Generic Price

Arguments: - item (str): price type, one of: 'open', 'high', 'low', 'close' (default), 'avg' or 'ohlc4' — average price (open + high + low + close) / 4, 'med' or 'hl2' — median price (high + low) / 2, 'typ' or 'hlc3' — typical price (high + low + close) / 3, 'wcl' or 'hlcc4' — weighted close (high + low + 2 * close) / 4

QUADREG

QUADREG(period: int = 20, offset: int = 0, *, src: polars.Expr | str | None = None) -> polars.Expr

Quadratic Regression (parabolic moving average)

Value of the regression parabola at the current bar, with offset projecting the parabola forward.

Arguments: - period (int): time period, default 20 - offset (int): forecast offset, default 0

QUADREG_CURVE

QUADREG_CURVE(period: int = 20, *, src: polars.Expr | str | None = None) -> polars.Expr

Quadratic Regression Curve

Arguments: - period (int): time period, default 20

QUADREG_RMSE

QUADREG_RMSE(period: int = 20, *, src: polars.Expr | str | None = None) -> polars.Expr

Quadratic Regression Root Mean Square Error

Arguments: - period (int): time period, default 20

QUADREG_RVALUE

QUADREG_RVALUE(period: int = 20, *, src: polars.Expr | str | None = None) -> polars.Expr

Quadratic Regression R-Value

Partial correlation of the quadratic term, given the linear term.

Arguments: - period (int): time period, default 20

QUADREG_SLOPE

QUADREG_SLOPE(period: int = 20, offset: int = 0, *, src: polars.Expr | str | None = None) -> polars.Expr

Quadratic Regression Slope

Slope of the regression parabola at the current bar, with offset projecting the slope forward.

Arguments: - period (int): time period, default 20 - offset (int): forecast offset, default 0

RMA

RMA(period: int, *, src: polars.Expr | str | None = None) -> polars.Expr

Rolling Moving Average (RSI style)

Exponential moving average with alpha = 2 / period, that starts as a simple moving average until number of bars is equal to period.

ROC

ROC(period: int = 1, *, src: polars.Expr | str | None = None) -> polars.Expr

Rate of Change

Arguments: - period (int): time period, default 1 when negative the calculation is shifted back

RSI

RSI(period: int = 14, *, src: polars.Expr | str | None = None) -> polars.Expr

Relative Strength Index

Arguments: - period (int): time period, default 14

SAR

SAR(afs: float = 0.02, maxaf: float = 0.2, *, src: polars.Expr | str | None = None) -> polars.Expr

Parabolic Stop and Reverse

Arguments: - afs (float): starting acceleration factor, default 0.02 - maxaf (float): maximum acceleration factor, default 0.2

SIGN

SIGN(*, src: polars.Expr | str | None = None) -> polars.Expr

Sign

SMA

SMA(period: int, *, src: polars.Expr | str | None = None) -> polars.Expr

Simple Moving Average

Arguments: - period (int): time period, required

STDEV

STDEV(period: int = 20, *, src: polars.Expr | str | None = None) -> polars.Expr

Standard Deviation

Arguments: - period (int): time period, default 20

STEP

STEP(threshold: float = 1.0, *, src: polars.Expr | str | None = None) -> polars.Expr

Step Function

Limit value changes to threshold (in absolute value)

Arguments: - threshold (float): threshold value, default 1.0

STOCH

STOCH(period: int = 14, fastn: int = 3, slown: int = 3, *, src: polars.Expr | str | None = None) -> polars.Expr

Stochastic Oscillator

Arguments: - period (int): time period of window, default, 14 - fastn (int): time period of fast average, default 3 - slown (int): time period of slow average, default 3

STREAK

STREAK(*, src: polars.Expr | str | None = None) -> polars.Expr

Consecutive streak of values above zero

SUM

SUM(period: int, *, src: polars.Expr | str | None = None) -> polars.Expr

Rolling sum

Arguments: - period (int): time period, required

TEMA

TEMA(period: int = 20, *, src: polars.Expr | str | None = None) -> polars.Expr

Triple Exponential Moving Average

Arguments: - period (int): time period, default 20

TRANGE

TRANGE(*, log_prices: bool = False, percent: bool = False, src: polars.Expr | str | None = None) -> polars.Expr

True Range

Arguments: - log_prices (bool): whether to apply log to prices before calculation - percent (bool): result as percentage of price

TYPPRICE

TYPPRICE(*, src: polars.Expr | str | None = None) -> polars.Expr

Typical Price

Value of (high + low + close ) / 3

UPDOWN

UPDOWN(up_level: float = 0.0, down_level: float = 0.0, *, src: polars.Expr | str | None = None) -> polars.Expr

Flag for value crossing up & down levels

Arguments: - up_level (float): flag set at 1 above that level - down_level (float): flag set at 0 below that level

WCLPRICE

WCLPRICE(*, src: polars.Expr | str | None = None) -> polars.Expr

Weighted Close Price

Value of (high + low + 2 * close) / 4

WMA

WMA(period: int, *, src: polars.Expr | str | None = None) -> polars.Expr

Weighted Moving Average

Arguments: - period (int): time period, required

ZLEMA

ZLEMA(period: int, *, src: polars.Expr | str | None = None) -> polars.Expr

Zero-Lag Exponential Moving Average

Arguments: - period (int): time period, required

Formula:

ZLEMA is an EMA applied to a de-lagged series data = 2 * value - value[lag] with lag = (period - 1) // 2