augment_fip_momentum

augment_fip_momentum(
    data,
    date_column,
    close_column,
    window=252,
    reduce_memory=False,
    engine='pandas',
)

Calculate the โ€œFrog In The Panโ€ (FIP) momentum metric over one or more rolling windows using either pandas or polars engine, augmenting the DataFrame with FIP columns.

The FIP momentum is defined as: FIP = Total Return * (percent of negative returns - percent of positive returns)

Parameters

Name Type Description Default
data Union[pd.DataFrame, pd.core.groupby.generic.DataFrameGroupBy] Input pandas DataFrame or grouped DataFrame containing time series data. required
date_column str Name of the column with dates or timestamps. required
close_column str Name of the column with closing prices to calculate returns. required
window Union[int, List[int]] Size of the rolling window(s) as an integer or list of integers (default is 252). 252
reduce_memory bool If True, reduces memory usage of the DataFrame. Default is False. False
engine str Computation engine: โ€˜pandasโ€™ or โ€˜polarsโ€™. Default is โ€˜pandasโ€™. 'pandas'

Returns

Name Type Description
pd.DataFrame DataFrame augmented with FIP momentum columns: - {close_column}fip_momentum{w}: Rolling FIP momentum for each window w

Examples

import pandas as pd
import pytimetk as tk

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

# Single window
fip_df = (
    df.query("symbol == 'AAPL'")
    .augment_fip_momentum(
        date_column='date',
        close_column='close',
        window=252
    )
)
fip_df.tail()
symbol date open high low close volume adjusted close_fip_momentum_252
8092 AAPL 2023-09-15 176.479996 176.500000 173.820007 175.009995 109205100 175.009995 -0.005537
8093 AAPL 2023-09-18 176.479996 179.380005 176.169998 177.970001 67257600 177.970001 -0.008667
8094 AAPL 2023-09-19 177.520004 179.630005 177.130005 179.070007 51826900 179.070007 -0.011206
8095 AAPL 2023-09-20 179.259995 179.699997 175.399994 175.490005 58436200 175.490005 -0.007016
8096 AAPL 2023-09-21 174.550003 176.300003 173.860001 173.929993 63047900 173.929993 -0.004738
# Multiple windows
fip_df = (
    df.groupby('symbol')
    .augment_fip_momentum(
        date_column='date',
        close_column='close',
        window=[63, 252],
        engine='polars'
    )
)
fip_df.tail()
symbol date open high low close volume adjusted close_fip_momentum_63 close_fip_momentum_252
16189 GOOG 2023-09-15 138.800003 139.360001 137.179993 138.300003 48947600 138.300003 -0.004736 0.000000
16190 GOOG 2023-09-18 137.630005 139.929993 137.630005 138.960007 16233600 138.960007 -0.009532 -0.002678
16191 GOOG 2023-09-19 138.250000 139.175003 137.500000 138.830002 15479100 138.830002 -0.009599 -0.002696
16192 GOOG 2023-09-20 138.830002 138.839996 134.520004 134.589996 21473500 134.589996 -0.008725 0.000000
16193 GOOG 2023-09-21 132.389999 133.190002 131.089996 131.360001 22042700 131.360001 -0.002879 0.000000