Here we have a MACD Divergence indicator for ThinkorSwim platform. This one should be able to give you potential signals for regular and hidden MACD divergences.

Here is another indicator that displays MACD divergence on the upper chart.

**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

https://tos.mx/Cbe8LUHere is another indicator that displays MACD divergence on the upper chart.

Code:

```
# MACD Divergence Pivots
# Mobius
# V03.05.2015 Upper Study
input n = 2;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
def h = high;
def l = low;
def bar = barNumber();
def Diff = MACD(fastLength, slowLength, MACDLength, averageType).Diff;
def CurrMACDh = if Diff > 0
then fold i = 1 to n + 1
with p = 1
while p
do Diff > getValue(Diff, -i)
else 0;
def CurrMACDPivotH = if (bar > n and
Diff == highest(Diff, n) and
CurrMACDh)
then h
else double.NaN;
def CurrMACDl = if Diff < 0
then fold j = 1 to n + 1
with q = 1
while q
do Diff < getValue(Diff, -j)
else 0;
def CurrMACDPivotL = if (bar > n and
Diff == lowest(Diff, n) and
CurrMACDl)
then l
else double.NaN;
def CurrPHBar = if !isNaN(CurrMACDPivotH)
then bar
else CurrPHBar[1];
def CurrPLBar = if !isNaN(CurrMACDPivotL)
then bar
else CurrPLBar[1];
def PHpoint = if !isNaN(CurrMACDPivotH)
then CurrMACDPivotH
else PHpoint[1];
def priorPHBar = if PHpoint != PHpoint[1]
then CurrPHBar[1]
else priorPHBar[1];
def PLpoint = if !isNaN(CurrMACDPivotL)
then CurrMACDPivotL
else PLpoint[1];
def priorPLBar = if PLpoint != PLpoint[1]
then CurrPLBar[1]
else priorPLBar[1];
def HighPivots = bar >= highestAll(priorPHBar);
def LowPivots = bar >= highestAll(priorPLBar);
def pivotHigh = if HighPivots
then CurrMACDPivotH
else double.NaN;
plot PlotHline = pivotHigh;
PlotHline.enableApproximation();
PlotHline.SetDefaultColor(GetColor(7));
PlotHline.SetStyle(Curve.Short_DASH);
plot pivotLow = if LowPivots
then CurrMACDPivotL
else double.NaN;
pivotLow.enableApproximation();
pivotLow.SetDefaultColor(GetColor(7));
pivotLow.SetStyle(Curve.Short_DASH);
plot PivotDot = if !isNaN(pivotHigh)
then pivotHigh
else if !isNaN(pivotLow)
then pivotLow
else double.NaN;
pivotDot.SetDefaultColor(GetColor(7));
pivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
pivotDot.SetLineWeight(3);
# End Code Pivots with Projections
```

Last edited by a moderator: