ADX and DI+ v4.5 Optimized
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @imbharat- This formula plots VWMA(ADX) vs EMA(DI+) under given timeframe.
// Strategy is to look for potential BUY opportunity when EMA(DI+) colored blue, entering upward into green zone where ADX counterpart (Default: Yellow) is also present.
// Originally developed by © BeikabuOyaji and further optimized by Bharat @imbharat
//@version=4


study("ADX and DI+ v4.5 Optimized")
len = input(7)
th = input(20)

TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0

SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange

SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus

SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100
ADX = sma(DX, len)

//plot(DIPlus, color=color.green, title="DI+")
//plot(DIMinus, color=color.red, title="DI-")
//plot(ADX, color=color.navy, title="ADX")
hline(th, color=color.black)

//indicator(title="Moving Average Exponential", shorttitle="EMA", overlay=true, timeframe="", timeframe_gaps=true)
len2 = input(35, minval=1, title="EMA Length")
offset2 = 0
//input(title="EMA Offset", defval=0, minval=-500, maxval=500)
out2 = ema(DIPlus, len2)
plot(out2, title="EMA of ADX_DIPlus", color=color.blue, offset=offset2)

out3 = vwma(ADX, len2)
plot(out3, title="VWMA of ADX", color=color.yellow, offset=offset2)

upperAdx = 80
medianAdx = 65
lowerAdx = 25

upperx  = hline( upperAdx,  "Extreme ADX",   color.new( color.silver, 60 ) )
upper   = hline( medianAdx,   "High ADX",           color.new( color.silver, 80 ) )
lower   = hline( lowerAdx,   "Normal ADX",           color.new( color.silver, 80 ) )
lowerx  = hline( 5,  "Low ADX",   color.new( color.silver, 60 ) )

//  channel fill
fill( upperx, upper, color.new( color.red, 85 ),    title = "Avoid" )
fill( upper, lower, color.new( color.green, 85 ),    title = "TREND UNDERWAY" )
fill( lower, lowerx,  color.new( color.red, 85 ),   title = "Avoid" )