Skip to content

Instantly share code, notes, and snippets.

@Chaztikov
Forked from tlian25/options_data.py
Created December 3, 2020 09:38
Show Gist options
  • Select an option

  • Save Chaztikov/dce2ded829399d9cf4abce8ee648bd76 to your computer and use it in GitHub Desktop.

Select an option

Save Chaztikov/dce2ded829399d9cf4abce8ee648bd76 to your computer and use it in GitHub Desktop.
Yahoo Finance Webscraping
import pandas as pd
import numpy as np
import yfinance as yf
def options_chain(symbol):
tk = yf.Ticker(symbol)
# Expiration dates
exps = tk.options
# Get options for each expiration
options = pd.DataFrame()
for e in exps:
opt = tk.option_chain(e)
opt = pd.DataFrame().append(opt.calls).append(opt.puts)
opt['expirationDate'] = e
options = options.append(opt, ignore_index=True)
# Bizarre error in yfinance that gives the wrong expiration date
# Add 1 day to get the correct expiration date
options['expirationDate'] = pd.to_datetime(options['expirationDate']) + datetime.timedelta(days = 1)
options['dte'] = (options['expirationDate'] - datetime.datetime.today()).dt.days / 365
# Boolean column if the option is a CALL
options['CALL'] = options['contractSymbol'].str[4:].apply(
lambda x: "C" in x)
options[['bid', 'ask', 'strike']] = options[['bid', 'ask', 'strike']].apply(pd.to_numeric)
options['mark'] = (options['bid'] + options['ask']) / 2 # Calculate the midpoint of the bid-ask
# Drop unnecessary and meaningless columns
options = options.drop(columns = ['contractSize', 'currency', 'change', 'percentChange', 'lastTradeDate', 'lastPrice'])
return options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment