Utilities¶
Shared building blocks for composing and cleaning — the tools you use around the indicators rather than the indicators themselves.
BaseIndicator— the reusable primitives every indicator is built from (sma,ema,true_range,check_fillna,get_min_max). Use these when you build a custom indicator so it inherits the same warm-up andfillnabehaviour as the built-ins.DataCleaner— detect and repair theNaN/inf/null/absurdly-large values that real market data is full of (dropna,get_invalid_indices,approximate_invalid_values) before they cascade through a rolling window and null out a whole column.
Clean first, compute second
A single bad tick inside a rolling window can poison every value that window
touches. Run DataCleaner on raw feeds before computing indicators — see
the cleaning how-to.
polars_ta.utils
¶
BaseIndicator
¶
Utility functions for the Polars TA library.
check_fillna
staticmethod
¶
check_fillna(expr: Expr | str, fillna: bool, value: int = 0) -> Expr
Check if fillna flag is True and fill gaps. Replaces inf/-inf with nulls before filling.
Source code in polars_ta/utils.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
true_range
staticmethod
¶
true_range(high: Expr | str, low: Expr | str, prev_close: Expr | str) -> Expr
Calculate the True Range using horizontal aggregation.
Source code in polars_ta/utils.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
sma
staticmethod
¶
sma(expr: Expr | str, periods: int, fillna: bool = False) -> Expr
Simple Moving Average
Source code in polars_ta/utils.py
50 51 52 53 54 55 | |
ema
staticmethod
¶
ema(expr: Expr | str, periods: int, fillna: bool = False) -> Expr
Exponential Moving Average
Source code in polars_ta/utils.py
57 58 59 60 61 62 | |
get_min_max
staticmethod
¶
get_min_max(expr1: Expr | str, expr2: Expr | str, function: str = 'min') -> Expr
Find min or max value between two series for each index.
Source code in polars_ta/utils.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
DataCleaner
¶
Methods to handle, track, and heal invalid data before applying TA indicators.
dropna
staticmethod
¶
dropna(df: DataFrame) -> DataFrame
Drop rows with nulls, NaNs, or excessively large numbers in numeric columns (safe alternative to the original ta library).
Source code in polars_ta/utils.py
104 105 106 107 108 109 110 111 112 | |
get_invalid_indices
staticmethod
¶
get_invalid_indices(df: DataFrame) -> list[int]
Returns a list of integer row indices where invalid data exists. Useful for logging or inspecting anomalies.
Source code in polars_ta/utils.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
approximate_invalid_values
staticmethod
¶
approximate_invalid_values(df: DataFrame) -> DataFrame
Replaces invalid values with Polars Nulls, then approximates them using linear interpolation and forward-filling based on past values.
Source code in polars_ta/utils.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |