MACD/ADX-DMI Combo

MakePlays

New member
Code:
declare lower;
input MACDfastLen = 10;
input MACDslowLen = 30;
input MACDLen = 10;
input showADX_DMI = { "No", default "Yes"};
input showMACD = { "No", default "Yes"};
input invertNegMACD = { "Yes", default "No"};
input MACDHeight = 50;
input MACDWidth = 3;
input ADX_Avg = 3;
input DMI_Len = 9;

def fastAvg = EMA2(data = close, "smoothing factor" = 2 / (1 + MACDfastLen));
def slowAvg = EMA2(data = close, "smoothing factor" = 2 / (1 + MACDslowLen));
def Value = fastAvg - slowAvg;
def nextAvg = ExpAverage(data = Value, MACDLen);
def HistoBar = Value - nextAvg[1];
def HiScale = HighestAll(HistoBar);
def LoScale = AbsValue(LowestAll(HistoBar));
def BarScale = if HiScale > LoScale then HiScale else LoScale;

plot macd_plot = If (showMACD, If( invertNegMACD, If ( HistoBar < 0, ( -1 * HistoBar * MACDHeight / BarScale ), ( HistoBar * MACDHeight / BarScale )), HistoBar * MACDHeight / BarScale), Double.NaN);

macd_plot.AssignValueColor(if invertNegMACD then if HistoBar >= 0 then Color.cyan else Color.mAGENTA else Color.CYAN);
macd_plot.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
macd_plot.SetLineWeight(MACDWidth);

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 = WildersAverage(TrueRange(high, close, low), DMI_Len);
plot "DI+" =
if showADX_DMI then 100 * WildersAverage(plusDM, DMI_Len) / ATR
else Double.NaN;
plot "DI-" =
if showADX_DMI then 100 * WildersAverage(minusDM, DMI_Len) / ATR
else Double.NaN;
def DX =
if ("DI+" + "DI-" > 0) then 100 * AbsValue("DI+" - "DI-") / ("DI+" + "DI-")
else 0;
plot ADX = if showADX_DMI then WildersAverage(DX, DMI_Len) else Double.NaN;
plot ADXAvg = if showADX_DMI then ExpAverage(ADX, ADX_Avg) else Double.NaN;

"DI+".SetDefaultColor(Color.GREEN);
"DI-".SetDefaultColor(Color.RED);
ADX.SetDefaultColor(Color.WHITE);
ADXAvg.SetDefaultColor(Color.YELLOW);
 
Last edited by a moderator:
Top