这是xts bug还是我不明白的功能?

时间:2012-06-14 17:37:04

标签: r finance xts

我正在尝试运行此代码:

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)

每月系列会产生明显意外的结果(例如,如果您尝试使用rbind(数据,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

这是关于我的会话的信息。

> 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  

我在Windows机器上遇到的问题略有不同......

实际上最大的问题是如果你试图

cbind(data, R1)

R chrashes ......

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

1 个答案:

答案 0 :(得分:4)

您需要在drop.time=TRUE的通话中设置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)

正如我在评论中所说,在没有索引的xts对象上调用merge.xtscbind.xts会导致段错误。它位于“FIX ME”列表中。