#
# An implementatio of ADX and DI+/- based on following Joe Rabil
#
# customized with cross line and individual DMI and ADX lengths
declare lower;
script func_DMI {
input length_di = 14; #DI+- length
input length_adx = 14; #ADX length
input averageType = AverageType.WILDERS;
def hiDiff = high - high[1];
def loDiff = low[1] - low;
def plusDM = if hiDiff > loDiff and hiDiff > 0 then hiDiff else 0;
def minusDM = if loDiff > hiDiff and loDiff > 0 then loDiff else 0;
def ATR = MovingAverage(averageType, TrueRange(high, close, low), length_di);
plot "DI+" = 100 * MovingAverage(averageType, plusDM, length_di) / ATR;
plot "DI-" = 100 * MovingAverage(averageType, minusDM, length_di) / ATR;
def DX = if ("DI+" + "DI-" > 0) then 100 * AbsValue("DI+" - "DI-") / ("DI+" + "DI-") else 0;
plot ADX = MovingAverage(averageType, DX, length_adx);
};
input di_length = 13; #original 14 Use Joe's 13 and 8
input adx_length = 8; #original 14
input averageType = AverageType.WILDERS;
#add cross line
input crossline = 25;
input showlabels = yes; #show labels
#CORE
DefineGlobalColor("Bullish", Color.GREEN);
DefineGlobalColor("Bearish", Color.RED);
DefineGlobalColor("Neutral", Color.dark_orange);
plot "DI+" = func_DMI(di_length, adx_length, averageType)."DI+";
plot "DI-" = func_DMI(di_length, adx_length, averageType)."DI-";
def ADXfast = func_DMI(di_length, adx_length, averageType)."ADX";
plot ADX = ADXfast;
"DI+".SetDefaultColor(GetColor(1));
"DI-".SetDefaultColor(GetColor(5));
ADX.SetDefaultColor(GetColor(8));
ADX.SetLineWeight(2);
plot diffDI = Round(("DI+" / "DI-" - 1) * 100,0);
diffDI.SetDefaultColor(Color.Orange);
diffDI.SetHiding(1);
def sellersOnTop = "DI+" > "DI-";
def sellersOverX = "DI+" > crossline;
def bullish = sellersOnTop and "DI+" * 1.025 > crossline and (ADX > crossline);
def bearish = diffDI < -5 or (!sellersOnTop and "DI-" > crossline * 0.80 and "DI-"[1] > crossline * 0.80);
plot bbsState = if bullish then 50 else if bearish then -20 else 0;
bbsState.Hide();
AddLabel(showlabels, if bbsState == 50 then ("Bullish " + diffDI + "% " + if sellersOverX then "+ " else "") else
if bbsState == -20 then "Bearish " + diffDI + "% " + if sellersOverX then "+ " else ""
else "Sideways " + diffDI + "% " + if sellersOverX then "+ " else "",
if IsNan(bbsState) then COLOR.GRAY else
if bbsState == 50 then GlobalColor("Bullish") else
if bbsState == -20 then GlobalColor("Bearish")
else GlobalColor("Neutral"));
AddLabel(showlabels and ADX < 18, " No Strength ", Color.Light_Orange);
AddLabel(showlabels and ADX > 18 and diffDI < -5, " SELLERS ", Color.Red);
plot cross_line = crossline;
cross_line.SetDefaultColor(GetColor(7));
cross_line.AssignValueColor( if IsNan(bbsState) then COLOR.GRAY else
if bbsState == 50 then GlobalColor("Bullish") else
if bbsState == -20 then GlobalColor("Bearish")
else GlobalColor("Neutral"));
cross_line.SetLineWeight(2);
cross_line.HideTitle();
# detect new session of day
def isNewDay = GetYYYYMMDD() != GetYYYYMMDD()[1];
# mark new sessions on intraday charts
AddVerticalLine(isNewDay && GetAggregationPeriod() < AggregationPeriod.DAY, "", Color.GRAY, curve.SHORT_DASH);
#END