Below is my TradingView conversion on Zero lag MACD.
Code:
#// ENHANCED ZERO LAG MACD
#// Version 1.2
#// Based on ZeroLag EMA - see Technical Analysis of Stocks and Commodities, April 2000
#// Original version by user Glaz. Thanks ! [URL]https://www.tradingview.com/chart/EURUSD/UV0YI6Wy-ZeroLag-Macd[/URL]
#// Ideas and code from @yassotreyo version.
#// Tweaked by Albert Callisto (AC)
#[URL]https://fr.tradingview.com/script/kfFN7M7u/[/URL]
#// (AC - 1.0) Histogram with two colors, choice between SMA/EMA (SMA = "Glaz mode"), names for sub-components, renaming of variables
#// (AC - 1.1) Added choice between "Glaz" and legacy algorithm + introduced EMA on MACD (thanks @yassotreyo for your original version)
#// (AC - 1.2) Added option to show dots above (requested by another user)
# converted by SAM4COK 07/2022 - Not exact conversion
declare lower;
input source = close;
input fastLength = 12; #"Fast MM period"
input slowLength = 26; #"Slow MM period"
input signalLength = 9; #"Signal MM period"
input MacdEmaLength = 9; #"MACD EMA period"
input useEma = yes; #"Use EMA (otherwise SMA)"
input useOldAlgo = no; #"Use Glaz algo (otherwise 'real' original zero lag)"
input showDots = yes; #"Show symbols to indicate crossing"
input showEMALine = no; #"Show EMA MACD Line"
input ShowCloud = yes; #"Show Cloud"
input ShowHist = yes; #"Show Hist"
def na = Double.NaN;
##### Color ###
DefineGlobalColor("UP" , CreateColor(100, 181, 246));
DefineGlobalColor("DOWN" , CreateColor(160,32,240));
DefineGlobalColor("EMAMACD", CreateColor(255,235,59));
#// Fast line
def ma1 = if useEma then ExpAverage(source, fastLength) else SimpleMovingAvg(source, fastLength);
def ma2 = if useEma then ExpAverage(ma1,fastLength) else SimpleMovingAvg(ma1,fastLength);
def zerolagEMA = ((2 * ma1) - ma2);
#// Slow line
def mas1 = if useEma then ExpAverage(source , slowLength) else SimpleMovingAvg(source , slowLength);
def mas2 = if useEma then ExpAverage(mas1 , slowLength) else SimpleMovingAvg(mas1 , slowLength);
def zerolagslowMA = ((2 * mas1) - mas2);
#// MACD line
def ZeroLagMACD = zerolagEMA - zerolagslowMA;
#// Signal line
def emasig1 = ExpAverage(ZeroLagMACD, signalLength);
def emasig2 = ExpAverage(emasig1, signalLength);
def signal = if useOldAlgo then SimpleMovingAvg(ZeroLagMACD, signalLength) else (2 * emasig1) - emasig2;
def hist = ZeroLagMACD - signal;
def upHist = if (hist > 0) then hist else 0;
def downHist = if (hist <= 0) then hist else 0;
#// Plot Lines
plot EMALine = if showEMALine then ExpAverage(ZeroLagMACD,MacdEmaLength) else na; # 'EMA on MACD line'
EMALine.SetPaintingStrategy(PaintingStrategy.LINE);
EMALine.SetDefaultColor(GlobalColor("EMAMACD"));
EMALine.SetLineWeight(1);
plot ZeroLagLine = if !ShowCloud then ZeroLagMACD else na; #, color=black, transp=0, linewidth=2, title='MACD line')
ZeroLagLine.SetPaintingStrategy(PaintingStrategy.LINE);
ZeroLagLine.SetDefaultColor(color.GREEN);
ZeroLagLine.SetLineWeight(1);
plot signalLine = if !ShowCloud then signal else na; #, color=gray, transp=0, linewidth=2, title='Signal')
signalLine.SetPaintingStrategy(PaintingStrategy.LINE);
signalLine.SetDefaultColor(color.RED);
signalLine.SetLineWeight(1);
#// Cloud
addcloud (if ShowCloud then ZeroLagMACD else na, signal, color.GREEN,color.RED, no);
#// Plot Hist
plot ZeroLine = 0;
ZeroLine.SetPaintingStrategy(PaintingStrategy.LINE);
ZeroLine.AssignValueColor(if upHist then GlobalColor("UP") else GlobalColor("DOWN"));
ZeroLine.SetLineWeight(1);
plot p1 = if ShowHist then upHist * 2 else na;
p1.SetDefaultColor(COLOR.GRAY);
p1.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
p1.SetLineWeight(3);
plot p2 = if ShowHist then downHist * 2 else na;
p2.SetDefaultColor(GlobalColor("DOWN"));
p2.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
p2.SetLineWeight(3);
#// Plot DOTS
plot DotUP = if showDots and ZeroLagMACD crosses above signal then ZeroLagMACD else na;
plot DotDN = if showDots and ZeroLagMACD crosses below signal then ZeroLagMACD else na;
DotUP.SetPaintingStrategy(PaintingStrategy.POINTS);
DotUP.SetDefaultColor(color.GREEN);
DotUP.SetLineWeight(3);
DotDN.SetPaintingStrategy(PaintingStrategy.POINTS);
DotDN.SetDefaultColor(Color.RED);
DotDN.SetLineWeight(3);
Last edited by a moderator: