Python Django is a high-level framework designed to facilitate the rapid development of web applications. I frequently work with Python, and when I opt for a framework, I typically choose Flask due to its flexibility, which allows me to build solutions with fewer restrictions. In contrast, Django is more opinionated, promoting a highly structured approach where there is often “one right way” to implement a solution. I thought this tutorial would be useful for introducing you to Django, while also serving as a helpful refresher for myself.

Recently, EODHD APIs introduced a new product called “Indices Historical Constituents Data.” This API is quite new and has not yet been integrated into the official Python library. It provides two key functions: one for listing indices and another for retrieving the constituents of each index. To illustrate, consider the S&P 500 index, which is denoted by the code “GSPC.INDX.” Although it’s called the S&P 500, the index actually comprises of 503 stocks. This discrepancy exists because while there are 500 companies in the index, some companies have more than one type of stock class included.

This new API offers up to 12 years of historical and current data for various global indices, including the S&P 500, S&P 600, S&P 100, and S&P 400, as well as key industry indices. The API provides detailed information such as the list of current constituents and any historical changes over time. Such data, which is difficult to find elsewhere, is highly valuable for in-depth analysis of market trends and long-term investment strategies. It’s designed for seamless integration and delivers structured data in JSON format, making it ideal for developers and analysts working on financial projects.

For more information, you can visit the official product pages on EODHD’s marketplace or forum, where they discuss the features and details of the API in more depth here and here.

Register & Get Data

Prerequisites

  1. Install Python 3, ideally 3.9 or better. I’m using 3.9.6 on my Mac.
  2. Install an IDE. I’m using Visual Studio Code, it’s free and excellent.
  3. Create a virtual environment called “python3 -m venv venv”.
  4. Upgrade Python PIP, “python3 -m pip install –upgrade pip”.
  5. Initialise the virtual environment “source venv/bin/activate”.
  6. Install Django using Python PIP. “python3 -m pip install django -U”.

Django – The Basics

Django has the concept of a project and app(s). A project may be made up of one or more app(s). You could consider an app as a component within the project. For my tutorial series I’m going to create a project called “eodhd_apis”. The project will consist of one more components or apps. The first app we will create will be called “spglobal”.

D3.js

I’m going to use a popular javascript visualisation library called D3.js to create my treemap.

Bootstrap

I’m going to use a popular javascript data tables library called Bootstrap to create my constituents data table. Bootstrap is great as it’s easy to add export, sorting and pagination functionality to webpage tables.

Setting up our Django project

Django has a utility (within the virtual environment) called “django-admin” that we’ll use to create our project.

Django will create a project script called “manage.py” that we’ll use to create and start our application.

Add the app to the Settings

Edit the file, “eodhd_apis/eodhd_apis/settings.py” and “spglobal” to the INSTALLED_APPS.

Creating our data models

We will want to map this API endpoint to a model (“SPGlobalIndex”):
https://eodhd.com/api/mp/unicornbay/spglobal/list?api_token=<YOUR_API_KEY>

We will want to map this API endpoint to a model (“IndexConstituent”):
https://eodhd.com/api/mp/unicornbay/spglobal/comp/GSPC.INDX?fmt=json&api_token=<YOUR_API_KEY>

Edit the file “eodhd_apis/spglobal/models.py” and add the two models below. I have done the hard part of mapping the API fields to the correct data types in the models.

Once the model is created, run the following commands to create and migrate the database.

Two actions are going to be done here:

  1. You will see a file called “db.sqlite3” created in your project root. SQLite is the default database for Python Django. This is where your database data will be stored. If you ever run into trouble you can delete this file and let the app rebuild it from API data.
  2. In your “eodhd_apis/spglobal/migrations” directory, you will see files created to keep track of the change log in the database. The first file would typically be called “0001_initial.py”. If you ever get stuck and have to delete the database mentioned above, make sure you delete all the transaction files as well.

Here are some useful model and database diagnostic commands if you need them…

Creating our Views

Edit the “eodhd_apis/spglobal/views.py” file and make it look as below. Make sure you replace <YOUR_API_KEY> with your subscription API key.

Create this file if it doesn’t exist to handle the routes, “eodhd_apis/spglobal/urls.py”.

And update this file with the following changes in bold, “eodhd_apis/eodhd_apis/urls.py”.

Treemap with D3.js

Create the template directory structure, “eodhd_apis/spglobal/templates/spglobal”

And create the file, “eodhd_apis/spgobal/templates/spglobal/index.html”.

And create the file, “eodhd_apis/spgobal/templates/spglobal/constituents.html”.

Add Data to the Database

In “eodhd_apis/spglobal/admin.py”, register the SPGlobalIndex and IndexConstituent models so you can add index via Django’s admin interface:

Now, run the server:

Summary:

  • Main Page (/): Displays the clickable treemap of people using D3.js.
  • Detail Page (/constituents/<id>/): Displays additional information about the selected index.

If everything is working as expected (so far), it should look like this…

Admin Interface (Optional)

We can either add the data manually through the admin interface or we can add it dynamically using an API call.

In order to access the admin interface (http://127.0.0.1:8000/admin), we will need to create a super user account.

If you manually add the indices here and reload http://127.0.0.1:8000, you will see them. I’ve developed the app to automatically populate the data using API data from EODHD APIs.

You will notice that I only registered “Sp global indices” in the admin interface “eodhd_apis/spglobal/admin.py”. The reason for this is the index list is static as the app runs. The constituents however changes depending on what is being viewed. I initially was adding the data to the model, but it makes the app unnecessarily slow for no good reason. The API data is not coded to store to the model, and as the model is empty, it doesn’t make sense to see it in the admin interface.

Conclusion

This Python Django application can be further enhanced to include many more EODHD APIs endpoints. The fundamentals data could be an interesting addition. It may also be interesting to be able to browse through historical data using a selectable timeframe.

Do you enjoy our articles?

We can send new ones right to your email box