Economic Indicators with Python

Retrieve and Plot Economic Indicators using Python and Plotly

Jose Manu (CodingFun)
Towards Data Science

--

Economic indicators are used often by economists and financial analysts to predict the cycle of the economy. This analysis is very important before making investment decisions.

In this article, we are going to automate the extraction of Economic indicator data with Python. All that we will need is Python and Pandas. We will analyse data for the European Union.

Photo by Paul Fiedler from Unsplash

Economic Indicators - Categories

First, let’s start with a bit of theory. There are three main categories of economic indicators that I would like to introduce in this post. Lagging indicators, coincident indicators and leading indicators.

  • Lagging indicators are those indicators that change after a new cycle of the economy is underway.
  • Coincident indicators do change simultaneously with cycle peaks
  • Leading indicators do anticipate changes since they happen before a change in the economic cycle.

These indicators have different measures and are published by different sources such as OECD and the Conference Board. By looking at the website of the Conference Board, we can easily find a few of these economic indicators for each of the three categories:

Within leading indicators, we find average weekly hours in manufacturing, building permits, stock prices and leading credit index among others.

Within coincident indicators, we find industrial production and manufacturing, trade sales, etc.

Finally, among lagging indicators, we find average duration of unemployment, commercial and industrial loans, consumer price index for services and inventory to sales ratio.

You can find a definition for each of these indicators in the Conference-Board site.

Let’s get a few of them with Python to analyse the current economic situation in the European Union. All indicators are extracted using the free API available in DBnomics.

The economic indicators that we will retrieve with Python are the followings: inflation, interest, GDP growth, unemployment rate, retail trade change and 10 years Euro yield curves. Ok, let’s move to the coding part!

Python script to retrieve Economic Indicators

First thing is to import all required libraries that we will be using. Namely, Pandas, Json, Requests and Plotly.

We will use Json and requests to retrieve data from the API. Then, we will use Pandas to handle the data. Finally, Plotly to create our Graph.

import pandas as pd 
import requests
import json
import plotly.graph_objects as go

Once we have imported all the needed libraries, we can move to retrieving the economic indicators. Since we will be extracting more than one indicator, we will build a function that we can reuse to extract all indicators. I have named the function checkindicator:

def checkindicator(url):
r= requests.get(url)
r = r.json()
periods = r['series']['docs'][0]['period']
values = r['series']['docs'][0]['value']
dataset = r['series']['docs'][0]['dataset_name']
indicators = pd.DataFrame(values,index=periods)
indicators.columns = [dataset]
return indicators

Our function makes a request to the API url that will return the economic data. The url is extracted from DBnomics. Simply, we go to DBnomics site, search for the indicator, select it and copy the API link.

Then, we pass the API link as an argument of our checkindicator function as shown below to get the selected indicator for the European Union. In our first example, we will extract the interest rates.

Since our API response returns a large json object, we parse it to extract the periods and values as shown in the code above. Then, we create a Pandas DataFrame containing the dates and the interest rates values.

interest = checkindicator('https://api.db.nomics.world/v22/series/Eurostat/ei_mfir_m/M.NSA.NAP.MF-LTGBY-RT.EU28?observations=1')

After running the code, our checklist function will return a Pandas DataFrame containing the interest information per month. Something like below:

Economic Indicators with Python

Now, we are ready to extract all other indicators. We will store them in variables in the form of a Pandas DataFrame. Now that we have our economic indicators extracted, we can move to plot them together.

euro_yields_10y = checkindicator('https://api.db.nomics.world/v22/series/Eurostat/irt_euryld_m/M.EA.INS_FWD.CGB_EA.Y10?observations=1')unemployment = checkindicator('https://api.db.nomics.world/v22/series/Eurostat/une_rt_m/M.NSA.TOTAL.PC_ACT.T.EA19?observations=1')interest = checkindicator('https://api.db.nomics.world/v22/series/Eurostat/ei_mfir_m/M.NSA.NAP.MF-LTGBY-RT.EU28?observations=1')inflation = checkindicator('https://api.db.nomics.world/v22/series/WB/WDI/FP.CPI.TOTL.ZG-EU?observations=1')GDPgrowth = checkindicator('https://api.db.nomics.world/v22/series/WB/WDI/NY.GDP.MKTP.KD.ZG-EU?observations=1')monthly_change_retail_trade = checkindicator('https://api.db.nomics.world/v22/series/Eurostat/sts_trtu_m/M.TOVT.G47.CA.PCH_SM.EA19?observations=1')

Plotting Economic Indicators with Python

In order to plot our Economic indicators with Python, we will use a library called Plotly.

First of all, we need to import it. Next, we need to create a Fig object where we will add the traces. In our case, a trace will represent an economic indicator. Therefore, we will have as many traces as indicators we want to include in the chart.

To keep it simple for now, let’s only plot the interest rate. Within our add_trace object, we specify the x which will be the index of our DataFrame (i.e. index represents the dates). While y will represent our values (i.e. the interest rates associated with each of the dates).

Then, we use fig.updated_layout in order to make the figure look nicer and include a date slider and a title:

import plotly.graph_objects as gofig = go.Figure()
fig.add_trace(go.Scatter(
x=interest.index,
y=interest['Interest rates - monthly data'],
name="interest",
line_color='deepskyblue',
opacity=0.8))
fig.update_layout(xaxis_range=['2003-07-01','2020-12-31'],
title_text="Interest Rates, Unemployment, 10y yields, inflation UE, volume sales",xaxis_rangeslider_visible=True)
fig.show()

If we run our code now, we will get below chart containing more than 15 years of interest rates in the European Union:

Plot economic indicators with Python
Economic indicators with Python and Plotly

So far so good. Let’s now add all other economic indicators such as unemployment, GDP growth, etc. to our Python graph.

We can simply do that by adding new traces. See below the code to plot all our macroeconomic indicators together. As mention above, each trace represents a different indicator. We change the name and the line color for each of the traces:

fig = go.Figure()fig.add_trace(go.Scatter(
x=interest.index,
y=interest['Interest rates - monthly data'],
name="interest",
line_color='deepskyblue',
opacity=0.8))
fig.add_trace(go.Scatter(
x=unemployment.index,
y=unemployment['Unemployment by sex and age - monthly average'],
name="unemployment",
line_color='red',
opacity=0.8))
fig.add_trace(go.Scatter(
x=euro_yields_10y.index,
y=euro_yields_10y['Euro yield curves - monthly data'],
name="euro_yields_10y",
line_color='green',
opacity=0.8))
fig.add_trace(go.Scatter(
x=inflation.index,
y=inflation['World Development Indicators'],
name="inflation",
line_color='orange',
opacity=0.8))
fig.add_trace(go.Scatter(
x=GDPgrowth.index,
y=GDPgrowth['World Development Indicators'],
name="GDP growth",
line_color='pink',
opacity=0.8))
fig.add_trace(go.Scatter(
x=monthly_change_retail_trade.index,
y=monthly_change_retail_trade['Turnover and volume of sales in wholesale and retail trade - monthly data'],
name="% Monthly Change Volume Sales",
line_color='black',
opacity=0.8))
fig.update_layout(xaxis_range=['2003-07-01','2020-12-31'],
title_text="Interest Rates, Unemployment, 10y yields, inflation UE, volume sales",xaxis_rangeslider_visible=True)
fig.show()
Macroeconomic Indicators with Python
Macroeconomic Indicators with Python

Nice! We can use the slider below the chart area to change the dates of the graph dynamically.

Wrapping up

We have created a script to retrieve economic indicators with Python. Then, we have plotted them together using Plotly.

You can change or add new indicators depending on the area of the economy that you want to analyse. A similar analysis can be done for different countries. We have selected the Euro Area for this post. But you can easily change it by modifying the url parameters.

For your reference, see below the final code to build a chart showing economic indicators with Python and Plotly.

If you had troubles understanding the code, I have a video on Youtube going through the code and API step by step:

Python for Finance — Plotting Economic Indicators with Python
import pandas as pd
import requests
import json
import plotly.graph_objects as go
def checkindicator(url):
r= requests.get(url)
r = r.json()
periods = r['series']['docs'][0]['period']
values = r['series']['docs'][0]['value']
dataset = r['series']['docs'][0]['dataset_name']
indicators = pd.DataFrame(values,index=periods)
indicators.columns = [dataset]
return indicators

euro_yields_10y = checkindicator('https://api.db.nomics.world/v22/series/Eurostat/irt_euryld_m/M.EA.INS_FWD.CGB_EA.Y10?observations=1')
unemployment = checkindicator('https://api.db.nomics.world/v22/series/Eurostat/une_rt_m/M.NSA.TOTAL.PC_ACT.T.EA19?observations=1')interest = checkindicator('https://api.db.nomics.world/v22/series/Eurostat/ei_mfir_m/M.NSA.NAP.MF-LTGBY-RT.EU28?observations=1')inflation = checkindicator('https://api.db.nomics.world/v22/series/WB/WDI/FP.CPI.TOTL.ZG-EU?observations=1')#inflation.columnsGDPgrowth = checkindicator('https://api.db.nomics.world/v22/series/WB/WDI/NY.GDP.MKTP.KD.ZG-EU?observations=1')monthly_change_retail_trade = checkindicator('https://api.db.nomics.world/v22/series/Eurostat/sts_trtu_m/M.TOVT.G47.CA.PCH_SM.EA19?observations=1')monthly_change_retail_trade.columnsimport plotly.graph_objects as gofig = go.Figure()fig.add_trace(go.Scatter(
x=interest.index,
y=interest['Interest rates - monthly data'],
name="interest",
line_color='deepskyblue',
opacity=0.8))
fig.add_trace(go.Scatter(
x=unemployment.index,
y=unemployment['Unemployment by sex and age - monthly average'],
name="unemployment",
line_color='red',
opacity=0.8))
fig.add_trace(go.Scatter(
x=euro_yields_10y.index,
y=euro_yields_10y['Euro yield curves - monthly data'],
name="euro_yields_10y",
line_color='green',
opacity=0.8))
fig.add_trace(go.Scatter(
x=inflation.index,
y=inflation['World Development Indicators'],
name="inflation",
line_color='orange',
opacity=0.8))
fig.add_trace(go.Scatter(
x=GDPgrowth.index,
y=GDPgrowth['World Development Indicators'],
name="GDP growth",
line_color='pink',
opacity=0.8))
fig.add_trace(go.Scatter(
x=monthly_change_retail_trade.index,
y=monthly_change_retail_trade['Turnover and volume of sales in wholesale and retail trade - monthly data'],
name="% Monthly Change Volume Sales",
line_color='black',
opacity=0.8))
# Use date string to set xaxis range
fig.update_layout(xaxis_range=['2003-07-01','2020-12-31'],
title_text="Interest Rates, Unemployment, 10y yields, inflation UE, volume sales",xaxis_rangeslider_visible=True)
fig.show()

Originally published at https://codingandfun.com on February 1, 2020.

--

--

Python for Finance. Learn step by step how to automate cool financial analysis tools.