Automatically Show MACD Divergence on ThinkorSwim Upper Study Chart

BenTen

Staff
VIP
This indicator will automatically show MACD Divergence pivots on the upper study of your ThinkorSwim chart. You have the option to pick between Simple Moving Average, Exponential Moving Average, and other moving averages.

thinkScript Code

Rich (BB 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``````

https://tos.mx/XLNFpQ

Related Script

Last edited:
K

KevinSammy

New member
VIP
Hi Ben - I was trying to use this, but it doesn't plot the lines in the upper study. Do you know if it still works? Thank you!

murkr

Member
VIP
@BenTen Are you still using this? I can't get it to plot the lines in the upper study like @KevinSammy was experiencing.

BenTen

Staff
VIP
@murkr I'm not. I've concluded that divergences should be spot manually.

T

timekeeper

New member
@murkr I'm not. I've concluded that divergences should be spot manually.

@BenTen hello. Im seeing what I think are false positives on the MACD_divergence_pivots. Does this code read divergence in Value line as well as Histogram.

Im looking for a divergence indicator for day trading. And notice you commented about manual spotting of divergence. Why do you say that?

B

british43

New member
I'm trying to code Auto Price Divergence Indicator but my low line is not showing on the chart. High line works perfect. Can someone QC the code for me and let me know what I maybe missing. Thank you.

Code:
``````# Price Divergence Pivots

# Mobius

# V03.05.2015 Upper Study

plot price = close;
price.hide();

input n = 9;

def h = high;

def l = low;

def bar = BarNumber();

def CurrPriceh = if price > 0

then fold i = 1 to n + 1

with p = 1

while p

do price > GetValue(price, -i)

else 0;

def CurrPricePivotH = if (bar > n and

price == Highest(price, n) and

CurrPriceh)

then h

else Double.NaN;

def CurrPricel = if price < 0

then fold j = 1 to n + 1

with q = 1

while q

do price < GetValue(price, -j)

else 0;

def CurrPricePivotL = if (bar > n and

price == Lowest(price, n) and

CurrPricel)

then l

else Double.NaN;

def CurrPHBar = if !IsNaN(CurrPricePivotH)

then bar

else CurrPHBar[1];

def CurrPLBar = if !IsNaN(CurrPricePivotL)

then bar

else CurrPLBar[1];

def PHpoint = if !IsNaN(CurrPricePivotH)

then CurrPricePivotH

else PHpoint[1];

def priorPHBar = if PHpoint != PHpoint[1]

then CurrPHBar[1]

else priorPHBar[1];

def PLpoint = if !IsNaN(CurrPricePivotL)

then CurrPricePivotL

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 CurrPricePivotH

else Double.NaN;

plot PlotHline = pivotHigh;

PlotHline.EnableApproximation();

PlotHline.SetDefaultColor(Color.YELLOW);
PlotHline.SetLineWeight(3);

PlotHline.SetStyle(Curve.SHORT_DASH);

plot pivotLow = if LowPivots

then CurrPricePivotL

else Double.NaN;

pivotLow.EnableApproximation();

pivotLow.SetDefaultColor(Color.YELLOW);
pivotLow.SetLineWeight(3);

pivotLow.SetStyle(Curve.SHORT_DASH);

plot PivotDot = if !IsNaN(pivotHigh)

then pivotHigh

else if !IsNaN(pivotLow)

then pivotLow

else Double.NaN;

PivotDot.SetDefaultColor(Color.YELLOW);

PivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);

PivotDot.SetLineWeight(3);

AddChartBubble(pivotLow is true, pivotLow, "BULL ENTRY", Color.GREEN, no);
AddChartBubble(PlotHline is true, PlotHline, "BEAR ENTRY", Color.RED, yes);

AddVerticalLine(pivotLow is true, "BEAR EXIT", Color.RED, Curve.SHORT_DASH );
AddVerticalLine(PlotHline is true, "BULL EXIT", Color.GREEN, Curve.SHORT_DASH );

#Dvg Columns
#def PosDvgPrice = LinearRegressionSlope()."LRS" is less than or equal to 0 and LinearRegressionSlope("price" = Price())."LRS" is greater than 0 and Price()is less than 0;

#def NegDvgPrice = LinearRegressionSlope()."LRS" is greater than or equal to 0 and LinearRegressionSlope("price" = Price())."LRS" is less than 0 and Price()is greater than 0;

#AddLabel(!PosDvgPrice and !NegDvgPrice, " ", color.black);

#AssignBackgroundColor(if PosDvgPrice then Color.GREEN else if NegDvgPrice then Color.RED else Color.BLACK);

# End Code Pivots with Projections``````

New member
Here is a label that I created that shows the \$TICK, MACD, CCI, STOCH as a label on the top right of chart

Code:
``````input TickSymb = "\$TICK";

def TICKS = if !IsNaN(close(TickSymb)) then close(TickSymb) else TICKS[1];

AddLabel(1, " Tick = " + TICKS, if TICKS < -1000 then Color. RED
else if TICKS > 1000 then Color.DARK_GREEN
else if Between(TICKS, -999, 999) then Color.GRAY
else Color.YELLOW);

input Dec_Places = numberformat.TWO_DECIMAL_PLACES;

input C_Length = 20;
input C_Over_Bought = 100;
input C_Over_Sold = -100;

input M_Fast_Length = 12;
input M_Slow_Length = 26;
input M_MACD_Length = 9;
input M_AverageType = AverageType.EXPONENTIAL;

input S_over_bought = 80;
input S_over_sold = 20;
input S_KPeriod = 21;
input S_DPeriod = 3;
input S_priceH = high;
input S_priceL = low;
input S_priceC = close;
input S_averageType = AverageType.SIMPLE;

def PBCCI = reference CCI(c_length,c_over_bought,c_over_sold);
def PBMCD = reference MACD(M_Fast_Length, M_Slow_Length, M_MACD_Length, M_AverageType).diff;
def PBSLST = reference Stochasticslow(S_over_bought, S_over_sold, S_KPeriod, S_DPeriod, S_priceH, S_priceL, S_priceC,s_averageType);

AddLabel (yes, "C: " + astext(PBCCI,dec_Places), if PBCCI > C_Over_Sold then Color.UPTICK else if PBCCI < C_Over_Bought then Color.DOWNTICK else Color.GRAY);

AddLabel (yes, "M: " + astext(PBMCD,dec_Places), if PBMCD > 0 then Color.UPTICK else if PBMCD < 0 then Color.DOWNTICK else Color.GRAY);

AddLabel(yes, "S: " + astext(PBSLST,dec_Places), if PBSLST between S_over_sold and S_over_bought then Color.GRAY else Color.UPTICK);``````