#//This indicator is based on the formulas in the original "ADX and DI" script of Masa Nakamuro
#//The indicator gives a multiplication of ADX and the DI+ and DI-values, color coded for bullish or bearish momentum
#//the indicator was optimised for use with HA-candles, but works with regular candles as well
#//and shows general market direction en divergence in a simpler visual way than the normal ADX-DI lines
#//a threshold mask and the actual DI-lines kan be shown or omitted as preferred
#//version 2. added support for MTF (thank you Chris Moody for the MTF-Code!!)
#//@ Duyck
#study("ADX-DI MTF trend indication - JD")
# Converted by Sam4Cok@Samer800 - 07/2023
declare lower;
input useChartTimeframe = {Default "Yes", "No"};
input manualTimeframe = AggregationPeriod.FIFTEEN_MIN;
input adxLength = 14;#(title="ADX Length", type=integer, defval=14)
input ShowThresholdMask= yes;#(title="Show threshold mask", type=bool,defval=true)
input showDI = yes;#(title="Show DI - lines", type=bool,defval=true)
input th1 = 10;#(title="threshold 1", type=integer, defval=10)
input th2 = 20;#(title="threshold 2", type=integer, defval=20)
input th3 = 40;#(title="threshold 3", type=integer, defval=40)
input indication = {Default "Trend indication" , "Retracement indication"};#, defval=1,minval=1,maxval=2)
def na = Double.NaN;
def last = isNaN(close);
def priority = indication==indication."Trend indication";
def l; def c; def h;
Switch (useChartTimeframe) {
Case "Yes" :
l = low;
c = close;
h = high;
Case "No" :
l = low(Period=manualTimeframe);
c = close(Period=manualTimeframe);
h = high(Period=manualTimeframe);
}
def TrueRange = TrueRange(h, c, l);#max(max(h - l, AbsValue(h - c[1])), AbsValue(l - c[1]));
def DirectionalMovementPlus = if (h - h[1]) > (l[1]-l) then max(h - h[1], 0) else 0;
def DirectionalMovementMinus = if (l[1] - l) > (h - h[1]) then max(l[1] - l, 0) else 0;
def SmoothedTrueRange = (SmoothedTrueRange[1]) - ((SmoothedTrueRange[1])/adxLength) + TrueRange;
def SmoothedDirectionalMovementPlus = (SmoothedDirectionalMovementPlus[1]) - ((SmoothedDirectionalMovementPlus[1])/adxLength) + DirectionalMovementPlus;
def SmoothedDirectionalMovementMinus = (SmoothedDirectionalMovementMinus[1]) - ((SmoothedDirectionalMovementMinus[1])/adxLength) + DirectionalMovementMinus;
def base_DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100;
def base_DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100;
#/output
def DIPlus = base_DIPlus;
def DIMinus = base_DIMinus;
def DIdiff = DIPlus - DIMinus;
def DIcolor = if DIdiff<0 then -1 else if DIdiff>0 then 1 else 0;
def DX = AbsValue(DIPlus-DIMinus) / (DIPlus+DIMinus)*100;
def ADX = Average(DX, adxLength);
def momentprod=DIdiff*ADX;
def posprod=DIPlus*40;
def negprod=DIMinus*-40;
def smooth= ExpAverage(momentprod,adxLength) ;
def thline=DIdiff*th1;
def thline2=DIdiff*th2;
def thline3=DIdiff*th3;
def ema_price= ExpAverage(close,adxLength);
def ema_up= if ema_price>ema_price[1] then yes else no;
def ema_down= if ema_price<ema_price[1] then yes else no;
def emacolor= if ExpAverage(close,4)> ExpAverage(close[1],4) then 1 else -1;
def smcolor= if smooth<smooth[1] then (if DIcolor==-1 then -1 else (if priority==2 and ema_down then -1 else emacolor)) else
if smooth>smooth[1] then (if DIcolor==1 then 1 else (if priority==2 and ema_down then 1 else emacolor)) else 0;
def DIPlus_strength = if ShowThresholdMask then if DIPlus>=th2 then 3 else 1 else 2;
def DIMinus_strength = if ShowThresholdMask then if DIMinus>=th2 then -3 else -1 else -2;
def lvlUp = 40*th2;
def lvlDn = -40*th2;
plot smoothLine = smooth;#,color=smcolor,linewidth=3,transp = 5)
def Dominantstrength = momentprod;#,color=DIcolor, title="Dominant strength", style = area,linewidth=1,transp = 20)
plot "DI+" = if showDI then posprod else na;#,color=DIPlus_strength, title="DI+", style = line,linewidth=1,transp = 60)
plot "DI-" = if showDI then negprod else na;#,color=DIMinus_strength, title="DI-", style = line,linewidth=1,transp = 60)
def threshold1 = if ShowThresholdMask then thline else na;#,title="threshold 1", color=color(black,40),linewidth=1, style = area ,transp = 0)
def threshold2 = if ShowThresholdMask then thline2 else na;#,title="threshold 2", color=color(black,70),linewidth=1, style = area ,transp = 0)
def threshold3 = if ShowThresholdMask then thline3 else na;#,title= "threshold 3", color=color(black,75),linewidth=1, style = area ,transp = 0)
AddCloud(Dominantstrength,0 , Color.DARK_GREEN, Color.DARK_RED);
#AddCloud(threshold3, 0, Color.DARK_GREEN, Color.DARK_RED);
AddCloud(if threshold1>0 then Dominantstrength else na, threshold1, Color.DARK_GREEN, Color.DARK_GREEN);
AddCloud(if threshold1<0 then Dominantstrength else na, threshold1, Color.DARK_RED, Color.DARK_RED);
AddCloud(if threshold2>0 then Dominantstrength else na, threshold2, Color.DARK_GREEN, Color.DARK_GREEN);
AddCloud(if threshold2<0 then Dominantstrength else na, threshold2, Color.DARK_RED, Color.DARK_RED);
AddCloud(Dominantstrength, threshold2, Color.DARK_GREEN, Color.DARK_RED);
"DI+".AssignValueColor(if DIPlus_strength==3 then Color.LIME else
if DIPlus_strength==2 then color.GREEN else Color.DARK_GREEN);
"DI-".AssignValueColor(if DIMinus_strength==-3 then Color.RED else
if DIMinus_strength==-2 then color.ORANGE else Color.DARK_RED);
smoothLine.SetLineWeight(3);
smoothLine.AssignValueColor(if smcolor > 0 then Color.GREEN else
if smcolor < 0 then Color.RED else Color.YELLOW);
#-- END of CODE