I am attempting to download stock option data via the library yfinance for a list of stock tickers represented by the variable: "tickers" for options with an expiration date represented by: "exp_date".
Frankly, I don't even know if my loop/append is correct as I am new to coding, but I am getting the error message:
"AttributeError: 'str' object has no attribute 'option_chain'".
I only get the error message if I try to do this loop for all stock tickers. If I manually input a single ticker and manually input the expiration date instead of using the "exp_date" variable, it works perfectly.
import yfinance as yf
import pandas as pd
import datetime
# Get Friday of current week for expiration date
current_time = datetime.datetime.now()
exp_date = (current_time.date() - datetime.timedelta(days=current_time.weekday()) + datetime.timedelta(days=4))
# Get options data and put in dataframe
opt_df = pd.DataFrame()
for ticker in tickers:
opt = ticker.option_chain(exp_date)
opt_df.pd.Dataframe.append(opt)
ticker
is the ticker symbol, not the ticker information returned by yfinance.
for symbol in tickers:
ticker = yf.Ticker(symbol)
opt = ticker.option_chain(exp_date)
opt_df.pd.Dataframe.append(opt)