The directional movement index (DX) is an indicator that identifies which direction the price of an asset is moving. The DX indicator is generally part of the Directional Movement Index (DMI)
The indicator functions by comparing prior highs and lows and drawing two lines. A negative directional movement line (-DI) and a positive directional movement line (+DI). An optional third line called the Directional Index (DX) or sometimes Average Directional Index (ADX) can also be used to gauge the strength of the uptrend or downtrend.
The source code for the DX function is available here.
/*
* The DM1 (one period) is base on the largest part of
* today's range that is outside of yesterdays range.
*
* The following 7 cases explain how the +DM and -DM are
* calculated on one period:
*
* Case 1: Case 2:
* C| A|
* | | C|
* | +DM1 = (C-A) B| | +DM1 = 0
* | -DM1 = 0 | -DM1 = (B-D)
* A| | D|
* | D|
* B|
*
* Case 3: Case 4:
* C| C|
* | A| |
* | +DM1 = (C-A) | | +DM1 = 0
* | -DM1 = 0 B| | -DM1 = (B-D)
* A| | |
* | | D|
* B| |
* D|
*
* Case 5: Case 6:
* A| A| C|
* | C| +DM1 = 0 | | +DM1 = 0
* | | -DM1 = 0 | | -DM1 = 0
* | D| | |
* B| B| D|
*
*
* Case 7:
*
* C|
* A| |
* | | +DM=0
* B| | -DM=0
* D|
*
* In case 3 and 4, the rule is that the smallest delta between
* (C-A) and (B-D) determine which of +DM or -DM is zero.
*
* In case 7, (C-A) and (B-D) are equal, so both +DM and -DM are
* zero.
*
* The rules remain the same when A=B and C=D (when the highs
* equal the lows).
*
* When calculating the DM over a period > 1, the one-period DM
* for the desired period are initialy sum. In other word,
* for a -DM14, sum the -DM1 for the first 14 days (that's
* 13 values because there is no DM for the first day!)
* Subsequent DM are calculated using the Wilder's
* smoothing approach:
*
* Previous -DM14
* Today's -DM14 = Previous -DM14 - -------------- + Today's -DM1
* 14
*
* Calculation of a -DI14 is as follow:
*
* -DM14
* -DI14 = --------
* TR14
*
* Calculation of the TR14 is:
*
* Previous TR14
* Today's TR14 = Previous TR14 - -------------- + Today's TR1
* 14
*
* The first TR14 is the summation of the first 14 TR1. See the
* TA_TRANGE function on how to calculate the true range.
*
* Calculation of the DX14 is:
*
* diffDI = ABS( (-DI14) - (+DI14) )
* sumDI = (-DI14) + (+DI14)
*
* DX14 = 100 * (diffDI / sumDI)
*
* Reference:
* New Concepts In Technical Trading Systems, J. Welles Wilder Jr
*/