Creating an FX Correlation Heatmap in Python
How to Create a Correlation Heatmap in Python to Aid in Currency Trading
Correlations across different assets and markets is crucial to determine any directional bias and trigger for a trade. If we have a buy signal on one asset and a sell signal on another, but yet they are almost perfectly correlated then we know that one of the signals is wrong because empirically, we have the information that they move together in the same direction.
Correlation management is part of risk management and is an important part in it. In this article, we will se how to create a correlation heatmap so as to get quick glances at the current state of correlation between different FX pairs.
I have released a new book after the success of my previous one “Trend Following Strategies in Python”. It features advanced contrarian indicators and strategies with a GitHub page dedicated to the continuously updated code. If you feel that this interests you, feel free to visit the below Amazon link (which contains a sample), or if you prefer to buy the PDF version, you could check the link at the end of the article.
Contrarian Trading Strategies in Python
Amazon.com: Contrarian Trading Strategies in Python: 9798434008075: Kaabar, Sofien: Bookswww.amazon.com
The Concept of Correlation
Correlation is the degree of linear relationship between two or more variables. It is bounded between -1 and 1 with one being a perfectly positive correlation, -1 being a perfectly negative correlation, and 0 as an indication of no linear relationship between the variables (they relatively go in random directions). The measure is not perfect and can be biased by outliers and non-linear relationships, it does however provide quick glances to statistical properties. Two famous types of correlation exist and are commonly used:
Spearman correlation measures the relationship between two continuous or ordinal variables. Variables may tend to change together, but not necessarily at a constant rate. It is based on the ranks of values rather than the raw data.
Pearson correlation measures the linear relationship between two continuous variables. A relationship can be considered linear when a change in one is accompanied with a proportional change in the other.
The measure is not perfect and can be biased by outliers and non-linear relationships, it does however provide quick glances to statistical properties.
We can code the correlation function between two variables in Python using the below. Note that it has to be an array and not a data frame:
def rolling_correlation(Data, first_data, second_data, lookback, where): for i in range(len(Data)): try: Data[i, where] = pearsonr(Data[i - lookback + 1:i + 1, first_data], Data[i - lookback + 1:i + 1, second_data]) except ValueError: pass Data = jump(Data, lookback) return Data
Importing OHLC Data
One of the most famous trading platforms in the retail community is the MetaTrader5 software. It is a powerful tool that comes with its own programming language and its huge online community support. Most importantly, it offers the possibility to export its historical short-term and long-term FX data. The first thing we need to do is to simply download it from the official website. Then, after creating the demo account, we are ready to import the library in Python that allows to import the OHLC data from MetaTrader5.
A library is a group of structured functions that can be imported into our Python interpreter from where we can call and use the ones we want.
The easiest way to install the library is to go to the Python prompt on our computer and type:
pip install MetaTrader5
This should install the library in our local Python. Now, we want to import it to the Python interpreter (such as Pycharm or SPYDER) so that we can use it. Let us actually import all the libraries we will be using for this:
import datetime # Date acquiring import pytz # Time zone management import pandas as pd # Mostly for Data frame manipulation import MetaTrader5 as mt5 # Importing OHLC data import matplotlib.pyplot as plt # Plotting charts import numpy as np # Mostly for array manipulation
Anything that comes after “as” is a shortcut. The plt shortcut is there so that each time we want to call a function from that library we do not have to type the full matplotlib.pyplot statement.
The first thing we can do is to select which time frame we want to import. Let us suppose that there are only two frames, the 30-minute and the hourly bars. We can therefore create variables that hold the statement to tell the MetaTrader5 library which frame we want.
# Choosing the 30-minute time frame frame_M30 = mt5.TIMEFRAME_M30# Choosing the hourly time frame frame_H1 = mt5.TIMEFRAME_H1
Then, by staying in the spirit of importing variables, we can define the variable that states what date is it now. This helps the algorithm know the stopping date of the import. We can do this by the simple line of code below.
# Defining the variable now to give out the current date now = datetime.datetime.now()
Note that these code snippet are better used chronologically, hence, I encourage you to copy them in order and then execute them one by one so that you understand the evolution of what you are doing. The below is a function that holds which assets we want. Generally, I use 10 or more but for simplicity, let us consider that there are only two currency pairs: EURUSD and USDCHF.
def asset_list(asset_set): if asset_set == 1: assets = ['EURUSD', 'USDCHF'] return assets
Now, with the key function that gets us the OHLC data. The below establishes a connection to MetaTrader5, applies the current date, and extracts the needed data. Notice the arguments year, month, and day. These will be filled by us to select from when do we want the data to start. Note, I have inputed Europe/Paris as my time zone, you should use your time zone to get more accurate data.
def get_quotes(time_frame, year = 2005, month = 1, day = 1, asset = "EURUSD"): # Establish connection to MetaTrader 5 if not mt5.initialize(): print("initialize() failed, error code =", mt5.last_error()) quit() timezone = pytz.timezone("Europe/Paris") utc_from = datetime.datetime(year, month, day, tzinfo = timezone) utc_to = datetime.datetime(now.year, now.month, now.day + 1, tzinfo = timezone) rates = mt5.copy_rates_range(asset, time_frame, utc_from, utc_to) rates_frame = pd.DataFrame(rates)
And finally, the last function we will use is the one that uses the below get_quotes function and then cleans the results so that we have a nice array. We have selected data since January 2019 as shown below.
def mass_import(asset, horizon): if horizon == 'M30': data = get_quotes(frame_M30, 2019, 1, 1, asset = assets[asset]) data = data.iloc[:, 1:5].values data = data.round(decimals = 5)
Finally, we are done building the blocks necessary to import the data. To import EURUSD OHLC historical data, we simply use the below code line:
# Choosing the horizon horizon = 'M30'
# Creating an array called EURUSD having M30 data since 2019 EURUSD = mass_import(0, horizon)
And voila, now we have the EURUSD OHLC data from 2019.
If you are also interested by more technical indicators and strategies, then my book might interest you:
The Book of Trading Strategies
Amazon.com: The Book of Trading Strategies: 9798532885707: Kaabar, Sofien: Bookswww.amazon.com
Creating the Correlation Heatmap
The heatmap will be created using a library called seaborn. It will take care of the plotting part. The data preparation and structuring part will be handled manually using the next code snippets. All we need is the importing function from the previous section. First, let us make sure we have the right libraries imported.
# Importing libraries import pandas as pd import numpy as np import seaborn as sn
The next step is to determine the array of the FX pairs we wish to cover. In our example, we have chosen 9 different currency pairs as shown below:
assets = ['EURUSD', 'USDCHF', 'GBPUSD', 'AUDUSD', 'NZDUSD', 'USDCAD', 'EURCAD', 'EURGBP', 'EURCHF']
The next step is to import them to the Python interpreter using the mass_import function which we have seen previously. For each of the following currency pairs, we will be doing three things:
Importing the OHLC historical data from MetaTrader5 using the mass_import function.
Removing all the columns except for the closing price column as we will calculate the correlation measure on it.
Select the last 1000 data as correlation is dynamic and is unlikely to resemble the correlation from a few years ago. With hourly values, we have selected the last 1000 data (hourly bars).
# Mass imports EURUSD = mass_import(0, horizon) EURUSD = EURUSD[:, 3:4] EURUSD = EURUSD[-1000:, ]USDCHF = mass_import(1, horizon) USDCHF = USDCHF[:, 3:4] USDCHF = USDCHF[-1000:, ]GBPUSD = mass_import(2, horizon) GBPUSD = GBPUSD[:, 3:4] GBPUSD = GBPUSD[-1000:, ]AUDUSD = mass_import(3, horizon) AUDUSD = AUDUSD[:, 3:4] AUDUSD = AUDUSD[-1000:, ]NZDUSD = mass_import(4, horizon) NZDUSD = NZDUSD[:, 3:4] NZDUSD = NZDUSD[-1000:, ]USDCAD = mass_import(5, horizon) USDCAD = USDCAD[:, 3:4] USDCAD = USDCAD[-1000:, ]EURCAD = mass_import(6, horizon) EURCAD = EURCAD[:, 3:4] EURCAD = EURCAD[-1000:, ]EURGBP = mass_import(7, horizon) EURGBP = EURGBP[:, 3:4] EURGBP = EURGBP[-1000:, ]EURCHF = mass_import(8, horizon) EURCHF = EURCHF[:, 3:4] EURCHF = EURCHF[-1000:, ]
The next step is to concatenate (join) every column from every pair into one array using the concatenate function from numpy as shown below.
Correlation_Matrix = np.concatenate((EURUSD, USDCHF, GBPUSD, AUDUSD, NZDUSD, USDCAD, EURCAD , EURGBP, EURCHF), axis = 1)
Then, we want to name the columns, but this can only be done in data frames. We will therefore simply convert the array into a dataframe and then name the columns as seen in the code snippet below.
Correlation_Matrix = pd.DataFrame(Correlation_Matrix)Correlation_Matrix.columns = ['EURUSD', 'USDCHF', 'GBPUSD', 'AUDUSD', 'NZDUSD', 'USDCAD', 'EURCAD', 'EURGBP', 'EURCHF']
We have all we need for the heatmap now. All is left to do is the line of seaborn to plot the map. This can be done easily following the below syntax.
# Visual representation sn.heatmap(Correlation_Matrix.corr(), cmap ="YlGnBu")
What does the heatmap above tell us? The first thing we must notice is the scale on the right which tells us how to interpret the colors (or the heat). Beige color refers to strong negative correlation while dark blue color refers to strong positive color. The green-ish blue is where correlation is close to zero, meaning that the two market move independently and are not really related.
Take a look at the correlation of the EURUSD vs GBPUSD, AUDUSD, and NZDUSD. We can notice that as they are USD majors, the greenback’s movements have been dominant and therefore causing them to move together and to be lead by the USD.
Unrelated pairs like the NZDUSD and EURCHF will likely have a zero correlation which is the case if we take a look at the above heatmap. The color green and blue is the prevalent one, proving this hypothesis.
As expected, the EURUSD and USDCHF have an extremely high negative correlation. When one moves up, the other is likely to move down. CHF is related to the Euro zone and therefore it is likely to have the same relationship with the USD as the EUR has with it.
Before taking a second trade or simultaneous trades, we need to check first the correlation and see if we are increasing or decreasing our risk.
Remember to always do your back-tests. You should always believe that other people are wrong. My indicators and style of trading may work for me but maybe not for you.
I am a firm believer of not spoon-feeding. I have learnt by doing and not by copying. You should get the idea, the function, the intuition, the conditions of the strategy, and then elaborate (an even better) one yourself so that you back-test and improve it before deciding to take it live or to eliminate it. My choice of not providing specific Back-testing results should lead the reader to explore more herself the strategy and work on it more.
To summarize up, what I am trying to do is to simply contribute to the world of objective technical analysis which is promoting more transparent techniques and strategies that need to be back-tested before being implemented. This way, technical analysis will get rid of the bad reputation of being a subjective and scientifically unfounded.
I recommend you always follow the the below steps whenever you come across a trading technique or strategy:
Have a critical mindset and get rid of any emotions.
Back-test it using real life simulation and conditions.
If you find potential, try optimizing it and running a forward test.
Always include transaction costs and any slippage simulation in your tests.
Always include risk management and position sizing in your tests.
Finally, even after making sure of the above, stay careful and monitor the strategy because market dynamics may shift and make the strategy unprofitable.
For the PDF alternative, the price of the book is 9.99 EUR. Please include your email in the note before paying so that you receive it on the right address. Also, once you receive it, make sure to download it through google drive.
Pay Kaabar using PayPal.Me
If you accept cookies, we’ll use them to improve and customize your experience and enable our partners to show you…www.paypal.com