How to Calculate MACD slope and scan for it

brock

New member
Hello,
I'm trying to calculate the MACD slope in order to open a trade when it's a steep slope up or down. Typically there is good price follow through for a couple bars following a sharp, steep hook upward or downward in the MACD. Then I can have a dynamic watchlist and alerts to notify me of a possible entry, if the slope value reaches a certain value (user defined input).
I've read the posts here about MACD slope, but they don't seem accurate. I used the basic code found on this website (MACD slope), on Youtube, and in TOS, but it's not giving what I want. Can anyone help?
Here's what I started with and have tried tweaking it many ways, but to no avail.
This is from XeoNoX in the post on MACD slope (I changed the name of one variable [def "Angle, deg"] to one without quotes, spaces, & commas instead [MACDValue]).

NOTE: I put this same script in the upper section and added a chart bubble to print the actual value of the slope which this script is supposed to calculate (changed "plot" to "def" where needed and removed the zeroline altogether). The value returned should be a number between 0 and -1 for downward slope, and between 0 and 1 for upward slope. Instead, I get both positive and negative numbers no matter which way the slope is, and the numbers don't make sense: higher numbers may show up when the slope is diminishing or vice versa--no rhyme or reason. And the numbers hit over 30 sometimes.

######
declare lower;
#The code calculates the angle of slope of the macd moving average with the given MACD length.
#The angle itself is calculated using the return of the arc tangent of an angle in the range of -pi/2 through pi/2.
#By XeoNoX via usethinkscript.com
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 height = avg - avg[MACDLength];
plot MACDValue = ATan(height/MACDLength) * 180 / Double.Pi;
MACDValue.SetDefaultColor(Color.GREEN);
plot zeroline = 0;
zeroline.SetDefaultColor(Color.red);

#####

On the line "plot MACDValue" I removed "MACDLength" from the equation, but still not right.
On the line that defines "height" I changed it to "avg - avg[1]" as this seems more reasonable, but results are still not right. For example, I don't want the slope to be based off the difference between current bar and 9 bars ago, but only off the previous bar. So shouldn't this only look back one bar?

Anyway, I'm no coding expert. I only began using TOS last year. Coding is not my hobby. I work full-time at another job and can only do this part time. I would appreciate any help on this!
There has to be a formula to calculate the slope of MACD from 1 bar to the next.
 
Solution
Hello,
I'm trying to calculate the MACD slope in order to open a trade when it's a steep slope up or down. Typically there is good price follow through for a couple bars following a sharp, steep hook upward or downward in the MACD. Then I can have a dynamic watchlist and alerts to notify me of a possible entry, if the slope value reaches a certain value (user defined input).
I've read the posts here about MACD slope, but they don't seem accurate. I used the basic code found on this website (MACD slope), on Youtube, and in TOS, but it's not giving what I want. Can anyone help?
Here's what I started with and have tried tweaking it many ways, but to no avail.
This is from XeoNoX in the post on MACD slope (I changed the name of...
Hello,
I'm trying to calculate the MACD slope in order to open a trade when it's a steep slope up or down. Typically there is good price follow through for a couple bars following a sharp, steep hook upward or downward in the MACD. Then I can have a dynamic watchlist and alerts to notify me of a possible entry, if the slope value reaches a certain value (user defined input).
I've read the posts here about MACD slope, but they don't seem accurate. I used the basic code found on this website (MACD slope), on Youtube, and in TOS, but it's not giving what I want. Can anyone help?
Here's what I started with and have tried tweaking it many ways, but to no avail.
This is from XeoNoX in the post on MACD slope (I changed the name of one variable [def "Angle, deg"] to one without quotes, spaces, & commas instead [MACDValue]).

NOTE: I put this same script in the upper section and added a chart bubble to print the actual value of the slope which this script is supposed to calculate (changed "plot" to "def" where needed and removed the zeroline altogether). The value returned should be a number between 0 and -1 for downward slope, and between 0 and 1 for upward slope. Instead, I get both positive and negative numbers no matter which way the slope is, and the numbers don't make sense: higher numbers may show up when the slope is diminishing or vice versa--no rhyme or reason. And the numbers hit over 30 sometimes.

######
declare lower;
#The code calculates the angle of slope of the macd moving average with the given MACD length.
#The angle itself is calculated using the return of the arc tangent of an angle in the range of -pi/2 through pi/2.
#By XeoNoX via usethinkscript.com
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 height = avg - avg[MACDLength];
plot MACDValue = ATan(height/MACDLength) * 180 / Double.Pi;
MACDValue.SetDefaultColor(Color.GREEN);
plot zeroline = 0;
zeroline.SetDefaultColor(Color.red);

#####

On the line "plot MACDValue" I removed "MACDLength" from the equation, but still not right.
On the line that defines "height" I changed it to "avg - avg[1]" as this seems more reasonable, but results are still not right. For example, I don't want the slope to be based off the difference between current bar and 9 bars ago, but only off the previous bar. So shouldn't this only look back one bar?

Anyway, I'm no coding expert. I only began using TOS last year. Coding is not my hobby. I work full-time at another job and can only do this part time. I would appreciate any help on this!
There has to be a formula to calculate the slope of MACD from 1 bar to the next.


if you want a slope number, i would forget about trig and just calculate a slope, rise/run, for a variable.
(a chart has different units for x and y, so an 'angle' number may not match what you expect)
https://en.wikipedia.org/wiki/Slope

this calculates the change in value of the macd value plot, over x bars.
the default is 1 bar , so current bar - 1 bar ago.

look at a few stocks and see what slope number will work for you, and change this,
input min_slope = 0.013;

it draws a line for the macd value and colored bubbles with the slope number.
i copied the macd study and modified it.

maybe this will help you figure out what you want to do.

Code:
# macd_slope_00

declare lower;

input slope_bars = 1;
input min_slope = 0.013;

input macd_fastLength = 12;
input macd_slowLength = 26;
input macd_Length = 9;
input macd_averageType = AverageType.EXPONENTIAL;
#input showBreakoutSignals = no;

def macd_value = MACD(macd_fastLength, macd_slowLength, macd_Length, macd_averageType).value;

# slope = rise/run = chg in Y / chg in x
def rise = macd_value - getvalue(macd_value, slope_bars);
def run = slope_bars;

# macd value change per bar
def macd_slope = rise/run;

plot z = macd_value;


input test_bubbles = yes;
addchartbubble(test_bubbles, macd_value*0.99,
 macd_slope
# + "\n" +  min_slope
, (if macd_slope > min_slope then color.green 
  else  if  macd_slope < -min_slope then color.red
  else color.gray), 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);
#

ABkevSH.jpg
 
Solution
if you want a slope number, i would forget about trig and just calculate a slope, rise/run, for a variable.
(a chart has different units for x and y, so an 'angle' number may not match what you expect)
https://en.wikipedia.org/wiki/Slope

this calculates the change in value of the macd value plot, over x bars.
the default is 1 bar , so current bar - 1 bar ago.

look at a few stocks and see what slope number will work for you, and change this,
input min_slope = 0.013;

it draws a line for the macd value and colored bubbles with the slope number.
i copied the macd study and modified it.

maybe this will help you figure out what you want to do.

Code:
# macd_slope_00

declare lower;

input slope_bars = 1;
input min_slope = 0.013;

input macd_fastLength = 12;
input macd_slowLength = 26;
input macd_Length = 9;
input macd_averageType = AverageType.EXPONENTIAL;
#input showBreakoutSignals = no;

def macd_value = MACD(macd_fastLength, macd_slowLength, macd_Length, macd_averageType).value;

# slope = rise/run = chg in Y / chg in x
def rise = macd_value - getvalue(macd_value, slope_bars);
def run = slope_bars;

# macd value change per bar
def macd_slope = rise/run;

plot z = macd_value;


input test_bubbles = yes;
addchartbubble(test_bubbles, macd_value*0.99,
 macd_slope
# + "\n" +  min_slope
, (if macd_slope > min_slope then color.green
  else  if  macd_slope < -min_slope then color.red
  else color.gray), 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);
#

ABkevSH.jpg
Thank you halcyonguy! That's what I was looking for. I should be able to play with this and tweak a little if needed.
That was fast--much appreciated!!!
 

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