thetradingfrog
New member
I am trying to pull the option bid price for a strike a given offset from the current underlying asset price.
For example if it is Monday November 14th and SPY is trading at 400 I would like to get the .SPY20221116C415 bid price.
Here is some code I have been trying to get working to display the price for the given option while looking at the SPY chart.
I can't figure out how to get the price to actually work, I have tried several things and it is always N/A
Any help is much appreciated. Thanks!
For example if it is Monday November 14th and SPY is trading at 400 I would like to get the .SPY20221116C415 bid price.
Here is some code I have been trying to get working to display the price for the given option while looking at the SPY chart.
Code:
# Weekly Options Implied Volatility Plotted intraday
# Mobius
# Chat Room Request
# 02.27.2016
#
# 2021-07-01 : Modified by rad14733 for personal needs
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 dow2 = GetDayOfWeek(GetYYYYMMDD());
AddLabel(yes, dow2, Color.GREEN);
def adjust = if dow2 < 3 or dow2 > 4 then 2 else 0;
def OptionDateString = ExpYear * 10000 + ExpMonth2 * 100 + ExpDOM - adjust;
def daysTill = DaysTillDate(OptionDateString);
def yyyy = Round(OptionDateString / 10000, 0);
def yy = Round((OptionDateString / 10000 - 2000), 0); # yy = 21
def mmdd = OptionDateString - 10000 * yyyy;
def mm = Round(mmdd / 100, 0);
def dd = mmdd - mm * 100;
AddLabel(yes, OptionDateString, Color.GREEN);
AddLabel(yes, "Days Till:" + daysTill, Color.GREEN);
AddLabel(yes, "Strike:" + (close + 15), Color.GREEN);
AddLabel(1, "Label Date = " +"20"+yy+mm+dd , Color.CYAN);
AddLabel(1, "OptionString = "+ ".SPY"+"20"+yy+mm+dd+"C"+Round(close("SPY") + 2,0), Color.CYAN);
def closeP = close(symbol = ".SPY+"+"20"+yy+mm+dd+"C"+Round(close("SPY")+2,0), priceType = PriceType.BID);
AddLabel(yes, "Option Price: " + closeP);
plot result = closeP;
I can't figure out how to get the price to actually work, I have tried several things and it is always N/A
Any help is much appreciated. Thanks!