Please note, API data availability depends on your subscription plan. Some data isn’t included in the free plan. Visit our pricing page to find the plan that fits your needs.

This article builds upon the previous one, Build a Financial Trading Dashboard with Python Django”, where I introduced the Python Django framework, and we created our first project and application. That tutorial guided you through the process of creating a treemap of market indices, with the ability to drill down into the market indices to view their constituents.

In this article, we will enhance the application by addressing some minor improvements, such as decoupling the hardcoded API keys from the code. Additionally, we will implement functionality to allow viewing of historical market data, and a market search.

As this new functionality is closely related to what was done in the previous article, we will use the same “spglobal” application instead of creating a new application within the Django project.

Register & Get Data

Decoupling the API keys

There are several ways to achieve this, but I’m going to demonstrate “the Django way”.

The first step is to create a .env file in the root of our project, which should look like this…

We will use a library called python-decouple, which loads the .env file, allowing us to access its contents within our application.

Edit the file, “eodhd_apis/eodhd_apis/settings.py” and make the following adjustments.

Edit the “eodhd_apis/spglobal/views.py” file and make it look as below.

When you run your application again, you’ll notice it runs smoothly as before. The key difference is that your private API key is now securely separated in the .env file.

Market Search API

EODHD APIs has a search API which I want to implement in this app. You can find the official documentation here.

I will need to create a data model for this. This will be different to the previous models because I don’t want to store the data in the database. I just want a structure for the API response to be stored.

We will need to create a model for our new historical data feature. Update the eodhd_apis/spglobal/models.py and include this in the file.

As per the same process as before we’ll need to update the urls.py, views.py, and create a new template called search_results.html.

eodhd_apis/spglobal/urls.py

eodhd_apis/spglobal/views.py

eodhd_apis/spglobal/templates/spglobal/search_results.html

This will have the same look and feel of the previous constituents table from the previous article.

I will update the constituents page to allow you to reach this new page. What is quite exciting about this is it works with all markets on EODHD. If you know the EODHD APIs code for any market with historical data you can view it.

Updating the Constituents results

What would be nice now is to be able to access either the market constituents OR the historical data from the treemap. I’ve made the updates to make this possible in the “eodhd_apis/spglobal/templates/spglobal/constituents.html” file.

“Gotcha”

You will notice that the constituents page returns a Code like AAPL. The historical data requires you to include the exchange like .US. This information is not available in the constituents list. The way I got this working was to append .US to the code. This should work in almost call cases. The problem may happen if for example an index had any cryptocurrency included as I know that uses .CC as a prefix. I’ve done a lot of testing and I’ve yet to find one that doesn’t work with a .US prefix but I just wanted you to be aware of the limitation. If EODHD APIs decide to add this additional information to the constituents results, then this would be an easy fix.

Next Steps

The next article will explore treemap alternatives that can be used instead of D3.js.

Do you enjoy our articles?

We can send new ones right to your email box