Hướng dẫn python yfinance

yfinance không được liên kết, xác nhận hoặc kiểm tra bởi Yahoo, Inc. Đây là một công cụ mã nguồn mở sử dụng các API có sẵn công khai của Yahoo và dành cho mục đích nghiên cứu và giáo dục

Bạn nên tham khảo Yahoo. điều khoản sử dụng của [tại đây, tại đây và tại đây] để biết chi tiết về quyền của bạn trong việc sử dụng dữ liệu thực tế đã tải xuống. Hãy nhớ - Yahoo. API tài chính chỉ dành cho mục đích sử dụng cá nhân

yfinance cung cấp một cách theo luồng và Pythonic để tải xuống dữ liệu thị trường từ Yahoo. Ⓡ tài chính

→ Hãy xem phần này để biết hướng dẫn chi tiết với các ví dụ về mã

Nhật ký thay đổi »

Có gì mới trong phiên bản 0. 2

Bắt đầu nhanh

Mô-đun Mã cổ phiếu

Mô-đun

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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...
5, cho phép bạn truy cập dữ liệu mã đánh dấu theo cách Pythonic hơn

import yfinance as yf

msft = yf.Ticker["MSFT"]

# get stock info
msft.info

# get historical market data
hist = msft.history[period="max"]

# show meta information about the history [requires history[] to be called first]
msft.history_metadata

# show actions [dividends, splits, capital gains]
msft.actions

# show dividends
msft.dividends

# show splits
msft.splits


# show capital gains [for mutual funds & etfs]
msft.capital_gains

# show share count
msft.shares

# show financials:
# - income statement
msft.income_stmt
msft.quarterly_income_stmt
# - balance sheet
msft.balance_sheet
msft.quarterly_balance_sheet
# - cash flow statement
msft.cashflow
msft.quarterly_cashflow
# see `Ticker.get_income_stmt[]` for more options

# show major holders
msft.major_holders

# show institutional holders
msft.institutional_holders

# show mutualfund holders
msft.mutualfund_holders

# show earnings
msft.earnings
msft.quarterly_earnings

# show sustainability
msft.sustainability

# show analysts recommendations
msft.recommendations
msft.recommendations_summary
# show analysts other work
msft.analyst_price_target
msft.revenue_forecasts
msft.earnings_forecasts
msft.earnings_trend

# show next event [earnings, etc]
msft.calendar

# Show future and historic earnings dates, returns at most next 4 quarters and last 8 quarters by default. 
# Note: If more are needed use msft.get_earnings_dates[limit=XX] with increased limit argument.
msft.earnings_dates

# 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

Nếu bạn muốn sử dụng máy chủ proxy để tải xuống dữ liệu, hãy sử dụng

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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...

Để sử dụng phiên

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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...
0 tùy chỉnh [ví dụ: để lưu lệnh gọi API vào bộ nhớ cache hoặc tùy chỉnh tiêu đề
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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...
1], hãy chuyển đối số
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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...
2 cho hàm tạo Ticker

import requests_cache
session = requests_cache.CachedSession['yfinance.cache']
session.headers['User-agent'] = 'my-program/1.0'
ticker = yf.Ticker['msft', session=session]
# The scraped response will be stored in the cache
ticker.actions

Để khởi tạo nhiều đối tượng

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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...
5, hãy sử dụng

import yfinance as yf

tickers = yf.Tickers['msft aapl goog']

# access each ticker using [example]
tickers.tickers['MSFT'].info
tickers.tickers['AAPL'].history[period="1mo"]
tickers.tickers['GOOG'].actions

Tìm nạp dữ liệu cho nhiều mã

import yfinance as yf
data = yf.download["SPY AAPL", start="2017-01-01", end="2017-04-30"]

Tôi cũng đã thêm một số tùy chọn để làm cho cuộc sống dễ dàng hơn. ]

________số 8

Lưu trữ bộ đệm múi giờ

Khi tìm nạp dữ liệu giá, tất cả các ngày được bản địa hóa theo múi giờ của sàn giao dịch chứng khoán. Tuy nhiên, việc truy xuất múi giờ tương đối chậm, vì vậy, yfinance sẽ cố gắng lưu chúng vào bộ nhớ cache trong thư mục bộ nhớ cache của người dùng của bạn. Bạn có thể chuyển bộ đệm sang một vị trí khác với

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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...
4

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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...
0

Quản lý cột đa cấp

Câu trả lời sau đây trên Stack Overflow dành cho Cách xử lý các tên cột đa cấp được tải xuống bằng yfinance?

  • 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_capital_gains[proxy="PROXY_SERVER"]
    msft.get_balance_sheet[proxy="PROXY_SERVER"]
    msft.get_cashflow[proxy="PROXY_SERVER"]
    msft.option_chain[..., proxy="PROXY_SERVER"]
    ...
    
    5 trả về một
    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_capital_gains[proxy="PROXY_SERVER"]
    msft.get_balance_sheet[proxy="PROXY_SERVER"]
    msft.get_cashflow[proxy="PROXY_SERVER"]
    msft.option_chain[..., proxy="PROXY_SERVER"]
    ...
    
    6 với các tên cột đa cấp, với một cấp cho mã cổ phiếu và một cấp cho dữ liệu giá cổ phiếu
    • câu trả lời thảo luận
      • Cách đọc chính xác các cột đa cấp sau khi lưu khung dữ liệu vào csv với
        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_capital_gains[proxy="PROXY_SERVER"]
        msft.get_balance_sheet[proxy="PROXY_SERVER"]
        msft.get_cashflow[proxy="PROXY_SERVER"]
        msft.option_chain[..., proxy="PROXY_SERVER"]
        ...
        
        7
      • Cách tải xuống một hoặc nhiều mã đánh dấu vào một khung dữ liệu duy nhất với tên cột cấp đơn và cột mã đánh dấu

ghi đè
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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...
8

Nếu mã của bạn sử dụng

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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...
8 và bạn muốn tải xuống dữ liệu nhanh hơn, bạn có thể "chiếm đoạt" phương pháp
import requests_cache
session = requests_cache.CachedSession['yfinance.cache']
session.headers['User-agent'] = 'my-program/1.0'
ticker = yf.Ticker['msft', session=session]
# The scraped response will be stored in the cache
ticker.actions
0 để sử dụng yfinance trong khi đảm bảo dữ liệu trả về có cùng định dạng với
import requests_cache
session = requests_cache.CachedSession['yfinance.cache']
session.headers['User-agent'] = 'my-program/1.0'
ticker = yf.Ticker['msft', session=session]
# The scraped response will be stored in the cache
ticker.actions
1 của pandas_datareader

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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...
8

Cài đặt

Cài đặt

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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...
5 bằng cách sử dụng
import requests_cache
session = requests_cache.CachedSession['yfinance.cache']
session.headers['User-agent'] = 'my-program/1.0'
ticker = yf.Ticker['msft', session=session]
# The scraped response will be stored in the cache
ticker.actions
3

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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...
1

Để cài đặt

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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...
5 sử dụng
import requests_cache
session = requests_cache.CachedSession['yfinance.cache']
session.headers['User-agent'] = 'my-program/1.0'
ticker = yf.Ticker['msft', session=session]
# The scraped response will be stored in the cache
ticker.actions
5, xem phần này

Yêu cầu

Tùy chọn [nếu bạn muốn sử dụng
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_capital_gains[proxy="PROXY_SERVER"]
msft.get_balance_sheet[proxy="PROXY_SERVER"]
msft.get_cashflow[proxy="PROXY_SERVER"]
msft.option_chain[..., proxy="PROXY_SERVER"]
...
8]

Thủ tục pháp lí

yfinance được phân phối theo Giấy phép Phần mềm Apache. Xem GIẤY PHÉP. txt trong bản phát hành để biết chi tiết

LẠI - yfinance không được liên kết, xác nhận hoặc kiểm duyệt bởi Yahoo, Inc. Đây là một công cụ mã nguồn mở sử dụng các API có sẵn công khai của Yahoo và dành cho mục đích nghiên cứu và giáo dục. Bạn nên tham khảo Yahoo. điều khoản sử dụng của [tại đây, tại đây và tại đây] để biết chi tiết về quyền của bạn trong việc sử dụng dữ liệu thực tế đã tải xuống

Chủ Đề