MACD Crossover Alert Indicator for ThinkorSwim

dmillz

Member
VIP
Below is the original MACD indicator with a few adjustments such as the crossover colors, alerts function to notify you of bullish and bearish MACD crossovers.

O5dT4kv.png


Code:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2020
#

declare lower;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);


Value.AssignValueColor(if value< avg

                     then color.cyan

                     else if avg> value

                     then color.yellow

                     else createColor(0, 0, 204));

# Alerts
Alert(UpSignal, "Bullish MACD Alert", Alert.Bar, Sound.Chimes);
Alert(DownSignal, "Bearish MACD Alert", Alert.Bar, Sound.Bell);

Bubble Containing Date And Price Of MACD Crossover (credit _ZZZ)

Code:
# BubbleContainingDateAndPriceOfMACD_Crossover_ZZZ
#


input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;

def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);

def Diff = Value - Avg;
def ZeroLine = 0;
def crossover = diff crosses 0;
addchartBubble(crossover,close * 1.05,asprice(getYYYYMMDD())+"\n"+asDollars(close),color.white);
 
Last edited by a moderator:
Awesome bro, Can you suggest me an indicator script that would alert me when the MACD line crosses the Signal line. Would be even better if the alert is displayed as text on the chart!
 

BenTen

Administrative
Staff
VIP
MACD Histogram Crossover

Rich (BB code):
#MACDHistogramCrossover_Paris
#Chatroom Request
#8.30.2018
#Displays number of bars since the last MACDHistogram Crossover signal
#Added logic to display direction of last crossover signal

input ShowEducationLabels = yes;  added by JQ for education purposes

input fastLength = 12;

input slowLength = 26;

input MACDLength = 9;

input averageType = AverageType.EXPONENTIAL;

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

def XUp;

def XDn;

if Crosses(Diff, 0, CrossingDirection.BELOW) {

    XUp = no;

    XDn = yes;
}


else if (Crosses(Diff, 0, CrossingDirection.ABOVE)) {

    XUp = yes;

    XDn = no;

}

else {

    XUp = XUp[1];

    XDn = XDn[1];

}

def crossBarUp = if XUp and !XUp[1] then BarNumber() else crossBarUp[1];

def crossBarDn = if XDn and !XDn[1] then BarNumber() else crossBarDn[1];

addlabel(yes,"crossBarUp: " + crossBarUp, color.lime);  added by JQ for education

addlabel(yes,"crossBarDn: " + crossBarDn, color.lime);  added by JQ for education

addlabel(yes,"BarNumber(): " + BarNumber(), color.lime);  added by JQ for education

plot bullSig = if XUp and !XUp[1] then low else Double.NaN;

     bullSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

     bullSig.SetDefaultColor(Color.YELLOW);

     bullSig.SetLineWeight(3);

plot bearSig = if XDn and !XDn[1] then high else Double.NaN;

     bearSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

     bearSig.SetDefaultColor(Color.CYAN);

     bearSig.SetLineWeight(3);

AddLabel(1, "Bars since last MACD Histogram " +

    (if crossBarUp > crossBarDn then "BULLISH"

                                else "BEARISH") + " Crossover:  " +

    (if crossBarUp > crossBarDn then BarNumber() - crossBarUp

                                else BarNumber() - crossBarDn),

    if crossBarUp > crossBarDn then Color.YELLOW else Color.CYAN);

# END STUDY

RvcOxEh.png


A really simple indicator which plot MACD breakout signals directly on your upper chart.

MACD Signals on the Upper Chart

Rich (BB code):
MACD Signals Upper Chart
Mobius
Chat Room Request 03.03.2016
input fastLength = 8;
input slowLength = 21;
input MACDLength = 5;
input averageType = AverageType.EXPONENTIAL;

def Value = MACD(fastLength, slowLength, MACDLength, averageType).Value;
def Avg = MACD(fastLength, slowLength, MACDLength, averageType).Avg;


plot upArrow = if Value < 0 and
                  Value crosses above Avg
                then low
                else double.nan;
upArrow.SetPaintingStrategy(paintingStrategy.Arrow_UP);
upArrow.SetDefaultColor(Color.Green);
upArrow.SetLineWeight(3);


plot dnArrow = if Value > 0 and
                  Value crosses below Avg
                then high
                else double.nan;
dnArrow.SetPaintingStrategy(paintingStrategy.Arrow_DOWN);
dnArrow.SetDefaultColor(Color.Red);
dnArrow.SetLineWeight(3);

Alert(upArrow, "UP", Alert.Bar, Sound.Chimes);
Alert(dnArrow, "DN", Alert.Bar, Sound.Ring);
 
Last edited:

BenTen

Administrative
Staff
VIP
@FOTM_8888 Here is the modified version of the default MACD backtesting strategy in ToS. I added the SMA above 200 as a condition as the video instructed.

This is for long trades only.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2013-2020
#

input price = close;
input length = 200;
input displace = 0;
def SMA = Average(price[-displace], length);
def cond = close > SMA;

input fastLength = 12;
input slowLength = 26;
input macdLength = 9;
input averageType = AverageType.EXPONENTIAL;

def diff = reference MACD(fastLength, slowLength, macdLength, averageType).Diff;

AddOrder(OrderType.BUY_TO_OPEN, diff crosses above 0 and cond, tickColor = GetColor(0), arrowColor = GetColor(0), name = "MACDStratLE");
AddOrder(OrderType.SELL_TO_CLOSE, diff crosses below 0, tickColor = GetColor(1), arrowColor = GetColor(1), name = "MACDStratSE");
 

drkellog

New member
VIP
I am trying to make a STUDY that will ring a bell when the RSI is below 30 and the MACD is above the zero line. Can anyone help me fix my code so it will RING. Its not ringing. A bonus would be if It could somehow plot it also but not necessary.

Here is my code...

Code:
def Bearish = RSI("length" = 14, "over bought" = 70, "over sold" = 30)."RSI" is greater than or equal to 70 and MACD()."Value" is less than 0;
Alert(Bearish, " RSI Possible SIGNAL DOWN ", Alert.BAR, Sound.Ding);

def Bullish =
RSI("length" = 14, "over bought" = 70, "over sold" = 30)."RSI" is less than or equal to 30 and MACD
()."Value" is greater than 0;
Alert(Bullish, " RSI Possible SIGNAL DOWN ", Alert.BAR, Sound.Ding);
 
Last edited by a moderator:

mkdear

New member
VIP
Hi @BenTen

For some reason above doesn't show any indicators on my charts.

Meanwhile, I have a request. Can someone provide the code with following signals?
  • StrongBull: MACD crosses above and SignalLine and crossover is bellow zero line
  • WeakBull: MACD crosses above and SignalLine and crossover is above zero line
  • StrongBear: MACD crosses bellow SignalLine and crossover is above zero line
  • WeakBear: MACD crosses bellow SignalLine and crossover is bellow zero line

Ci8aWsm.png
 

mkdear

New member
VIP
Right, I was using it for backtesting. I think I understood the issue, for the interval I have selected MACD crossover happened bellow 200EMA hence it was not showing. Thanks!
 

JP382

New member
I installed this script and ran it via "OnDemand" but the alerts (chimes) did not go off nor was there a msg in "Messages" (if I read correctly in the edit cog) when the lines crossed

The color changes (dark blue to light blue) which is cool when it goes from bullish to bearish

What should I do?

Ran this live during market hours, it did not send alerts of any kind
 
Last edited:

mark0482

New member
VIP
Hello guys

Id like to add lines to hear ring always when MACD shows breakout

original's TOS formula for MACD:
Code:
#

# TD Ameritrade IP Company, Inc. (c) 2007-2021

#



declare lower;



input fastLength = 12;

input slowLength = 26;

input MACDLength = 9;

input averageType = AverageType.EXPONENTIAL;

input showBreakoutSignals = no;



plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);

plot Avg = MovingAverage(averageType, Value, MACDLength);



plot Diff = Value - Avg;

plot ZeroLine = 0;



plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;

plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;



UpSignal.SetHiding(!showBreakoutSignals);

DownSignal.SetHiding(!showBreakoutSignals);



Value.SetDefaultColor(GetColor(1));

Avg.SetDefaultColor(GetColor(8));

Diff.SetDefaultColor(GetColor(5));

Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

Diff.SetLineWeight(3);

Diff.DefineColor("Positive and Up", Color.GREEN);

Diff.DefineColor("Positive and Down", Color.DARK_GREEN);

Diff.DefineColor("Negative and Down", Color.RED);

Diff.DefineColor("Negative and Up", Color.DARK_RED);

Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));

ZeroLine.SetDefaultColor(GetColor(0));

UpSignal.SetDefaultColor(Color.UPTICK);

UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

DownSignal.SetDefaultColor(Color.DOWNTICK);

DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);


I've tried with some lines but it doesnt work.

Could you help ?
Best regards!
 

JP382

New member
Hello guys

Id like to add lines to hear ring always when MACD shows breakout

original's TOS formula for MACD:
Code:
#

# TD Ameritrade IP Company, Inc. (c) 2007-2021

#



declare lower;



input fastLength = 12;

input slowLength = 26;

input MACDLength = 9;

input averageType = AverageType.EXPONENTIAL;

input showBreakoutSignals = no;



plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);

plot Avg = MovingAverage(averageType, Value, MACDLength);



plot Diff = Value - Avg;

plot ZeroLine = 0;



plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;

plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;



UpSignal.SetHiding(!showBreakoutSignals);

DownSignal.SetHiding(!showBreakoutSignals);



Value.SetDefaultColor(GetColor(1));

Avg.SetDefaultColor(GetColor(8));

Diff.SetDefaultColor(GetColor(5));

Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

Diff.SetLineWeight(3);

Diff.DefineColor("Positive and Up", Color.GREEN);

Diff.DefineColor("Positive and Down", Color.DARK_GREEN);

Diff.DefineColor("Negative and Down", Color.RED);

Diff.DefineColor("Negative and Up", Color.DARK_RED);

Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));

ZeroLine.SetDefaultColor(GetColor(0));

UpSignal.SetDefaultColor(Color.UPTICK);

UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

DownSignal.SetDefaultColor(Color.DOWNTICK);

DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);


I've tried with some lines but it doesnt work.

Could you help ?
Best regards!
I think you and I are the only ones on this thread anymore... I can't get it to work and it sucks bc Ive missed cpl good opportunities
I really need to find something that sets off an alert when these cross
 

rad14733

Well-known member
VIP
I'm still reading every post so you guys aren't totally alone...

@mark0482 Have you gotten the alerts to work yet...??? By now you should have managed to get to the Learning Center and researched the Alert() function... With the basic MACD code you posted, it's just a matter of adding the Alerts based on UpSignal and DownSignal... Give it a go and if you are still having issues then we'll go from there... I'm not simply going to hand you the two lines of code because you can learn this very easily without much effort... ;)
 

JP382

New member
I'm still reading every post so you guys aren't totally alone...

@mark0482 Have you gotten the alerts to work yet...??? By now you should have managed to get to the Learning Center and researched the Alert() function... With the basic MACD code you posted, it's just a matter of adding the Alerts based on UpSignal and DownSignal... Give it a go and if you are still having issues then we'll go from there... I'm not simply going to hand you the two lines of code because you can learn this very easily without much effort... ;)
Nice to know but Im no programmer
I was hoping someone would know why this indicator isnt working for me... and Im also looking for another (unrelated to MACD)but not having any luck there either
 

rad14733

Well-known member
VIP
@JP382 It doesn't sound as though you've made any effort... We're not here to do everyone's coding or them, we are here to guide members along... If you're not willing to make an effort then perhaps you should be satisfied with the freely available indicators already posted here in the forums or elsewhere on the internet... These forums are intended for members to share their creations and to help members learn, not just a place where those who can code to do all of the work for those unwilling to make an effort... Your request is very simple and even you should be able to figure out how to add two lines of code based on how other indicators go about the same basic functionality...

Yes, it took me longer to write this than to hand over the code, but I'm attempting to prove a point to the overall membership here, not just to you... The whole being expected to just hand out code upon request has gotten terribly out of hand and change has to start somewhere... 💡
 

JP382

New member
@JP382 It doesn't sound as though you've made any effort... We're not here to do everyone's coding or them, we are here to guide members along... If you're not willing to make an effort then perhaps you should be satisfied with the freely available indicators already posted here in the forums or elsewhere on the internet... These forums are intended for members to share their creations and to help members learn, not just a place where those who can code to do all of the work for those unwilling to make an effort... Your request is very simple and even you should be able to figure out how to add two lines of code based on how other indicators go about the same basic functionality...

Yes, it took me longer to write this than to hand over the code, but I'm attempting to prove a point to the overall membership here, not just to you... The whole being expected to just hand out code upon request has gotten terribly out of hand and change has to start somewhere... 💡
Sorry I was not aware coding knowledge/expertise is required to be on this board
 

rad14733

Well-known member
VIP
Sorry I was not aware coding knowledge/expertise is required to be on this board

I will leave this subject with the following... If members aren't willing to learn Thinkscript then they should be content with the code that has been contributed, freely, by members who do know how to code, rather than simply expecting others to toil away for them...
 

JP382

New member
Never said any of that, and that just goes to show the arrogance of some ppl assuming they know everything.

FTR I added the code, tried to see whats wrong, but bc IM NOT AN EXPERT I DONT KNOW WHATS WRONG!

DFWM
 

Similar threads

Top