I updated the title of this question to better match the context of it’s description.
Here is the code to perform the scan as requested.
The Inputs:
There are two inputs to adjust how the crossover detection is handled. The lookbackPeriod is the length of time the two lines of the MACD should be crossed to the downside. This is the same as the Histogram being below zero line and thus the code uses this instead of testing the two lines. The other input is called barsBeforeCrossover and this tells how many bars before the crossover to look for the lowest price of the Value line.
How it Works:
The code compares this low to the lowest price of the Value line during the lookbackPeriod. This is a very simplistic approach. It does not detect if the two lines of the MACD had been in a steady decline throughout the lookbackPeriod. The default value of the lookbackPeriod is set to one year of daily bars. I imagine this condition is very rare.
The Scan:
The final statement at the end is testing for the signal having occurred a number of bars in the past. In this case, it is looking backward to see if the given conditions were present anytime within the last 30 bars. In order to get any signals to show up, I had to set the lookbackPeriod down to 60.
Don’t forget to up-vote the answers that best solve your question!
Here is the code:
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input lookbackPeriod = 252;
input barsBeforeCrossover = 5;
input averageType = AverageType.EXPONENTIAL;
def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
def Diff = Value - Avg;
def belowZeroForLookbackPeriod = Highest(Diff, lookbackPeriod) < 0;
def theCrossOver = Diff[1] < 0 and Diff > 0;
def lowBeforeCrossOver = if theCrossover then Lowest(Value, barsBeforeCrossover) else 0;
def crossoverLowIsLowestInLookbackPeriod = theCrossover and lowBeforeCrossover < Lowest(Value[barsBeforeCrossover], lookbackPeriod);
def signal = belowZeroForLookbackPeriod[1] and crossoverLowIsLowestInLookbackPeriod;
plot scan = Highest(signal, 30) > 0;
Yes, I see the contribution. That is not how we handle premium Thinkscript projects. I’ll refund your contribution right away. That is truly used only for voluntary contributions. In order to initiate a premium Thinkscript project you you go to our About page, read the section where our rates are defined, then fill out the contact page at the bottom. That hits my inbox and we hash out the exact specifications before quoting a final price. Here is the link to the About page: https://www.hahn-tech.com/about/
If you do not want to go that route and wanted to modify the existing code per your last comment. Simply remove ”belowZeroForLookbackPeriod[1] and ” from the second to last line: ”def signal = belowZeroForLookbackPeriod[1] and crossoverLowIsLowestInLookbackPeriod;”
So that is says: ”def signal = crossoverLowIsLowestInLookbackPeriod;”