Earnings IV Grid

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

Main@thinkorswim [build 1978] 2023-09-09 12-24-17.jpg


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:
Solution
This is just a simple copy/past job to incorporate the upper's code into the lower's. Bear in mind, I didn't sift through all that code in fine detail. The line below is primarily responsible for achieving the proper offset, with the value being offset marked as XYZ, any other values you need can be placed there.

def OffsetValue = if isnan(GetValue(close, barNumber - HighestAll(eBar))) or IsNaN(close) or BarNumber() < startbar then Double.NaN else GetValue(XYZ, lookback);


I updated one of the lower's labels as an example (in green). The old ones are still intact but not offset. That part is just copy/paste, I'll leave that to you.

def LabelValueExample = if isnan(GetValue(close, barNumber - HighestAll(eBar))) or...
This is just a simple copy/past job to incorporate the upper's code into the lower's. Bear in mind, I didn't sift through all that code in fine detail. The line below is primarily responsible for achieving the proper offset, with the value being offset marked as XYZ, any other values you need can be placed there.

def OffsetValue = if isnan(GetValue(close, barNumber - HighestAll(eBar))) or IsNaN(close) or BarNumber() < startbar then Double.NaN else GetValue(XYZ, lookback);


I updated one of the lower's labels as an example (in green). The old ones are still intact but not offset. That part is just copy/paste, I'll leave that to you.

def LabelValueExample = if isnan(GetValue(close, barNumber - HighestAll(eBar))) or IsNaN(close) or BarNumber() < startbar then Double.NaN else GetValue(ImpPts, lookback);

AddLabel(DisplayDaily1StandardDev , Concat("1 Day SD +/- $", AsText( LabelValueExample, NumberFormat.TWO_DECIMAL_PLACES)), color.lime);


You can also just replace percentile with imp_volatility if you want the raw implied volatility, I wasn't sure what you were really going for.

EWN2SSt.png


Code:
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);
def 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));
 



 # 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 eVOLp = if isnan(GetValue(close, barNumber - HighestAll(eBar))) or IsNaN(close) or BarNumber() < startbar then Double.NaN else GetValue(percentile, lookback);
plot IVPct = eVOLp;

def LabelValueExample =
if isnan(GetValue(close, barNumber - HighestAll(eBar))) or IsNaN(close) or BarNumber() < startbar then Double.NaN else GetValue(ImpPts, lookback);

AddLabel(DisplayDaily1StandardDev , Concat("1 Day SD +/- $", AsText( LabelValueExample, NumberFormat.TWO_DECIMAL_PLACES)), color.lime);

 # 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);
 
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
364 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top