Get price of option price at specific strike

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.

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

Screenshot-from-2022-11-11-12-56-47.png


Any help is much appreciated. Thanks!
 
Solution
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


sometimes, the simplest mistakes trip you up
i missed this at first

the option symbol only needs 2 digits for a year.
hmm , still error...


i have seen where numbers may not work for option date.
so i changed the year variable to be one with a value directly derived...
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


when things don't work, make it as simple as possible, then add formulas to see where it breaks.


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


closep is na , an error
the symbol displayed in the previous label, looks ok , but ...

sMevAhH.jpg


bypass all the formulas and try the simplest method for getting a price, a symbol in a watchlist.
go to a watchlist and type in the option symbol.

.SPY20221123C398
it doesn't work...


go to the trade tab and to the option chain,
right click on a call bid price.
look at the symbol that is displayed, it has 2 digits for the year.


your option symbol has 4 digits for the year instead of 2.



jOFghQV.jpg


get an option symbol
TRADE > OPTION CHAIN
right click on a BID price
copy the symbol
 
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


sometimes, the simplest mistakes trip you up
i missed this at first

the option symbol only needs 2 digits for a year.
hmm , still error...


i have seen where numbers may not work for option date.
so i changed the year variable to be one with a value directly derived from year(). yr2

i added formulas at the end, sequentually changing 1 thing at a time.
it appeared to be working.....

then i looked back up at the code again, i noticed....


there is a + within the quotes
".spy+" +



def closeP = close(symbol = ".SPY+" + yr2 + mm + dd + "C"+Round(close("SPY")+2,0), priceType = PriceType.BID);
AddLabel(yes, "Option Price: " + closeP, color.yellow);


Code:
# get_option_price_00


# https://usethinkscript.com/threads/get-price-of-option-price-at-specific-strike.13332/
# Get price of option price at specific strike
# thetradingfrog  Nov 11, 2022

#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



# test with 12/16


#--------------------------------------------------

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


input strike_offset = 15;

AddLabel(yes, "DOW " + dow2, Color.GREEN);
AddLabel(yes, "date " + OptionDateString, Color.GREEN);
AddLabel(yes, "Days Till: " + daysTill, Color.GREEN);
AddLabel(yes, "close + off , Strike: " + (close + strike_offset), 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);

AddLabel(1, "Label Date = " + yy+"/"+mm+"/"+dd , Color.CYAN);
#AddLabel(1, "OptionString = "+ ".SPY"+yy+mm+dd+"C"+Round(close("SPY") + 2,0), Color.CYAN);

# yr2
AddLabel(1, "OptionString = "+ ".SPY"+ yr2 + 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);

# doesnt work
#def closeP = close(symbol = ".SPY+"+yy+mm+dd+"C"+Round(close("SPY")+2,0), priceType = PriceType.BID);
#AddLabel(yes, "Option Price: " + closeP);


# yr2
# error    + is within the quotes
def closeP = close(symbol = ".SPY+" + yr2 + mm + dd + "C"+Round(close("SPY")+2,0), priceType = PriceType.BID);
AddLabel(yes, "Option Price: " + closeP, color.yellow);

def s = Round(close("SPY")+2,0);
addlabel(1, "strike " + s, color.yellow);

# error
def closeP2 = close(symbol = (".SPY" + yr2 + mm + dd + "C" + s), priceType = PriceType.BID);
AddLabel(yes, "Option Price: " + closeP2, color.yellow);

# error
def closeP3 = close(symbol = (".SPY" + yr2 + mm + dd + "C" + s));
AddLabel(yes, "Option Price: " + closeP3, color.yellow);



#plot result = closeP;

#def x = close(symbol = ".SPY20221123C398", priceType = PriceType.BID);

# symbol is bad , doesn't work in watchlist , wed 
#def x = close(symbol = ".SPY20221123C398");



# this works
# monday
#def x = close(symbol = ".SPY221128C398");
#   .SPY221128C398


#def x = close(symbol = ".SPY221123C398", priceType = PriceType.BID);

addlabel(1, " ", color.black);
# this works
def x = close(symbol = ".SPY221123C398", priceType = PriceType.BID);
addlabel(1, x, color.magenta);

# ok
def x2 = close(symbol = ".SPY" + yr2+ "1123C398", priceType = PriceType.BID);
addlabel(1, x2, color.magenta);

# ok
def x3 = close(symbol = ".SPY" + yr2 + mm + "23C398", priceType = PriceType.BID);
addlabel(1, x3, color.magenta);

# ok
def x4 = close(symbol = ".SPY" + yr2 + mm + dd + "C398", priceType = PriceType.BID);
addlabel(1, x4, color.magenta);

# ok with a constant for strike
def h = 398;
def x5 = close(symbol = ".SPY" + yr2 + mm + dd + "C" + h, priceType = PriceType.BID);
addlabel(1, x5, color.magenta);


# def s = Round(close("SPY")+2,0);
def x6 = close(symbol = ".SPY" + yr2 + mm + dd + "C" + s, priceType = PriceType.BID);
addlabel(1, x6, color.magenta);
#
 
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
519 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