I was playing with this a little bit and found something that can help you with the issue:

def ADX(data: pd.DataFrame, period: int):
    """
    Computes the ADX indicator.
    """
    
    df = data.copy()
    alpha = 1/period

    # TR
    df['H-L'] = df['High'] - df['Low']
    df['H-C'] = np.abs(df['High'] - df['Close'].shift(1))
    df['L-C'] = np.abs(df['Low'] - df['Close'].shift(1))
    df['TR'] = df[['H-L', 'H-C', 'L-C']].max(axis=1)
    del df['H-L'], df['H-C'], df['L-C']

    # ATR
    df['ATR'] = df['TR'].ewm(alpha=alpha, adjust=False).mean()

    # +-DX
    df['H-pH'] = df['High'] - df['High'].shift(1)
    df['pL-L'] = df['Low'].shift(1) - df['Low']
    df['+DX'] = np.where(
        (df['H-pH'] > df['pL-L']) & (df['H-pH']>0),
        df['H-pH'],
        0.0
    )
    df['-DX'] = np.where(
        (df['H-pH'] < df['pL-L']) & (df['pL-L']>0),
        df['pL-L'],
        0.0
    )
    del df['H-pH'], df['pL-L']

    # +- DMI
    df['S+DM'] = df['+DX'].ewm(alpha=alpha, adjust=False).mean()
    df['S-DM'] = df['-DX'].ewm(alpha=alpha, adjust=False).mean()
    df['+DMI'] = (df['S+DM']/df['ATR'])*100
    df['-DMI'] = (df['S-DM']/df['ATR'])*100
    del df['S+DM'], df['S-DM']

    # ADX
    df['DX'] = (np.abs(df['+DMI'] - df['-DMI'])/(df['+DMI'] + df['-DMI']))*100
    df['ADX'] = df['DX'].ewm(alpha=alpha, adjust=False).mean()
    del df['DX'], df['ATR'], df['TR'], df['-DX'], df['+DX'], df['+DMI'], df['-DMI']

    return df

At the beginning the values aren't correct (as always with the EWM approach) but after several computations it converges to the correct value.

Answer from Fabio Gomez on Stack Overflow
🌐
EODHD
eodhd.com › home › algorithmic trading with average directional index in python
Algorithmic Trading with Average Directional Index in Python
February 5, 2025 - Learn about the Average Directional Index (ADX) and how it can help traders identify market trends and build effective trading strategies in Python.
Top answer
1 of 4
9

I was playing with this a little bit and found something that can help you with the issue:

def ADX(data: pd.DataFrame, period: int):
    """
    Computes the ADX indicator.
    """
    
    df = data.copy()
    alpha = 1/period

    # TR
    df['H-L'] = df['High'] - df['Low']
    df['H-C'] = np.abs(df['High'] - df['Close'].shift(1))
    df['L-C'] = np.abs(df['Low'] - df['Close'].shift(1))
    df['TR'] = df[['H-L', 'H-C', 'L-C']].max(axis=1)
    del df['H-L'], df['H-C'], df['L-C']

    # ATR
    df['ATR'] = df['TR'].ewm(alpha=alpha, adjust=False).mean()

    # +-DX
    df['H-pH'] = df['High'] - df['High'].shift(1)
    df['pL-L'] = df['Low'].shift(1) - df['Low']
    df['+DX'] = np.where(
        (df['H-pH'] > df['pL-L']) & (df['H-pH']>0),
        df['H-pH'],
        0.0
    )
    df['-DX'] = np.where(
        (df['H-pH'] < df['pL-L']) & (df['pL-L']>0),
        df['pL-L'],
        0.0
    )
    del df['H-pH'], df['pL-L']

    # +- DMI
    df['S+DM'] = df['+DX'].ewm(alpha=alpha, adjust=False).mean()
    df['S-DM'] = df['-DX'].ewm(alpha=alpha, adjust=False).mean()
    df['+DMI'] = (df['S+DM']/df['ATR'])*100
    df['-DMI'] = (df['S-DM']/df['ATR'])*100
    del df['S+DM'], df['S-DM']

    # ADX
    df['DX'] = (np.abs(df['+DMI'] - df['-DMI'])/(df['+DMI'] + df['-DMI']))*100
    df['ADX'] = df['DX'].ewm(alpha=alpha, adjust=False).mean()
    del df['DX'], df['ATR'], df['TR'], df['-DX'], df['+DX'], df['+DMI'], df['-DMI']

    return df

At the beginning the values aren't correct (as always with the EWM approach) but after several computations it converges to the correct value.

2 of 4
4

This gives you the exact numbers as Tradingview and Thinkorswim.

import numpy as np

def ema(arr, periods=14, weight=1, init=None):
    leading_na = np.where(~np.isnan(arr))[0][0]
    arr = arr[leading_na:]
    alpha = weight / (periods + (weight-1))
    alpha_rev = 1 - alpha
    n = arr.shape[0]
    pows = alpha_rev**(np.arange(n+1))
    out1 = np.array([])
    if 0 in pows:
        out1 = ema(arr[:int(len(arr)/2)], periods)
        arr = arr[int(len(arr)/2) - 1:]
        init = out1[-1]
        n = arr.shape[0]
        pows = alpha_rev**(np.arange(n+1))
    scale_arr = 1/pows[:-1]
    if init:
        offset = init * pows[1:]
    else:
        offset = arr[0]*pows[1:]
    pw0 = alpha*alpha_rev**(n-1)
    mult = arr*pw0*scale_arr
    cumsums = mult.cumsum()
    out = offset + cumsums*scale_arr[::-1]
    out = out[1:] if len(out1) > 0 else out
    out = np.concatenate([out1, out])
    out[:periods] = np.nan
    out = np.concatenate(([np.nan]*leading_na, out))
    return out


def atr(highs, lows, closes, periods=14, ema_weight=1):
    hi = np.array(highs)
    lo = np.array(lows)
    c = np.array(closes)
    tr = np.vstack([np.abs(hi[1:]-c[:-1]),
                    np.abs(lo[1:]-c[:-1]),
                    (hi-lo)[1:]]).max(axis=0)
    atr = ema(tr, periods=periods, weight=ema_weight)
    atr = np.concatenate([[np.nan], atr])
    return atr


def adx(highs, lows, closes, periods=14):
    highs = np.array(highs)
    lows = np.array(lows)
    closes = np.array(closes)
    up = highs[1:] - highs[:-1]
    down = lows[:-1] - lows[1:]
    up_idx = up > down
    down_idx = down > up
    updm = np.zeros(len(up))
    updm[up_idx] = up[up_idx]
    updm[updm < 0] = 0
    downdm = np.zeros(len(down))
    downdm[down_idx] = down[down_idx]
    downdm[downdm < 0] = 0
    _atr = atr(highs, lows, closes, periods)[1:]
    updi = 100 * ema(updm, periods) / _atr
    downdi = 100 * ema(downdm, periods) / _atr
    zeros = (updi + downdi == 0)
    downdi[zeros] = .0000001
    adx = 100 * np.abs(updi - downdi) / (updi + downdi)
    adx = ema(np.concatenate([[np.nan], adx]), periods)
    return adx
🌐
QuantInsti
blog.quantinsti.com › adx-indicator-python
Mathematical Intuition of the ADX Indicator: A Python Approach
July 28, 2025 - This blog tells us how to calculate and plot the ADX indicator. We will also go through a trading strategy using the ADX indicator in python.
🌐
Technical Analysis Library in Python
technical-analysis-library-in-python.readthedocs.io › en › latest › ta.html
Documentation — Technical Analysis Library in Python 0.1.4 documentation
The Average Directional Index (ADX) is in turn derived from the smoothed averages of the difference between +DI and -DI, and measures the strength of the trend (regardless of direction) over time.
🌐
DataCamp
campus.datacamp.com › courses › financial-trading-in-python › technical-indicators
Calculate the ADX | Python
Calculate the ADX using the appropriate function from talib, and the High, Low and Close columns in the stock_data.
🌐
DataCamp
campus.datacamp.com › courses › financial-trading-in-python › technical-indicators
Strength indicator: ADX | Python
ADX can be implemented in Python by calling talib dot ADX, and passing three types of price data as input, the high, low and close price. Originally Welles Wilder used a 14-period lookback window for ADX calculations, which became the industry standard. You can change the default period with ...
Find elsewhere
🌐
AskPython
askpython.com › home › understanding the adx indicator using python
Understanding the ADX Indicator using Python - AskPython
April 10, 2025 - Recommended: (4/5) MACD Indicator: Python Implementation and Technical Analysis · As mentioned before, the ADX indicator tells us about the strength of a trend. It is also one of the directional movement indicators, just like the Simple Moving ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › azure › data-explorer › python-query-data
Query Data Using Azure Data Explorer Python Library - Azure Data Explorer | Microsoft Learn
August 27, 2025 - Azure Data Explorer provides a data client library for Python. This library enables you to query data from your code.
🌐
Medium
medium.com › codex › algorithmic-trading-with-average-directional-index-in-python-2b5a20ecf06a
Algorithmic Trading with Average Directional Index in Python | by Nikhil Adithyan | CodeX | Medium
October 5, 2023 - In this article, we are going to explore one of the most popular trend indicators, the Average Directional Index (shortly known as ADX). We will first build some basic understanding of what ADX is all about and its calculation, then, move on to building the indicator from scratch and construct a trading strategy based on that indicator in python.
🌐
GitHub
github.com › mantasavas › trading-strategy-automation
GitHub - mantasavas/trading-strategy-automation: Python automated script for outputting and displaying stock data. Two widespread indicators are being used: Average Directional Index (ADX) and Bollinger Bands.
Python automated script for outputting and displaying stock data. Two widespread indicators are being used: Average Directional Index (ADX) and Bollinger Bands. - mantasavas/trading-strategy-automation
Starred by 7 users
Forked by 4 users
Languages   Python 100.0% | Python 100.0%
🌐
Quora
quora.com › How-do-we-calculate-ADX-in-Python-for-backtesting
How do we calculate ADX in Python (for backtesting)? - Quora
Answer: To calculate the Average ... and close prices for the asset you are analyzing. The ADX is a technical indicator used to measure the strength of a trend, whether it's an uptrend or dow......
🌐
Medium
medium.com › @slisowski › the-adx-trading-strategy-in-python-a36c00ee05a8
The ADX trading strategy in Python | by Slawomir Lisowski | Medium
August 8, 2023 - The ADX trading strategy in Python Article shows how to use ADX trading strategy, code it in Python and Backtest What is the ADX technical indicator? ADX — Average Directional Movement index was …
🌐
Medium
janelleturing.medium.com › adx-decoded-a-comprehensive-guide-to-trading-with-the-average-directional-index-c7da9ea8b29e
ADX Decoded: A Comprehensive Guide to Trading with the Average Directional Index | by Janelle Turing | Medium
October 12, 2023 - In this tutorial, we explored the Average Directional Index (ADX) and learned how to use it in Python for trading purposes. We discussed the components of the ADX, including the ADX line, +DI line, and -DI line.
🌐
CodeRivers
coderivers.org › blog › python-adx-calculation
Python ADX Calculation: A Comprehensive Guide - CodeRivers
April 12, 2025 - The ADX is calculated as the smoothed moving average of the Difference between $+DI$ and $-DI$ (the Directional Movement Index, DMI). [DMI = |+DI - -DI|] ADX is then calculated as an exponential moving average (EMA) of DMI over a specified period (usually 14 days). pandas is a powerful library for data manipulation and analysis in Python.
🌐
Stack Overflow
stackoverflow.com › questions › 79331942 › how-to-get-the-adx-with-pandas-ta-library-in-python-without-smoothing
algorithmic trading - How to get the ADX with pandas-ta library in Python without smoothing? - Stack Overflow
import pandas as pd def ExponentialMA(i, period, prev_value, values): if i == 0: return prev_value else: ema = (values[i] - prev_value) * 2 / (period + 1) + prev_value return ema def ADX(high, low, close, adx_period): # Initialize the PDI and NDI arrays with the same length as the input arrays pdi = [0] * len(high) ndi = [0] * len(high) # Initialize the ADX array with the same length as the input arrays adx = [0] * len(high) # Initialize the temporary buffer with the same length as the input arrays tmp_buffer = [0] * len(high) # Iterate through the data points for i in range(1, len(high)): # G
🌐
Stockindicators
python.stockindicators.dev › indicators › Adx
Average Directional Index (ADX) | Stock Indicators for Python
November 3, 2024 - from stock_indicators import indicators ... indicators.get_adx(quotes, lookback_periods) Created by J. Welles Wilder, the Average Directional Movement Index is a measure of price directional movement....
🌐
PyPI
pypi.org › project › stockstats
stockstats · PyPI
February 16, 2026 - Python :: Implementation :: CPython · Topic · Utilities · Report project as malware · Supply a wrapper StockDataFrame for pandas.DataFrame with inline stock statistics/indicators support. Supported statistics/indicators are: Moving Averages: SMA, EMA, SMMA, TEMA, LRMA, KAMA, VWMA, DMA · Momentum: RSI, StochRSI, MACD, PPO, KDJ, ROC, CMO, KST, Coppock, AO, BOP, CTI, Inertia, PSL · Trend: Supertrend, Aroon, Ichimoku, CR, DMI (+DI/-DI/ADX/ADXR), TRIX, WT ·
      » pip install stockstats
    
Published   Feb 16, 2026
Version   0.6.8
🌐
GitHub
github.com › topics › adx
adx · GitHub Topics · GitHub
And one new technical indicator is designed using the before-mentioned three indicators named as "BIRA". ... A comprehensive Python library for calculating popular technical indicators used in financial markets.