1. 安裝 yfinance
函式庫
pip install yfinance
2. 下載股票歷史數據
範例:下載台積電 (TSM) 的日線數據
1 2 3 4 5 6 7 8 9 10 | import yfinance as yf # 設定股票代碼 ticker = 'TSM' # 下載歷史數據 data = yf.download(ticker, start='2024-01-01', end='2024-06-01') # 輸出數據的前五筆記錄 print(data.head()) |
執行結果:
Price Adj Close Close ... Open Volume
Ticker TSM TSM ... TSM TSM
Date ...
2024-01-02 100.430679 101.529999 ... 102.250000 9020900
2024-01-03 99.085411 100.169998 ... 100.339996 6650600
2024-01-04 98.056664 99.129997 ... 99.529999 7996700
2024-01-05 98.531471 99.610001 ... 99.000000 7344900
2024-01-08 101.132988 102.239998 ... 100.629997 12455600
[5 rows x 6 columns]
3. 下載多隻股票的數據
範例:下載特斯拉 (TSLA)、微軟 (MSFT)、亞馬遜 (AMZN) 的數據
1 2 3 4 5 6 7 8 9 10 | import yfinance as yf # 設定多隻股票的代碼 tickers = ['TSLA', 'MSFT', 'AMZN'] # 下載股票數據 data = yf.download(tickers, start='2024-01-01', end='2024-06-01') # 顯示數據 print(data['Close'].head()) |
執行結果:
Ticker AMZN MSFT TSLA
Date
2024-01-02 149.929993 370.869995 248.419998
2024-01-03 148.470001 370.600006 238.449997
2024-01-04 144.570007 367.940002 237.929993
2024-01-05 145.240005 367.750000 237.490005
2024-01-08 149.100006 374.690002 240.449997
4. 取得股票的財務報表
範例:查詢輝達 (NVDA) 的財務報表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import yfinance as yf # 設定股票代碼 stock = yf.Ticker("NVDA") # 取得財務報表 balance_sheet = stock.balance_sheet income_statement = stock.financials cashflow = stock.cashflow # 顯示部分報表 print("資產負債表:") print(balance_sheet) print("\n損益表:") print(income_statement) print("\n現金流量表:") print(cashflow) |
執行結果:
資產負債表:
2024-01-31 ... 2020-01-31
Treasury Shares Number NaN ... 13700576960.0
Ordinary Shares Number 24640000000.0 ... NaN
Share Issued 24640000000.0 ... NaN
Net Debt 2429000000.0 ... NaN
Total Debt 11056000000.0 ... NaN
... ... ... ...
Allowance For Doubtful Accounts Receivable NaN ... -11000000.0
Gross Accounts Receivable NaN ... 1668000000.0
Cash Cash Equivalents And Short Term Investments 25984000000.0 ... NaN
Other Short Term Investments 18704000000.0 ... NaN
Cash And Cash Equivalents 7280000000.0 ... NaN
[84 rows x 5 columns]
損益表:
2024-01-31 ... 2021-01-31
Tax Effect Of Unusual Items 0.0 ... 0.0
Tax Rate For Calcs 0.12 ... 0.017
Normalized EBITDA 35583000000.0 ... 5691000000.0
Total Unusual Items 0.0 ... 0.0
Total Unusual Items Excluding Goodwill 0.0 ... 0.0
Net Income From Continuing Operation Net Minori... 29760000000.0 ... 4332000000.0
Reconciled Depreciation 1508000000.0 ... 1098000000.0
Reconciled Cost Of Revenue 16621000000.0 ... 6279000000.0
EBITDA 35583000000.0 ... 5691000000.0
EBIT 34075000000.0 ... 4593000000.0
Net Interest Income 609000000.0 ... -127000000.0
Interest Expense 257000000.0 ... 184000000.0
Interest Income 866000000.0 ... 57000000.0
Normalized Income 29760000000.0 ... 4332000000.0
Net Income From Continuing And Discontinued Ope... 29760000000.0 ... 4332000000.0
Total Expenses 27950000000.0 ... 12143000000.0
Total Operating Income As Reported 32972000000.0 ... 4532000000.0
Diluted Average Shares 24940000000.0 ... 25120000000.0
Basic Average Shares 24690000000.0 ... 24680000000.0
Diluted EPS 1.193 ... 0.1725
Basic EPS 1.205 ... 0.1755
Diluted NI Availto Com Stockholders 29760000000.0 ... 4332000000.0
Net Income Common Stockholders 29760000000.0 ... 4332000000.0
Net Income 29760000000.0 ... 4332000000.0
Net Income Including Noncontrolling Interests 29760000000.0 ... 4332000000.0
Net Income Continuous Operations 29760000000.0 ... 4332000000.0
Tax Provision 4058000000.0 ... 77000000.0
Pretax Income 33818000000.0 ... 4409000000.0
Other Income Expense 237000000.0 ... 4000000.0
Other Non Operating Income Expenses 237000000.0 ... 4000000.0
Special Income Charges 0.0 ... 0.0
Restructuring And Mergern Acquisition 0.0 ... 0.0
Net Non Operating Interest Income Expense 609000000.0 ... -127000000.0
Interest Expense Non Operating 257000000.0 ... 184000000.0
Interest Income Non Operating 866000000.0 ... 57000000.0
Operating Income 32972000000.0 ... 4532000000.0
Operating Expense 11329000000.0 ... 5864000000.0
Research And Development 8675000000.0 ... 3924000000.0
Selling General And Administration 2654000000.0 ... 1940000000.0
Gross Profit 44301000000.0 ... 10396000000.0
Cost Of Revenue 16621000000.0 ... 6279000000.0
Total Revenue 60922000000.0 ... 16675000000.0
Operating Revenue 60922000000.0 ... 16675000000.0
[43 rows x 4 columns]
<未完>
沒有留言:
張貼留言