augment_qsmomentum

augment_qsmomentum(data, date_column, close_column, roc_fast_period=21, roc_slow_period=252, returns_period=126, reduce_memory=False, engine='pandas')

The function augment_qsmomentum calculates Quant Science Momentum for financial data.

Parameters

Name Type Description Default
data Union[pd.DataFrame, pd.core.groupby.generic.DataFrameGroupBy] The data parameter in the augment_qsmomentum function is expected to be a pandas DataFrame or a pandas DataFrameGroupBy object. This parameter represents the input data on which the momentum calculations will be performed. required
date_column str The date_column parameter in the augment_qsmomentum function refers to the column in your input data that contains the dates associated with the financial data. This column is used for time-based operations and calculations within the function. required
close_column str The close_column parameter in the augment_qsmomentum function refers to the column in the input DataFrame that contains the closing prices of the financial instrument or asset for which you want to calculate the momentum. required
roc_fast_period Union[int, Tuple[int, int], List[int]] The roc_fast_period parameter in the augment_qsmomentum function determines the period used for calculating the fast Rate of Change (ROC) momentum indicator. 21
roc_slow_period Union[int, Tuple[int, int], List[int]] The roc_slow_period parameter in the augment_qsmomentum function represents the period used for calculating the slow rate of change (ROC) in momentum analysis. 252
returns_period Union[int, Tuple[int, int], List[int]] The returns_period parameter in the augment_qsmomentum function determines the period over which the returns are calculated. 126
reduce_memory bool The reduce_memory parameter in the augment_qsmomentum function is a boolean flag that indicates whether memory reduction techniques should be applied to the input data before and after the momentum calculation process. If set to True, memory reduction methods will be used to optimize memory usage, potentially reducing False
engine str The engine parameter in the augment_qsmomentum function specifies the computation engine to be sed for calculating momentum. It can have two possible values: β€œpandas” or β€œpolars”. 'pandas'

Returns

Type Description
The function augment_qsmomentum returns a pandas DataFrame that has been augmented with columns representing the Quant Science Momentum (QSM) calculated based on the specified parameters such as roc_fast_period, roc_slow_period, and returns_period.

Notes

The Quant Science Momentum (QSM) is a momentum indicator that is calculated based on the Slow Rate of Change (ROC) usually over a 252-day period and the Fast Rate of Change (ROC) usually over a 21-day period.

The QSM is calculated as the difference between the slow and fast ROCs divided by the standard deviation of the returns over a specified period.

This provides a measure of momentum that is normalized by the rolling volatility of the returns.

Examples

import pandas as pd
import polars as pl
import pytimetk as tk

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

df.glimpse()
<class 'pandas.core.frame.DataFrame'>: 16194 rows of 8 columns
symbol:    object            ['META', 'META', 'META', 'META', 'META', 'M ...
date:      datetime64[ns]    [Timestamp('2013-01-02 00:00:00'), Timestam ...
open:      float64           [27.440000534057617, 27.8799991607666, 28.0 ...
high:      float64           [28.18000030517578, 28.46999931335449, 28.9 ...
low:       float64           [27.420000076293945, 27.59000015258789, 27. ...
close:     float64           [28.0, 27.770000457763672, 28.7600002288818 ...
volume:    int64             [69846400, 63140600, 72715400, 83781800, 45 ...
adjusted:  float64           [28.0, 27.770000457763672, 28.7600002288818 ...
# PANDAS QS MOMENTUM CALCULATION
df_qsmom = (
    df 
        .query('symbol == "GOOG"') 
        .augment_qsmomentum(
            date_column = 'date', 
            close_column = 'close', 
            roc_fast_period = [1, 5, 21], 
            roc_slow_period = 252, 
            returns_period = 126, 
            engine = "pandas"
        ) 
)

df_qsmom.dropna().glimpse()
<class 'pandas.core.frame.DataFrame'>: 2448 rows of 11 columns
symbol:                  object            ['GOOG', 'GOOG', 'GOOG', 'GOO ...
date:                    datetime64[ns]    [Timestamp('2013-12-31 00:00: ...
open:                    float64           [27.70216560363769, 27.782365 ...
high:                    float64           [27.92034721374512, 27.839401 ...
low:                     float64           [27.553224563598636, 27.60303 ...
close:                   float64           [27.913124084472656, 27.72408 ...
volume:                  int64             [54519590, 73129082, 66917888 ...
adjusted:                float64           [27.913124084472656, 27.72408 ...
close_qsmom_1_252_126:   float64           [36.513639518933225, 35.71389 ...
close_qsmom_5_252_126:   float64           [35.16871574164793, 36.369633 ...
close_qsmom_21_252_126:  float64           [26.25608616038381, 26.452917 ...
# POLARS QS MOMENTUM CALCULATION
df_qsmom = (
    df 
        .query('symbol == "GOOG"') 
        .augment_qsmomentum(
            date_column = 'date', 
            close_column = 'close', 
            roc_fast_period = [1, 5, 21], 
            roc_slow_period = 252, 
            returns_period = 126, 
            engine = "polars"
        ) 
)

df_qsmom.dropna().glimpse()
<class 'pandas.core.frame.DataFrame'>: 2448 rows of 11 columns
symbol:                  object            ['GOOG', 'GOOG', 'GOOG', 'GOO ...
date:                    datetime64[ns]    [Timestamp('2013-12-31 00:00: ...
open:                    float64           [27.70216560363769, 27.782365 ...
high:                    float64           [27.92034721374512, 27.839401 ...
low:                     float64           [27.553224563598636, 27.60303 ...
close:                   float64           [27.913124084472656, 27.72408 ...
volume:                  int64             [54519590, 73129082, 66917888 ...
adjusted:                float64           [27.913124084472656, 27.72408 ...
close_qsmom_1_252_126:   float64           [36.36845548662661, 35.571893 ...
close_qsmom_5_252_126:   float64           [35.02887933997205, 36.225021 ...
close_qsmom_21_252_126:  float64           [26.15168779003297, 26.347736 ...