MACD Format, Triggers, Scan, Label, Watchlist For ThinkOrSwim

@raymasa Actually, I was bored and curiosity got the best of me so I coded a replica based off the standard TOS MACD indicator... I never checked to see whether a comparable script might already be posted here in the forums... It didn't take long considering how my initial reply was only about 1H 15m ago, and I did a few other things along the way... Just goes to show how easy code can be tweaked with very little effort...

Ruby:
# MACD_with_Signals_and_ColorBars
# Adapted from Tradingview MACD_with_Signals
# https://www.tradingview.com/script/ETBoNc7O-MACD-with-Signals/
# Based on TOS MACD with additional features
# TD Ameritrade IP Company, Inc. (c) 2007-2021
# Created by rad14733 for usethinkscript.com
# v1.0 : 2021-02-25 : Initial release

declare lower;

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

plot Diff = MACD(fastLength, slowLength, MACDLength, averageType).Diff;
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.BLUE);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.YELLOW);
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"));

plot UpSignal = if Diff crosses above 0 then 0 else Double.NaN;
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UpSignal.SetLineWeight(2);
UpSignal.SetHiding(!showBreakoutSignals);

plot DownSignal = if Diff crosses below 0 then 0 else Double.NaN;
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DownSignal.SetLineWeight(2);
DownSignal.SetHiding(!showBreakoutSignals);

plot Value = MACD(fastLength, slowLength, MACDLength, averageType).Value;
Value.SetDefaultColor(Color.CYAN);
Value.DefineColor("UpTrend", Color.CYAN);
Value.DefineColor("DownTrend", Color.MAGENTA);
Value.AssignValueColor(if Value > Value[1] then Value.color("UpTrend") else if Value < Value[1] then Value.color("DownTrend") else Color.CURRENT);
Value.SetLineWeight(2);

plot Avg = MACD(fastLength, slowLength, MACDLength, averageType).Avg;
Avg.SetDefaultColor(Color.DARK_ORANGE);
Avg.SetLineWeight(1);

plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.WHITE);
ZeroLine.SetLineWeight(1);

AssignPriceColor(if colorChartBars then 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") else Color.CURRENT);

# END - MACD_with_Signals_and_ColorBars

pS18n1I.png
 
Last edited:

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

@raymasa Have you searched the forums...??? There are more than a few variations of MACD indicators here in the forums... The same basic layout can be created by combining several standard TOS indicators... We don't generally just do conversions based on one members request, and especially not for a member with only three posts... There needs to be some greater interest... I quickly pieced something quite similar together by stacking several existing indicators in one lower section... What you see below took me less than 2 minutes to accomplish... Very little extra work would be required to make it exactly the same...

Edited to add: It was actually simpler than the original version... Only 2 indicators instead of the original 4...

oby17a7.png


hxwnv8v.png
I searched, but I missed it, still trying to find my way around this great site...:). Thank you for the studies, I will try these out.
 
@raymasa Actually, I was bored and curiosity got the best of me so I coded a replica based off the standard TOS MACD indicator... I never checked to see whether a comparable script might already be posted here in the forums... It didn't take long considering how my initial reply was only about 1H 15m ago, and I did a few other things along the way... Just goes to show how easy code can be tweaked with very little effort...

Ruby:
# MACD_with_Signals_and_ColorBars
# Adapted from Tradingview MACD_with_Signals
# https://www.tradingview.com/script/ETBoNc7O-MACD-with-Signals/
# Based on TOS MACD with additional features
# TD Ameritrade IP Company, Inc. (c) 2007-2021
# Created by rad14733 for usethinkscript.com
# v1.0 : 2021-02-25 : Initial release

declare lower;

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

plot Diff = MACD(fastLength, slowLength, MACDLength, averageType).Diff;
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.BLUE);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.YELLOW);
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"));

plot UpSignal = if Diff crosses above 0 then 0 else Double.NaN;
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UpSignal.SetLineWeight(2);
UpSignal.SetHiding(!showBreakoutSignals);

plot DownSignal = if Diff crosses below 0 then 0 else Double.NaN;
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DownSignal.SetLineWeight(2);
DownSignal.SetHiding(!showBreakoutSignals);

plot Value = MACD(fastLength, slowLength, MACDLength, averageType).Value;
Value.SetDefaultColor(Color.CYAN);
Value.DefineColor("UpTrend", Color.CYAN);
Value.DefineColor("DownTrend", Color.MAGENTA);
Value.AssignValueColor(if Value > Value[1] then Value.color("UpTrend") else if Value < Value[1] then Value.color("DownTrend") else Color.CURRENT);
Value.SetLineWeight(2);

plot Avg = MACD(fastLength, slowLength, MACDLength, averageType).Avg;
Avg.SetDefaultColor(Color.DARK_ORANGE);
Avg.SetLineWeight(1);

plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.WHITE);
ZeroLine.SetLineWeight(1);

AssignPriceColor(if colorChartBars then 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") else Color.CURRENT);

# END - MACD_with_Signals_and_ColorBars

pS18n1I.png
Thanks for this as well, looks like I have busy weekend playing with these. Thank you.
 
I run 4 charts (1,2,5&15min) on any trade I take and I like using the MACD. I get distracted going through my list of the day seeing where things stand, where in some cases Ive actually missed entries for other stocks bc I was looking away checking the charts. I made this scanner to save time and from being distracted to alert me (like to add bells & whistles once its working reliably) when all 4 charts are in "sync" where the MACD is greater than the Signal but it's not working as you can see in the pics below.
If I just run any of these scans on their own I get a result, even just running the 5&15 together I got results sometimes but running all four fail. As you can see in the example the MACD was greater than the Signal in all 4 charts for SRNE but the scanner said no results!?

I had sporadic results running each timeframe on their own too, where I would pull a stock up it would be positive yet the scanner wasnt picking it up.

Also when I added this as a Watchlist it wouldnt produce any results, even though I was getting some sporadically when I would hit the scan button.

What am I doing wrong?

RJcfM59.png

fOq08Q5.png
 
Last edited by a moderator:
@JP382 How long is your watchlist...??? I monitor under 30 symbols and only trade a portion of those... The rest are just to get the feel of the market... On any given day I can easily come up with 4 - 6 symbols, minimum that are worth taking trades on, and sometimes multiple opportunities per day... As I've mentioned in other topics, I monitor a 4-up chart window set to 3m, 5m, 10m, and 15m... I also monitor a 30m chart and a 5m chart with additional indicators... Sometimes I may have a few more full panel charts up... I can monitor my watchlist, which has just a few Custom Watchlist Columns, and view a 5m, 10m, or 30m chart in the right panel... If a symbols shows promise I can check the 4-up chart window for additional information... Then I have anywhere from 3 - 5 Windows with my scalping setups for individual trades... This setup makes trading efficient... It has taken years to refine the setup to my needs... All I can say is it works for me and has garnered the attention of several members here as well as customer/friends who also trade... I can post images here if you'd like to see my overall setup or you can contact me on the Discord Channel... Perhaps we can refine your system to suit your needs...
 
I'm wondering how I can setup an alert in TOS for when the faster moving MACD line cross above or below the zero line (NOT signal line). I've already done a search on this website via the site's own search function, however, the word 'zero' is not allowed to be used in search, perhaps too generic. So I instead just searched for the word 'MACD' only and look through some of what pulled up. I also did a google search for the term 'usethinkscript macd zero line' and pulled up some pages. Such as the following links...

https://usethinkscript.com/threads/macd-crossover-alert-indicator-for-thinkorswim.4350/
https://usethinkscript.com/threads/scan-alert-for-macd-crossover-below-zeroline-in-thinkorswim.547/
https://usethinkscript.com/threads/...-highest-value-since-crossing-zero-line.1989/

A couple of these links I've mentioned here sort of is almost asking what what I'm asking for at the moment, but not quite. All I want is an alert when the faster MACD line crosses above or below the zero line. I'm not looking for MACD crossover. I don't know how to set this up. Thank you in advance for any clues you can provide.
 
@petech If you're adding to existing MACD code then use the first one, if not use the second one.
Code:
Alert(Value crosses 0, "", Alert.BAR, Sound.Chimes);
Alert(reference MACD.Value crosses 0, "", Alert.BAR, Sound.Chimes);
 
@petech Change the plot value statement to equal your definition of "the faster moving MACD line"
Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

declare lower;

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

plot Value = MACD(fastLength, slowLength, MACDLength, averageType).Value;
plot ZeroLine = 0;
plot CrossUp = if Value crosses above Zeroline then Zeroline else double.NaN ;
plot CrossDn = if Value crosses below Zeroline then Zeroline else double.NaN ;

Value.SetDefaultColor(GetColor(1));
ZeroLine.SetDefaultColor(GetColor(0));
CrossUp.SetDefaultColor(color.blue);
CrossDn.SetDefaultColor(color.dark_orange);

CrossUp.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
CrossDn.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);

# Alerts
Alert(Value crosses above 0, "Cross above MACD Alert", Alert.Bar, Sound.Chimes);
Alert(Value crosses below 0, "Cross below MACD Alert", Alert.Bar, Sound.Bell);
HxktRHP.png
 
Last edited:
@generic
Thanks you !!!

@MerryDay

Thanks for the help. When I say the 'faster' MACD line, I mean the line that is cyan/blue color with the default TOS colors. I copied and pasted the code you showed me into TOS and it gives me the same color dot as shown in your example. I tested it just now with OnDemand and put the play on 3X speed for the 5 mins chart. I watched the MACD line from your script past the zero line and I saw a circle appear, as expected. I waited for the entire 5 mins bar to finish and start on the next 5 mins bar. I was listening for the ''chime'. I didn't hear one. Don't know if the chime doesn't work on OnDemand. Or if I need to edit the script some more?

P.S. It really doesn't matter too much if the chime doesn't work. Now that I can see a dot from the script you provided, I can see that I like looking at the dot. It is just more confirmation from what I can already see on the default MACD script from TOS.
 
@petech
I have not played w/ OnDemand so I am aware of its limitations. The syntax that I provided was correct and the same as in the studies that you referenced.
It shouldn't make a difference but I edited my post above, to use the same syntax that @generic recommended. Try it, if you still do not hear the chimes, I would assume that it is an OnDemand issue.
HTH
 
Hello, so you know the MACD histogram how it has 4 colors by default? Green, Darker Green, Red, and Darker red.

So i'm wondering if theres a way to setup a scanner to detect the first darker red bar of the MACD Histogram (beginning of reversal). In the attached pic it would be the fourth bar in on the right side.
-Thanks in advance

https://ibb.co/0fS6GY4
 
I have the following code down which gives me the value of the value line in the macd study. It looks at the previous candles value and color codes the label based on if the value is higher or lower and above 0 or below 0. What i want to do (but dont know how to ) is create a 3rd color which will turn yellow whenever the current candles value is inbetween .5 and -.5. so it will be like this

Above .5 = green
between .5 to -.5 = yellow
below -.5 = red

for both the previous and the current labels.
Any help would be appreciated. thank you!

Code:
input Period1 = AggregationPeriod.FOUR_HOURS;


#MACD Primer Cloud
#==============================================================================================
def c1 = close(period = Period1);
def c2 = close(period = Period1)[1];

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

def Value1 = MovingAverage(averageType, c1, fastLength) - MovingAverage(averageType, c1, slowLength);
def Value2 = MovingAverage(averageType, c2, fastLength) - MovingAverage(averageType, c2, slowLength);


input MacdLabel4h = yes;
input MacdLabelPrev4h = yes;
#AddLabel(MacdLabel4h, “Value 4H: " + Value1, color = if Value1 >= Value2 then Color.DARK_GREEN else Color.DARK_RED);
AddLabel(MacdLabel4h, “Value 4H: " + Value1, color = if Value1 >= 0 then if Value1 > Value2 then Color.GREEN else Color.DARK_GREEN else if  Value1 < Value2 then Color.RED else Color.DARK_RED);



#AddLabel(MacdLabelPrev4h, “Value 4H[1]: " + Value2, color = if Value2 >= Value1 then Color.Dark_Green else Color.DARK_Red);
#AddLabel(MacdLabel4h, "MACD 4hr: " + Diff1, color = if Diff1 >= 0 then if Diff1 > Diff1[1] then Color.GREEN else Color.#DARK_GREEN else if  Diff1 < Diff1[1] then Color.RED else Color.DARK_RED);

AddLabel(MacdLabelPrev4h, “Value 4H[1]: " + Value2, color = if Value2 >= 0 then if Value2 > Value1 then Color.GREEN else Color.DARK_GREEN else if  Value2 < Value1 then Color.RED else Color.DARK_RED);

############################
 
@kingkunta Not sure what needs to be above and below .5 and -.5 but the logic should be correct so change MACD? to whatever you need.
Code:
if MACD? > .5 then color.GREEN else if MACD? < -.5 then color.RED else color.YELLOW
 
@kingkunta Not sure what needs to be above and below .5 and -.5 but the logic should be correct so change MACD? to whatever you need.
Code:
if MACD? > .5 then color.GREEN else if MACD? < -.5 then color.RED else color.YELLOW
Sorry i dont know how to incorporate that into the code. anyway you can help me out with it?
 
@kingkunta I would if I knew what you wanted. Your post uses value for everything and I'm not sure what needs to be compared with .5 and -.5. Also sounds like you already have color code for >.5 and <-.5 so changing that logic should do.
 
@kingkunta It would be easier to help if you posted a screenshot with an explanation - a picture is worth a thousand words. Perhaps this is coloring you're looking for?

Code:
AddLabel(MacdLabel4h, “Value 4H: " + Value1, color = if Value1 > -.5 or Value1 < 0.5 then color.yellow else if Value1 >= 0 then if Value1 > Value2 then Color.GREEN else Color.DARK_GREEN else if  Value1 < Value2 then Color.RED else Color.DARK_RED);

AddLabel(MacdLabelPrev4h, “Value 4H[1]: " + Value2, color = if Value2 > -.5 or Value2 < 0.5 then color.yellow else if Value2 >= 0 then if Value2 > Value1 then Color.GREEN else Color.DARK_GREEN else if  Value2 < Value1 then Color.RED else Color.DARK_RED);
 
I have the following code down which gives me the value of the value line in the macd study. It looks at the previous candles value and color codes the label based on if the value is higher or lower and above 0 or below 0. What i want to do (but dont know how to ) is create a 3rd color which will turn yellow whenever the current candles value is inbetween .5 and -.5. so it will be like this

Above .5 = green
between .5 to -.5 = yellow
below -.5 = red

for both the previous and the current labels.
Any help would be appreciated. thank you!

Code:
input Period1 = AggregationPeriod.FOUR_HOURS;


#MACD Primer Cloud
#==============================================================================================
def c1 = close(period = Period1);
def c2 = close(period = Period1)[1];

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

def Value1 = MovingAverage(averageType, c1, fastLength) - MovingAverage(averageType, c1, slowLength);
def Value2 = MovingAverage(averageType, c2, fastLength) - MovingAverage(averageType, c2, slowLength);


input MacdLabel4h = yes;
input MacdLabelPrev4h = yes;
#AddLabel(MacdLabel4h, “Value 4H: " + Value1, color = if Value1 >= Value2 then Color.DARK_GREEN else Color.DARK_RED);
AddLabel(MacdLabel4h, “Value 4H: " + Value1, color = if Value1 >= 0 then if Value1 > Value2 then Color.GREEN else Color.DARK_GREEN else if  Value1 < Value2 then Color.RED else Color.DARK_RED);



#AddLabel(MacdLabelPrev4h, “Value 4H[1]: " + Value2, color = if Value2 >= Value1 then Color.Dark_Green else Color.DARK_Red);
#AddLabel(MacdLabel4h, "MACD 4hr: " + Diff1, color = if Diff1 >= 0 then if Diff1 > Diff1[1] then Color.GREEN else Color.#DARK_GREEN else if  Diff1 < Diff1[1] then Color.RED else Color.DARK_RED);

AddLabel(MacdLabelPrev4h, “Value 4H[1]: " + Value2, color = if Value2 >= 0 then if Value2 > Value1 then Color.GREEN else Color.DARK_GREEN else if  Value2 < Value1 then Color.RED else Color.DARK_RED);

############################
Hi King, Please post the final code when you get it the way you want it. I like what you are doing. Thanks
 
@kingkunta It would be easier to help if you posted a screenshot with an explanation - a picture is worth a thousand words. Perhaps this is coloring you're looking for?

Code:
AddLabel(MacdLabel4h, “Value 4H: " + Value1, color = if Value1 > -.5 or Value1 < 0.5 then color.yellow else if Value1 >= 0 then if Value1 > Value2 then Color.GREEN else Color.DARK_GREEN else if  Value1 < Value2 then Color.RED else Color.DARK_RED);

AddLabel(MacdLabelPrev4h, “Value 4H[1]: " + Value2, color = if Value2 > -.5 or Value2 < 0.5 then color.yellow else if Value2 >= 0 then if Value2 > Value1 then Color.GREEN else Color.DARK_GREEN else if  Value2 < Value1 then Color.RED else Color.DARK_RED);
Hey i think this is close to what I need however it makes all the labels yellow no matter what the value is. Do you know what could be missing? The idea that you posted is exactly what I want but just the labels seem to all be yellow with this
Code:
AddLabel(MacdLabel4h, “Value 4H: " + Value1, color = if Value1 >= -.5 or Value1 <= 0.5 then color.yellow else if Value1 >= .5 then if Value1 > Value2 then Color.GREEN else Color.DARK_GREEN else if  Value1 < Value2 then Color.RED else Color.DARK_RED);
 
Hey i think this is close to what I need however it makes all the labels yellow no matter what the value is. Do you know what could be missing? The idea that you posted is exactly what I want but just the labels seem to all be yellow with this
Code:
AddLabel(MacdLabel4h, “Value 4H: " + Value1, color = if Value1 >= -.5 or Value1 <= 0.5 then color.yellow else if Value1 >= .5 then if Value1 > Value2 then Color.GREEN else Color.DARK_GREEN else if  Value1 < Value2 then Color.RED else Color.DARK_RED);
@kingkunta My original snippet had a mistake - I think this is better, I used "and" instead of "or" in the code for defining what area to color yellow. Check it, let me know if it solves the issue or if I'll have to dig deeper.
Code:
AddLabel(MacdLabel4h, “Value 4H: " + Value1, color = if Value1 >= -.5 and Value1 <= 0.5 then color.yellow else if Value1 >= .5 then if Value1 > Value2 then Color.GREEN else Color.DARK_GREEN else if  Value1 < Value2 then Color.RED else Color.DARK_RED);
 
@kingkunta My original snippet had a mistake - I think this is better, I used "and" instead of "or" in the code for defining what area to color yellow. Check it, let me know if it solves the issue or if I'll have to dig deeper.
Code:
AddLabel(MacdLabel4h, “Value 4H: " + Value1, color = if Value1 >= -.5 and Value1 <= 0.5 then color.yellow else if Value1 >= .5 then if Value1 > Value2 then Color.GREEN else Color.DARK_GREEN else if  Value1 < Value2 then Color.RED else Color.DARK_RED);
hey man thank u so much. Seems to be working fine. Ill post the entire code when i fix it up later.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
242 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