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

Can someone please help with this code, its just the default MACDHistogramCrossover code in TOS and I wanted to see if someone can modify the code to where it just shows the last 3 arrows instead of all the arrows on the chart, below is theMACDHistogramCrossover Code: Thank you

#
# TD Ameritrade IP Company, Inc. (c) 2009-2022
#

#wizard input: crossingType
#wizard text: Inputs: fast length:
#wizard input: fastLength
#wizard text: slow length:
#wizard input: slowLength
#wizard text: macd length:
#wizard input: MACDLength
#wizard text: average type:
#wizard input: AverageType

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input crossingType = {default "Positive to Negative", "Negative to Positive"};

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

plot signal = crosses(Diff, 0, crossingType == CrossingType."Negative to Positive");

signal.DefineColor("Negative to Positive", GetColor(2));
signal.DefineColor("Positive to Negative", GetColor(3));
signal.AssignValueColor(if crossingType == CrossingType."Negative to Positive" then signal.color("Negative to Positive") else signal.color("Positive to Negative"));

signal.SetPaintingStrategy(if crossingType == CrossingType."Negative to Positive"
then PaintingStrategy.BOOLEAN_ARROW_UP
else PaintingStrategy.BOOLEAN_ARROW_DOWN);
 
Can someone please help with this code, its just the default MACDHistogramCrossover code in TOS and I wanted to see if someone can modify the code to where it just shows the last 3 arrows instead of all the arrows on the chart, below is theMACDHistogramCrossover Code: Thank you

#
# TD Ameritrade IP Company, Inc. (c) 2009-2022
#

#wizard input: crossingType
#wizard text: Inputs: fast length:
#wizard input: fastLength
#wizard text: slow length:
#wizard input: slowLength
#wizard text: macd length:
#wizard input: MACDLength
#wizard text: average type:
#wizard input: AverageType

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input crossingType = {default "Positive to Negative", "Negative to Positive"};

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

plot signal = crosses(Diff, 0, crossingType == CrossingType."Negative to Positive");

signal.DefineColor("Negative to Positive", GetColor(2));
signal.DefineColor("Positive to Negative", GetColor(3));
signal.AssignValueColor(if crossingType == CrossingType."Negative to Positive" then signal.color("Negative to Positive") else signal.color("Positive to Negative"));

signal.SetPaintingStrategy(if crossingType == CrossingType."Negative to Positive"
then PaintingStrategy.BOOLEAN_ARROW_UP
else PaintingStrategy.BOOLEAN_ARROW_DOWN);

This will limit the plot of the arrows based upon the value of input arrow_limiter.

Screenshot-2022-10-15-121626.png
Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2009-2022
#

#wizard input: crossingType
#wizard text: Inputs: fast length:
#wizard input: fastLength
#wizard text: slow length:
#wizard input: slowLength
#wizard text: macd length:
#wizard input: MACDLength
#wizard text: average type:
#wizard input: AverageType

input arrow_limiter = 3;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input crossingType = {default "Positive to Negative", "Negative to Positive"};

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

def cond = if Crosses(Diff, 0, crossingType == crossingType."Negative to Positive") then 1 else 0;
def signalcount = CompoundValue(1, if IsNaN(cond[1]) then 0 else if cond then signalcount[1] + 1 else signalcount[1], 0);

plot signal = if HighestAll(signalcount) - signalcount <= arrow_limiter - 1 then cond else Double.NaN;

signal.DefineColor("Negative to Positive", GetColor(2));
signal.DefineColor("Positive to Negative", GetColor(3));
signal.AssignValueColor(if crossingType == crossingType."Negative to Positive" then signal.Color("Negative to Positive") else signal.Color("Positive to Negative"));

signal.SetPaintingStrategy(if crossingType == crossingType."Negative to Positive"
then PaintingStrategy.BOOLEAN_ARROW_UP
else PaintingStrategy.BOOLEAN_ARROW_DOWN);
 
@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
Is there any way to add that to a scan? I'd like to find stocks that are below the zero line (the horizontal purple line on the MACD).
 
Would like the candle colors on upper chart to be the same colors as this MACD Histogram script colors. Thanks a lot!

declare lower;

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

plot Diff = MACD(fastLength, slowLength, MACDLength, averageType).Diff;
plot UpSignal = if Diff crosses above 0 then 0 else Double.NaN;
plot DownSignal = if Diff crosses below 0 then 0 else Double.NaN;

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

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"));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
 
Would like the candle colors on upper chart to be the same colors as this MACD Histogram script colors. Thanks a lot!

declare lower;

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

plot Diff = MACD(fastLength, slowLength, MACDLength, averageType).Diff;
plot UpSignal = if Diff crosses above 0 then 0 else Double.NaN;
plot DownSignal = if Diff crosses below 0 then 0 else Double.NaN;

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

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"));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

Try this

Screenshot-2022-11-01-045957.png
Ruby:
declare lower;

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

plot Diff = MACD(fastLength, slowLength, MACDLength, averageType).Diff;
plot UpSignal = if Diff crosses above 0 then 0 else Double.NaN;
plot DownSignal = if Diff crosses below 0 then 0 else Double.NaN;

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

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"));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);



input pricecolor = yes;
AssignpriceColor(if !pricecolor then color.current else 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"));
 
I have a scan using various parameters, but one more filter I'd like to add is to filter tickers where the MACDHistogram diff value of the last bar is less than the previous bar on a 5 minute timeframe (or greater, depending on what I'm scanning for) . I would specify either lower or higher in my scan depending on what I want to search
I'm using 12, 26, 9 exponential for my MACD (see screenshot for clarification)


KGfIi8A.png


I tried this in the scan, but that it gives me no results, so unsure if I'm doing this correctly:

Code:
MACDHistogram()."Diff" is less than MACDHistogram()."Diff"


ftJ1s26.png



I already checked out this thread below, but that was related to the crossover
https://usethinkscript.com/threads/...chlist-for-thinkorswim.7745/page-6#post-73061
 
I'd like to add is to filter tickers where the MACDHistogram diff value of the last bar is less than the previous bar on a 5 minute timeframe (or greater, depending on what I'm scanning for) .
I tried this in the scan, but that it gives me no results, so unsure if I'm doing this correctly:

Code:
MACDHistogram()."Diff" is less than MACDHistogram()."Diff"

Your code looks for where the current bar is less than the current bar. The syntax for previous bar is: [1]
Code:
MACDHistogram()."Diff" is less than MACDHistogram()."Diff"[1]
 
I would like to have an alert set up on ToS to notify me when the MACD line crosses above or below the zero line. Can someone help me with that?
 
Hello,

I am trying to paste that script in TOS, but I am getting error , not sure how to fix that.

Here's the script

Code:
input price = close;
input macdFastLength = 12;
input macdSlowLength = 26;
input macdSignalLength = 9;


def macd = MACD(price, macdFastLength, macdSlowLength, macdSignalLength);


def longEntry = macd > high[1] and macd > macd[1];
def longExit = macd < low[1] or macd < macd[1];
def shortEntry = macd < low[1] and macd < macd[1];
def shortExit = macd > high[1] or macd > macd[1];

plot longSignal = longEntry;
plot shortSignal = shortEntry;

longSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
longSignal.SetLineWeight(3);
shortSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
shortSignal.SetLineWeight(3);


alert(longEntry, "Long entry signal", Alert.BAR, Sound.Ring);
alert(longExit, "Long exit signal", Alert.BAR, Sound.Ring);
alert(shortEntry, "Short entry signal", Alert.BAR, Sound.Ring);
alert(shortExit, "Short exit signal", Alert.BAR, Sound.Ring);

I am getting error at Line

Code:
def macd = MACD(price, macdFastLength, macdSlowLength, macdSignalLength);

At MACD?

I do appreciate your help. Thanks.
 
Hello,

I am trying to paste that script in TOS, but I am getting error , not sure how to fix that.

Here's the script

Code:
input price = close;
input macdFastLength = 12;
input macdSlowLength = 26;
input macdSignalLength = 9;


def macd = MACD(price, macdFastLength, macdSlowLength, macdSignalLength);


def longEntry = macd > high[1] and macd > macd[1];
def longExit = macd < low[1] or macd < macd[1];
def shortEntry = macd < low[1] and macd < macd[1];
def shortExit = macd > high[1] or macd > macd[1];

plot longSignal = longEntry;
plot shortSignal = shortEntry;

longSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
longSignal.SetLineWeight(3);
shortSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
shortSignal.SetLineWeight(3);


alert(longEntry, "Long entry signal", Alert.BAR, Sound.Ring);
alert(longExit, "Long exit signal", Alert.BAR, Sound.Ring);
alert(shortEntry, "Short entry signal", Alert.BAR, Sound.Ring);
alert(shortExit, "Short exit signal", Alert.BAR, Sound.Ring);

I am getting error at Line

Code:
def macd = MACD(price, macdFastLength, macdSlowLength, macdSignalLength);

At MACD?

I do appreciate your help. Thanks.

when you get an error, look up the functions used in the formula.
you are trying to use the macd study and pass input parameters to it.

here is the macd study
https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/M-N/MACD

look at the inputs, do you notice anything different in the parameters in your formula and the sequence of inputs in the manual?




macd doesn't have a price input parameter.

try removing price from the macd() function
so it looks like this,

def macd = MACD( macdFastLength, macdSlowLength, macdSignalLength);
 
Last edited:
Hi, looking for a MACD study that will signal with an arrow on my chart(upper).
I would like it to signal when the moving averages cross
any help would be great
 
Can someone code a signal arrow for when the histogram changes colors ex.... green to dark green and red to dark red?
 
Can someone code a signal arrow for when the histogram changes colors ex.... green to dark green and red to dark red?

you didn't say where you want to see something...


this plots a n arrow, on the upper chart, when the macd histogram changes color

Code:
# macd_arrows

def na = double.nan;

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

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

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


def up = (diff > diff[1]);
def dwn = (diff < diff[1]);

def dir = if up and Diff >= 0 then 2
     else if up and Diff <= 0 then 1
     else if dwn and Diff >= 0 then -1
     else if dwn and Diff <= 0 then -2
     else 0;

plot zup = if up and dir != dir[1] then low*0.999 else na;
plot zdwn = if dwn and dir != dir[1] then high*1.001 else na;

zup.SetPaintingStrategy(PaintingStrategy.arrow_up);
zup.SetLineWeight(3);
zup.AssignValueColor(if up and Diff >= 0 then color.green
  else if up and Diff <= 0 then color.dark_red
#  else if up and Diff <= 0 then color.yellow
  else color.gray);

zdwn.SetPaintingStrategy(PaintingStrategy.arrow_down);
zdwn.SetLineWeight(3);
zdwn.AssignValueColor(
  if dwn and Diff >= 0 then color.dark_green
#  if dwn and Diff >= 0 then color.cyan
  else if dwn and Diff <= 0 then color.red
  else color.gray);
#
 
Appreciate the quick response thank you
Didn't even think about putting on upper chart, maybe thats better. I'll give it a look when I get home
 
you didn't say where you want to see something...


this plots a n arrow, on the upper chart, when the macd histogram changes color

Code:
# macd_arrows

def na = double.nan;

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

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

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


def up = (diff > diff[1]);
def dwn = (diff < diff[1]);

def dir = if up and Diff >= 0 then 2
     else if up and Diff <= 0 then 1
     else if dwn and Diff >= 0 then -1
     else if dwn and Diff <= 0 then -2
     else 0;

plot zup = if up and dir != dir[1] then low*0.999 else na;
plot zdwn = if dwn and dir != dir[1] then high*1.001 else na;

zup.SetPaintingStrategy(PaintingStrategy.arrow_up);
zup.SetLineWeight(3);
zup.AssignValueColor(if up and Diff >= 0 then color.green
  else if up and Diff <= 0 then color.dark_red
#  else if up and Diff <= 0 then color.yellow
  else color.gray);

zdwn.SetPaintingStrategy(PaintingStrategy.arrow_down);
zdwn.SetLineWeight(3);
zdwn.AssignValueColor(
  if dwn and Diff >= 0 then color.dark_green
#  if dwn and Diff >= 0 then color.cyan
  else if dwn and Diff <= 0 then color.red
  else color.gray);
#
plotted this on my chart ... it looked like i spilled a bag of arrow skittles on my laptop screen lol. Not complaining it seemed like a good idea
one last request- can you code an arrow on curves? like when the MA line starts curve down or up?the arrow under the ma line when its starts to curve up and above it when curving down
 
I'm trying to add this MACD Script to use in a watchlist but it's not working when I copy and paste the script.
I found the steps to this script in a video on youtube

def fastlength = 12;

def slowlength = 26;

def MACDLength = 9;

def averageType = AverageType.EXPONENTIAL;

def showBreakoutSignals = no;

def ZeroLine = 0;



plot Value = MovingAverage (averageType, close, fastlength) – MovingAverage (averageType, close, slowlength);

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



plot Diff = Value – Avg;



addlabel (yes, if value >= zeroline and avg >= zeroline and value crosses below avg within 3 bars then “Cross OB” else if value <= zeroline and avg <= zeroline and value crosses above avg within 3 bars then “Cross OS” else if value >= zeroline and avg >= zeroline and value > avg then “Bull Acc” else if
value >= zeroline and avg >= zeroline and value <avg then “Bull Dec” else if
value <= zeroline and avg <= zeroline and value < then “Bear Acc” else if

<= zeroline and avg <= zeroline and value > avg then “Bear Dec”

else

"_";
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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