import pytimetk as tk
import polars as pl
= tk.load_dataset("stocks_daily", parse_dates=["date"])
df
# Pandas example (engine inferred)
= (
cmo_pd "symbol")
df.groupby(
.augment_cmo(="date",
date_column="close",
close_column=[14, 28],
periods
)
)
# Polars example using the tk accessor
= (
cmo_pl "symbol == 'AAPL'"))
pl.from_pandas(df.query(
.tk.augment_cmo(="date",
date_column="close",
close_column=14,
periods
) )
augment_cmo
augment_cmo(
data,
date_column,
close_column,=14,
periods=False,
reduce_memory='auto',
engine )
Calculate the Chande Momentum Oscillator (CMO) using pandas or polars backends.
Parameters
Name | Type | Description | Default |
---|---|---|---|
data | DataFrame or GroupBy(pandas or polars) | Input financial data. Grouped inputs are processed per group before the indicator columns are appended. | required |
date_column | str | Name of the column containing date information. | required |
close_column | str | Name of the column containing closing prices. | required |
periods | int, tuple, or list | Lookback window(s) applied to the CMO calculation. Accepts a single integer, an inclusive tuple range, or an explicit list. Defaults to 14 . |
14 |
reduce_memory | bool | Attempt to reduce memory usage when operating on pandas data. If a polars input is supplied a warning is emitted and no conversion occurs. | False |
engine | (auto, pandas, polars) | Execution engine. "auto" (default) infers the backend from the input data while allowing explicit overrides. |
"auto" |
Returns
Name | Type | Description |
---|---|---|
DataFrame | DataFrame with {close_column}_cmo_{period} columns appended for every requested period. The return type matches the input backend. |
Notes
The Chande Momentum Oscillator (CMO) compares the magnitude of recent gains to recent losses over the supplied lookback window. Values range from -100 (all losses) to +100 (all gains). Division-by-zero cases are guarded by returning NaN
which matches the pandas behaviour.