4

I have the following code:

library(quantmod)
tckrs <- c("TLT", "LQD", "HYG", "SPY", "DBC")
NumTckrs  <-  length(tckrs)
getSymbols(tckrs, from="1900-01-01", to=Sys.Date())

# merge to allign the start dates
MainDF <- merge(Ad(TLT), Ad(LQD), Ad(HYG), Ad(SPY), Ad(DBC), all=FALSE)

I would like to not have to repeat the stock symbols in the last line. Does anyone know how this could be done?

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418

3 Answers3

5

Load all the data into an environment, then call Ad on each, and merge them. Also note that getSymbols returns an xts object by default, therefore your MainDF is an xts object, not a data.frame.

library(quantmod)
# create new environment
myEnv <- new.env()
# pull all data and load into myEnv
getSymbols("TLT;LQD;HYG;SPY;DBC", env=myEnv)
# eapply calls Ad on each symbol in myEnv and returns a list
# do.call calls merge with each element returned from eapply as an argument
MainXTS <- do.call(merge, c(eapply(myEnv, Ad),all=FALSE))
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
3

This is one line of code with my qmao package

library(qmao)
p <- makePriceFrame(tckrs, prefer='Adjusted', silent=TRUE)

For convenience, PF is an alias for makePriceFrame. Also, since by default, the function will find and use the "Adjusted" column if it exists, you can leave out the prefer argument.

p <- PF(tckrs)

You can also combine a bunch of these types of functions

library(FinancialInstrument)
p <- PF(getSymbols(stock(tckrs, currency("USD"))))

Also, note that if you hadn't assigned your Symbol names to tckrs, you'd be able to get them from the PriceFrame.

names(p)
[1] "TLT" "LQD" "HYG" "SPY" "DBC"

If you do not specify a prefer argument, it looks for a column that contains "Adjusted", then "Close", then "Mid", then Price". To see which column was used to make the PriceFrame, look at the "prefer" attribute

attr(p, "prefer")
[1] "Adjusted"

If you keep data in separate environments, PF can handle that as well.

getSymbols(tckrs, env=myEnv)
p <- PF(ls(myEnv), env=myEnv)
GSee
  • 48,880
  • 13
  • 125
  • 145
  • Will qmao ever find itself on CRAN, or is it eternally banished to R-Forge? – Joshua Ulrich Jun 25 '12 at 00:07
  • Thanks for the suggestion @JoshuaUlrich. I guess it's about that time. – GSee Jun 25 '12 at 00:50
  • @JoshuaUlrich it `Depends` on FinancialInstrument, soooo... maybe it is banished. I think the main reason we've been putting off pushing FinancialInstrument to CRAN is because the package-level docs need polishing up. I've also been hanging on to a sliver of hope that one day I'll have time to put currencies in their own environment, but I'm not sure anyone shares that hope ;-). So, maybe it's time to just get them both on CRAN. – GSee Jun 25 '12 at 01:24
  • 1
    @cdcaveman, install the package, then type `help(package=qmao, help_type="html")` – GSee Apr 18 '13 at 12:39
2

Joshua's is probably more elegant than mine:

res <- do.call( merge,  lapply(  lapply(tckrs, get) , Ad) )
Dason
  • 60,663
  • 9
  • 131
  • 148
IRTFM
  • 258,963
  • 21
  • 364
  • 487