1

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.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Tatha
  • 127
  • 2
  • 11

3 Answers3

2

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")
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Many thanks Joshua. I tried this. The problem is that the object name is stored as y now and not, say, XLB. So, when I do this in a loop, all tickers are saved as y and when I load them back from disk in a loop, they are all y. I need to re-load and access the data as I would do after downloading fresh. So, need the data when I enter XLB. – Tatha Dec 19 '11 at 14:18
  • I haven't used quantmod, but the manual seems to imply `getSymbols` does not return anything, just loads the item into the environment. That's why I proposed my version of `lapply` below. Please correct me if I'm wrong - I don't want to send people astray. – Carl Witthoft Dec 19 '11 at 14:23
  • @Joshua, may I suggest an extension to your saveSymbols function, which would carry out incremental updates to the files on disk. I have my own half-baked routine to do this, which is why I’m posting this question here, trying to implement it in a loop. But if saveSymbols would merge data into an older file on disk, then my query would be redundant. – Tatha Dec 19 '11 at 14:52
  • @Tatha: Sorry for that incorrect solution; see my update. I'm not familiar with the `saveSymbols` function, but I'll pass your suggestion to the quantmod author. – Joshua Ulrich Dec 19 '11 at 15:22
  • @Joshua, Many thanks for your prompt reply. It works. I need to now extend it to re-load it from disk, and append recent data. So, I may be back...:) – Tatha Dec 19 '11 at 15:58
  • @Joshua, sorry, I mistakenly presumed you were involved with quantmod (having seen your name umpteen times from the xts work that you've done). That environment saving idea, however, doesn't work, because the number of symbols is large; and I wanted not having to load all of them just to analyze any one. Also I use the the make.index.unique(rbind(GSPC_old,GSPC),drop=T) styled statements to append new data individually to tickers after loading them from disk....so, I need to learn the loop/lapply methods...:) – Tatha Dec 19 '11 at 16:02
  • @Tatha: I am somewhat involved with quantmod, just not familiar with that specific function. Glad this solved your problem. – Joshua Ulrich Dec 19 '11 at 16:05
  • @Joshua, that 'list=' statement makes all the difference; and with my level of expertise, I would never have come up with it...:) – Tatha Dec 19 '11 at 16:07
  • @Joshua: your `list=` is a much better idea than my double-lapply thing. – Carl Witthoft Dec 19 '11 at 16:51
2

Can you verify your getSymbols call succeeded by doing something like summary(YHOO) ? You definitely do not want to write (save(get(x)) because there is no object named "get(x)".

I suspect your problem is related to proper use of lapply . This works:

foo <- 5
oof <- 4
bar<-c("foo","oof")
lapply(lapply(bar,get),sqrt)

(where of course you'd be using buildhist not sqrt)

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • Yes getSymbols succeeds, and get(tckr[1]) does show all the data on screen. But, you're right it is not an object. In quantmod, one can assign the getSymbols to an object as the author suggested to me above. But, I later get stuck in my naming requirements for the save function. Let me try to play with double lapply code above :) – Tatha Dec 19 '11 at 14:40
2

In below, SymbolList is a vector that has the symbols. i.e.

SymbolList <- c("IBM","GOOG","YHOO")

Get the historical data for these using getSymbols

getSymbols(SymbolList)

define a function and use the do.call,

fun <- function(i) {return(adjustOHLC(get(SymbolList[i]),adjust = "split", use.Adjusted=TRUE))}
mydata <- do.call(merge, lapply(1:length(SymbolList), fun))

this will give you the data merged, couple other things you could use, i.e. if you want only the closing values and adjust for the splits.

 fun <- function(i) {return(Cl(adjustOHLC(get(SymbolList[i]),adjust = "split", use.Adjusted=TRUE)))}

you may want to cast into a data frame as well.

mydata<-as.data.frame(mydata)

from there you could write this to a csv file.

Arda Ucar
  • 21
  • 1