In this article we look at a package that will retrieve financial information for Yahoo finance
You should refer to Yahoo!'s terms of use (here, here, and here) for details on your rights to use the actual data downloaded. Remember – the Yahoo! finance API is intended for personal use only.
Installation
pip install yfinance
Requirements
- Python >= 2.7, 3.4+
- Pandas (tested to work with >=0.23.1)
- Numpy >= 1.11.1
- requests >= 2.14.2
- lxml >= 4.5.1
When you use the package you need to know the ticker for a company, what is a ticker I hear you ask. Well here is a summary
A ticker symbol or stock symbol is an abbreviation used to uniquely identify publicly traded shares of a particular stock on a particular stock market.
In short, Ticker symbols are basically arrangements of symbols or characters (generally Latin letters or digits) representing specific assets or securities listed on a stock exchange or traded publicly.
A stock symbol may consist of letters, numbers or a combination of both. “Ticker symbol” refers to the symbols that were printed on the ticker tape of a ticker tape machine.
Some examples of US Stock symbols include:
- AAPL – Apple
- GOOG – Alphabet
- HPQ – Hewlett-Packard
- INTC – Intel
- MSFT – Microsoft
- WMT – Walmart
- AMZN – Amazon
- FB – Facebook
Examples
This example will get all of the information
# Import yfinance module import yfinance as yf # Using ticker for the Microsoft in yfinance function FBInfo = yf.Ticker("MSFT") # Looping over items to split them in key-value pair print("Items from the financial data of the Microsoft page in the key-value page: ") for keyItem, valueItem in FBInfo.info.items(): print(keyItem, ":", valueItem)
This will output a lot of data, this is the first few lines
>>> %Run yfinancebasic.py Items from the financial data of the Microsoft page in the key-value page: zip : 98052-6399 sector : Technology fullTimeEmployees : 181000 longBusinessSummary : city : Redmond phone : 425 882 8080 state : WA country : United States
Retrieving financial metrics
# Import yfinance module import yfinance as yf # Using ticker for the Microsoft in yfinance function msInfo = yf.Ticker("MSFT") # Microsoft financial data we retrieved sectorInfo = msInfo.info['sector'] # Company Sector key shortNameInfo = msInfo.info['shortName'] # Price Earning ratio key weekInfo = msInfo.info['52WeekChange'] # Company Beta key # Print the Company Sector Information print("Sector is: ", sectorInfo) # Print the Short name print("The Short name is ", shortNameInfo) # Print the 52 week info print("52 week info: ", weekInfo)
When run I saw the following displayed
>>> %Run yfinanceother.py
Sector is: Technology
The Short name is Microsoft Corporation
52 week info: 0.2617643
More detailed example
This is the example that is on the github page
import yfinance as yf msft = yf.Ticker("MSFT") # get stock info print (msft.info) # show actions (dividends, splits) print (msft.actions) # show dividends print (msft.dividends) # show splits print (msft.splits) # show financials print (msft.financials) print (msft.quarterly_financials) # show major holders print (msft.major_holders) # show institutional holders print (msft.institutional_holders) # show balance sheet print (msft.balance_sheet) print (msft.quarterly_balance_sheet) # show cashflow print (msft.cashflow) print (msft.quarterly_cashflow) # show earnings print (msft.earnings) print (msft.quarterly_earnings) # show sustainability print (msft.sustainability) # show analysts recommendations print (msft.recommendations) # show next event (earnings, etc) print (msft.calendar) # show ISIN code - *experimental* # ISIN = International Securities Identification Number print (msft.isin) # show options expirations print (msft.options) # show news print (msft.news)