Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only get closes #322

Open
ggrothendieck opened this issue Jan 7, 2021 · 7 comments
Open

only get closes #322

ggrothendieck opened this issue Jan 7, 2021 · 7 comments

Comments

@ggrothendieck
Copy link

A common situation is that one just wants the close (or adjusted close) from a set of tickers. It would be nice if getSymbols or some other function could directly output an xts object with one column per symbol.

@joshuaulrich
Copy link
Owner

That's a good suggestion and has been raised before. You can find an implementation in the qmao package by @gsee. I would be interested in your thoughts on the makePriceFrame() function. I haven't taken a close look at it in awhile.

The references to FinancialInstrument would have to be removed, since it depends on quantmod and would create a circular dependency.

@ethanbsmith
Copy link
Contributor

I have a function that merges multiple symbols into a data.table

getSymbolsAsDT <- function(Symbols, src = "yahoo", env = new.env(), ...) {
  importDefaults("getSymbolsAsDT")
  #returns a single OHLCV data.table of the requested symbols with Symbol as a column
  Symbols <- getSymbols(Symbols, src = src, env = env, auto.assign = T, reload.Symbols = F, ...)
  
  r <- sapply(Symbols, simplify = F, USE.NAMES = T, FUN = function(k){
    z <- env[[k]]
    z <- if (is.xts(z) && nrow(z) >= 1L) as.data.table(z) else NULL
    return(z)
  })
  r <- rbindlist(r, idcol = "Symbol")

  setnames(r, 2:7, c("index", "Open", "High", "Low", "Close", "Volume"))
  setkey(r, Symbol, index)
  return(r)
}

you can then easily dcast to get the shape you want, and convert back to xts:
as.xts(dcast(getSymbolsAsDT("SPY;TSLA"), index ~ Symbol, value.var = "Close", fill = T))

yes, a lot of conversion going on, but much of it is the super efficient and fast data.table code. I find it plenty fast enough for my needs

@braverock
Copy link
Collaborator

@ggrothendieck : Just to be explicit, you're asking for a wide format object containing multiple symbols from a single query, correct?

I would see two ways of doing this that wouldn't break backwards compatibility. One would be a wrapper for getSymbols which would call getSymbols and reshape the output data. This seems simplest from a technical perspective given how the code currently works.

The other way to do it would be much more work to redesign how getSymbols and the various method functions work internally, so I think a wrapper would make the most sense working with a private (to the function) environment.

@ggrothendieck
Copy link
Author

ggrothendieck commented Jan 7, 2021 via email

@ggrothendieck
Copy link
Author

ggrothendieck commented Jan 7, 2021 via email

@ethanbsmith
Copy link
Contributor

i didnt know about eapply that's cool.

Seems like a SymbolApply(Symbols, FUN) function that wraps the getSymbols, eapply, cbind would go a long way as a generic solution. Especially if it had nice error handling and output column naming

@joshuaulrich
Copy link
Owner

Related: #39.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants