In response to a request to modify the code to account for MACD “near or at” it’s 52 week high. The following code provides this functionality. A very important thing to note is that using the default setting of 1.0 percent will make the signal extremely rare.
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input percentLimit = 1.0;
def factor = 1 - (percentLimit * 0.01);
def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
def Diff = Value - Avg;
# use this scan to find 52 week high MACD Histogram
plot scan = Diff <= Highest(Diff[1], 252) and Diff >= Highest(Diff[1], 252) * factor;
# use this scan to find 52 week high MACD Value
#plot scan = Value < Highest(Value[1], 252) and Value >= Highest(Value[1], 252) * factor;
I noticed it is rare indeed because the scanner didn’t find any stocks, I tried deleting the one percent requirement so that the scan would just search for a MACD 52-week equal high but did not work either, I am sure there is a way to code it but I’m just not sure how to.
Let’s pretend this is the MACD and the right hand side is at exactly the same high as in the past, this is
what I am trying to scan for. Hopefully I explained it correctly.
\ / < ~~~~~~~~~~~~~~ scanning for this edge, not over past higher highs. \ / \____ /
Eliminating the percentage would actually make it nearly impossible to find a signal. The MACD being exactly the same as a previous value is almost never going to happen. If you want to increase the frequency, keep the percentage filter and increase the percentage value. Start at a very high number, say 50%. Then work your way down until you get just a few results.
Thank you for clarifying Pete