Categories: PythonWeb Scraping

How to get options data with Python

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

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"]

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.

Andrew Treadway

Recent Posts

Software Engineering for Data Scientists (New book!)

Very excited to announce the early-access preview (MEAP) of my upcoming book, Software Engineering for…

2 years ago

How to stop long-running code in Python

Ever had long-running code that you don't know when it's going to finish running? If…

3 years ago

Faster alternatives to pandas

Background If you've done any type of data analysis in Python, chances are you've probably…

3 years ago

Automated EDA with Python

In this post, we will investigate the pandas_profiling and sweetviz packages, which can be used…

3 years ago

How to plot XGBoost trees in R

In this post, we're going to cover how to plot XGBoost trees in R. XGBoost…

4 years ago

Python collections tutorial

In this post, we'll discuss the underrated Python collections package, which is part of the…

4 years ago