How to get options data with Python

python get options data


In a previous post, we talked about how to get real-time stock prices with Python. This post will go through how to download financial options data with Python. We will be using the yahoo_fin package.

The yahoo_fin package comes with a module called options. This module allows you to scrape option chains and get option expiration dates. To get started we’ll just import this module from yahoo_fin.


from yahoo_fin import options

How to get options expiration dates

Any option contract has an expiration date. To get all of the option expiration dates for a particular stock, we can use the get_expiration_dates method in the options package. This method is equivalent to scraping all of the date selection boxes on the options page for an individual stock (e.g. https://finance.yahoo.com/quote/NFLX/options?p=NFLX).


nflx_dates = options.get_expiration_dates("nflx")

python get options expiration dates

How to get the option chain for a single ticker and expiration date

We can call the get_options_data method to get calls and puts data for some ticker. If no date is passed to this method, it will return the options chain information associated with the earliest upcoming expiration date.


chain = options.get_options_chain("nflx")

This method returns a dictionary with two elements. The keys of this dictionary are “calls” and “puts”. Each of these refers to the calls / puts tables scraped from Yahoo Finance, respectively.



chain["calls"]

chain["puts"]

python get call options chain

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.


options.get_options_chain("nflx", "April 26, 2019")

options.get_options_chain("nflx", "05/03/19")

options.get_options_chain("nflx", "05/10/2019")

If you want to just get the calls or puts information directly, rather than both in a single dictionary, you can use the get_calls and get_puts methods respectively.



options.get_calls("nflx")

options.get_calls("nflx", "04/26/19")


options.get_puts("nflx")

options.get_puts("nflx", "04/26/19")


How to get options data for each expiration date

Extending the code from above, if we want to get the options data for each expiration date for a given ticker, we can do it like below.



nflx_dates= options.get_expiration_dates("nflx")

info = {}
for date in nflx_dates:
    info[date] = options.get_options_chain("nflx")

Now we can view the data for an individual expiration date by using the date as a key:


info["October 18, 2019"]

How to get options data for every stock in the Dow

Suppose instead that we want to get the options data for each stock in the Dow. To get the data for the next expiration date for each stock, we just need to call options.get_options_chain for each of the Dow’s tickers. We can obtain those using the stock_info module within yahoo_fin.


from yahoo_fin import stock_info as si

dow_tickers = si.tickers_dow()

# replace DOW with DWDP in ticker list
dow_tickers.remove("DOW")
dow_tickers.append("DWDP")

# scrape the options data for each Dow ticker
dow_data = {}
for ticker in dow_tickers:
    try:
        dow_data[ticker] = options.get_options_chain(ticker)
    except Exception:
        print(ticker + " failed")


Now we can refer to any Dow stock’s data by its ticker.


dow_data["AAPL"]["calls"]

dow_data["WMT"]["calls"]

That concludes this post! Please check out my other Python posts here.