Ok, give this a try and see if it does the trick. I see from your original code you are trying to do the entire scan signal on one line of code. That is one way to drive yourself crazy. I find it best to break down each piece into it’s own line of code.
I also use variable names that describe each element clearly. I don’t just do this for instruction purposes. I actually apply this everywhere I write code. Readability makes a world of difference. Especially when you have to go back to something you wrote years ago.
One very important note before I post the code. When using the function named Highest(). It is sometimes required that you reference the previous value. In your example: Highest(high, 252) is going to cause problems. Because it’s including the current high in the test for Highest. In your case you actually want to use Highest(high[1], 252). This will not include the current high in the test for highest high in 252.
Here is the code. I have not tested it so let me know what you get.
def fiftyTwoWeekHigh = Highest(high[1], 252);
def highestInOneMonth = Highest(high[1], 21);
def fivePercentFromHigh = fiftyTwoWeekHigh * 0.95;
def signal = high < fiftyTwoWeekHigh and high > highestInOneMonth and high >= fivePercentFromHigh;
plot scan = signal;
Let’s make sure I understand this. Current high is less than 52 week high but greater than 52 week high times 0.95. You said previous high needs to be at least 1 month old but I need to know if the previous high and 52 week high can be one and the same. Or are you saying that the current high must be greater than the highest high in more than a month? A screenshot showing this pattern would be a great help.