Yahoo!Finance is one of the most popular websites for investors. It offers various market data on stocks, bonds, currencies, and cryptocurrencies. It also offers market news, reports and analysis, options, and fundamental data, which sets it apart from some of its competitors.
yfinance is a Python library that allows users to download historical data from Yahoo Finance for free. It also includes fundamental data such as income statements, trading multiples, and dividends, among many others.
In this article, we are going to look at what yfinance is, how to install it, and how to get historical market and fundamental data using Python.
What is yfinance?
yfinance is a popular open source library developed by Ran Aroussi as a means to access financial data available on Yahoo!Finance.
Yahoo Finance used to have its own official API, but this was taken down on May 15, 2017, after widespread data misuse. Today there are a variety of unofficial APIs and libraries for accessing the same data, including, of course, yfinance.
The main drawback would be in the quality of the data, since it is not guaranteed that they are correct. However, it’s still a free API.
We have covered the aspect of bad data in our comprehensive backtesting guide.
Installing the yfinance library is pretty simple, but be aware that Python and PIP have to be installed beforehand. Now all you need is to open the terminal of your computer and write the following prompt:
pip install yfinance
And that’s it! And just in case, remember that you need to import the library in the notebook that you will be working with.
The yfinance library has a ton of functions, but we can simplify into 3 modules:
The first one, yf.Tickers, is used for almost everything. We will show you later all the functions it includes.
yf.Download is obviously for downloading historical data. It returns a dataframe with the open, low, high, close, adjusted close, and volume on any ticker.
Finally, yf.pandas_datareader is for back compatibility with legacy code, which we will ignore since if you’re reading this, you are probably a new user of the library.
How to download historical prices using Yahoo Finance
Downloading historical data of a stock is easier than you think. We just need to create a variable and use the yf.download function.
The result of this simple line of code is all the historical open, high, low, close, adjusted close, and volume values for Apple since 1980!
The yf.download functions has the following parameters as inputs:
- Ticker: here goes the ticker of the stock or ETF you want to download data of. It can also be a list of tickers.
- Start: The day you want to start to download the data in the format yyyy-mm-dd
- End: The day you want to end downloading the data in the format yyyy-mm-dd
- Period: If you don’t need specific dates and just want to download the year-to-date or last year’s data this is the way to go. Valid periods are:
- “1d”, “5d”, “1mo”, “3mo”, “6mo”, “1y”, “2y”, “5y”, “10y”, “ytd”, “max”
- Interval: it can be daily, weekly, bi-weekly, monthly, etc. Valid intervals are:
- “1m”, “2m”, “5m”, “15m”, “30m”, “60m”, “90m”, “1h”, “1d”, “5d”, “1wk”, “1mo”, “3mo”
Plotting this data in a chart is the best way to look at it. Typically we would use matplotlib, but to keep it simple today, we are going to use an easier and quicker method:
We selected the column we wanted to plot (Adj Close) and add .plot() at the end. Here is the output:
It’s a pretty useless chart, but the idea was to demonstrate how easily it can be done with Python!
How to download fundamental data using Yahoo Finance
To download fundamental data, we will have to use the yf.Ticker method. But what data is available? Literally everything you see on the Yahoo Finance website is available. For example, income statements, multiples, analyst’s ratings, etc.
Ticker.info() returns a dictionary with a wide range of information about a ticker. As we mentioned before, it includes such things as a summary description, market cap, dividends etc.
We have to create a variable with the yf.Ticker method and put in the parentheses the ticker of the stock. Then we add .info to the variable, and it returns the dictionary with all the information.
The output of this is an enormous list of data in the form of a dictionary (too long to post it here). You can try it yourself and see the results.
To get specific data, we have to put the name of the key (the variable associated with a value in a dictionary) in brackets. Here is an example of how to get the forward P/E:
We can also get the dividend yield:
Or the market cap:
As you can see, it is not rocket science. There is plenty of data to play around and build some control panels and trading models.
How to download data from Yahoo Finance with Python – conclusion
To sum up, today we learned how to download from Yahoo Finance using the yfinance library in Python.
However, keep in mind that the data is not 100% reliable all of the time. If you are looking to build a trading algorithm or something more professional, you should look for another source of data. Nevertheless, for beginners, it is more than enough to get into quantitative trading.