The solution to this is complicated by the fact that earnings can be reported after market close or before market open. So our logic needs to have enough dimensions to account for this. It took some effort to get everything balanced and working for both scenarios. I had to plot the various elements on the chart so I could exactly how to construct the code to get the right prices. Some of the screenshots below demonstrate part of that process.
Here is the code for the scan:
Edit: The original code contained math that needed correction. I am replacing that with the code below.
There are a couple more changes.
1) The percentEarningMove is enclosed within AbsValue() method in order to pick up both the positive and negative percent moves
2) A filter was added to prevent picking up stocks that have reported earnings in previous 3 days
rec preEarningsClose = if HasEarnings(EarningTime.BEFORE_MARKET) then close[1] else if HasEarnings(EarningTime.AFTER_MARKET) then close else preEarningsClose[1];
rec postEarningsOpen = if HasEarnings(EarningTime.BEFORE_MARKET)[1] then open[1] else if HasEarnings(EarningTime.AFTER_MARKET)[1] then open else postEarningsOpen[1];
rec percentEarningsMove = if HasEarnings(EarningTime.AFTER_MARKET)[1] then 100 * ( postEarningsOpen / preEarningsClose[1] - 1) else if HasEarnings(EarningTime.BEFORE_MARKET)[1] then 100 * (postEarningsOpen / preEarningsClose - 1) else percentEarningsMove[1];
plot scan = Highest(HasEarnings(), 3) < 1 and AbsValue(percentEarningsMove) > 6.0;
Pete: I may be asking a stupid question but what makes your code give all 6% moves whether the stock opened up compared to the previous close or down?
It does not. In order to scan for 6% down after earnings you would need to change from:
plot scan = percentEarningsMove > 6.0;
To this:
plot scan = percentEarningsMove < -6.0;
Or if you wanted to scan for both you could combine them:
plot scan = percentEarningsMove > 6.0 or percentEarningsMove < -6.0;
Based on some emails received from Arun NY I have updated the code in my original answer and included some comments about the changes made. Thank Arun NY for taking the time to dig into the details and report your findings to me.