Downloading Data for Your Trading Strategy

Downloading Data for Your Trading Strategy from Yahoo! Finance Using Python

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.

Related reading:

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.

Python-related resources

We have written many articles about Python, and you might find these interesting:

Installing yfinance

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. 

yfinance functions

The yfinance library has a ton of functions, but we can simplify into 3 modules:

yf.Ticker

yf.download

yf.pandas_datareader

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.

How to download historical prices using Yahoo finance

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!

How to download historical prices using Yahoo Finance example

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:

Plotting data charts using Python

We selected the column we wanted to plot (Adj Close) and add .plot() at the end. Here is the output:

How to make a stock market chart using Python

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.

How to download fundamental data using Yahoo Finance

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:

How to download fundamental data using Yahoo Finance example

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.

FAQ:

What is yfinance, and how does it differ from the official Yahoo Finance API?

Yfinance is an open-source Python library developed by Ran Aroussi to access financial data available on Yahoo Finance. The official Yahoo Finance API was taken down in 2017, and yfinance serves as an unofficial alternative. It allows users to retrieve historical market data, including stocks, bonds, currencies.

Why should I use yfinance for financial data retrieval?

Yfinance provides a convenient and free way to access financial data from Yahoo Finance. It offers historical market data and a wide range of fundamental information. While the data quality may not be guaranteed, yfinance remains a valuable tool, especially for beginners and those looking to explore quantitative trading.

What precautions should I take while using data from yfinance for quantitative trading?

While yfinance is suitable for exploration and learning, users should be aware that the data may have limitations. For professional or production-level trading algorithms, it’s recommended to thoroughly evaluate the reliability of data and consider alternative sources to ensure accuracy in decision-making.

Similar Posts