Download stock and option data using python

Raghava kattamudi picture Raghava kattamudi · Dec 19, 2018 · Viewed 7k times · Source

I need to download historical "stock data" and current "option price data" for a ticker. Can someone please point me to right package. I tried yahoo-finance package, but it is not working. Can someone please post a code snippet to download the same. I have seen several posts to download the stock data, but none to download the option data. So, any help to download both would be greatly appreciated.

Here are the links for historical data and options data from yahoo finance, just for your reference.

https://finance.yahoo.com/quote/MSFT/history?p=MSFT https://finance.yahoo.com/quote/MSFT/options?p=MSFT

Thanks Raghava

Answer

atreadw picture atreadw · Apr 13, 2019

You can get current options data and historical stock price data with the yahoo_fin package (see here: http://theautomatic.net/yahoo_fin-documentation/). It comes with two modules, stock_info and options.

To get current options data, you can do:

from yahoo_fin import options

# gets the data for nearest upcoming expiration date
options.get_option_chain("nflx")

# specific expiration date
options.get_options_chain("nflx", "04/26/2019")


# get call options only
options.get_calls("nflx", "04/26/2019")


# get put options only
options.get_puts("nflx", "04/26/2019")

For historical stock price data, you can do:

from yahoo_fin import stock_info as si

# pulls historical OHLC data into a pandas data frame
si.get_data("nflx")

# or some other ticker
si.get_data("insert ticker here")