I can not resolve why error in simple creation of xts object
我无法解析为什么在简单创建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
appeared while this was working perfectly 14 days ago when I last used the same code (since then the only difference is that TICK.NYSE grow in length since data was added since then).
在14天前,当我最后一次使用相同的代码时,出现了这种情况(从那时起,唯一的区别就是这个标记)。自那以后,纽交所的数据一直在增长。
More details below:
更多的细节如下:
> 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")))
How is this possible? Using function xts::timeBased returns FALSE if I copy the source of the function from xtime::timeBased as timeBased_test and use it returns TRUE... Strange.
这怎么可能?使用函数xts::基于时间的返回FALSE,如果我从xtime中复制函数的源代码::基于timeBased_test的时间,然后使用它返回TRUE…奇怪。
> 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
OK, so, I think I found the reason for this. In xts in file period.R I can find this piece of code:
好的,我想我找到了原因。在xts文件期间。我能找到这段代码:
`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
}
And this is, I guess, actually being called inside of xts. I thought it is xtime:timeBased being called which would be fine since that one has "POSIXct":
这是,我猜,实际上是在xts里面。我认为是xtime:基于时间的被调用,这是很好的,因为那个有“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
}
What I do not understand here? Since checking inheritance of POSIXct works fine (returns TRUE) while POSIXt dos not (returns FALSE).
我不明白的是什么?因为检查POSIXct的继承是否可以正常工作(返回TRUE),而POSIXt dos not(返回FALSE)。
1 个解决方案
#1
3
Ok, Samo, that's fixable with this
好的,Samo,这是固定的。
indexClass(TICK.NYSE) <- c("POSIXt", "POSIXct")
because timeBased
checks to see if it inherits "POSIXt", but doesn't check for "POSIXct"
因为基于时间的检查会检查它是否继承了“POSIXt”,而不检查“POSIXct”
There's at least one other place that has this problem in xts. xts:::.drop.time
only works if indexClass(x)[1] == "POSIXt"
在xts中至少还有一个地方有这个问题。xt:::.drop。只有当indexClass(x)[1] == "POSIXt"时,时间才有效
#1
3
Ok, Samo, that's fixable with this
好的,Samo,这是固定的。
indexClass(TICK.NYSE) <- c("POSIXt", "POSIXct")
because timeBased
checks to see if it inherits "POSIXt", but doesn't check for "POSIXct"
因为基于时间的检查会检查它是否继承了“POSIXt”,而不检查“POSIXct”
There's at least one other place that has this problem in xts. xts:::.drop.time
only works if indexClass(x)[1] == "POSIXt"
在xts中至少还有一个地方有这个问题。xt:::.drop。只有当indexClass(x)[1] == "POSIXt"时,时间才有效