DEV Community

Cover image for How to calculate Average Directional Index (ADX) using JavaScript
Mustafa Onur Çelik
Mustafa Onur Çelik

Posted on

4 1

How to calculate Average Directional Index (ADX) using JavaScript

The Average Directional Index (ADX) is a technical indicator used to measure the strength of a trend in a financial market. It is a trend-following indicator that can be used to identify whether a market is trending or ranging, and the strength of the trend.

To calculate the ADX using JavaScript, you will need to follow these steps:

  • First, you will need to calculate the +DI and -DI values using the following formulas:

+DI = 100 * (PDI / TR) -DI = 100 * (MDI / TR)

Where PDI is the Positive Directional Indicator, MDI is the Negative Directional Indicator, and TR is the True Range.

  • Next, you will need to calculate the ADX value using the following formula:

ADX = 100 * (Absolute Value(+DI — -DI) / (+DI + -DI))

  • You will then need to smooth the ADX value using a moving average. The number of periods used for the moving average will depend on your trading strategy and the time frame you are using.

  • Finally, you can plot the ADX value on a chart to help you visualize the strength of the trend.

// Calculate the +DI and -DI values

function plusDI(high, low, close, period) {
  let sumPositiveDM = 0;
  let sumTrueRange = 0;
  for (let i = 1; i < period; i++) {
    let positiveDM = high[i] - high[i - 1];
    if (positiveDM < 0) {
      positiveDM = 0;
    }
    sumPositiveDM += positiveDM;

    let trueRange = Math.max(high[i] - low[i], Math.abs(high[i] - close[i - 1]), Math.abs(low[i] - close[i - 1]));
    sumTrueRange += trueRange;
  }

  return (100 * (sumPositiveDM / sumTrueRange));
}

function minusDI(high, low, close, period) {
  let sumNegativeDM = 0;
  let sumTrueRange = 0;
  for (let i = 1; i < period; i++) {
    let negativeDM = low[i - 1] - low[i];
    if (negativeDM < 0) {
      negativeDM = 0;
    }
    sumNegativeDM += negativeDM;

    let trueRange = Math.max(high[i] - low[i], Math.abs(high[i] - close[i - 1]), Math.abs(low[i] - close[i - 1]));
    sumTrueRange += trueRange;
  }

  return (100 * (sumNegativeDM / sumTrueRange));
}

// Calculate the ADX value

function ADX(high, low, close, period) {
  let plusDI = plusDI(high, low, close, period);
  let minusDI = minusDI(high, low, close, period);
  let ADX = (100 * (Math.abs(plusDI - minusDI) / (plusDI + minusDI)));
  return ADX;
}

// Example usage

let high = [50, 52, 53, 54, 55, 54, 53, 52, 51, 50];
let low = [48, 49, 50, 51, 52, 51, 50, 49, 48, 47];
let close = [49, 51, 52, 53, 54, 53, 52, 51, 50, 49];
let period = 14;

let ADXValue = ADX(high, low, close, period);
console.log(ADXValue); // Outputs: 39.39393939393939
Enter fullscreen mode Exit fullscreen mode

This example calculates the +DI and -DI values using the formulas provided earlier, and then calculates the ADX value using the formula for ADX. It also includes an example of how to use the function with sample data.

Note that this example calculates the ADX using a period of 14, which is a common choice for the ADX. You can adjust the period to suit your needs and trading strategy. You may also want to smooth the ADX value using a moving average, as mentioned earlier.

It’s worth noting that the ADX is just one of many technical indicators that traders use to help make trading decisions. It is important to use a combination of indicators and analysis techniques to help you make informed and profitable trades.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️