Last active
August 30, 2020 19:31
-
-
Save sguzman/770e2e5e7a3eb038537383c02f5cb869 to your computer and use it in GitHub Desktop.
A simple correlation of two securities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
With[{ | |
a = "SPXL", | |
b = "VIXY" | |
}, | |
With[{ | |
apath = FinancialData[a, "Close", All]["DatePath"], | |
bpath = FinancialData[b, "Close", All]["DatePath"] | |
}, | |
With[{ | |
adiff = Transpose[{ | |
Map[First, apath][[;; -2]], | |
Ratios@Map[Last, apath] | |
}], | |
bdiff = Transpose[{ | |
Map[First, bpath][[;; -2]], | |
Ratios@Map[Last, bpath] | |
}] | |
}, | |
With[{ | |
joined = GatherBy[Join[adiff, bdiff], First] | |
}, | |
With[{ | |
select = Select[joined, Length@# == 2 &] | |
}, | |
With[{ | |
trim = | |
Table[{First@First@s, {Last@First@s, Last@Last@s}}, {s, | |
select}] | |
}, | |
With[{ | |
part = Partition[trim, 5, 1] | |
}, | |
With[{ | |
co = Table[ | |
With[{ | |
veca = Map[Composition[Last, First], p], | |
vecb = Map[Composition[Last, Last], p] | |
}, | |
{First@First@p, | |
Normalize[veca].Normalize[vecb] | |
}], {p, part}] | |
}, | |
co | |
]]]]]]]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment