# Directional Trend Index - DTI converted from TradingView by mbarcala
# Original Link: https://www.tradingview.com/script/CCo7yIeS-Directional-Trend-Index-DTI/
declare lower;
input r = 5;
input s = 20;
input u = 5;
input SigLength = 3;
input arrows = 0;
input dotsize = 3;
input APC = 0;
def xHMU = if(high - high[1] > 0, high - high[1], 0);
def xLMD = if(low - low[1] < 0, -(low - low[1]), 0);
def xPrice = xHMU - xLMD;
def xPriceAbs = AbsValue(xPrice);
def xuXA = ExpAverage(ExpAverage(ExpAverage(xPrice, r),s),u);
def xuXAAbs = ExpAverage(ExpAverage(ExpAverage(xPriceAbs, r),s),u);
def Val1 = 100 * xuXA;
def Val2 = xuXAAbs;
plot DTI = if(Val2 != 0, Val1 / Val2, 0);
DTI.setdefaultColor(Color.Cyan);
DTI.SetLineWeight(2);
DTI.HideBubble();
Plot Signal = ExpAverage(DTI,Siglength);
Signal.SetdefaultColor(Color.Magenta);
signal.SetLineWeight(2);
plot cline = 0;
cline.SetDefaultColor(Color.GRAY);
AddCloud(DTI,Signal,Color.Cyan,Color.Magenta);
def Mmax= Max(DTI,Signal);
def MMin= Min(DTI,Signal);
AddCloud(MMin,0,Color.green,Color.Black);
AddCloud(Mmax,0,Color.Black,Color.Red);
Def Con1 = DTI >= 0;
Def Con2 = DTI < 0;
Def Con3 = DTI > DTI [1];
Def Con4 = DTI < DTI[1];
Plot BullRise = If Con1 and Con3 then MMin else 0;
BullRise.SetPaintingStrategy(PaintingStrategy.Histogram);
BullRise.SetDefaultColor(Color.Green);
BullRise.SetLineWeight(3);
Plot BullFall = if Con1 and Con4 then MMin else 0;
BullFall.SetPaintingStrategy(PaintingStrategy.Histogram);
BullFall.SetDefaultColor(Color.Dark_Green);
BullFall.SetLineWeight(3);
Plot BearFall = if Con2 and Con4 then MMax else 0;
BearFall.SetPaintingStrategy(PaintingStrategy.Histogram);
BearFall.SetDefaultColor(Color.Dark_Red);
BearFall.SetLineWeight(3);
Plot BearRise = if Con2 and Con3 then Mmax else 0;
BearRise.SetPaintingStrategy(PaintingStrategy.Histogram);
BearRise.SetDefaultColor(Color.Red);
BEarRise.SetLineWeight(3);
Plot UZ = Average(DTI,34) + (1.3185*StDev(DTI,34));
UZ.SetPaintingStrategy(PaintingStrategy.Line);
UZ.SetLineWeight(2);
UZ.SetDefaultColor(Color.Yellow);
Plot LZ = Average(DTI,34) - (1.3185*StDev(DTI,34));
LZ.SetPaintingStrategy(PaintingStrategy.Line);
LZ.SetLineWeight(2);
LZ.SetDefaultColor(Color.Yellow);
plot OB = 25;
OB.SetDefaultColor(Color.BLUE);
plot OS = -25;
OS.SetDefaultColor(Color.RED);
#ARROWS
plot ArrowDown = if arrows and (DTI Crosses below Signal) then Signal else double.nan;
ArrowDown.setpaintingStrategy(paintingStrategy.Arrow_Down);
ArrowDown.setDefaultColor(color.Magenta);
ArrowDown.setLineWeight(dotsize);
plot ArrowUp = if arrows and (DTI crosses above Signal) then signal else double.nan;
ArrowUp.setpaintingStrategy(paintingStrategy.Arrow_Up);
ArrowUp.setDefaultColor(color.Cyan);
ArrowUp.setLineWeight(dotsize);
plot ArrowDown2 = if arrows and (DTI Crosses below 0) then 0 else double.nan;
ArrowDown2.setpaintingStrategy(paintingStrategy.Arrow_Down);
ArrowDown2.setDefaultColor(color.Red);
ArrowDown2.setLineWeight(dotsize);
plot ArrowUp2 = if arrows and (DTI crosses above 0 ) then 0 else double.nan;
ArrowUp2.setpaintingStrategy(paintingStrategy.Arrow_Up);
ArrowUp2.setDefaultColor(color.Blue);
ArrowUp2.setLineWeight(dotsize);
plot ArrowDown3 = if arrows and (DTI Crosses below UZ) then UZ else double.nan;
ArrowDown3.setpaintingStrategy(paintingStrategy.Arrow_Down);
ArrowDown3.setDefaultColor(color.Yellow);
ArrowDown3.setLineWeight(dotsize);
plot ArrowUp3 = if arrows and (DTI crosses above LZ ) then LZ else double.nan;
ArrowUp3.setpaintingStrategy(paintingStrategy.Arrow_Up);
ArrowUp3.setDefaultColor(color.Yellow);
ArrowUp3.setLineWeight(dotsize);
plot ArrowDown4 = if arrows and (DTI Crosses below OB) then OB else double.nan;
ArrowDown4.setpaintingStrategy(paintingStrategy.Arrow_Down);
ArrowDown4.setDefaultColor(color.Blue);
ArrowDown4.setLineWeight(dotsize);
plot ArrowUp4 = if arrows and (DTI crosses above OS ) then OS else double.nan;
ArrowUp4.setpaintingStrategy(paintingStrategy.Arrow_Up);
ArrowUp4.setDefaultColor(color.Red);
ArrowUp4.setLineWeight(dotsize);
AssignPriceColor( if APC ==1 and DTI >= signal then Color.Cyan else if APC == 1 and DTI < Signal then Color.Magenta else If APC ==2 and DTI >=0 then Color.Green else If APC ==2 and DTI <0 then Color.Red else Color.Current);