simpliTEE
New member
Looking for some help with developing this earnings grid view. The upper study has a selector to display the earnings release by year and quarter at the top. I'd like to add a view of the typical IV behavior on the lower study. However, it just shows the most recent timeframe. Is there a way to add the year and quarter selector to the lower study as well, so it matches with the timeframe in the upper? The idea is to create a grid where you can quickly scan stocks for historical IV crush response around earnings. Thanks in advance!
https://tos.mx/i01WO6q
Here's the upper:
Here's the lower:
https://tos.mx/i01WO6q
Here's the upper:
Code:
# Earnings research toolcolor.blue
declare lower;
input WhichYear = {default year0, year1, year2, year3, year4};
input WhichQuarter = {default Q1, Q2, Q3, Q4};
input daysBefore = 20;
input daysAfter = 30;
# assign each month to a calendar quarter
def theQtr;
switch (WhichQuarter) {
case Q1:
theQtr = GetMonth() == 1 or GetMonth() == 2 or GetMonth() == 3;
case Q2:
theQtr = GetMonth() == 4 or GetMonth() == 5 or GetMonth() == 6;
case Q3:
theQtr = GetMonth() == 7 or GetMonth() == 8 or GetMonth() == 9;
case Q4:
theQtr = GetMonth() == 10 or GetMonth() == 11 or GetMonth() == 12;
}
# define this year and the previous 4 years
# find the year which has the last full quarter of earnings
def fullYear = HighestAll(if HasEarnings() and theQtr then GetYear() else 0);
def theYear;
switch (WhichYear) {
case year0:
theYear = GetYear() == fullYear;
case year1:
theYear = GetYear() == fullYear - 1;
case year2:
theYear = GetYear() == fullYear - 2;
case year3:
theYear = GetYear() == fullYear - 3;
case year4:
theYear = GetYear() == fullYear - 4;
}
def barNumber = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else barNumber);
# get the bar number of the specified earnings date
def eBar = if HasEarnings() and theYear and theQtr then BarNumber() else 0;
# get the earnings date
def eDay = GetValue(GetDayOfMonth(GetYYYYMMDD()), barNumber - HighestAll(eBar));
def eMonth = GetValue(GetMonth(), barNumber - HighestAll(eBar));
def eYear = GetValue(GetYear(), barNumber - HighestAll(eBar)) - 2000;
AddLabel(yes, if isnan(GetValue(close, barNumber - HighestAll(eBar))) then "Data not available" else "20" + eYear + "/" + eMonth + "/" + eDay, Color.YELLOW);
def lookback = lastBar - HighestAll(eBar) - daysAfter;
def startbar = lastBar - daysBefore - daysAfter;
def eOpen = if isnan(GetValue(close, barNumber - HighestAll(eBar))) or IsNaN(close) or BarNumber() < startbar then Double.NaN else GetValue(open, lookback);
def eHigh = if isnan(GetValue(close, barNumber - HighestAll(eBar))) or IsNaN(close) or BarNumber() < startbar then Double.NaN else GetValue(high, lookback);
def eLow = if isnan(GetValue(close, barNumber - HighestAll(eBar))) or IsNaN(close) or BarNumber() < startbar then Double.NaN else GetValue(low, lookback);
def eClose = if isnan(GetValue(close, barNumber - HighestAll(eBar))) or IsNaN(close) or BarNumber() < startbar then Double.NaN else GetValue(close, lookback);
# Building a candlestick chart
AddChart(fallcolor = Color.MAGENTA, high = eHigh, low = eLow, open = eOpen, close = eClose, type = ChartType.CANDLE);
# Marking the days
AddVerticalLine(BarNumber() == startbar, daysBefore + " Days Before", Color.GREEN, Curve.FIRM);
AddVerticalLine(BarNumber() == startbar + daysBefore, "", Color.YELLOW, Curve.FIRM);
AddVerticalLine(BarNumber() == startbar + daysBefore + daysAfter, daysAfter + " Days After", Color.CYAN, Curve.FIRM);
Here's the lower:
Code:
declare lower;
declare hide_on_intraday;
#IVPercentile
def vol = imp_volatility();
input DisplayIVPercentile = yes;
input TimePeriod = 252;
input LowVolLimit = 30;
input HighVolLimit = 50;
input DisplayShorterTerm = no;
input ShortTimePeriod = 63;
input DisplayImpVolatility = no;
input DisplayDaily1StandardDev = yes;
input DisplayWeekly1StandardDev = yes;
input DisplayMonthly1StandardDev = yes;
input InvertRedGreen = yes;
def data = if !IsNaN(vol) then vol else vol[-1];
def hi = Highest(data, TimePeriod);
def lo = Lowest(data, TimePeriod);
def ShortHi = Highest(data, ShortTimePeriod);
def ShortLo = Lowest(data, ShortTimePeriod);
plot Percentile = (data - lo) / (hi - lo) * 100;
def lowend = Percentile < LowVolLimit;
def highend = Percentile > HighVolLimit;
def sPercentile = (data - ShortLo) / (ShortHi - ShortLo) * 100;
def ShortLowend = sPercentile < LowVolLimit;
def ShortHighend = sPercentile > HighVolLimit;
DefineGlobalColor("lowcolor", if InvertRedGreen then Color.RED else Color.GREEN);
DefineGlobalColor("Shortlowcolor", if InvertRedGreen then Color.GREEN else Color.GREEN);
DefineGlobalColor("highcolor", if InvertRedGreen then Color.GREEN else Color.RED);
DefineGlobalColor("Shorthighcolor", if InvertRedGreen then Color.GREEN else Color.RED);
#Labels
AddLabel(DisplayIVPercentile , Concat("IV Percentile: ", AsPercent(Round(Percentile / 100, 2))), if lowend then color.red else if highend then color.green else Color.light_gray);
AddLabel(DisplayShorterTerm , Concat("Short IV Rank: ", AsPercent(Round(sPercentile / 100, 2))), if ShortLowend then GlobalColor("Shortlowcolor") else if ShortHighend then GlobalColor("Shorthighcolor") else Color.light_gray);
AddLabel(DisplayImpVolatility, Concat("ImpVol: ", AsPercent(vol)), if lowend then GlobalColor("lowcolor") else if highend then GlobalColor("highcolor") else Color.light_gray);
def ImpPts = (vol / Sqrt(252)) * close;
AddLabel(DisplayDaily1StandardDev , Concat("1 Day SD +/- $", AsText( ImpPts, NumberFormat.TWO_DECIMAL_PLACES)), if lowend then color.light_gray else if highend then color.light_gray else Color.light_gray);
def ImpPts2 = (vol / Sqrt(52)) * close;
AddLabel(DisplayWeekly1StandardDev, Concat("1 Week SD +/- $", AsText( ImpPts2, NumberFormat.TWO_DECIMAL_PLACES)), if lowend then color.light_gray else if highend then color.light_gray else Color.light_gray);
def ImpPts3 = (vol / Sqrt(12)) * close;
AddLabel(DisplayMonthly1StandardDev, Concat("1 Month SD +/- $", AsText( ImpPts3, NumberFormat.TWO_DECIMAL_PLACES)), if lowend then color.light_gray else if highend then color.light_gray else Color.light_gray);
plot LowVol = LowVolLimit;
plot HighVol = HighVolLimit;
LowVol.SetDefaultColor(GetColor(6));
HighVol.SetDefaultColor(GetColor(5));
Last edited: