Yfinance雅虎金融市場數(shù)據(jù)下載器
自從雅虎金融部門停用了他們的歷史數(shù)據(jù) API 后,許多依賴它的程序都無法正常工作。yfinance 旨在通過提供一種可靠、線程化和 Pythonic 的方式從 Yahoo 金融下載歷史市場數(shù)據(jù)來解決這個問題。
快速開始
Ticker 模塊
該Ticker模塊允許你以更 Pythonic 的方式訪問股票數(shù)據(jù):
注意:雅虎金融的日期時間接收為 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
如果要使用代理服務器下載數(shù)據(jù),請使用:
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 的調(diào)用或自定義User-agent標頭),請將session=參數(shù)傳遞給 Ticker 構(gòu)造函數(shù)。
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
獲取多個代碼的數(shù)據(jù)
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
要求
- Python >= 2.7, 3.4+
- Pandas(經(jīng)測試可與 >=0.23.1 一起使用)
- Numpy >= 1.11.1
- requests >= 2.14.2
- lxml >= 4.5.1
可選(如果你想使用pandas_datareader)
- pandas_datareader >= 0.4.0
評論
圖片
表情
