首页 > 解决方案 > 使用 python 下载股票和期权数据

问题描述

我需要下载股票代码的历史“股票数据”和当前“期权价格数据”。有人可以指点我正确的包裹吗?我尝试了 yahoo-finance 软件包,但它不起作用。有人可以发布代码片段以下载相同的内容。我看过几篇下载股票数据的帖子,但没有看到下载期权数据的帖子。因此,对于下载两者的任何帮助将不胜感激。

以下是来自雅虎财经的历史数据和期权数据的链接,仅供参考。

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

标签: pythonfinanceyahoo-financestockgoogle-finance

解决方案


您可以使用 yahoo_fin 包获取当前期权数据和历史股票价格数据(参见此处:http ://theautomatic.net/yahoo_fin-documentation/ )。它带有两个模块,stock_info 和 options。

要获取当前选项数据,您可以执行以下操作:

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

对于历史股价数据,您可以:

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

推荐阅读