Correlation plays an important role in market analysis and portfolio management. If you are interested in building a simple correlation matrix which visualises the historical correlation of an instrument against a benchmark, this article is aptly right for you.
Using the Grid API of Equilla (the programming language of Tradesignal), we will develop an indicator which displays the changes in correlation on a monthly basis. This enables us to find out, whether a chosen asset delivers diversification potential or not and how strong correlation is. Here`s an example showing the correlation of gold against the S&P 500 going back to 1988:
Grid API – How does it work?
The Grid API allows to develop small windows containing table-structured data. Such an indicator can be used as an overlay within a chart. In this window you can visualise strategy performance data, quote fields or a histogram of level II data etc. The Grid API can also be used to collect user input in the form of buttons, text boxes, checkboxes and drop down lists. We will cover that topic in one of our articles in the future. For now, let`s jump right into the code to see how you can create a correlation matrix.
Defining all Inputs, Instruments and Variables
The first part of our programming code defines all input parameters; for example the benchmark ticker in line 8. This way, any ticker can be entered in the properties section. The variables in lines 26 to 29 will be used for the setting of rows, columns and various calculations.
Creating the Grid
In order to place tabular data into a window, we first need to create multiple windows using the built-in function GridCreateWindow and define visual parameters like colors and alignment with the GridSet functions.
Populating the Cells of the Correlation Matrix
In the next step, we need to populate the cells we just created. Therefore, we define the rows and columns and calculate the correlation. Then, using the GridSetValue and GridCreateLabel functions, the visual formatting of the window is done.
Gradient RGB Colors visualise Correlation Changes
In the final step, we color the cells based on the monthly correlation values. If it`s positive, we use green, if it`s negative, we prefer red color. This is implemented by using the built-in function called GridSetBackColor. Another function, GradientColor, allows to control the RGB color value based on the correlation level.
Tradesignal® is a registered trademark of Tradesignal GmbH. Unauthorized use or misuse is strictly prohibited. Data by Refinitiv Eikon.