Measuring trading system correlation using Python

One of the most important measurements when deciding what to trade is correlation. Building a trading system portfolio with strategies that are highly correlated is a recipe for failure since your strategies will all face drawdowns at similar times and your protection from strategy down-turns will be reduced compared to a properly diversified portfolio where strategies are sparingly correlated. On today’s post I want to show you how you can carry out this analysis in practice using Python. We will calculate global as well as rolling correlations for a group of four trading strategies. By using the code within this script you will then be able to diagnose the relationship between your trading strategy returns and build portfolios with strategies that only have low correlations. To reproduce the graphs within this post please make sure you download this sample system files.

The Pearson correlation measurement is a mathematical tool that helps us determine the degree of linear relationship between two variables. If two variables have a high correlation then a variation in one has been historically related with a variation in the other variable. In the case of trading systems a high correlation in strategy returns implies that the strategies are trading using similar market instruments or triggering behavior. Two trading systems that follow daily timeframe trends on the same instrument – even when using different indicators – are expected to be correlated to some degree since trends happen at the same time and therefore if both systems have been able to exploit them they must share some degree of correlation. We would want to avoid trading two strategies that have a high correlation in returns, since this implies that trading both is alike trading the same strategy with an increase in risk.

Correlations are however more complicated since there is more than one way to calculate them when talking about trading strategies (read here for more information). In general I calculate them using monthly returns, since this gives the most meaningful measurement as how different systems relate to different market conditions. Using shorter term correlations makes systems naturally appear more uncorrelated — as you can see in the linked post before. In the script shared above the correlation is calculated using monthly returns as well but you can change this by changing the resampling frequency in the pandas dataframe loading line (line 41) from “M” (monthly), to “D” (daily) or “W” (weekly).

The script above first calculates a correlation map which is a useful tool that tells you the global correlation value between the strategies. You can hover your mouse over the values when the matplotlib dialogue opens to see the value for each different square. In the above example all systems share a low correlation with the highest correlation being between systems 3 and 4 at a value of 0.266. This means that these strategies are largely uncorrelated in their monthly returns meaning that they hedge each other’s performance quite well. When evaluating a group of trading strategies the global correlation maps are a good way to identify groups of more or less related strategies and also to discard strategies that may already be largely correlated to systems that have already been created in the past.

Additionally the script also calculates the system-by-system 12 month rolling correlation (you can adjust the window size by changing the literal constant in the beginning of the script). This provides a deeper piece of information since it allows you to see how the monthly correlation has varied between each system pair as a function of time. In the example below you can see that although system 4 has a very low global correlation with system 1 – as seen in the correlation map – the maximum value for the rolling correlation is actually quite high, at close to 0.8. This means that although the strategies remain largely uncorrelated most of the time there are specific periods where the strategy correlations might spike significantly. You might want to control the standard deviation of the system-by-system correlation or you might also want to limit the maximum spike that any system pair within your portfolio has seen.