1

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))

chollida
  • 7,834
  • 11
  • 55
  • 85
  • 1
    `get("MSFT")` will give you the variable `MSFT`. That said, you may be better off putting each of these in a list, which would let you do nice `apply` things to all of them. – Gregor Thomas Oct 27 '14 at 17:09
  • @Gregor, wow, you are right. So simple, but I have no idea how to search for that. Could you please put that in an answer so I can accept it? – chollida Oct 27 '14 at 17:11

2 Answers2

7

The easiest way is to store the data in an environment, and then loop over all the objects in the environment with eapply:

library(quantmod)
dataEnv <- new.env()
getSymbols("MSFT;ORCL;AAPL;FB", env=dataEnv)
# Extract the close column from all objects,
# then merge all close columns into one object
cl <- do.call(merge, eapply(dataEnv, Cl))
# note the warning in the Details section of ?cor
# regarding the use of "pairwise.complete.obs"
cor(cl, use="pairwise.complete.obs")
#            AAPL.Close ORCL.Close MSFT.Close   FB.Close
# AAPL.Close  1.0000000  0.6467283  0.2528689 -0.5997337
# ORCL.Close  0.6467283  1.0000000  0.6597159  0.8211939
# MSFT.Close  0.2528689  0.6597159  1.0000000  0.8979836
# FB.Close   -0.5997337  0.8211939  0.8979836  1.0000000
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
2

The get() function does what you're looking for, e.g. get("MSFT").

Generally, you might do better to store everything in a list rather than in the global environment. If you want to do something to all of them having them in a list makes it a bit easier. I talk a little more about that in this answer, though I'm sure there are better discussions out there.

Edit: In this case, rather than a list, putting things in their own environment as in @Josh Ulrich's answer is probably best.

Community
  • 1
  • 1
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294