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.
UPDATE: The solution below doesn't solve the OP's problem (see comments). See the edit after the jump.
The default of auto.assign=TRUE
is supposed to make things easier when using getSymbols
interactively. Set auto.assign=FALSE
when using getSymbols
in a function; it will make things much easier.
buildhist <- function(x,start,end) {
y <- getSymbols(x, from=start, to=end, adjust=TRUE, auto.assign=FALSE)
save(y, file= paste(x, "hist.rda", sep="_"), ascii = FALSE)
}
You can remove punctuation characters (including the caret) via gsub
. See ?gsub
and ?regex
for details.
X <- gsub("[[:punct:]]","",x) # remove all punctuation
X <- gsub("\\^","",x) # remove just the carat
I didn't test my initial answer. This solution should work.
buildhist <- function(x,start,end) {
getSymbols(x, from=start, to=end, adjust=TRUE)
X <- toupper(gsub("\\^","",x)) # what getSymbols.yahoo does
save(list=X, file= paste(X, "hist.rda", sep="_"), ascii = FALSE)
}
require(quantmod)
tckr <- c("^GSPC","YHOO","XLB")
lapply(tckr,buildhist,start="1995-01-01",end="2011-11-30")
If you're only using daily data on a small number of symbols, you may be able to load them all to one environment and just save the environment. That could save you a lot of trouble. Then you could load the environment to a new session, attach it, and have all the data at your fingers.
myEnv <- new.env()
getSymbols(paste(tckr,sep=";"), start="1995-01-01", end="2011-11-30",
env=myEnv, adjust=TRUE)
save(myEnv, file="myTickerData.rda")