MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap
Revision 8 as of 2022-10-27 22:12:19
  • Python
  • yfinance

yfinance

  • https://pypi.org/project/yfinance/

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

  • pip install yfinance --user

   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 """
   2 cd ~/Documents/
   3 mkdir yfinance-test
   4 cd yfinance-test/
   5 sudo apt install python3-venv
   6 python3 -m venv testVenv
   7 source testVenv/bin/activate
   8 pip install yfinance
   9 """
  10 import yfinance as yf
  11 msft = yf.Ticker("MSFT")
  12 
  13 historyDataFrame = msft.history(period="5d")
  14 print("Available columns")
  15 for colname in historyDataFrame:
  16     print("  "+colname)
  17 
  18 print("Available indexes")
  19 for row in historyDataFrame.index:
  20     print("  "+str(row))
  21 
  22 columnClose = historyDataFrame['Close']
  23 rowidx = 0
  24 print("Symbol:"+msft.info['symbol'])
  25 for closeValue in columnClose.values:
  26     print(historyDataFrame.index[rowidx],  closeValue)
  27     rowidx += 1
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01