#### Introduction

In this part of my series Implementing Technical Indicators I provide you with the R implementation of another technical indicator. Please refer to my article Technical Indicators: An Introduction for an overview of the concept of technical indicators and for details on the particular one presented in this article.

DISCLAIMER: None of the below is intended to be considered as any kind of investment advice. All examples serve as illustrative material only.

#### Implementation

For an implementation of the average directional movement index (ADX) please see the R code below. The function `ADX` calculates the indicator values, which may afterwards be plotted with help of the function `plotADX`. Note that both function use tools introduced in my article Implementing Technical Indicators: Tools.

``````
#
# AVERAGE DIRECTIONAL MOVEMENT INDEX
#

source("Indicators/IndicatorHelperFunctions.R")
source("Indicators/IndicatorPlottingFunctions.R")

source("Indicators/ATR.R")

# indicator calculation
ADX = function(v_high, v_low, v_close, i_window=10)
{
checkVectors(v_high, v_low, v_close)
checkWindow(v_close, i_window)

i_length = length(v_close)

# directional movement
v_high_diff = c(0, (v_high[2:i_length] - v_high[1:(i_length-1)]))
v_low_diff  = c(0, (v_low[1:(i_length-1)] - v_low[2:i_length]))
v_mDM = rep(0, i_length)
v_pDM = rep(0, i_length)
v_mDM[(v_low_diff > v_high_diff)] = pmax(0, v_low_diff[(v_low_diff > v_high_diff)])
v_pDM[(v_high_diff > v_low_diff)] = pmax(0, v_high_diff[(v_high_diff > v_low_diff)])

# directional index
v_atr = ATR(v_high, v_low, v_close, i_window)\$ATR
v_mDI = 100 * v_mADM / v_atr
v_pDI = 100 * v_pADM / v_atr

v_dx = abs((v_pDI - v_mDI) / (v_pDI + v_mDI))
v_dx[is.na(v_dx)] = 0
v_adx = 100 * movingAverage(v_dx, i_window)

}

# indicator plotting
{

v_colors = c("black", "blue")