Get stock exchange quotations with Python

Get stock exchange quotations with Python

In this article, we will see two very simple ways to retrieve historical stock quotes in Python.

To simplify the article, we will deliberately not see the possibilities that require an API key or registration with a provider (AlphaVantage, Barchart, Enigma, etc.)

And we will use data from Yahoo Finance which is freely available.

The pandas-datareader and yfinance libraries are not official libraries of Yahoo, Inc.These libraries make use of Yahoo’s public APIs, and are intended for research and educational purposes.

How to get the right Yahoo Finance ticker?

The functions in the Python libraries we will see use tickers. A ticker is an abbreviation that identifies a particular stock on a given exchange.

To get the right ticker on Yahoo Finance, just do a search on Yahoo Finance and the ticker will be indicated in parentheses after the stock of the stock as on the screenshot below which concerns the Apple stock.

Get stock exchange quotations with Python

Some examples of tickers obtained from Yahoo Finance:

  • AAPL – Apple Inc.
  • MSFT – Microsoft Corporation
  • TSLA – Tesla, Inc.
  • EURUSD=X – EUR/USD currency pair
  • BTC-USD – Bitcoin USD
  • ^GSPC – S&P 500
  • ^DJI – Dow Jones Industrial Average

Stock exchange quotations with pandas-datareader library

The pandas-datareader library enables data to be retrieved from a wide range of suppliers, including Yahoo Finance.

Documentation of the pandas-datareader library on github.io

Install using pip command

pip install pandas-datareader

Example of the use of pandas_datareader library

We request the data for Apple, Microsoft and Bitcoin from 01/01/2010 to today, and store the data in a DataFrame.

# Get stock exchange quotations with pandas_datareader library  import pandas as pd  import pandas_datareader as pdr  symbols = ['AAPL', 'MSFT', 'BTC-USD']  df = pdr.get_data_yahoo(symbols, start="2010-01-01")  df
Get stock exchange quotations with Python
Output dataframe

The get_data_yahoo function allows you to get data from several tickers at once.

You can specify the end parameter if you want an end date different from today’s date.

If you do not specify a date, the function will return the last 5 years since the current date.

Fix get_data_yahoo from Pandas Datareader!

If an error occurs when executing the get_data_yahoo function, you just need to update your Pandas Datareader library to the last version (v0.10.0 of July 11, 2021) with the pip command below.

pip install --upgrade pandas-datareader

Stock exchange quotations with yfinance library

Install using pip command

Example of the use of yfinance library

Documentation of the yfinance library on pypi.org and more detailed use cases on the yfinance author’s blog.

As in the previous example, we request the data for Apple, Microsoft and Bitcoin from 01/01/2010 to today, and store the data in a DataFrame.

# Get stock exchange quotations with yfinance library import yfinance as yf import pandas as pd df = yf.download("AAPL MSFT BTC-USD", start="2010-01-01") df
Get stock exchange quotations with Python
Output dataframe

The download function allows you to get data from several tickers at once.

You can specify the end parameter if you want an end date different from today’s date.

Conclusion

You can see that it’s really not complicated and that there are many ways to get historical stock data and then do data analysis.

By reading the documentation of each library, you will see that it is also possible to recover fundamental data.

Get stock exchange quotations with Python
CC BY licence

Article published under CC BY licence