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