这是xts bug还是我不理解的特性?

时间:2021-12-15 12:38:02

I am trying to run this code:

我试着运行这段代码:

require(quantmod)
getSymbols("SPY")

data <- to.weekly(SPY)
# CentralPivot Point (P) = (High + Low + Close) / 3
center <- xts(rowSums(HLC(data))/3, order.by=index(data))
head(center)
head(Lo(data))
R1 <- (2*center) - Lo(data)  # First Resistance (R1) = (2*P) - Low
head(R1)
NROW(R1)

data <- to.monthly(SPY)
# CentralPivot Point (P) = (High + Low + Close) / 3
center <- xts(rowSums(HLC(data))/3, order.by=index(data))
head(center)
head(Lo(data))
R1 <- (2*center) - Lo(data)  # First Resistance (R1) = (2*P) - Low
R1
NROW(R1)

Which for the monthly series produces clearly unexpected result (and crashes R if you try to rbind(data, R1) for example):

对于月度系列来说,它会产生明显出乎意料的结果(如果您试图进行rbind(例如,data, R1),则会崩溃R):

> require(quantmod)
> getSymbols("SPY")
[1] "SPY"
> 
> data <- to.weekly(SPY)
> # CentralPivot Point (P) = (High + Low + Close) / 3
> center <- xts(rowSums(HLC(data))/3, order.by=index(data))
> head(center)
               [,1]
2007-01-05 141.2600
2007-01-12 142.2433
2007-01-19 142.8633
2007-01-26 142.5633
2007-02-02 143.8333
2007-02-09 144.2300
> head(Lo(data))
           SPY.Low
2007-01-05  140.38
2007-01-12  140.25
2007-01-19  142.31
2007-01-26  141.58
2007-02-02  141.74
2007-02-09  143.39
> R1 <- (2*center) - Lo(data)  # First Resistance (R1) = (2*P) - Low
> head(R1)
            SPY.Low
2007-01-05 142.1400
2007-01-12 144.2367
2007-01-19 143.4167
2007-01-26 143.5467
2007-02-02 145.9267
2007-02-09 145.0700
> NROW(R1)
[1] 285
> 
> data <- to.monthly(SPY)
> # CentralPivot Point (P) = (High + Low + Close) / 3
> center <- xts(rowSums(HLC(data))/3, order.by=index(data))
> head(center)
             [,1]
Jan 2007 142.7100
Feb 2007 142.1167
Mar 2007 140.8533
Apr 2007 146.5233
May 2007 151.6267
Jun 2007 150.9633
> head(Lo(data))
         SPY.Low
Jan 2007  140.25
Feb 2007  139.00
Mar 2007  136.75
Apr 2007  141.48
May 2007  147.67
Jun 2007  148.06
> R1 <- (2*center) - Lo(data)  # First Resistance (R1) = (2*P) - Low
> R1
Data:
numeric(0)

Index:
NULL
> NROW(R1)
[1] 0

This is information on my session.

这是我的会议信息。

> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8          LC_NUMERIC=C                 
 [3] LC_TIME=en_US.UTF-8           LC_COLLATE=en_US.UTF-8       
 [5] LC_MONETARY=en_US.UTF-8       LC_MESSAGES=en_US.UTF-8      
 [7] LC_PAPER=en_US.UTF-8          LC_NAME=en_US.UTF-8          
 [9] LC_ADDRESS=en_US.UTF-8        LC_TELEPHONE=en_US.UTF-8     
[11] LC_MEASUREMENT=en_US.UTF-8    LC_IDENTIFICATION=en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] quantmod_0.3-18 TTR_0.21-0      xts_0.8-7       zoo_1.7-7      
[5] Defaults_1.1-1  rj_1.1.0-4     

loaded via a namespace (and not attached):
[1] grid_2.15.0    lattice_0.20-0 tools_2.15.0  

I get the same problem on my windows machine with slightly different configuration...

我在windows电脑上遇到了同样的问题,配置稍有不同……

The biggest problem actually is that if you try to

最大的问题是,如果你想

cbind(data, R1)

R chrashes...

R chrashes……

Is this xts bug or is there some other problem?

这是xts错误还是有其他问题?

1 个解决方案

#1


4  

You need to set drop.time=TRUE in your call to to.monthly.

你需要设置drop。在你给to.monthly打电话的时候,它是真的。

data <- to.monthly(SPY, drop.time=TRUE)
# CentralPivot Point (P) = (High + Low + Close) / 3
center <- xts(rowSums(HLC(data))/3, order.by=index(data))
head(center)
head(Lo(data))
R1 <- (2*center) - Lo(data)  # First Resistance (R1) = (2*P) - Low
R1
NROW(R1)

And, as I said in my comment, calling merge.xts or cbind.xts on an xts object with no index causes a segfault. It's on the "FIX ME" list.

就像我在评论中说的,调用merge。xt或cbind。没有索引的xts对象上的xts会导致segfault。它在“修理我”名单上。

#1


4  

You need to set drop.time=TRUE in your call to to.monthly.

你需要设置drop。在你给to.monthly打电话的时候,它是真的。

data <- to.monthly(SPY, drop.time=TRUE)
# CentralPivot Point (P) = (High + Low + Close) / 3
center <- xts(rowSums(HLC(data))/3, order.by=index(data))
head(center)
head(Lo(data))
R1 <- (2*center) - Lo(data)  # First Resistance (R1) = (2*P) - Low
R1
NROW(R1)

And, as I said in my comment, calling merge.xts or cbind.xts on an xts object with no index causes a segfault. It's on the "FIX ME" list.

就像我在评论中说的,调用merge。xt或cbind。没有索引的xts对象上的xts会导致segfault。它在“修理我”名单上。