# ADX_DMI indicator
# cabe1332
# 20210814
# ADX code start
declare lower;
# You can modify ADX setting below to you liking
input ADXCutoff = 25;
input DMILength = 6;
input MaFastLength=9;
input DIRangeFilter=10;
#DMI, ADX plots
def 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), DMILength);
def "DI+" = 100 * MovingAverage(averageType, plusDM, DMILength) / ATR;
def "DI-" = 100 * MovingAverage(averageType, minusDM, DMILength) / ATR;
def DX = if ("DI+" + "DI-" > 0) then 100 * AbsValue("DI+" - "DI-") / ("DI+" + "DI-") else 0;
plot ADX = MovingAverage(averageType, DX, DMILength);
#DIline - small diff = sideways
def DIrange = AbsValue(AbsValue("DI+")-AbsValue("DI-"));
#dirange.setPaintingStrategy(paintingStrategy.HISTOGRAM);
def DIok=DIRange>DIRangeFilter;
#adx filters
plot adxavg = MovingAverage(AverageType.SIMPLE, ADX, DMILength);
def ADXOk = ADX > ADXCutoff;# and adx>adxavg;
def DMICutBull = ("DI-" < ADXCutoff and "DI+" > ADXCutoff) or ("DI+" > ADX and "DI-" < ADX) ;
def DMICutBear = ("DI+" < ADXCutoff and "DI-" > ADXCutoff) or ("DI+" < ADX and "DI-" > ADX);
def dxcrossBull = if IsNaN(dxcrossBull[1]) then "DI+" > "DI-" #for init -what it is on first bar
else if "DI+" crosses above "DI-" then 1
else if dxcrossBull[1] > 0 and "DI+" > "DI-" then 1
else 0;
#plot dxXBull=dxCrossBull;
def dxcrossBear = if IsNaN(dxcrossBear[1]) then "DI-" > "DI+"
else if "DI+" crosses below "DI-" then 1
else if dxcrossBear[1] > 0 and "DI+" < "DI-" then 1
else 0 ;
#plot dxXBear=dxCrossBear;
#==========Pivots
def pivotprice=close;
#pivots per sami. changed logic for watchlist so there is no [-1]
def engbull= close>high[1] and low<=low[1];
def engbear= close<low[1] and high>=high[1];
def pivotHigh=(low[2] >low[1] and low>low[1] and close>pivotprice[1]) or engbull ;
def pivotLow=(high[2] <high[1] and high<high[1] and close<pivotprice[1]) or engbear;
def lastPH= if isnan(close[1]) then low else if PivotHigh then low[1] else LastPH[1];
def lastPL= if isnan(close[1]) then high else if PivotLow then high[1] else LastPL[1];
def breakout=if close> lastPL and close[1]<= lastPL then 1 else 0 ;
def breakdown= if close< lastPH and close[1]>= lastPH then 1 else 0;
#=============================
def signalbull = if dxcrossBull and ADXOk and DIOk then 1 else 0;
def signalbear = if dxcrossBear and ADXOk and DIOk then 1 else 0;
# ignore the signal if prevsignal was same direction and had higher DI
def signal = if signalbull then 1 else if signalbear then -1 else 0;
def prevSignal = if IsNaN(prevSignal[1]) then signal[1]
else if (signal[1] <> signal) then signal[1]
else prevSignal[1];
def DAPEntry = if signalbull and !signalbull[1] and prevSignal <= 0 then 1
else if signalbear and !signalbear[1] and prevSignal >= 0 then -1
else 0;
# Entry based on mas
input ATRMult = 0.3;
def MAFast = ExpAverage(close, MaFastLength);
#vwap
def vwp = reference VWAP("time frame" = "DAY");
def ATRMod = ATRMult * ATR;
def uptrend = signalbull;
def downtrend = signalbear ;
def PivotEntry = if uptrend and pivothigh then 1
else if downtrend and pivotlow then -1
else 0 ;
def VWPPB = if uptrend and low <= vwp + ATRMod and close > vwp then 1
else if downtrend and high >= vwp - ATRMod and close < vwp then -1
else 0 ;
#significant cross
def sigcross = if MAFast crosses above vwp and uptrend then 1.5
else if MAFast crosses below vwp and downtrend then -1.5
else 0;
#=== combined entry signal
def entry =
if sigcross <> 0 then sigcross
else
if VWPPB > 0 and pivotEntry>0 then 2
else if VWPPB < 0 and pivotEntry<0 then- 2
else if pivotEntry>0 then 1
else if pivotEntry<0 then -1
else 0
;
#======study display
plot pDIPlus = "DI+";
plot pDIminus = "DI-";
pDIPlus.SetDefaultColor(Color.GREEN);
pDIminus.SetDefaultColor(Color.RED);
AddCloud(pDIPlus, pDIminus, Color.UPTICK);
#plot padxcutoff = ADXCutoff;
plot padxcutoff = if !isnan(close) then ADXCutoff else double.nan;
padxcutoff.assignValueColor(if (adx < adxCutoff) or (pDiminus > adxcutoff) or (pDiplus < adxCutoff) then color.light_red else Color.green);
padxcutoff.SetLineWeight(1);
padxcutoff.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
#adx cross plots
#pivots
def prange=2; #pivot range
def adxbull = ADX > lowest(ADX,prange)[1];
def adxbear = ADX < highest(ADX,prange)[1];
ADX.AssignValueColor(if adxbull then Color.cyan else if adxbear then Color.orange else Color.GRAY);
def ptrend=adx;
def adxPH= if isnan(ptrend[-1]) then 0 else lowest(ptrend,prange)[1] > ptrend and lowest(ptrend,prange)[-prange]> ptrend ;
def adxPL= if isnan(ptrend[-1]) then 0 else highest(ptrend,prange)[1] < ptrend and highest(ptrend,prange)[-prange]< ptrend ;
# Trend Direction
def dmisprd = pdiPlus - pdiMinus;
AddLabel(yes, if DMICutBull then "Trend Status: UP | D+: " + round(pdiPlus,2) + " D-: " + round(pdiMinus,2) + " Spread: " + round(dmisprd,2) else "Trend Status: DOWN | D-: " + round(pdiMinus,2) + " D+: " + round(pdiPlus,2) + " Spread: " + round(dmisprd,2), if DMICutBull then color.green else color.red);
# ADX Dynamic Label
# Adds ADX value and context
input ADXLength = 6;
input ADXLower = 25;
input ADXMid = 40;
input ADXHigh = 75;
#input MALength = 10;
input MALength = 9;
def ADXlabel = reference ADX(ADXLength).ADX;
def MA = reference movAvgExponential(close,MALength);
# ADX Label
AddLabel(yes, if ADXlabel < ADXLower then " Strenght: " + Round(ADXlabel,2) + " SETTING UP - Watch/Wait " else if ADXlabel < ADXMid then " Strenght: " + Round(ADXlabel,2) + " CHOPPY - Watch/Wait " else if ADXlabel < ADXlabel[1] then " Strenght: " + Round(ADXlabel,2) + " TREND WEAKENING " else if ADXlabel > ADXlabel[1] and MA > MA[1] and ADXlabel < ADXHigh then " Strenght: " + Round(ADXlabel,2) + " UPTRENDING " else if ADXlabel > ADXlabel[1] and MA < MA[1] then " Strenght: " + Round(ADXlabel,2) + " DOWNTRENDING " else if ADXlabel > ADXHigh then " Strenght: " + Round(ADXlabel,2) + " HIGH/WAIT TO COOL OFF " else " ", if ADXlabel < ADXLower then Color.CYAN else if ADXlabel < ADXMid then Color.RED else if (ADXlabel < ADXlabel[1]) then Color.YELLOW else if ADXlabel > ADXlabel[1] and MA > MA[1] and ADXlabel < ADXHigh then Color.GREEN else if ADXlabel > ADXlabel[1] and MA < MA[1] then Color.RED else if ADXlabel > ADXHigh then Color.MAGENTA else Color.light_gray);
# end of ADX