augment_macd

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

Calculate MACD 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 MACD calculation. 12
slow_period int Number of periods for the slow EMA in MACD calculation. 26
signal_period int Number of periods for the signal line EMA in MACD calculation. 9
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 MACD line, signal line, and MACD histogram added.

Notes

The MACD (Moving Average Convergence Divergence) is a trend-following momentum indicator that shows the relationship between two moving averages of a security’s price. Developed by Gerald Appel in the late 1970s, the MACD is one of the simplest and most effective momentum indicators available.

MACD Line: The MACD line is the difference between two exponential moving averages (EMAs) of a security’s price, typically the 12-day and 26-day EMAs.

Signal Line: This is usually a 9-day EMA of the MACD line. It acts as a trigger for buy and sell signals.

Histogram: The MACD histogram plots the difference between the MACD line and the signal line. A histogram above zero indicates that the MACD line is above the signal line (bullish), and below zero indicates it is below the signal line (bearish).

Crossovers: The most common MACD signals are when the MACD line crosses above or below the signal line. A crossover above the signal line is a bullish signal, indicating it might be time to buy, and a crossover below the signal line is bearish, suggesting it might be time to sell.

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

# MACD pandas engine
df_macd = (
    df
        .groupby('symbol')
        .augment_macd(
            date_column = 'date', 
            close_column = 'close', 
            fast_period = 12, 
            slow_period = 26, 
            signal_period = 9, 
            engine = "pandas"
        )
)

df_macd.glimpse()
<class 'pandas.core.frame.DataFrame'>: 16194 rows of 11 columns
symbol:                          object            ['META', 'META', 'MET ...
date:                            datetime64[ns]    [Timestamp('2013-01-0 ...
open:                            float64           [27.440000534057617,  ...
high:                            float64           [28.18000030517578, 2 ...
low:                             float64           [27.420000076293945,  ...
close:                           float64           [28.0, 27.77000045776 ...
volume:                          int64             [69846400, 63140600,  ...
adjusted:                        float64           [28.0, 27.77000045776 ...
close_macd_line_12_26_9:         float64           [0.0, -0.018347541830 ...
close_macd_signal_line_12_26_9:  float64           [0.0, -0.003669508366 ...
close_macd_histogram_12_26_9:    float64           [0.0, -0.014678033464 ...
# MACD polars engine
df_macd = (
    df
        .groupby('symbol')
        .augment_macd(
            date_column = 'date', 
            close_column = 'close', 
            fast_period = 12, 
            slow_period = 26, 
            signal_period = 9, 
            engine = "polars"
        )
)

df_macd.glimpse()
<class 'pandas.core.frame.DataFrame'>: 16194 rows of 11 columns
symbol:                          object            ['META', 'META', 'MET ...
date:                            datetime64[ns]    [Timestamp('2013-01-0 ...
open:                            float64           [27.440000534057617,  ...
high:                            float64           [28.18000030517578, 2 ...
low:                             float64           [27.420000076293945,  ...
close:                           float64           [28.0, 27.77000045776 ...
volume:                          int64             [69846400, 63140600,  ...
adjusted:                        float64           [28.0, 27.77000045776 ...
close_macd_line_12_26_9:         float64           [0.0, -0.018347541830 ...
close_macd_signal_line_12_26_9:  float64           [0.0, -0.003669508366 ...
close_macd_histogram_12_26_9:    float64           [0.0, -0.014678033464 ...