Looking at Correlation

Recently I started to look a bit more closely at correlation. From any book/article on Modern Portfolio Theory, one can learn that diversification improves the return/risk ratio. Through adding assets the overall return/risk ratio of the total portfolio improves as it moves towards the efficient frontier.  The main driver of this improvement is the diversification that is brought by (less) correlated assets. Triggered by this thought I started to look at correlation of assets and correlation of strategies

First the tools. As I am using Amibroker to do my analysis, I used the built-in correlation() function. In the AB knowledgebase I found some code to produce a correlation table. I have modified this code to calculate the correlation of returns of the tickers in a watchlist. The parameters that can be set are the watchlist number that contains the targeted assets  and the length of the period to calculated the correlation. The code for the table is at the end of this post. It needs to be run in explore mode.

Running it on the tickers that are selected for the WTAA strategy, for a period of 21 Days (1 Month) and 252 (1 Year) produces the tables below:

 

 

 

 

 

 

 

 

Having seen these two tables I wanted to get more insight in the dynamic profile of the correlations. In other words, how does the correlation fluctuate over time? So I changed the code and created an indicator that can be added to any chart in Amibroker. Again the two parameters are the watchlist with tickers under analysis and the correlation period. Below is the graph for correlation period 21 and colleration 252 days. The code for the indicator is at the end of this post.

This concludes the post for today. In the follow up post we will look at applying the tools on a set of strategies.

– QD –

CODE

// eCorrelationTable.afl
WLNum = Param("WatchlistNr",0,0,255,1);
CorrPd = Param("Correlation Period",252,1,300,1);
list = GetCategorySymbols( categoryWatchlist, WLNum);
SetOption("NoDefaultColumns",True);
Filter = DateNum()==Status("rangetodate");
//SetSortColumns( 2 );
AddTextColumn(Name(),"Corr("+NumToStr(Corrpd,1.0)+")",1.0,width=70);
Ticker1= Name();
for( Col=0; (Ticker2=StrExtract( List, Col))!= ""; Col++)
{
Var2 = Foreign(Ticker2,"C");
Corr = Correlation( C/Ref(C,-1), Var2/Ref(Var2,-1), CorrPd);
Color = IIf(Corr>0.5, colorLime, IIf(Corr>0,colorBrightGreen,IIf(Corr<-0.5, colorRed,IIf(Corr<0,colorOrange,colorWhite))));
Color = IIf(Ticker1==Ticker2, 1, color);
AddColumn( Corr, Ticker2, 1.3, 1, Color,width=60);
}
// iCorrelation.afl
WLNum = Param("Watchlist",0,0,255,1);
CorrPd = Param("Correlation Period",252,1,300,1);
list = GetCategorySymbols( categoryWatchlist, WLNum);
Ticker1= Name();
for( x=0; (Ticker1=StrExtract( List, x))!= ""; x++)
{
for( y=0; (Ticker2=StrExtract( List, y))!= ""; y++)
{
if (x>y)
{
Var1 = Foreign(Ticker1,"C");
Var2 = Foreign(Ticker2,"C");
Corr = Correlation( Var1/Ref(Var1,-1), Var2/Ref(Var2,-1), CorrPd);
Plot(Corr, Ticker1+"x"+Ticker2, colorBlack+x );
}
}
}

Leave a Comment