Skip to content

Instantly share code, notes, and snippets.

@sguzman
Created March 27, 2021 22:59
Show Gist options
  • Save sguzman/8c2d454dffcdbcdd814c281c1fbb862d to your computer and use it in GitHub Desktop.
Save sguzman/8c2d454dffcdbcdd814c281c1fbb862d to your computer and use it in GitHub Desktop.
Rank correlation of sp500 stocks for this year
Block[{tmp},
items = EntityList@EntityClass["Financial", "SP500"];
begin = DateObject[{2021, 1, 1}, "Day", "Gregorian", -7.`];
end = DateObject[{2021, 3, 3}, "Day", "Gregorian", -7.`];
fin[str_, beg_, en_] :=
fin[str, beg, en] = FinancialData[str, "Close", {beg, en}];
corr[stra_, strb_, beg_, en_] :=
corr[stra, strb, beg, en] =
Correlation[fin[stra, beg, en], fin[strb, beg, en]];
pairs = Subsets[items, {2}];
final = Map[{
First@#,
Last@#,
corr[First@#, Last@#, begin, end]
} &, pairs];
sorted = TakeSmallestBy[final, Last, 100];
Grid@
sorted
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment