oops. should have been:

alignSymbols <- function(symbols, env=.GlobalEnv) {
if (length(symbols) < 2)
stop("Must provide at least 2 symbols")
if (any(!is.character(symbols)))
stop("Symbols must be vector of character strings.")
ff <- get(symbols[1],env=env)
for (sym in symbols[-1]) {
tmp.sym <- get(sym,env=env)
ff <- merge(ff,tmp.sym,all=FALSE)
}
for (sym in symbols) {
assign(sym,ff[,grep(sym,colnames(ff))],env=env)
}
symbols
}

Garrett

On Tue, May 10, 2011 at 11:50 AM, G See <gsee000@gmail.com> wrote:

> My demo chokes if either symbol has data for a date that the other one
> doesn't.  Since there are a lot of functions in general that expect xts
> objects to have identical timestamps, I find myself doing something like
> this a lot:
>
> tmp <- merge(get(symb1),get(symb2),all=FALSE)
> assign(symb1,tmp[,1:6],env=.GlobalEnv)
> assign(symb2,tmp[,7:12], env=.GlobalEnv)
>
> It seems like either getSymbols should support this (i.e. when you call
> getSymbols to get more than one symbol at once there should be an option in
> getSymbols to only return rows that all of the symbols have in common), or
> that there should be a function in xts, quantmod, or quantstrat that does
> this, but I haven't found it.  Is there?  If not, here is something that
> might come in handy (especially if you need to do something like this for
> several symbols):
>
> #DESCRIPITION: Give it the names of several xts objects,
> #and for all objects, it will delete the
> #rows where any them are missing a value.
>
> alignSymbols <- function(symbols, env=.GlobalEnv) {
>  if (length(symbols) < 2)
>  stop("Must provide at least 2 symbols")
>  if (any(!is.character(symbols)))
>  stop("Symbols must be vector of character strings.")
>  ff <- get(symbols[1],env=env)
>  for (sym in symbols[-1]) {
>  tmp.sym <- get(sym,env=env)
>  ff <- merge(ff,tmp.sym,all=FALSE)
>  }
> for (sym in symbols) {
>  assign(sym,ff[,grep(sym,colnames(get(sym)))],env=env)
>  }
> symbols
> }
>
> require(quantmod)
> symbols <- c('DBA','CORN','SPY','DIA')
> getSymbols(symbols)
> length(DBA[,1]);length(CORN[,1]);length(SPY[,1]);length(DIA[,1])
> alignSymbols(symbols)
> length(DBA[,1]);length(CORN[,1]);length(SPY[,1]);length(DIA[,1])
>
>

	[[alternative HTML version deleted]]

