我无法解决为什么在简单创建xts对象时出现错误
xts(rep(0, NROW(TICK.NYSE)), order.by = index(TICK.NYSE))
Error in xts(rep(0, NROW(TICK.NYSE)), order.by = index(TICK.NYSE)) :
order.by requires an appropriate time-based object
出现了14天前,当我上次使用相同的代码时,这是完美的工作(从那时起,唯一的区别是TICK.NYSE长度增长,因为从那时起数据被添加)。
以下更多详情:
> Sys.getenv("TZ")
[1] "EST"
> tail(xts(rep(0, NROW(TICK.NYSE)), order.by = index(TICK.NYSE)))
Error in xts(rep(0, NROW(TICK.NYSE)), order.by = index(TICK.NYSE)) :
order.by requires an appropriate time-based object
> class(index(TICK.NYSE["T09:30/T09:31"]))
[1] "POSIXct"
> tail(xts(rep(0, NROW(tail(TICK.NYSE))), order.by = index(tail(TICK.NYSE))))
Error in xts(rep(0, NROW(tail(TICK.NYSE))), order.by = index(tail(TICK.NYSE))) :
order.by requires an appropriate time-based object
> tail(TICK.NYSE)
TICK-NYSE.Open TICK-NYSE.High TICK-NYSE.Low TICK-NYSE.Close
2012-03-15 14:54:00 -278 -89 -299 -89
2012-03-15 14:55:00 -89 427 -89 201
2012-03-15 14:56:00 201 318 30 234
2012-03-15 14:57:00 234 242 -222 -64
2012-03-15 14:58:00 -64 346 -82 346
2012-03-15 14:59:00 346 525 36 525
TICK-NYSE.Volume TICK-NYSE.WAP TICK-NYSE.hasGaps
2012-03-15 14:54:00 0 0 0
2012-03-15 14:55:00 0 0 0
2012-03-15 14:56:00 0 0 0
2012-03-15 14:57:00 0 0 0
2012-03-15 14:58:00 0 0 0
2012-03-15 14:59:00 0 0 0
TICK-NYSE.Count
2012-03-15 14:54:00 31
2012-03-15 14:55:00 31
2012-03-15 14:56:00 31
2012-03-15 14:57:00 31
2012-03-15 14:58:00 29
2012-03-15 14:59:00 30
> str(TICK.NYSE)
An ‘xts’ object from 2011-01-18 09:30:00 to 2012-03-15 14:59:00 containing:
Data: num [1:114090, 1:8] -5 -144 24 -148 -184 -77 20 121 111 -60 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:8] "TICK-NYSE.Open" "TICK-NYSE.High" "TICK-NYSE.Low" "TICK-NYSE.Close" ...
Indexed by objects of class: [POSIXct,POSIXt] TZ:
xts Attributes:
List of 4
$ from : chr "20110119 23:59:59"
$ to : chr "20110124 23:59:59"
$ src : chr "IB"
$ updated: POSIXct[1:1], format: "2012-01-19 02:34:52"
> str(index(TICK.NYSE))
Class 'POSIXct' atomic [1:114090] 1.3e+09 1.3e+09 1.3e+09 1.3e+09 1.3e+09 ...
..- attr(*, "tzone")= chr ""
..- attr(*, "tclass")= chr [1:2] "POSIXct" "POSIXt"
> Sys.getenv("TZ")
[1] "EST"
> tail(index(TICK.NYSE))
[1] "2012-03-15 14:54:00 EST" "2012-03-15 14:55:00 EST"
[3] "2012-03-15 14:56:00 EST" "2012-03-15 14:57:00 EST"
[5] "2012-03-15 14:58:00 EST" "2012-03-15 14:59:00 EST"
> head(index(TICK.NYSE))
[1] "2011-01-18 09:30:00 EST" "2011-01-18 09:31:00 EST"
[3] "2011-01-18 09:32:00 EST" "2011-01-18 09:33:00 EST"
[5] "2011-01-18 09:34:00 EST" "2011-01-18 09:35:00 EST"
> Sys.info()
sysname
"Linux"
release
"3.0.0-16-generic"
version
"#28-Ubuntu SMP Fri Jan 27 17:44:39 UTC 2012"
> sessionInfo()
R version 2.14.1 (2011-12-22)
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] lattice_0.20-0 multicore_0.1-7
[3] doSNOW_1.0.5 snow_0.3-8
[5] doRedis_1.0.4 rredis_1.6.3
[7] foreach_1.3.2 codetools_0.2-8
[9] iterators_1.0.5 PerformanceAnalytics_1.0.3.3
[11] quantstrat_0.6.1 blotter_0.8.4
[13] twsInstrument_1.3-3 FinancialInstrument_0.10.6
[15] IBrokers_0.9-6 quantmod_0.3-18
[17] TTR_0.21-0 xts_0.8-4
[19] Defaults_1.1-1 strucchange_1.4-6
[21] sandwich_2.2-8 zoo_1.7-7
[23] rj_1.0.2-5
loaded via a namespace (and not attached):
[1] grid_2.14.1 tools_2.14.1
> dput(tail(TICK.NYSE))
structure(c(-385, -213, -42, -334, -233, -111, -121, 20, -14,
-125, -73, 265, -583, -269, -426, -520, -443, -440, -213, -42,
-334, -233, -111, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 31, 31, 30, 30, 31, 31), class = c("xts", "zoo"
), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "",
from = "20110119 23:59:59", to = "20110124 23:59:59", src = "IB",
updated = structure(1326958492.96405, class = c("POSIXct",
"POSIXt")), index = structure(c(1331927640, 1331927700, 1331927760,
1331927820, 1331927880, 1331927940), tzone = "", tclass = c("POSIXct",
"POSIXt")), .Dim = c(6L, 8L), .Dimnames = list(NULL, c("TICK-NYSE.Open",
"TICK-NYSE.High", "TICK-NYSE.Low", "TICK-NYSE.Close", "TICK-NYSE.Volume",
"TICK-NYSE.WAP", "TICK-NYSE.hasGaps", "TICK-NYSE.Count")))
这怎么可能?如果我将函数的源代码从xtime :: timeBased复制为timeBased_test并使用它返回TRUE ... Strange。
使用函数xts :: timeBased返回FALSE> timeBased(index(TICK.NYSE))
[1] FALSE
>
> sapply(c("Date", "POSIXct", "chron", "dates", "times",
+ "timeDate", "yearmon", "yearqtr", "xtime"), function(xx) inherits(index(TICK.NYSE),
+ xx))
Date POSIXct chron dates times timeDate yearmon yearqtr
FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
xtime
FALSE
> any(sapply(c("Date", "POSIXct", "chron", "dates", "times",
+ "timeDate", "yearmon", "yearqtr", "xtime"), function(xx) inherits(index(TICK.NYSE),
+ xx)))
[1] TRUE
> timeBased_test <- function (x)
+ {
+ if (!any(sapply(c("Date", "POSIXct", "chron", "dates", "times",
+ "timeDate", "yearmon", "yearqtr", "xtime"), function(xx) inherits(x,
+ xx)))) {
+ FALSE
+ }
+ else TRUE
+ }
>
> timeBased_test(index(TICK.NYSE))
[1] TRUE
好的,所以,我想我找到了原因。在xts in file period.R我可以找到这段代码:
`is.timeBased` <- `timeBased` <-
function(x) {
if (!any(sapply(c("Date", "POSIXt", "chron", "dates", "times",
"timeDate", "yearmon", "yearqtr", "xtime"), function(xx) inherits(x,
xx)))) {
FALSE
} else TRUE
}
这就是,我想,实际上是在xts里面调用的。我认为这是xtime:timeBased被调用,因为那个有“POSIXct”就可以了:
timeBased <- function (x)
{
if (!any(sapply(c("Date", "POSIXct", "chron", "dates", "times",
"timeDate", "yearmon", "yearqtr", "xtime"), function(xx) inherits(x,
xx)))) {
FALSE
}
else TRUE
}
我在这里不明白?由于检查POSIXct的继承是正常的(返回TRUE)而POSIXt不是(返回FALSE)。
答案 0 :(得分:3)
好的,萨莫,这可以解决这个问题
indexClass(TICK.NYSE) <- c("POSIXt", "POSIXct")
因为timeBased
检查它是否继承“POSIXt”,但不检查“POSIXct”
至少还有一个地方在xts中有这个问题。 xts:::.drop.time
仅在indexClass(x)[1] == "POSIXt"