yfinance

yfinance aimes to solve this problem by offering a reliable, threaded, and Pythonic way to download historical market data from Yahoo! finance.

   1 import yfinance
   2 from pandas._libs.tslibs.timestamps import Timestamp
   3 data = yfinance.download("AMZN",start="2019-01-01",end="2020-02-12")
   4 d = data.to_dict()
   5 d['Close'].key
   6 # for k in d['Close'].iterkeys(): print (k)
   7 d['Close'][Timestamp('2019-11-22 00:00:00')]
   8 d['Close'][Timestamp('2020-02-10 00:00:00')]

MSFT example

   1 cd ~/Documents/
   2 mkdir yfinance-test
   3 cd yfinance-test/
   4 sudo apt install python3-venv
   5 python3 -m venv testVenv
   6 source testVenv/bin/activate
   7 pip install yfinance
   8 python3 test-msft.py

test-msft.py

   1 import yfinance as yf
   2 msft = yf.Ticker("MSFT")
   3 
   4 historyDataFrame = msft.history(period="5d")
   5 print("Available columns")
   6 for colname in historyDataFrame:
   7     print("  "+colname)
   8 
   9 print("Available indexes")
  10 for row in historyDataFrame.index:
  11     print("  "+str(row))
  12 
  13 columnClose = historyDataFrame['Close']
  14 rowidx = 0
  15 print("Symbol:"+msft.info['symbol'])
  16 for closeValue in columnClose.values:
  17     print(historyDataFrame.index[rowidx],  closeValue)
  18     rowidx += 1

Python/yfinance (last edited 2022-10-27 22:14:22 by localhost)