Want company and country financial data in a Python Pandas database | by Bruce H. Cottman, Ph.D. | June 2022

0

The illustration shows a method for retrieving asset prices based on time and global economic data.

Company stock price. picture by Nick Chong on Unsplash

There are many databases for financial data. The main free databases (without paywall) that I use are:

  • Yahoo finance is a good source of downloadable asset prices against time from any US market ticker. I show a Pandas method that bundles the Yahoo API into a pandas python DataFrame.
  • Fred (Federal Reserve Economic Data) contains more than 400,000 data sets.
  • CALCBANCIER is a financial database of over 9,000 publicly traded US companies. The free area allows you to view and download the data in Excel. Other features are accessible behind a (high) paywall. While many of its features are only available to premium users, the free version allows users to view all data and download it to Excel.

Jgoy one purses worldwide have a market capitalization of over US$1 trillion.

What is a stock symbol?

Stock symbols are unique identifiers for each Security traded in a particular market. A stock symbol can be made up of letters, numbers, or a combination of the two and is a way to uniquely identify that stock.

Stock symbols depend on the stock exchange. For instance, IBM is the NYEX (New York Exchange) ticker. On the London Stock Exchange, it is IBM.L using the Yahoo data provider.

Also, the stock symbols of the same company may change. A recent example is the ticker FB change to META.

Depending on the data provider, the tickers also change. I always encounter differences between Bloomberg, Reuters and Yahoo sources. I use Yahoo in this blog post.

Detailed explanations of stock symbols are found here and here.

How do you get a company’s ticker symbol?

I write in Google Chrome or Apple’s Safari search. For example, I want the stock symbol of Alibaba, a Chinese company. The result is BABA:

Figure 2. Google search result of “Alibaba ticker”. Screenshot of the author’s desktop computer.

I can get a list of all downloadable symbols in a CSV file, comma separated value and text formatted file from here.

A tedious but simple approach is to use the Yahoo API.

However, I use the pandas_datareader.data.DataReader method that brings together the different http requestcalls in a line of python code. The first example shows how to get historical price data from IBM.

ticker = 'IBM'
rd_type = 'yahoo'
start_date = '1960/05/10'
end_date = '2030/07/30'
column_name = 'Adj Close'
EFT_df = DataReader(ticker, rd_type,
start = start_date, end = end_date)
EFT_df.head(n=2), EFT_df.tail(n=2)
Figure 3. EFT_df (Pandas DataFrame instance) of IBM daily price data. Screenshot of the author’s desktop computer.

Notice the convenience of the call interface. I can put the start_date earlier than the oldest date available. Also, I can put the_end_date later than the last date of data available. Very useful for obtaining the full range of data available.

The second example shows how to get IBM historical price data from the London Stock Exchange.

ticker = 'IBM.L'
rd_type = 'yahoo'
start_date = '1960/05/10'
end_date = '2030/07/30'
column_name = 'Adj Close'
EFT_df = DataReader(ticker, rd_type,
start = start_date, end = end_date)
EFT_df.head(n=2), EFT_df.tail(n=2)
Figure 4. EFT_df (Pandas DataFrame instance) of IBM.L daily price data. Screenshot of the author’s desktop computer.

To note: The price values ​​are different for IBMand IBM.L because the first set of data is in momentary units of dollars while the second set of data is in momentary units of pounds.

The third example shows how to obtain historical price data from Alibaba, a Chinese company listed on a US stock exchange.

from pandas_datareader.data import DataReaderticker = 'BABA'
rd_type = 'yahoo'
start_date = '1960/05/10'
end_date = '2030/07/30'
EFT_df = DataReader(ticker, rd_type,
start = start_date, end = end_date)
EFT_df.head(n=2), EFT_df.tail(n=2)
Figure 5. EFT_df (Pandas DataFrame instance) of daily BABA price data. Screenshot of the author’s desktop computer.

We can get historic prices for almost any stock on over twenty-one international exchanges!

Using pandas_datareader.data.DataReader, I can access any business financial data or government economics I need.

You find all the sources supported by thepandas_datareader.data.DataReader method here. Besides Yahoo and FRED, you can get data from twelve other sources such as When the, world Bankand OECD.

What is FRED?

Fred is the Ffederal RReserve Eeconomic Da database with over 400,000 data sets. The FRED database contains all US economic indices and other international economic indices produced by various government agencies.

A sample of FRED categories is Consumer Price Index (CPI), Gross Domestic Product (GDP), Treasury Rate, Unemployment, Exchange Rate, Inflation and Household Income for the United States and other countries.

FRED has an API to access its database via the web.

However, again, I use the pandas_datareader.data.DataReader method that brings together the different http requestcalls in a line of python code. The pandas_datareader.data.DataReader is the only call you’ll need for fourteen data sources, such as Yahoo Finance, Fred, When the, world Bankand OECD.

The first example shows how to obtain the monthly variation over 12 rolling months of the Consumer Price Index (CPI) data.


ticker = 'MEDCPIM158SFRBCLE'
rd_type = 'fred'
start_date = '1960/05/10'
end_date = '2022/07/30'
EFT_df = DataReader(ticker, rd_type,
start = start_date, end = end_date)
EFT_df.head(n=2), EFT_df.tail(n=2)
Figure 6. EFT_df (Pandas DataFrame instance) of FRED CPI. Screenshot of the author’s desktop computer.

How to find a dataset in FRED

Over the years I have searched for a dictionary for the over 400,000 datasets in FRED. Let me know if you know how to get such a beast.

Even if such a FRED lookup dictionary existed, I might not use it.

FRED provides a hierarchical set of forms to locate the dataset and its ticker.

For example, I show how I was able to find the ticker ‘MEDCPIM158SFRBCLE’ or the rolling 12-month monthly change in the Consumer Price Index (CPI) dataset in four clicks.

0. Start to tps://fred.stlouisfed.org/

  1. Click on CPI in the “trend search terms” in the lower left quadrant of the web page.
Figure 7. FRED home page. Screenshot of the author’s desktop computer.
  1. Click “Frequency” in the lower left quadrant of the web page.
  2. Click “Monthly” in the lower left quadrant of the webpage.
  3. Click on “Median Consumer Price Index” in the middle of the web page.
Figure 8. FRED “IPC Datasets web page. Screenshot from author’s desktop.

4. Click on “Median Consumer Price Index” in the middle of the web page.

Figure 9. FRED “CPI dataset chart webpage. Screenshot from author’s desktop.

You see the URL https://fred.stlouisfed.org/series/MEDCPIM158SFRBCLE. The ticket” MEDCPIM158SFRBCLE is how you reference this dataset in the pandas_datareader.data.DataReader method.

Consider becoming a Medium member for $5 per month. You will have unlimited access to my articles and thousands of articles by other authors on investing, data science, coding, relationships, humor, health, lifestyle and much more .

Share.

Comments are closed.