I am stuck with a minor quantmod problem; if anyone can suggest a tweak to my code, I’d really appreciate that. I don’t know progamming as such; maybe that’s why I miss the obvious. The problem is arising because getSymbols
takes a string as input (e.g. "YHOO"), but returns just YHOO (without quotes) as the xts object which holds the data. Also, for market indices, Yahoo includes a caret in the string for the code (e.g. "^GSPC"), but quantmod returns plain GSPC as the data object.
I am trying to download and save to individual binary files the data of multiple tickers. This is so as to create a work environment which can function from data stored on disk, instead of requiring internet access necessarily.
I tried writing the function:
buildhist <- function(x,start,end) {
getSymbols(x, from=start, to=end, adjust=TRUE)
save(get(x), file= paste(x, "hist.rda", sep="_"), ascii = FALSE)
}
Then use
require(quantmod)
tckr <- c("YHOO","XLB")
lapply(tckr,buildhist,start="1995-01-01",end="2011-11-30")
But, it errors out at the save command (saying "object ‘get(x)’ not found"). If I don’t use get(x)
, the save
command will only save the ticker name as string, so I can’t use that. No other version such as save(noquote(x), file=paste(x, "hist.rda", sep="_"), ascii=FALSE)
works either.
What command should I use so that the ticker data will be saved using the same object name as it is originally returned by quantmod? In my code above I haven’t even tried to tackle the other problem – that of stripping the caret sign from the name if it exists. Any pointers to that would be much appreciated too.