Plotting Economic Indicators with Python

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 what we will need is Python and Pandas. We will analyse data for the European Union.

Python for Economic Indicators
Photo by Paul Fiedler from Unsplash

Categories of Economic Indicators

First, let’s start with a bit of theory. There are three main categories of economy 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 on the economy 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, some of the Lagging indicators are 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 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 make 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 return 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 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 call 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 go

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.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

So far so good. Let’s now add all other economic indicators such as unemployment, GDP growht, 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 represent 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))

# 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()
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 plot them together using Plotly.

You can change or add new indicators depending on the area of the economy that you want to analyse. Also, same 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 in Youtube going through the code and API step by step:

Do not forget to check my other articles in CodingandFun to find amazing posts on Python for Finance and Python for stock analysis.

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.columns


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')
monthly_change_retail_trade.columns


import plotly.graph_objects as go


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))

# 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()