Enhanced Zero Lag MACD Indicator for ThinkorSwim

samer800

Moderator - Expert
VIP
Lifetime
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:
By far, my favorite substudy. I would love to use this on ThinkorSwim but I've no idea how to convert the script to TOS. Here are the details below. This code is for the Zero Lag MACD indicator from TradingView.

Rich (BB code):
// ENHANCED ZERO LAG MACD
// Version 1.1
//
// Based on ZeroLag EMA - see Technical Analysis of Stocks and Commodities, April 2000
// Original version by user Glaz. Thanks ! https://www.tradingview.com/chart/EURUSD/UV0YI6Wy-ZeroLag-Macd
// Ideas and code from @yassotreyo version.
// Tweaked by Albert Callisto (AC)
//
// Last Update 20.11.2016
// (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)

study(title="Zero Lag MACD Enhanced - Version 1.1", shorttitle="Zero Lag MACD Enhanced 1.1")
source = close

fastLength = input(12, minval=1)
slowLength = input(26, minval=1)
signalLength =input(9, minval=1)
MacdEmaLength =input(9, minval=1)
useEma = input(true, title="Use EMA (otherwise SMA)")
useOldAlgo = input(false, title="Use Glaz algo (otherwise 'real' original zero lag)")

// Fast line
ma1= useEma ? ema(source, fastLength) : sma(source, fastLength)
ma2 = useEma ?  ema(ma1,fastLength) :  sma(ma1,fastLength)
zerolagEMA = ((2 * ma1) - ma2)

// Slow line
mas1=  useEma ? ema(source , slowLength) :  sma(source , slowLength)
mas2 =  useEma ? ema(mas1 , slowLength): sma(mas1 , slowLength)
zerolagslowMA = ((2 * mas1) - mas2)

// MACD line
ZeroLagMACD = zerolagEMA - zerolagslowMA

// Signal line
emasig1 = ema(ZeroLagMACD, signalLength)
emasig2 = ema(emasig1, signalLength)
signal = useOldAlgo ? sma(ZeroLagMACD, signalLength) : (2 * emasig1) - emasig2

hist = ZeroLagMACD - signal

upHist = (hist > 0) ? hist : 0
downHist = (hist <= 0) ? hist : 0

p1 = plot(upHist, color=green, transp=40, style=columns, title='Positive delta')
p2 = plot(downHist, color=purple, transp=40, style=columns, title='Negative delta')

zeroLine = plot(ZeroLagMACD, color=black, transp=0, linewidth=2, title='MACD line')
signaLine = plot(signal, color=gray, transp=0, linewidth=2, title='Signal')

ribbonDiff = hist > 0 ? green : purple
fill(zeroLine, signaLine, color=ribbonDiff)

plot(ema(ZeroLagMACD,MacdEmaLength) , color=red, transp=0, linewidth=2, title='EMA on MACD line')

Where you ever able to convert this to thinkorswim?
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
565 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top