Correlation history over x number of previous years

Hi everyone,

The correlation function requires a lookback period. Im trying to come up with a way to get the correction of a symbol to an index over a 12 month period spanning back 10 years. For example correlation between symbol and index for yr 2021, 2020, 2019 ect.

This has been my lame duck attempt so far. It returns 10 results but each result compounds the years rather than reporting the correlation for just that 12 month period.

function Tick_Correl_Index(lookbackperiod)
AORD = Close;
CurrentSymbol = Close;
Correlation_ = Prec(Correlation(AORD,currentsymbol,lookbackperiod),2);

return Correlation_;


printf("\n Correl Index %g \n",Tick_Correl_Index(365*i));


Hi kgalea, a possible solution using array processing could be something like this:

//	parameters
stockTkr = "AAPL";          // example
indexTkr = "$SPX";         // example
corrLookBack = 250;	// approx number of trading days on each solar year 

//	correlation's calc
indexClose = Foreign(indexTkr, "C");
stockClose = Foreign(stockTkr, "C");
correlationArray = Correlation(stockClose, indexClose, corrLookBack);
firstDayOfTheNewYear = DayOfYear() < Ref(DayOfYear(), -1 );
yearlyCorrCoeff = IIf(firstDayOfTheNewYear, correlationArray, Null);

//	exploration
colName = "[" + stockTkr + " / " + indexTkr + "] yr. corr index";
AddColumn(yearlyCorrCoeff, colName, 1.3);
Filter = NOT IsNull(yearlyCorrCoeff) AND Name() == stockTkr;

You should run it as an exploration to give a tabular result of the correlation index:

or embed it into a proper function.


Wow, thats amazing. thank you so much 2dd for taking the time to write this out.

much appreciated.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.