augment_cmo

augment_cmo(data, date_column, close_column, periods=14, reduce_memory=False, engine='pandas')

The augment_cmo function calculates the Chande Momentum Oscillator (CMO) for a given financial instrument using either pandas or polars engine, and returns the augmented DataFrame.

Parameters

Name Type Description Default
data Union[pd.DataFrame, pd.core.groupby.generic.DataFrameGroupBy] The data parameter is the input data that can be either a pandas DataFrame or a pandas DataFrameGroupBy object. It contains the data on which the Chande Momentum Oscillator (CMO) will be calculated. required
date_column str The name of the column in the data that contains the dates or timestamps. required
close_column str The close_column parameter is used to specify the column in the input data that contain the values on which the CMO will be calculated. required
periods Union[int, Tuple[int, int], List[int]] The periods parameter in the augment_cmo function specifies the number of rolling periods over which the Chande Momentum Oscillator (CMO) is calculated. It can be provided as an integer, a tuple of two integers (start and end periods), or a list of integers. 14
reduce_memory bool The reduce_memory parameter is a boolean flag that indicates whether or not to reduce the memory usage of the data before performing the CMO calculation. If set to True, the function will attempt to reduce the memory usage of the input data. If set to False, the function will not attempt to reduce the memory usage of the input data. False
engine str The engine parameter specifies the computation engine to use for calculating the Chande Momentum Oscillator (CMO). It can take two values: โ€˜pandasโ€™ or โ€˜polarsโ€™. 'pandas'

Returns

Type Description
pd.DataFrame The function augment_cmo returns a pandas DataFrame that contains the augmented data with the Chande Momentum Oscillator (CMO) values added.

Notes

The Chande Momentum Oscillator (CMO), developed by Tushar Chande, is a technical analysis tool used to gauge the momentum of a financial instrument. It is similar to other momentum indicators like the Relative Strength Index (RSI), but with some distinct characteristics. Hereโ€™s what the CMO tells us:

Momentum of Price Movements:

The CMO measures the strength of trends in price movements. It calculates the difference between the sum of gains and losses over a specified period, normalized to oscillate between -100 and +100. Overbought and Oversold Conditions:

Values close to +100 suggest overbought conditions, indicating that the price might be too high and could reverse. Conversely, values near -100 suggest oversold conditions, implying that the price might be too low and could rebound. Trend Strength:

High absolute values (either positive or negative) indicate strong trends, while values near zero suggest a lack of trend or a weak trend. Divergences:

Divergences between the CMO and price movements can be significant. For example, if the price is making new highs but the CMO is declining, it may indicate weakening momentum and a potential trend reversal. Crossing the Zero Line:

When the CMO crosses above zero, it can be seen as a bullish signal, whereas a cross below zero can be interpreted as bearish. Customization:

The period over which the CMO is calculated can be adjusted. A shorter period makes the oscillator more sensitive to price changes, suitable for short-term trading. A longer period smooths out the oscillator for a longer-term perspective. Itโ€™s important to note that while the CMO can provide valuable insights into market momentum and potential price reversals, it is most effective when used in conjunction with other indicators and analysis methods. Like all technical indicators, the CMO should not be used in isolation but rather as part of a comprehensive trading strategy.

References: 1. https://www.fmlabs.com/reference/default.htm?url=CMO.htm

Examples

import pandas as pd
import pytimetk as tk

df = tk.load_dataset('stocks_daily', parse_dates=['date'])
df

# Example 1 - Calculate CMO for a single column
cmo_df = (
    df
        .query("symbol == 'AAPL'")
        .augment_cmo(
            date_column='date',
            close_column='adjusted',
            periods=[14, 28]
        )
)
cmo_df
symbol date open high low close volume adjusted adjusted_cmo_14 adjusted_cmo_28
5398 AAPL 2013-01-02 19.779285 19.821428 19.343929 19.608213 560518000 16.791180 NaN NaN
5399 AAPL 2013-01-03 19.567142 19.631071 19.321428 19.360714 352965200 16.579241 NaN NaN
5400 AAPL 2013-01-04 19.177500 19.236786 18.779642 18.821428 594333600 16.117437 NaN NaN
5401 AAPL 2013-01-07 18.642857 18.903570 18.400000 18.710714 484156400 16.022623 NaN NaN
5402 AAPL 2013-01-08 18.900356 18.996071 18.616072 18.761070 458707200 16.065746 NaN NaN
... ... ... ... ... ... ... ... ... ... ...
8092 AAPL 2023-09-15 176.479996 176.500000 173.820007 175.009995 109205100 175.009995 -11.097429 -6.370009
8093 AAPL 2023-09-18 176.479996 179.380005 176.169998 177.970001 67257600 177.970001 -6.564165 -2.713367
8094 AAPL 2023-09-19 177.520004 179.630005 177.130005 179.070007 51826900 179.070007 -16.295529 1.931561
8095 AAPL 2023-09-20 179.259995 179.699997 175.399994 175.490005 58436200 175.490005 -39.175190 -3.650570
8096 AAPL 2023-09-21 174.550003 176.300003 173.860001 173.929993 63047900 173.929993 -43.051227 -6.140628

2699 rows ร— 10 columns

# Example 2 - Calculate CMO for multiple groups
cmo_df = (
    df
        .groupby('symbol')
        .augment_cmo(
            date_column='date',
            close_column='adjusted',
            periods=[14, 28]
        )
)
cmo_df.groupby('symbol').tail(1)
symbol date open high low close volume adjusted adjusted_cmo_14 adjusted_cmo_28
2698 META 2023-09-21 295.700012 300.260010 293.269989 295.730011 21300500 295.730011 -0.277495 -4.703549
5397 AMZN 2023-09-21 131.940002 132.240005 129.309998 129.330002 70234800 129.330002 -27.450935 -16.697312
8096 AAPL 2023-09-21 174.550003 176.300003 173.860001 173.929993 63047900 173.929993 -43.051227 -6.140628
10795 NFLX 2023-09-21 386.500000 395.899994 383.420013 384.149994 5547900 384.149994 -56.124625 -19.430200
13494 NVDA 2023-09-21 415.829987 421.000000 409.799988 410.170013 44893000 410.170013 -83.624257 0.671283
16193 GOOG 2023-09-21 132.389999 133.190002 131.089996 131.360001 22042700 131.360001 -33.482367 2.917383
# Example 3 - Calculate CMO for polars engine
cmo_df = (
    df
        .query("symbol == 'AAPL'")
        .augment_cmo(
            date_column='date',
            close_column='adjusted',
            periods=[14, 28],
            engine='polars'
        )
)
cmo_df
symbol date open high low close volume adjusted adjusted_cmo_14 adjusted_cmo_28
0 AAPL 2013-01-02 19.779285 19.821428 19.343929 19.608213 560518000 16.791180 NaN NaN
1 AAPL 2013-01-03 19.567142 19.631071 19.321428 19.360714 352965200 16.579241 NaN NaN
2 AAPL 2013-01-04 19.177500 19.236786 18.779642 18.821428 594333600 16.117437 NaN NaN
3 AAPL 2013-01-07 18.642857 18.903570 18.400000 18.710714 484156400 16.022623 NaN NaN
4 AAPL 2013-01-08 18.900356 18.996071 18.616072 18.761070 458707200 16.065746 NaN NaN
... ... ... ... ... ... ... ... ... ... ...
2694 AAPL 2023-09-15 176.479996 176.500000 173.820007 175.009995 109205100 175.009995 -11.097429 -6.370009
2695 AAPL 2023-09-18 176.479996 179.380005 176.169998 177.970001 67257600 177.970001 -6.564165 -2.713367
2696 AAPL 2023-09-19 177.520004 179.630005 177.130005 179.070007 51826900 179.070007 -16.295529 1.931561
2697 AAPL 2023-09-20 179.259995 179.699997 175.399994 175.490005 58436200 175.490005 -39.175190 -3.650570
2698 AAPL 2023-09-21 174.550003 176.300003 173.860001 173.929993 63047900 173.929993 -43.051227 -6.140628

2699 rows ร— 10 columns

# Example 4 - Calculate CMO for polars engine and groups
cmo_df = (
    df
        .groupby('symbol')
        .augment_cmo(
            date_column='date',
            close_column='adjusted',
            periods=[14, 28],
            engine='polars'
        )
)
cmo_df.groupby('symbol').tail(1)
symbol date open high low close volume adjusted adjusted_cmo_14 adjusted_cmo_28
2698 META 2023-09-21 295.700012 300.260010 293.269989 295.730011 21300500 295.730011 -0.277495 -4.703549
5397 AMZN 2023-09-21 131.940002 132.240005 129.309998 129.330002 70234800 129.330002 -27.450935 -16.697312
8096 AAPL 2023-09-21 174.550003 176.300003 173.860001 173.929993 63047900 173.929993 -43.051227 -6.140628
10795 NFLX 2023-09-21 386.500000 395.899994 383.420013 384.149994 5547900 384.149994 -56.124625 -19.430200
13494 NVDA 2023-09-21 415.829987 421.000000 409.799988 410.170013 44893000 410.170013 -83.624257 0.671283
16193 GOOG 2023-09-21 132.389999 133.190002 131.089996 131.360001 22042700 131.360001 -33.482367 2.917383