joshuaulrich / xts

Extensible time series class that provides uniform handling of many R time series classes by extending zoo.
http://joshuaulrich.github.io/xts/
GNU General Public License v2.0
220 stars 71 forks source link

complex xts objects #346

Closed ggrothendieck closed 3 years ago

ggrothendieck commented 3 years ago

This gives an error:

library(quantmod)
tickers <- c("AAPL", "TSLA")
getSymbols(tickers, env = stocks <- new.env())
L <- eapply(stocks, function(x) Cl(x) + Vo(x) * 1i)
res <- do.call("cbind", L)
## Error in merge.xts(..., all = all, fill = fill, suffixes = suffixes) : 
##   REAL() can only be applied to a 'numeric', not a 'complex'

If we convert to zoo and back then we do not get any errors:

library(quantmod)
tickers <- c("AAPL", "TSLA")
getSymbols(tickers, env = stocks <- new.env())
L <- eapply(stocks, function(x) Cl(as.zoo(x)) + Vo(as.zoo(x)) * 1i)
res <- as.xts(do.call("cbind", L))
joshuaulrich commented 3 years ago

Thanks for the report! I'll take a look.

joshuaulrich commented 3 years ago

Here's a minimal reproducible example:

library(xts)
data(sample_matrix)

x <- as.xts(sample_matrix) 
y <- x[1:5,1] * 1i
z <- x[3:9,4] * 1i
a <- merge(y, z)
# Error in merge.xts(y, z) : 
#   REAL() can only be applied to a 'numeric', not a 'complex'
ggrothendieck commented 3 years ago

Regarding the quantmod example what would be nice would be if a column could contain the entire OHLCV rather than just close and volulme. Would it be feasible to support a new tuple type that is like complex except it would allow each column to contain multiple columns, not just 2 like complex does.