joshuaulrich / quantmod

Quantitative Financial Modelling Framework
http://www.quantmod.com/
GNU General Public License v3.0
814 stars 224 forks source link

only get closes #322

Open ggrothendieck opened 3 years ago

ggrothendieck commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

Yes, basically this:

  library(quantmod);
  tickers <- c("AAPL", "TSLA");
  getSymbols(tickers, env = stocks <- new.env());
  result <- do.call("cbind", eapply(stocks, CL))

On Thu, Jan 7, 2021 at 11:18 AM Brian G. Peterson notifications@github.com wrote:

@ggrothendieck https://github.com/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.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/joshuaulrich/quantmod/issues/322#issuecomment-756217957, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB32F7Q66EUTC7GJRFNVJUTSYXNGNANCNFSM4VZESOJA .

-- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com

ggrothendieck commented 3 years ago

CL was supposed to be Cl

On Thu, Jan 7, 2021 at 11:18 AM Brian G. Peterson notifications@github.com wrote:

@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.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

-- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com

ethanbsmith commented 3 years ago

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 commented 3 years ago

Related: #39.