DMI - Visual
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RB_TRADER

//@version=4
study(title="Directional Movement Index", shorttitle="DMI", format=format.price, precision=4, resolution="")
lensig = input(14, title="ADX Smoothing", minval=1, maxval=50)
len = input(14, minval=1, title="DI Length")
up = change(high)
down = -change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
trur = rma(tr, len)
plus = fixnan(100 * rma(plusDM, len) / trur)
minus = fixnan(100 * rma(minusDM, len) / trur)
sum = plus + minus
adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)
plot(adx, color=#FF006E, title="ADX")
plot(plus, color=#0094FF, title="+DI")
plot(minus, color=#FF6A00, title="-DI")
bgcolor(adx<plus and adx<minus ? color.white : plus>=minus ? color.blue : color.red)
bgcolor(adx<plus and adx<minus ? color.white : plus>=minus and adx>=minus and adx>=adx[1] ? color.blue : na)
bgcolor(adx<plus and adx<minus ? color.white : plus<minus and adx>=plus and adx>=adx[1] ? color.red : na)
level = input(20)
hline(level)
plotshape(plus, style=shape.circle, color=crossover(plus, level) ? color.blue:na, location=location.bottom)
plotshape(minus, style=shape.circle, color=crossover(minus, level) ? color.red:na, location=location.top)
plot(plus, style=plot.style_circles, linewidth=3, color=crossover(plus, minus) ? color.blue:na)
plot(minus, style=plot.style_circles, linewidth=3, color=crossover(minus, plus) ? color.red:na)
