augment_adx

augment_adx(
    data,
    date_column,
    high_column,
    low_column,
    close_column,
    periods=14,
    reduce_memory=False,
    engine='pandas',
)

Calculate Average Directional Index (ADX), +DI, and -DI for a financial time series to determine strength of trend.

Parameters

Name Type Description Default
data Union[pd.DataFrame, pd.core.groupby.generic.DataFrameGroupBy] Input pandas DataFrame or GroupBy object with time series data. required
date_column str Column name containing dates or timestamps. required
high_column str Column name with high prices. required
low_column str Column name with low prices. required
close_column str Column name with closing prices. required
periods Union[int, Tuple[int, int], List[int]] Number of periods for ADX calculation. Accepts int, tuple (start, end), or list. Default is 14. 14
reduce_memory bool If True, reduces memory usage before calculation. Default is False. False
engine str Computation engine: β€˜pandas’ or β€˜polars’. Default is β€˜pandas’. 'pandas'

Returns

Name Type Description
pd.DataFrame DataFrame with added columns: - {close_column}plus_di{period}: Positive Directional Indicator (+DI) - {close_column}minus_di{period}: Negative Directional Indicator (-DI) - {close_column}adx{period}: Average Directional Index (ADX)

Notes

  • The ADX is a trend strength indicator that ranges from 0 to 100.
  • A high ADX value indicates a strong trend, while a low ADX value indicates a weak trend.
  • The +DI and -DI values range from 0 to 100.
  • The ADX is calculated as the average of the DX values over the specified period.
  • The DX value is calculated as 100 * |(+DI - -DI)| / (+DI + -DI).
  • The True Range (TR) is the maximum of the following:
    • High - Low
    • High - Previous Close
    • Low - Previous Close
  • The +DM is calculated as follows:
    • If High - Previous High > Previous Low - Low, then +DM = max(High - Previous High, 0)
    • Otherwise, +DM = 0
  • The -DM is calculated as follows:
    • If Previous Low - Low > High - Previous High, then -DM = max(Previous Low - Low, 0)
    • Otherwise, -DM = 0

References:

  • https://www.investopedia.com/terms/a/adx.asp

Examples

import pandas as pd
import pytimetk as tk

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

# Example 1 - Single stock ADX with pandas engine
adx_df = (
    df.query("symbol == 'AAPL'")
    .augment_adx(
        date_column='date',
        high_column='high',
        low_column='low',
        close_column='close',
        periods=[14, 28]
    )
)
adx_df.head()
symbol date open high low close volume adjusted close_plus_di_14 close_minus_di_14 close_adx_14 close_plus_di_28 close_minus_di_28 close_adx_28
5398 AAPL 2013-01-02 19.779285 19.821428 19.343929 19.608213 560518000 16.791180 0.000000 0.000000 NaN 0.000000 0.000000 NaN
5399 AAPL 2013-01-03 19.567142 19.631071 19.321428 19.360714 352965200 16.579241 0.000000 2.858569 NaN 0.000000 2.858569 NaN
5400 AAPL 2013-01-04 19.177500 19.236786 18.779642 18.821428 594333600 16.117437 0.000000 41.242622 NaN 0.000000 41.242622 NaN
5401 AAPL 2013-01-07 18.642857 18.903570 18.400000 18.710714 484156400 16.022623 0.000000 50.429407 NaN 0.000000 50.429407 NaN
5402 AAPL 2013-01-08 18.900356 18.996071 18.616072 18.761070 458707200 16.065746 4.107886 41.919204 NaN 4.107886 41.919204 NaN
# Example 2 - Multiple stocks with groupby using pandas engine
adx_df = (
    df.groupby('symbol')
    .augment_adx(
        date_column='date',
        high_column='high',
        low_column='low',
        close_column='close',
        periods=14
    )
)
adx_df.groupby('symbol').tail(1)
symbol date open high low close volume adjusted close_plus_di_14 close_minus_di_14 close_adx_14
2698 META 2023-09-21 295.700012 300.260010 293.269989 295.730011 21300500 295.730011 16.554034 13.201018 31.777829
5397 AMZN 2023-09-21 131.940002 132.240005 129.309998 129.330002 70234800 129.330002 19.587053 32.805871 21.477234
8096 AAPL 2023-09-21 174.550003 176.300003 173.860001 173.929993 63047900 173.929993 11.542643 40.326654 22.071067
10795 NFLX 2023-09-21 386.500000 395.899994 383.420013 384.149994 5547900 384.149994 13.288621 38.850220 35.312001
13494 NVDA 2023-09-21 415.829987 421.000000 409.799988 410.170013 44893000 410.170013 2.733215 47.062716 35.449938
16193 GOOG 2023-09-21 132.389999 133.190002 131.089996 131.360001 22042700 131.360001 11.977595 32.162086 31.099873
# Example 3 - Single stock ADX with polars engine
adx_df = (
    df.query("symbol == 'AAPL'")
    .augment_adx(
        date_column='date',
        high_column='high',
        low_column='low',
        close_column='close',
        periods=[14, 28],
        engine='polars'
    )
)
adx_df.head()
symbol date open high low close volume adjusted close_plus_di_14 close_minus_di_14 close_adx_14 close_plus_di_28 close_minus_di_28 close_adx_28
5398 AAPL 2013-01-02 19.779285 19.821428 19.343929 19.608213 560518000 16.791180 0.000000 0.000000 NaN 0.000000 0.000000 NaN
5399 AAPL 2013-01-03 19.567142 19.631071 19.321428 19.360714 352965200 16.579241 0.000000 2.858569 NaN 0.000000 2.858569 NaN
5400 AAPL 2013-01-04 19.177500 19.236786 18.779642 18.821428 594333600 16.117437 0.000000 41.242622 NaN 0.000000 41.242622 NaN
5401 AAPL 2013-01-07 18.642857 18.903570 18.400000 18.710714 484156400 16.022623 0.000000 50.429407 NaN 0.000000 50.429407 NaN
5402 AAPL 2013-01-08 18.900356 18.996071 18.616072 18.761070 458707200 16.065746 4.107886 41.919204 NaN 4.107886 41.919204 NaN
# Example 4 - Multiple stocks with groupby using polars engine
adx_df = (
    df.groupby('symbol')
    .augment_adx(
        date_column='date',
        high_column='high',
        low_column='low',
        close_column='close',
        periods=14,
        engine='polars'
    )
)
adx_df.groupby('symbol').tail(1)
symbol date open high low close volume adjusted close_plus_di_14 close_minus_di_14 close_adx_14
2698 META 2023-09-21 295.700012 300.260010 293.269989 295.730011 21300500 295.730011 16.554034 13.201018 31.777829
5397 AMZN 2023-09-21 131.940002 132.240005 129.309998 129.330002 70234800 129.330002 19.587053 32.805871 21.477234
8096 AAPL 2023-09-21 174.550003 176.300003 173.860001 173.929993 63047900 173.929993 11.542643 40.326654 22.071067
10795 NFLX 2023-09-21 386.500000 395.899994 383.420013 384.149994 5547900 384.149994 13.288621 38.850220 35.312001
13494 NVDA 2023-09-21 415.829987 421.000000 409.799988 410.170013 44893000 410.170013 2.733215 47.062716 35.449938
16193 GOOG 2023-09-21 132.389999 133.190002 131.089996 131.360001 22042700 131.360001 11.977595 32.162086 31.099873