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 the close column.
Price-based indicators use semantic keyword sources such as high=, low=,
close=, and volume=, defaulting to columns with those names.
Multi-output indicators like MACD and BBANDS return a polars struct expression
that can be unpacked with .unnest().
ABS
ABS(*, src: IntoExpr = 'close') -> polars.Expr
Absolute Value
ADX
ADX(period: int = 14, *, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> 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: IntoExpr = 'close') -> polars.Expr
Arnaud Legoux Moving Average
ATR
ATR(period: int = 14, *, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> polars.Expr
Average True Range
Arguments: - period (int): time period, default 14
AVGPRICE
AVGPRICE(*, open: IntoExpr = 'open', high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> polars.Expr
Average Price
Value of (open + high + low + close) / 4
BBANDS
BBANDS(period: int = 20, nbdev: float = 2.0, *, src: IntoExpr = 'close') -> 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: IntoExpr = 'close') -> polars.Expr
Bollinger Bands Percent (%B)
Returns the source position within the bands as a ratio, where 0 is the lower band and 1 is the upper band.
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: IntoExpr = 'close') -> polars.Expr
Bollinger Bands Width
Returns band width relative to the middle band as a ratio.
Arguments: - period (int): time period, default 20 - nbdev (float): bands width in number of standard deviations
BOP
BOP(*, open: IntoExpr = 'open', high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> polars.Expr
Balance of Power
CCI
CCI(period: int = 20, *, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> polars.Expr
Commodity Channel Index
Arguments: - period (int): time period, default 20
CLAG
CLAG(period: int = 1, *, src: IntoExpr = 'close') -> polars.Expr
Confirmation Lag
Changes value only after a confirmation period
Arguments: - period (int): time period, default 1
CMF
CMF(period: int = 20, *, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close', volume: IntoExpr = 'volume') -> polars.Expr
Chaikin Money Flow
Arguments: - period (int): time period, default 20
CROSSOVER
CROSSOVER(level: float = 0.0, *, src: IntoExpr = 'close') -> 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: IntoExpr = 'close') -> 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: IntoExpr = 'close') -> polars.Expr
Double Exponential Moving Average
Arguments: - period (int): time period, required
DIFF
DIFF(period: int = 1, *, src: IntoExpr = 'close') -> 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, *, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> polars.Expr
Directional Movement Indicator
Arguments: - period (int): time period, default 14
DONCHIAN
DONCHIAN(period: int = 20, *, high: IntoExpr = 'high', low: IntoExpr = 'low') -> polars.Expr
Donchian Channel
Arguments: - period (int): time period, default 20
EMA
EMA(period: int, *, adjust: bool = False, src: IntoExpr = 'close') -> 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: IntoExpr = 'close') -> polars.Expr
Exponential
ExprBundle
Named collection of expressions destined for one frame context.
FLAG
FLAG(*, src: IntoExpr = 'close') -> polars.Expr
Flag Value
Flag value of 1 for positive, 0 for zero or negative, and NaN otherwize
HMA
HMA(period: int, *, src: IntoExpr = 'close') -> polars.Expr
Hull Moving Average
Arguments: - period (int): time period, required
KAMA
KAMA(period: int = 10, fastn: int = 2, slown: int = 30, *, src: IntoExpr = 'close') -> 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, *, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> 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: IntoExpr = 'close') -> polars.Expr
Kaufman Efficiency Ratio
Arguments: - period (int): time period, default 10
LAG
LAG(period: int, *, src: IntoExpr = 'close') -> polars.Expr
Lag Function
Arguments: - period (int): time period, required
LINREG
LINREG(period: int = 20, offset: int = 0, *, src: IntoExpr = 'close') -> 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: IntoExpr = 'close') -> polars.Expr
Linear Regression Root Mean Square Error
Arguments: - period (int): time period, default 20
LINREG_RVALUE
LINREG_RVALUE(period: int = 20, *, src: IntoExpr = 'close') -> polars.Expr
Linear Regression R-Value
Arguments: - period (int): time period, default 20
LINREG_SLOPE
LINREG_SLOPE(period: int = 20, *, src: IntoExpr = 'close') -> polars.Expr
Linear Regression Slope
Arguments: - period (int): time period, default 20
LOG
LOG(*, src: IntoExpr = 'close') -> polars.Expr
Logarithm
LROC
LROC(period: int = 1, *, src: IntoExpr = 'close') -> polars.Expr
Logarithmic Rate of Change
Equivalent to the difference of log values
Arguments: - period (int): time period, default 1
MACD
MACD(n1: int = 12, n2: int = 26, n3: int = 9, *, src: IntoExpr = 'close') -> 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, *, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> 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: IntoExpr = 'close') -> polars.Expr
Rolling Mean Absolute Deviation
MAV
MAV(period: int = 20, *, matype: str = 'sma', src: IntoExpr = 'close') -> polars.Expr
Generic Moving Average
Moving average computed according to matype
Arguments: - matype (str): one of 'sma', 'ema', 'wma', 'hma', 'dema', 'tema' defaults to 'sma'
MAX
MAX(period: int, *, src: IntoExpr = 'close') -> polars.Expr
Rolling Maximum
MDI
MDI(period: int = 14, *, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> polars.Expr
Minus Directional Index
Arguments: - period (int): time period, default 14
MEDPRICE
MEDPRICE(*, high: IntoExpr = 'high', low: IntoExpr = 'low') -> polars.Expr
Median Price
Value of (high + low) / 2
MFI
MFI(period: int = 14, *, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close', volume: IntoExpr = 'volume') -> polars.Expr
Money Flow Index
Arguments: - period (int): time period, default 14
MIN
MIN(period: int, *, src: IntoExpr = 'close') -> polars.Expr
Rolling Minimum
Arguments: - period (int): time period, required
NATR
NATR(period: int = 14, *, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> polars.Expr
Normalized Average True Range
Returns 100 * ATR(period) / close in percentage points.
Arguments: - period (int): time period, default 14
OBV
OBV(*, close: IntoExpr = 'close', volume: IntoExpr = 'volume') -> polars.Expr
On-Balance Volume
Adds volume when close rises, subtracts volume when close falls, and carries the previous value when close is unchanged. The first value is initialized with the first volume.
PDI
PDI(period: int = 14, *, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> polars.Expr
Plus Directional Index
Arguments: - period (int): time period, default 14
PPO
PPO(n1: int = 12, n2: int = 26, n3: int = 9, *, src: IntoExpr = 'close') -> 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
QUADREG
QUADREG(period: int = 20, offset: int = 0, *, src: IntoExpr = 'close') -> 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: IntoExpr = 'close') -> polars.Expr
Quadratic Regression Curve
Arguments: - period (int): time period, default 20
QUADREG_RMSE
QUADREG_RMSE(period: int = 20, *, src: IntoExpr = 'close') -> polars.Expr
Quadratic Regression Root Mean Square Error
Arguments: - period (int): time period, default 20
QUADREG_RVALUE
QUADREG_RVALUE(period: int = 20, *, src: IntoExpr = 'close') -> 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: IntoExpr = 'close') -> 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: IntoExpr = 'close') -> polars.Expr
Rolling Moving Average (RSI style)
Exponential moving average with alpha = 1 / period,
that starts as a simple moving average until
number of bars is equal to period.
ROC
ROC(period: int = 1, *, src: IntoExpr = 'close') -> polars.Expr
Rate of Change
Returns rate of change as a percentage. For example, a 10% increase returns 10.0.
Arguments: - period (int): time period, default 1
ROCP
ROCP(period: int = 1, *, src: IntoExpr = 'close') -> polars.Expr
Rate of Change Percentage
Returns rate of change as a fraction. For example, a 10% increase returns 0.1.
Arguments: - period (int): time period, default 1
RSI
RSI(period: int = 14, *, src: IntoExpr = 'close') -> polars.Expr
Relative Strength Index
Arguments: - period (int): time period, default 14
SAR
SAR(afs: float = 0.02, maxaf: float = 0.2, *, high: IntoExpr = 'high', low: IntoExpr = 'low') -> 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: IntoExpr = 'close') -> polars.Expr
Sign
SMA
SMA(period: int, *, src: IntoExpr = 'close') -> polars.Expr
Simple Moving Average
Arguments: - period (int): time period, required
STDEV
STDEV(period: int = 20, *, src: IntoExpr = 'close') -> polars.Expr
Standard Deviation
Arguments: - period (int): time period, default 20
STEP
STEP(threshold: float = 1.0, *, src: IntoExpr = 'close') -> 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, *, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> 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: IntoExpr = 'close') -> polars.Expr
Consecutive streak of values above zero
SUM
SUM(period: int, *, src: IntoExpr = 'close') -> polars.Expr
Rolling sum
Arguments: - period (int): time period, required
TEMA
TEMA(period: int = 20, *, src: IntoExpr = 'close') -> polars.Expr
Triple Exponential Moving Average
Arguments: - period (int): time period, default 20
TRANGE
TRANGE(*, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> polars.Expr
True Range
TYPPRICE
TYPPRICE(*, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> polars.Expr
Typical Price
Value of (high + low + close ) / 3
UPDOWN
UPDOWN(up_level: float = 0.0, down_level: float = 0.0, *, src: IntoExpr = 'close') -> 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(*, high: IntoExpr = 'high', low: IntoExpr = 'low', close: IntoExpr = 'close') -> polars.Expr
Weighted Close Price
Value of (high + low + 2 * close) / 4
WMA
WMA(period: int, *, src: IntoExpr = 'close') -> polars.Expr
Weighted Moving Average
Arguments: - period (int): time period, required
ZLEMA
ZLEMA(period: int, *, src: IntoExpr = 'close') -> 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