I'm trying to create a study that will plot a chart bubble on the earnings date that will show what the % IV drop was from IV % prior to the earnings report to the IV % on the day after the report. The below code is what I came up with but not sure if it is correct or not. I also am getting an "expected double" error. Can anyone help figure this out? Also, is it possible to have this use the IV% from the soonest expiring options contract, or does the ImpVolatility() pull a different value than the soonest expiring contract? Thanks for any help that can be provided!
Code:
def isBefore = HasEarnings(EarningTime.BEFORE_MARKET);
def isAfter = HasEarnings(EarningTime.AFTER_MARKET);
def vol = imp_volatility();
def beforevol = if isAfter then vol else if isBefore then vol[1] else 0;
def aftervol = if isAfter then vol[-1] else if isBefore then vol else 0;
def volcalc = (aftervol - beforevol) / beforevol;
def downvol = volcalc < 0;
AddChartBubble(downvol, high[1], "IV Crush: " + AsPercent(aftervol - beforevol) / beforevol, Color.YELLOW);