Hidden MACD Divergence Indicator for ThinkorSwim (Bullish / Bearish Signals)

BenTen

BenTen

Administrative
Staff
VIP
Here we have a MACD Divergence indicator for ThinkorSwim platform. The previous indicator that was shared only showed the regular types. This one should be able to give you potential signals for regular and hidden MACD divergences.



Full notes from the author:

The MACD Histogram divergence as per Alexander Elder's definition was tricky to code (also your reference links were not working so I had to search and read his book for the definition) and it took me many days but I finally solved it! To my knowledge, no one has coded the MACD Histogram divergence per Elder's definition in thinkScript before. The following script will plot large red arrows at potential regular bearish divergence, large green arrows at potential regular bullish divergence, small pink arrows at potential hidden bearish divergence and small lime arrows at potential hidden bullish divergence. The code will also alert you if arrow is plotted.

The reason I am using the word "potential" is because the code compares the high or low with the previous swing high or swing low to determine divergence (instead of comparing two swing highs or two swing lows), in order to get a fast signal before divergence is complete. Once you get the arrow(s) then wait for the MACD Histogram bar to become smaller for entry signal. If after the arrows are plotted, the histogram bars keep getting bigger to the point there is no divergence, then it is a no go.

I could have coded for divergence between two swing highs or two swing lows once the swing points have been determined, but it takes few smaller bars after the highest or lowest bar to determine swing high or swing low point, so plotting of the arrow will be delayed by few bars and by then price might already have moved a lot and signal might not be as useful. My code does not lag and arrow is plotted immediately if there is a potential divergence. You can change the number of bars used to determine the previous swing high or swing low in "edit properties" (default is 2).

This code is an upper study only and will not plot the MACD Histogram. Use the standard MACD Histogram in ToS as the lower study. In the Auto versus Manual pane in top right corner, make sure to uncheck all boxes except "fit study markers". Switching extended hours on or off will make a difference in divergences.


thinkScript Code

Rich (BB code):
input bar = 2;

input fastLength = 12;

input slowLength = 26;

input MACDLength = 9;

input averageType = AverageType.EXPONENTIAL;

plot Diff = MACD(fastLength, slowLength, MACDLength, averageType).Diff;

def SwingHigh = Diff > 0 and Diff >= highest(Diff[1], bar) and Diff >= highest(Diff[-bar], bar);

def SHprice = if SwingHigh then Diff else SHprice[1];

def SHBar = if SwingHigh then BarNumber() else SHBar[1];

def CrossBarL = if Diff crosses below 0 then BarNumber() else CrossBarL[1];

def SwingLow = Diff < 0 and Diff <= lowest(Diff[1], bar) and Diff <= lowest(Diff[-bar], bar);

def SLprice = if SwingLow then Diff else SLprice[1];

def SLBar = if SwingLow then BarNumber() else SLBar[1];

def CrossBarH = if Diff crosses above 0 then BarNumber() else CrossBarH[1];

def SHSP = if SwingHigh then high else SHSP[1];

def SLSP = if SwingLow then low else SLSP[1];

def BearDiv = Diff > 0 and CrossBarL[1] > SHBar[1] and Diff < SHprice[1] and high > SHSP[1] and SHprice[1] - Diff > 0.005;

def BullDiv = Diff < 0 and CrossBarH[1] > SLBar[1] and Diff > SLprice[1] and low < SLSP[1] and Diff - SLprice[1] > 0.005;

def HiddenBearDiv = Diff > 0 and Diff > SHprice[1] and high < SHSP[1] and Diff - SHprice[1] > 0.005;

def HiddenBullDiv = Diff < 0 and Diff < SLprice[1] and low > SLSP[1] and SLprice[1] - Diff > 0.005;

plot BearD = if BearDiv then high else Double.NaN;

        BearD.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

        BearD.AssignValueColor(Color.RED);

        BearD.SetLineWeight(3);

plot BullD = if BullDiv then low else Double.NaN;

        BullD.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

        BullD.AssignValueColor(Color.UPTICK);

        BullD.SetLineWeight(3);

plot HiddenBearD = if HiddenBearDiv then high else Double.NaN;

        HiddenBearD.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

        HiddenBearD.AssignValueColor(Color.PINK);

        HiddenBearD.SetLineWeight(1);

plot HiddenBullD = if HiddenBullDiv then low else Double.NaN;

        HiddenBullD.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

        HiddenBullD.AssignValueColor(Color.LIME);

        HiddenBullD.SetLineWeight(1);

Alert(BearDiv[1], "Short MACD divergence", Alert.BAR, Sound.Ring);

Alert(BullDiv[1], "Long MACD divergence", Alert.BAR, Sound.Ring);

Alert(HiddenBearDiv[1], "Short hidden MACD divergence", Alert.BAR, Sound.Ring);

Alert(HiddenBullDiv[1], "Long hidden MACD divergence", Alert.BAR, Sound.Ring);

Shareable Link

 
Last edited:
Ace_Trader

Ace_Trader

New member
VIP
VERY NICE BEN...!! I look forward to back-testing it..!! :) Thank you for this.

 
Last edited:
BenTen

BenTen

Administrative
Staff
VIP
@Ace_Trader Try the older version. I'm not liking this one that much. Do know that the divergence will be delayed by 2 bars due to confirmation.

 
Last edited:
N

netarchitech

New member
Just in case anyone wants to go directly to the original author's post, here's the link...

@BenTen Does the original author's post contain the older version you mentioned?

 
Last edited:
BenTen

BenTen

Administrative
Staff
VIP
@netarchitech I also linked the original post above. I have backtested both versions and I'm liking the previous divergence more.

 
Last edited:
N

netarchitech

New member
@BenTen Sounds good! Thanks for the linking and backtesting...

Great site you've got here! The Thinkscript Community really needed a new destination to gather in...

Keep up the great work :D

 
Last edited:
BenTen

BenTen

Administrative
Staff
VIP
@netarchitech Thank you &#128512;

 
Last edited:
K

kbtarl

New member
Hi Guys. I am new here but am just flabber gasted buy the great work you have done. I know its been mentioned before but what do I do if I load the indicator ( like this one for MACD Divergence) and it squeezes the chart?

 
Last edited:
K

kbtarl

New member
I read further and know about unchecking in the auto/manual tab all but fit study markers. THANKS! I will try to calm my excitement down.

 
Last edited:
BenTen

BenTen

Administrative
Staff
VIP
@kbtarl Welcome. Glad you were able to fix it.
 
Last edited:
BenTen

BenTen

Administrative
Staff
VIP
Here's another version of MACD divergence, shared by @john3

Code:
# MACDDIVERGENCE_R1V1
# R1V1   2011.05.14:22:15 - TONY LIPTON
#        monitors macd divergenge for entry

#        MAJOR CODE CONTRIBUTION by KumoBob aka Bob Campbell
#        http://fibonacci-financial.blogspot.com/

INPUT VFL = 12;
INPUT VSL = 26;

DEF VLB = VFL + VSL;

DEF vh34 = highest(MACDHistogram(VFL,VSL),VLB);
DEF vl34 = lowest(MACDHistogram(VFL,VSL),VLB);

input ChartBubblesON = NO;
input LinesON = Yes;

rec top;
rec bot;

top = if
MACDHistogram(VFL,VSL) == VH34
then High  else top[1];
bot = if
MACDHistogram(VFL,VSL) == VL34
then Low  else bot[1];

plot topline = if !LinesOn then Double.NaN else top;
topline.SetLineWeight(1);
topline.assignValueColor(if ((top < HIGH)) then color.DOWNTICK  else color.gray);
topline.SetPaintingStrategy(PaintingStrategy.points);
plot bottomline =  if !LinesOn then Double.NaN else bot;
bottomline.SetLineWeight(1);
bottomline.assignValueColor(if ((LOW < bot)) then color.UPTICK else color.gray);
bottomline.SetPaintingStrategy(PaintingStrategy.dashes);
# _____________________________________________________________
# Thinkscript is the property of ThinkorSwim and TDAmeritrade
# Sections of this script may have been copied or modified from
# one or more Thinkscript studies in part or in their entirety.
# _____________________________________________________________
 
E

ester

New member
Wow thank you so much! I was having trouble finding the true MACD divergence on my own, but your code blew my mind 🤯
 

Top