I load historical prices with the quantmod getSymbols function for several tickers and convert them to a list or multidimensional XTS with the following code:
library(quantmod) myenv <- new.env() tickers <- c("^GSPC", "AAPL", "MSFT", "GOOG", "^RUT") getSymbols(tickers, env=myenv) ll <- eapply(myenv, function(x) x) # Convert to list ts <- do.call(merge, (eapply(myenv, Ad))) # Convert to multivariate XTS and extract only the adjusted price
The problem with this approach is that the order of the tickers in the list and XTS does not match what I specified in tickers :
> names(ll) [1] "AAPL" "GSPC" "GOOG" "RUT" "MSFT" > names(ts) [1] "AAPL.Adjusted" "GSPC.Adjusted" "GOOG.Adjusted" "RUT.Adjusted" [5] "MSFT.Adjusted"
I think this is because eapply performs operations randomly, as described in the eapply man pages:
Note that the order of the components is arbitrary for hashed environments.
How do I perform the same operations above but have output that is in the same order as in my tickers vector? That is, the first element of the list / first column of the XTS should correspond to the first element of the tickers vector, etc.
source share