I have a string of tickers that I'm trying to get data for using Quantmod. My list is long and there are some bad tickers so I use the incorporate the try function.
I'm having trouble using the Cl function from Quantmod. As far as I can tell it should work - but I'm sure I have a small problem with my syntax.
Code:
tickers = c("IBM","GM") #This is an example for purpose here
stock_data <- lapply(tickers, function(tickers), try(getsymbols(tickers,
auto.assign=FALSE)))
close_prices <- do.call(merge,Cl(stock_data))
stock_data comes back as a list of xts objects identified by the ticker with the rows of each element as a date value and the columns a series of ticker-related data (open, close, etc.).
close_prices should be a list or data frame of only the closing price column of all tickers (i.e, the closing price column from IBM, the closing price column from GM, etc.).
When I apply the do.call (which I have in various permutations - none of them correct) I get an error that tells me their are no column values within the xts object that contain "close" - which just isn't the case. For each xts element (identified as "IBM" and "GM" in this case) there is a column named "XXX.close" (e.g., IBM.Close, GM.Close). I don't know why I can't get the syntax right or get Cl to see the close columns.
Any help appreciated.
Thank you.
**** Edit/UPDATE ***
My list stock_data for the sample vector has the following structure:
str(stock_data)
List of 2
$ :An ‘xts’ object on 2015-01-05/2015-12-07 containing:
Data: num [1:234, 1:6] 161 160 157 156 158 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:6] "IBM.Open" "IBM.High" "IBM.Low" "IBM.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "yahoo"
..$ updated: POSIXct[1:1], format: "2015-12-09 16:21:31"
$ :An ‘xts’ object on 2015-01-05/2015-12-07 containing:
Data: num [1:234, 1:6] 35 34.4 35.2 36.1 36.2 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:6] "GM.Open" "GM.High" "GM.Low" "GM.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "yahoo"
..$ updated: POSIXct[1:1], format: "2015-12-09 16:21:31"
The structure is the same for the actual vector - it is just longer with more dimensions.
try the following:
library(plyr)
library(quantmod)
stocks <- c("IBM","GM")
data.env <- new.env()
### here we use l_ply so that we don't double save the data
### getSymbols() does this already so we just want to be memory efficient
### go through every stock and try to use getSymbols()
l_ply(stocks, function(sym) try(getSymbols(sym,env=data.env),silent=T))
### now we only want the stocks that got stored from getSymbols()
### basically we drop all "bad" tickers
stocks <- stocks[stocks %in% ls(data.env)]
### now we just loop through and merge our good stocks
### if you prefer to use an lapply version here, that is also fine
### since now we are just collecting all the good stock xts() objects
data <- xts()
for(i in seq_along(stocks)) {
symbol <- stocks[i]
data <- merge(data, Ad(get(symbol,envir=data.env)))
}