I'm using the quantmod package in R
to pull historical data from yahoo finance.
To use the package I create a list of tickers like so:
symbols <- c("MSFT", "ORCL", "AAPL", "FB")
To get the historical data I call the quantlib method getSymbols:
try(getSymbols(sym, src="yahoo"))
This creates variables in my environment called MSFT
, ORCL
, APPL
and FB
.
To calculate a correlation between MSFT and ORCL with closing prices I can use
cor(Cl(MSFT), Cl(ORCL))
To get:
ORCL.Close
MSFT.Close 0.6597159
How can I make this generic so that i can pull say 20 symbols and run the correlation between them?
I don't know how to refer to a variable given a string. ie I have the string "MFST", how do I refer to the variable MSFT
?
I've got the string "MFST" and "ORCL" how can I use that to write cor(Cl(MSFT), Cl(ORCL))