kgalea
November 15, 2022, 2:15pm
1
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)
{
SetForeign(".AORD");
AORD = Close;
RestorePriceArrays();
CurrentSymbol = Close;
Correlation_ = Prec(Correlation(AORD,currentsymbol,lookbackperiod),2);
return Correlation_;
}
for(i=1;i<10;i++)
{
printf("\n Correl Index %g \n",Tick_Correl_Index(365*i));
}
2DD
November 15, 2022, 5:50pm
2
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.
3 Likes
kgalea
November 16, 2022, 9:56am
3
Wow, thats amazing. thank you so much 2dd for taking the time to write this out.
much appreciated.
1 Like
system
Closed
February 24, 2023, 9:57am
4
This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.