V N
V N

Reputation: 33

How to use yahoo_fin to get data for an individual option contract

I'm trying to use yahoo_fin to find the delta of a portfolio of options, I have the code to calculate the delta working but it currently gets a chain of option data instead of the data for each individual option in the portfolio. Is there a method to get the data for one specific option a time? In my code I get the options for a certain stock with a certain expiry, is there a way to also use the strike?

Here's my current code:

from yahoo_fin import options
from yahoo_fin import stock_info as si
import numpy as np
from scipy.stats import norm
import pandas as pd
from datetime import *

stock = input("Enter stock symbol: Ex:QQQ ")
Day = int(input("Enter option expiration day: Ex:8 "))
month = int(input("Enter option expiration month: Ex:11 "))
year = int(input("Enter option expiration year: Ex:2021 "))
today = date.today()
future = date(year,month,Day)
expiry = future
str(future - today)
pd.set_option("display.max_rows", None, "display.max_columns", None)
options.get_options_chain(stock)
chain = options.get_options_chain(stock,expiry)

r = .025
S = si.get_live_price(stock)
K = chain["calls"].Strike
t = float((future - today).days)
T = t/365
s = chain["calls"]['Implied Volatility']
sigma = chain["calls"]["Implied Volatility"].apply(lambda x: float(x[:-1]) / 100)

def delta_calc(r, S, K, T, sigma):
    d1 = (np.log(S/K)+(r+sigma**2/2)*T)/(sigma*np.sqrt(T))
    delta_calc = norm.cdf(d1, 0, 1) 
    return delta_calc

print("Option Delta is: ", (delta_calc(r, S, K, T, sigma)))

Upvotes: 3

Views: 2463

Answers (1)

isuckatcoding
isuckatcoding

Reputation: 26

http://theautomatic.net/2019/04/17/how-to-get-options-data-with-python/

I think this might help.

"To get the data for a specific expiration date, we can just pass whatever date we need to the get_options_chain method. A variety of date formats are supported." It also shows the strikes

Upvotes: 1

Related Questions