How To Compute Average Directional Index (ADX) in JavaScript?

by noelia.friesen , in category: Technical Analysis , a month ago

How To Compute Average Directional Index (ADX) in JavaScript?

Facebook Twitter LinkedIn Whatsapp

1 answer

by ayana_reilly , a month ago

@noelia.friesen 

To compute the Average Directional Index (ADX) in JavaScript, you can use the following steps:

  1. Calculate the True Range (TR) for each period:
  • TR = Math.max(high - low, Math.abs(high - closePrev), Math.abs(low - closePrev))
  1. Calculate the Directional Movement (DM) for each period:
  • Positive Directional Movement (PDM) = high - highPrev (if this value is negative, set it to 0)
  • Negative Directional Movement (NDM) = lowPrev - low (if this value is negative, set it to 0)
  1. Calculate the Smoothed True Range (ATR) for the specified period:
  • ATR = (ATRprev * (n-1) + TR) / n
  1. Calculate the Directional Indexes (DX) for each period:
  • PDI = (PDM / ATR) * 100
  • NDI = (NDM / ATR) * 100
  1. Calculate the ADX value for the specified period:
  • DXdiff = Math.abs(PDI - NDI)
  • DXsum = PDI + NDI
  • ADX = ((ADXprev * (n-1)) + DXdiff) / n


Here is an example of how you can implement this computation in JavaScript:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function calculateADX(data, period) {
  let ATR = [];
  let DX = [];
  let ADX = [];

  for (let i = 0; i < data.length; i++) {
    let TR = Math.max(data[i].high - data[i].low, Math.abs(data[i].high - data[i - 1].close), Math.abs(data[i].low - data[i - 1].close));
    let PDM = Math.max(data[i].high - data[i - 1].high, 0);
    let NDM = Math.max(data[i - 1].low - data[i].low, 0);

    if (i < period) {
      ATR.push(TR);
      DX.push(0);
      ADX.push(0);
    } else if (i === period) {
      let sumATR = ATR.reduce((acc, val) => acc + val, 0);
      ATR.push(sumATR / period);

      let sumDX = DX.reduce((acc, val) => acc + val, 0);
      DX.push(sumDX / period);

      ADX.push(0);
    } else {
      ATR.push(((ATR[i - 1] * (period - 1)) + TR) / period);

      let PDI = (PDM / ATR[i]) * 100;
      let NDI = (NDM / ATR[i]) * 100;
      let DXval = Math.abs(PDI - NDI) / (PDI + NDI);

      DX.push(((DX[i - 1] * (period - 1)) + DXval) / period);

      if (i >= period * 2 - 1) {
        ADX.push(((ADX[i - 1] * (period - 1)) + DX[i]) / period);
      } else {
        ADX.push(0);
      }
    }
  }

  return ADX;
}

// Example usage:
let data = [
  { high: 10, low: 5, close: 8 },
  { high: 12, low: 6, close: 10 },
  { high: 15, low: 8, close: 12 },
  { high: 14, low: 9, close: 11 },
  // Add more data points as needed
]

let period = 14;
let adxValues = calculateADX(data, period);
console.log(adxValues);


This is a basic implementation of calculating the Average Directional Index (ADX) in JavaScript. You may need to adjust the code based on the data structure and specific requirements of your application.