# Weekly Options Implied Volatility Plotted intraday
# Mobius
# Chat Room Request
# 02.27.2016
#
# 2021-07-01 : Modified by rad14733 for personal needs
declare Once_Per_Bar;
input series = 1;
input showBands = no;
input show_itm_labels = yes;
input show_atm_labels = yes;
input show_otm_labels = yes;
input Days_In_Contract = 0;
Assert(series > 0, "'series' must be positive: " + series);
def RTHopen = open(period = AggregationPeriod.Day);
def CurrentYear = GetYear();
def CurrentMonth = GetMonth();
def CurrentDOM = GetDayOfMonth(GetYYYYMMDD());
def Day1DOW1 = GetDayOfWeek(CurrentYear * 10000 + CurrentMonth * 100 + 1); # First DOM is this DOW
def FirstFridayDOM1 = if Day1DOW1 < 6
then 6 - Day1DOW1
else if Day1DOW1 == 6
then 7
else 6;
def SecondFridayDOM = FirstFridayDOM1 + 7;
def ThirdFridayDOM = FirstFridayDOM1 + 14;
def FourthFridayDOM = FirstFridayDOM1 + 21;
def RollDOM = FirstFridayDOM1 + 21; #14; changed to 21 to pick up all Fridays of the current month for weekly options
def ExpMonth1 = if RollDOM > CurrentDOM
then CurrentMonth + series - 1
else CurrentMonth + series;
def ExpMonth2 = if ExpMonth1 > 12
then ExpMonth1 - 12
else ExpMonth1;
def ExpYear = if ExpMonth1 > 12
then CurrentYear + 1
else CurrentYear;
def Day1DOW = GetDayOfWeek(ExpYear * 10000 + ExpMonth2 * 100 + 1);
def FirstFridayDOM = if Day1DOW < 6
then 6 - Day1DOW
else if Day1DOW == 6
then 7
else 6;
def ExpDOM = if currentDOM < FirstFridayDOM -1
then FirstFridayDOM1
else if between(currentDOM, FirstFridayDOM, SecondFridayDOM-1)
then SecondFridayDOM
else if between(currentDOM, SecondFridayDOM, ThirdFridayDOM-1)
then ThirdFridayDOM
else if between(currentDOM, ThirdFridayDOM, FourthFridayDOM-1)
then FourthFridayDOM
else FirstFridayDOM;
def NextFriday = DaysTillDate(ExpYear * 10000 + ExpMonth2 * 100 + ExpDOM);
def ExpirationDate = GetYYYYMMDD() + NextFriday;
def ExpData = (ExpirationDate / 1) + 1;
def yr = Round(GetYear() / 100, 0);
def yr2 = GetYear() - 2000;
def OptionDateString = ExpYear * 10000 + ExpMonth2 * 100 + ExpDOM + 1;
def ATMCprice = if isNaN(close(symbol = GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.CALL)))
then ATMCprice[1]
else close(symbol = GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.CALL));
def ATMPprice = if isNaN(close(symbol = GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.PUT)))
then ATMCprice[1]
else close(symbol = GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.PUT));
def HPD = if Days_In_Contract == 0 #HPD = Holding Period Days
then NextFriday
else Days_In_Contract;
def t = HPD / 365;
def ClosedForm_IV_est = if isNaN(((ATMCprice[1] * Sqrt(2 * Double.Pi)) / (RTHopen * Sqrt(t)))) then ClosedForm_IV_est[1] else ((ATMCprice * Sqrt(2 * Double.Pi)) / (RTHopen * Sqrt(t)));
def Intraday_IV = if ClosedForm_IV_est > 0 then ClosedForm_IV_est else double.nan;
def ImpMove = Round((close[1] * Intraday_IV / Sqrt(365)) / tickSize(), 0) * tickSize();
plot upper = RTHopen + ImpMove[1];
upper.SetStyle(Curve.Firm);
upper.SetDefaultColor(Color.Cyan);
upper.SetHiding(!showBands);
plot lower = RTHopen - ImpMove[1];
lower.SetStyle(Curve.Firm);
lower.SetDefaultColor(Color.Cyan);
lower.SetHiding(!showBands);
def ActualMove = close[1] - RTHopen;
# Next OTM Prices
def OTMCprice = if isNaN(close(symbol = GetNextOTMOption(GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.CALL)))) then OTMCprice[1] else close(symbol = GetNextOTMOption(GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.CALL)));
def OTMPprice = if isNaN(close(symbol = GetNextOTMOption(GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.PUT)))) then ATMCprice[1] else close(symbol = GetNextOTMOption(GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.PUT)));
# Next ITM Prices
def ITMCprice = if isNaN(close(symbol = GetNextITMOption(GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.CALL)))) then ITMCprice[1] else close(symbol = GetNextITMOption(GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.CALL)));
def ITMPprice = if isNaN(close(symbol = GetNextITMOption(GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.PUT)))) then ITMPprice[1] else close(symbol = GetNextITMOption(GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.PUT)));
# AddLabels
AddLabel(show_itm_labels and IsOptionable(), "ITM Call: " + GetNextITMOption(GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.CALL)) + " = " + AsDollars(ITMCprice), Color.GREEN);
AddLabel(show_itm_labels and IsOptionable(), "ITM Put: " + GetNextITMOption(GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.PUT)) + " = " + AsDollars(ITMPprice), Color.GREEN);
AddLabel(show_atm_labels and IsOptionable(), "ATM Call: " + GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.CALL) + " = $" + ATMCprice, Color.LIGHT_GRAY);
AddLabel(show_atm_labels and IsOptionable(), "ATM Put: " + GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.PUT) + " = $" + ATMPprice, Color.LIGHT_GRAY);
AddLabel(show_otm_labels and IsOptionable(), "OTM Call: " + GetNextOTMOption(GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.CALL)) + " = " + AsDollars(OTMCprice), Color.RED);
AddLabel(show_otm_labels and IsOptionable(), "OTM Put: " + GetNextOTMOption(GetATMOption(GetUnderlyingSymbol(), OptionDateString, OptionClass.PUT)) + " = " + AsDollars(OTMPprice), Color.RED);
# End Code Weekly Options