Yfinance雅虎金融市场数据下载器
自从雅虎金融部门停用了他们的历史数据 API 后,许多依赖它的程序都无法正常工作。yfinance 旨在通过提供一种可靠、线程化和 Pythonic 的方式从 Yahoo 金融下载历史市场数据来解决这个问题。
快速开始
Ticker 模块
该Ticker模块允许你以更 Pythonic 的方式访问股票数据:
注意:雅虎金融的日期时间接收为 UTC。
import yfinance as yf
msft = yf.Ticker("MSFT")
# get stock info
msft.info
# get historical market data
hist = msft.history(period="max")
# show actions (dividends, splits)
msft.actions
# show dividends
msft.dividends
# show splits
msft.splits
# show financials
msft.financials
msft.quarterly_financials
# show major holders
msft.major_holders
# show institutional holders
msft.institutional_holders
# show balance sheet
msft.balance_sheet
msft.quarterly_balance_sheet
# show cashflow
msft.cashflow
msft.quarterly_cashflow
# show earnings
msft.earnings
msft.quarterly_earnings
# show sustainability
msft.sustainability
# show analysts recommendations
msft.recommendations
# show next event (earnings, etc)
msft.calendar
# show ISIN code - *experimental*
# ISIN = International Securities Identification Number
msft.isin
# show options expirations
msft.options
# show news
msft.news
# get option chain for specific expiration
opt = msft.option_chain('YYYY-MM-DD')
# data available via: opt.calls, opt.puts
如果要使用代理服务器下载数据,请使用:
import yfinance as yf
msft = yf.Ticker("MSFT")
msft.history(..., proxy="PROXY_SERVER")
msft.get_actions(proxy="PROXY_SERVER")
msft.get_dividends(proxy="PROXY_SERVER")
msft.get_splits(proxy="PROXY_SERVER")
msft.get_balance_sheet(proxy="PROXY_SERVER")
msft.get_cashflow(proxy="PROXY_SERVER")
msft.option_chain(..., proxy="PROXY_SERVER")
...
要使用自定义requests会话(例如缓存对 API 的调用或自定义User-agent标头),请将session=参数传递给 Ticker 构造函数。
import requests_cache
session = requests_cache.CachedSession('yfinance.cache')
session.headers['User-agent'] = 'my-program/1.0'
ticker = yf.Ticker('msft aapl goog', session=session)
# The scraped response will be stored in the cache
ticker.actions
要初始化多个Ticker对象,请使用
import yfinance as yf
tickers = yf.Tickers('msft aapl goog')
# ^ returns a named tuple of Ticker objects
# access each ticker using (example)
tickers.tickers.MSFT.info
tickers.tickers.AAPL.history(period="1mo")
tickers.tickers.GOOG.actions
获取多个代码的数据
import yfinance as yf
data = yf.download("SPY AAPL", start="2017-01-01", end="2017-04-30")
安装
使用pip安装yfinance
$ pip install yfinance --upgrade --no-cache-dir
要求
可选(如果你想使用pandas_datareader)
- pandas_datareader >= 0.4.0
评论
