admin

Matplotlib for data visualization on ALGOGENE IDE

Programming


Visualizing datasets on ALGOGENE Research Lab is extremely simple. This articles shows you how to apply a popular Python charting library 'matplotlib' to acheive this. With 'matplotlib', you can create and customize various charts in Python. Here is the official document https://matplotlib.org/ to explore what kind of charts available.

matplotlib

Figure 1 - Python Matplotlib on ALGOGENE


Before we go into the example, let's go back a little bit to ALGOGENE's cloud environment structure. When you create an ALGOGENE account, you will be automatically assigned with a partition of computing resources including disk storage, random assess memory, etc. All your backtest, livetest or real-trading scripts will be encrypted and execute on your dedicated environment separately and independently. If you want to display some customized calculation results (eg. charts), it is necessary to dump the "data" to your assigned directory first. Then, ALGOGENE Web Server would be able to pull the results and display to frontend interface.

In the following, suppose we are interested to visualize the dispersion of daily return between 2 instruments. On ALGOGENE Backtest environment, we choose S&P500 and Hang Seng Index as an example. Now, an important step is to save the charts to our assigned image directory, i.e. self.evt.path_img specified at line 32 below. That's it!


Full Source Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from AlgoAPI import AlgoAPIUtil, AlgoAPI_Backtest
from datetime import datetime, timedelta
import matplotlib.pyplot as plt

class AlgoEvent:
    def __init__(self):
        self.lasttradetime = datetime(2000,1,1)
        self.arrSize = 100
        self.last_price_X = 0
        self.last_price_Y = 0
        self.arr_return_Y = []
        self.arr_return_X = []

    def start(self, mEvt):
        self.myinstrument_Y = mEvt['subscribeList'][0]
        self.myinstrument_X = mEvt['subscribeList'][1]
        self.evt = AlgoAPI_Backtest.AlgoEvtHandler(self, mEvt)
        self.evt.start()

    def on_bulkdatafeed(self, isSync, bd, ab):
        if isSync:
            # get new day price
            if bd[self.myinstrument_Y]['timestamp'] > self.lasttradetime + timedelta(hours=24):
                self.lasttradetime = bd[self.myinstrument_Y]['timestamp']
                # calculate daily return
                if self.last_price_X>0 and self.last_price_Y>0:
                    self.arr_return_Y.append(bd[self.myinstrument_Y]['lastPrice']/self.last_price_Y - 1)
                    self.arr_return_X.append(bd[self.myinstrument_X]['lastPrice']/self.last_price_X - 1)

                    # print observation
                    self.evt.consoleLog(self.arr_return_Y, self.arr_return_X)

                    # plot chart
                    plt.scatter(self.arr_return_X, self.arr_return_Y, alpha=0.5)
                    plt.title('Scatter plot as of '+str(self.lasttradetime))
                    plt.xlabel(self.myinstrument_X)
                    plt.ylabel(self.myinstrument_Y)
                    plt.savefig(self.evt.path_img+"a.png")

                else:
                    self.last_price_X = bd[self.myinstrument_X]['lastPrice']
                    self.last_price_Y = bd[self.myinstrument_Y]['lastPrice']
                # keep array size = 100
                if len(self.arr_return_Y)>self.arrSize:
                    self.arr_return_Y = self.arr_return_Y[-self.arrSize:]
                if len(self.arr_return_X)>self.arrSize:
                    self.arr_return_X = self.arr_return_X[-self.arrSize:]
                

    def on_marketdatafeed(self, md, ab):
        pass

    def on_orderfeed(self, of):
        pass

    def on_dailyPLfeed(self, pl):
        pass

    def on_openPositionfeed(self, op, oo, uo):
        pass

Console Result

result

Figure 2 - Console result on ALGOGNEE Web IDE


 
David
Hi, I am using NLTK package for NLP stuff. How can I visualize NLTK results on the web platform, likes what you did for Matplotlib?
 
admin
Original Posted by - b'David': Hi, I am using NLTK package for NLP stuff. How can I visualize NLTK results on the web platform, likes what you did for Matplotlib?

As mentioned above, in order to visualize customized charts on ALGOGENE Web Platform, we need to firstly save the images to our dedicated cloud directory.

However, according to NLTK's implementation (https://github.com/nltk/nltk), the charts are built from 'matplotlib', with its plt.show() always called. Once the chart is closed, plt.savefig() has no active chart to save anymore. A workaround is to turn on the interactive mode, such that the plt.show() inside the NLTK function does not block. Then we can call plt.savefig() to save the current chart. After that, the interactive mode can be turned off again.

For the following example, the mentioned workaround can be referred to lines #26 - 29


Source Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from AlgoAPI import AlgoAPIUtil, AlgoAPI_Backtest
from datetime import datetime, timedelta
import nltk
from nltk import word_tokenize
import matplotlib.pyplot as plt


class AlgoEvent:
    def __init__(self):
        self.lasttradetime = datetime(2000,1,1)
        self.isPlotted = False

    def start(self, mEvt):
        self.evt = AlgoAPI_Backtest.AlgoEvtHandler(self, mEvt)
        self.evt.start()

    def on_bulkdatafeed(self, isSync, bd, ab):
        pass

    def on_marketdatafeed(self, md, ab):
        pass

    def on_orderfeed(self, of):
        pass

    def on_newsdatafeed(self, nd):
        if not self.isPlotted:
            self.isPlotted = True
            text1 = nltk.Text(word_tokenize(nd.text))
            plt.ion()
            dplot = text1.dispersion_plot(["Trump","fired"])
            plt.savefig(self.evt.path_img+"a.png")
            plt.ioff()

    def on_dailyPLfeed(self, pl):
        pass

    def on_openPositionfeed(self, op, oo, uo):
        pass

Console Result

result_nltk

 
admin
Since the new release of Jupyter Notebook in Jun-2021, it has more feasibility to visualize and interact with the dataset. 
Examples can refer to post Data exploration with Jupyter Notebook