Visualizing Nigerian & Nairobi Stock Exchange data using Python and Quantextive AEX

At Quantextive, our core mission is to provide easy access to stock market and financial data for publicly traded companies within major African markets; not just for professional traders and asset managers but also for casual market enthusiast and developers. This mission propelled us to create an API layer which sits on top on of our data stores and allows anyone to export, analyze and visualize our data within their own platforms and computers.

High level architecture diagram of the Quantextive platform

Given that we are python enthusiasts and also had numerous requests for a python module, we went a few steps further and created a python helper which would help our users experiment (back test, create dynamic visualizations etc.) with our data within their own environments.

In order to begin using our API or the associated python module, you will need to first request a token. Fortunately we are giving early users a 30-day trial access to our APIs in addition to the free access that everyone already gets to the AEX portal.

How To Receive Your Trial API Token

1). The fastest way to receive a trial token is to register and sign in to the AEX portal. Once you have access to the portal, you can go to your profile link (at the top right-hand corner of the portal) and click the “Request API Key” and fill out your use case.

You should receive your API Key by email within 2 hours and it will also be within your profile for future use

Click your name at the top right-hand corner within the AEX portal and select ‘Request API Key’

**Due to agreements that we have with the individual stock exchanges, you will be required to provide your use case for the API.

2). Alternatively, you can just request your trial token using the request form on our site but these requests tend to take longer.

Once you receive your token, you’re in business and ready to start playing with the data APIs. You can visit our API documentation section to get an overview of all of the API endpoints we host.

Tutorial: Visualizing End-Of-Day Market Data

This tutorial covers the use of our python module for exploring and visualizing end-of-day market data for publicly listed companies (e.g. 7UP bottling company from the Nigerian Stock Exchange).

Prerequisites
-
In order to easily visualize the returned data, this tutorial will be using the jupyter (ipython) notebook. If you have anaconda installed on your platform/computer, you’re all set. If not, you can view the install guide here.

- You will also need to install pip (the python package manager) if you don’t already have it installed. You can see install guides for windows, mac or ubuntu here

Once you’re done with the prerequisites, you should be ready to start with the core tutorial.

1). Install the quantextive python module to a directory of your choice.

pip install qtx

The quantextive module has dependencies on two additional modules which must be installed in order to use the qtx module

pip install pandas
pip install requests

2). Create/Open a new python file.

As mentioned above, we will be using ipython (jupyter) notebook since we want to visualize our data. Run the command below from your shell to start the jupyter notebook.

ipython notebook

For this demonstration, we will also be importing the previously installed python pandas package as well as the matplotlib package

from qtx import qtx
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
%pylab inline
pylab.rcParams['figure.figsize'] = (15, 9)

(In some cases, matplotlib will come installed with anaconda but if you get an error importing matplotlib, run “pip install matplotlib” from your shell)

3). Enter your trial api key

api_key = ‘************’ ##enter your api token

4). As mentioned earlier, we will only be exploring end-of-day market data in this tutorial so we will define the endpoint we want to extract data from. (You can also explore our other APIs here)

api = 'market-data-eod'

5). The market-data-eod endpoint requires 3 parameters in order to return results (the security id of the company you want to return data for, the start date and the end date). For this example, we’ll be returning data for the 7UP (Seven-Up) bottling company in Nigeria.

params = {
'securityId': 'NSE:7UP',
'startDate': '2016–11–01',
'endDate': '2017–03–18'
}

Click here to view all companies that we currently cover as well as their Security IDs and required parameters.

6). The next two lines will initialize the module and return the data respectively.

client = qtx.ApiClient()
data = client.get(api_key, api, params).data_frame()

**You can also return the data as json() but that’s for another tutorial.

7). You should now be able to take a quick peek at the data within the dataframe returned.

data.head()

**This feature is only available if you’re using ipython/jupyter notebook

Note on dataframe reformatting
We made a conscious decision to return majority of our data in its purest form (without any initial manipulations) so that users can apply their own manipulations without having to reverse ours.

8. We will change the datatype of the date column to datetime

data['date'] = data[['date']].apply(pd.to_datetime)

9. We also want to convert all “numeric-like” values to numeric format (integers & floats)

data2 = data.convert_objects(convert_numeric=True, copy=True)

10. In order to reformat the data for visualization, we will need to re-index the data using the date column since the date will be the axis which we plot the close_values against.

data3 = data2.set_index('date')

11. You can choose to view a sample of the reformatted data again.

data3.head()

12. We should now be able to run some quick visualizations using matplotlib. For example, we can create a line chart using the close price

data3["close_value"].plot(grid = True)

You can view the full ipython/jupyter notebook tutorial source here.

This tutorial is just the starting point for more interesting projects that you can create using the AEX API.

Watch out for upcoming tutorials using the Quantextive AEX APIs. If you have any tutorial requests, you can email us at api@quantextive.com and we’ll be happy to oblige.

On the other hand, if you create any cool projects with the API, please tag us and we’ll be happy to promote your work.

To access our API directly, visit out API documentation page

In the meantime, you can check our site and AEX portal. Also, please follow us on facebook or twitter 👍🏾