WW DMI with Wilders smooting
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © JakeTheDane

//@version=4
study(title="WW DMI with Wilders smooting", shorttitle="WW DMI", overlay=false)

// User Inputs
len = input(14, minval=1, title="DI Length")
lensig = input(14, title="ADX Smoothing", minval=1, maxval=50)

// True Range Calculation
var float trueRange = na
diff1 = high - low
diff2 = abs(high - close[1])
diff3 = abs(low - close[1])
trueRange := na(trueRange) ? diff1 : trueRange - (trueRange / len) + iff(na(diff1), 0.0, diff1)

// Directional Movement Calculation
var float plusDM = na
var float minusDM = na
upMove = high - high[1]
downMove = low[1] - low
plusDM := na(plusDM) ? 0.0 : plusDM - (plusDM / len) + (upMove > downMove and upMove > 0.0 ? upMove : 0.0)
minusDM := na(minusDM) ? 0.0 : minusDM - (minusDM / len) + (downMove > upMove and downMove > 0.0 ? downMove : 0.0)

// Calculating +DI and -DI
plusDI = 100 * (plusDM / trueRange)
minusDI = 100 * (minusDM / trueRange)

// Calculating DX
dx = abs(plusDI - minusDI) / (plusDI + minusDI) * 100

// Calculating ADX
var float adx = na
adx := na(adx) ? dx : (adx * (lensig - 1) + dx) / lensig

// Plotting the indicators with updated colors
plot(adx, color= color.blue)
plot(plusDI, color= color.green)
plot(minusDI, color= color.red)

// Adding horizontal lines at essential value points
hline(20, "", color= color.gray, linestyle=hline.style_dotted)
hline(50, "", color= color.gray, linestyle=hline.style_dotted)


