# Correlations between sectors

Hi,
I want to write a code for correlations between sectors… . Can you help me?
Thank you for help, Maury

@maury314 Some places with information on correlation afl's.

Function in User Guide,
https://www.amibroker.com/guide/afl/correlation.html

Correlation table in Knowledge Base
http://www.amibroker.com/kb/2014/12/04/how-to-display-correlation-between-symbols/

Discussion on this forum,

Code for correlation between two symbols with color coded line Plot,

``````// Measure and Plot correlation between two tickers
// code derived from http://backtestwizard.com/correlation-indicator-amibroker-2/

// If you right-click on the indicator and click “parameters”,
// you are able to choose the second ticker which is used to calculate the
// is the first ticker.  You can also change the look-back
// period used to calculate the correlation co-efficient.

Ticker2	= ParamStr( "Ticker", "SPY" );
Index 	= Foreign( ticker2, "C" );
CorrLB 	= Param( "Lookback", 60, 1, 300, 1 ); //correlation look-back period//
Corr 	= Correlation( log( C / Ref( C, -1 ) ), log( index / Ref( index, -1 ) ), CorrLB );

Plot( Corr, "(" + Ticker2 + ")", IIf( Corr >= 0.8, colorGreen, IIf( Corr >= 0.6
AND Corr < 0.8, colorLime, IIf( Corr >= 0.4
AND Corr < 0.6, colorPaleGreen, IIf( Corr >= 0.2
AND Corr < 0.4, colorWhite, IIf( corr >= 0.0
AND corr < 0.2, colorRose, colorRed ) ) ) ) ), 0, 0, 0, 0, 0, 3 );
///////////////////////////////////////////////////
/*
You’ll notice that the indicator is colour coded.
These are the conditional colour parameters…

Very Highly Correlated 		= GREEN
Highly Correlated 			= LIME
Moderately Correlated 		= PaleGreen
Weakly Correlated 			= WHITE
Very Weak to No Correlation = ROSE
Negatively Correlated 		= RED

*/
``````

10 Likes

Thank you very much!!!

1 Like