Skip to content

Instantly share code, notes, and snippets.

@sguzman
Last active August 30, 2020 19:31
Show Gist options
  • Save sguzman/770e2e5e7a3eb038537383c02f5cb869 to your computer and use it in GitHub Desktop.
Save sguzman/770e2e5e7a3eb038537383c02f5cb869 to your computer and use it in GitHub Desktop.
A simple correlation of two securities
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