augment_ppo

augment_ppo(data, date_column, close_column, fast_period=12, slow_period=26, reduce_memory=False, engine='pandas')

Calculate PPO for a given financial instrument using either pandas or polars engine.

Parameters

Name Type Description Default
data Union[pd.DataFrame, pd.core.groupby.generic.DataFrameGroupBy] Pandas DataFrame or GroupBy object containing financial data. required
date_column str Name of the column containing date information. required
close_column str Name of the column containing closing price data. required
fast_period int Number of periods for the fast EMA in PPO calculation. 12
slow_period int Number of periods for the slow EMA in PPO calculation. 26
reduce_memory bool Whether to reduce memory usage of the data before performing the calculation. False
engine str Computation engine to use (β€˜pandas’ or β€˜polars’). 'pandas'

Returns

Type Description
pd.DataFrame DataFrame with PPO values added.

Notes

The Percentage Price Oscillator (PPO) is a momentum oscillator that measures the difference between two moving averages as a percentage of the larger moving average. The PPO is best used to confirm the direction of the price trend and gauge its momentum.

The PPO is calculated by subtracting a long-term EMA from a short-term EMA, then dividing the result by the long-term EMA, and finally multiplying by 100.

Advantages Over MACD: The PPO’s percentage-based calculation allows for easier comparisons between different securities, regardless of their price levels. This is a distinct advantage over the MACD, which provides absolute values and can be less meaningful when comparing stocks with significantly different prices.

Examples

import pandas as pd
import pytimetk as tk

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

df
symbol date open high low close volume adjusted
0 META 2013-01-02 27.440001 28.180000 27.420000 28.000000 69846400 28.000000
1 META 2013-01-03 27.879999 28.469999 27.590000 27.770000 63140600 27.770000
2 META 2013-01-04 28.010000 28.930000 27.830000 28.760000 72715400 28.760000
3 META 2013-01-07 28.690001 29.790001 28.650000 29.420000 83781800 29.420000
4 META 2013-01-08 29.510000 29.600000 28.860001 29.059999 45871300 29.059999
... ... ... ... ... ... ... ... ...
16189 GOOG 2023-09-15 138.800003 139.360001 137.179993 138.300003 48947600 138.300003
16190 GOOG 2023-09-18 137.630005 139.929993 137.630005 138.960007 16233600 138.960007
16191 GOOG 2023-09-19 138.250000 139.175003 137.500000 138.830002 15479100 138.830002
16192 GOOG 2023-09-20 138.830002 138.839996 134.520004 134.589996 21473500 134.589996
16193 GOOG 2023-09-21 132.389999 133.190002 131.089996 131.360001 22042700 131.360001

16194 rows Γ— 8 columns

# PPO pandas engine
df_ppo = (
    df
        .groupby('symbol')
        .augment_ppo(
            date_column = 'date', 
            close_column = 'close', 
            fast_period = 12, 
            slow_period = 26, 
            engine = "pandas"
        )
)

df_ppo.glimpse()
<class 'pandas.core.frame.DataFrame'>: 16194 rows of 9 columns
symbol:                object            ['META', 'META', 'META', 'META' ...
date:                  datetime64[ns]    [Timestamp('2013-01-02 00:00:00 ...
open:                  float64           [27.440000534057617, 27.8799991 ...
high:                  float64           [28.18000030517578, 28.46999931 ...
low:                   float64           [27.420000076293945, 27.5900001 ...
close:                 float64           [28.0, 27.770000457763672, 28.7 ...
volume:                int64             [69846400, 63140600, 72715400,  ...
adjusted:              float64           [28.0, 27.770000457763672, 28.7 ...
close_ppo_line_12_26:  float64           [0.0, -0.06556683019189882, 0.1 ...
# PPO polars engine
df_ppo = (
    df
        .groupby('symbol')
        .augment_ppo(
            date_column = 'date', 
            close_column = 'close', 
            fast_period = 12, 
            slow_period = 26, 
            engine = "polars"
        )
)

df_ppo.glimpse()
<class 'pandas.core.frame.DataFrame'>: 16194 rows of 9 columns
symbol:                object            ['META', 'META', 'META', 'META' ...
date:                  datetime64[ns]    [Timestamp('2013-01-02 00:00:00 ...
open:                  float64           [27.440000534057617, 27.8799991 ...
high:                  float64           [28.18000030517578, 28.46999931 ...
low:                   float64           [27.420000076293945, 27.5900001 ...
close:                 float64           [28.0, 27.770000457763672, 28.7 ...
volume:                int64             [69846400, 63140600, 72715400,  ...
adjusted:              float64           [28.0, 27.770000457763672, 28.7 ...
close_ppo_line_12_26:  float64           [0.0, -0.06556683019189882, 0.1 ...