我正在尝试使用R,因为对于一些个人项目,我需要R和quantmod来为我创建OHCL图表。我被困在创建candleChart的步骤中,我不确定我明白为什么。使用“每日”输入文件可以很好地工作,但尝试从每小时数据创建每小时图表是一个很大的失败。
下面是我使用的数据示例:
> zz <- read.csv(dataFile, sep=",", header=TRUE, stringsAsFactors=T)
> head(zz)
DATE OPEN HIGH LOW CLOSE VOLUME
1 2012-10-23 02:00:00 22 22 22 22 171
2 2012-10-23 03:00:00 22 22 22 22 171
3 2012-10-23 04:00:00 22 22 22 22 171
4 2012-10-23 05:00:00 22 22 22 22 171
5 2012-10-23 06:00:00 22 22 22 22 171
6 2012-10-23 07:00:00 22 22 22 22 171正如你所看到的,第一个数据是一样的,但是在我的输入文件中有一年的数据,价格在最后上升到96。
我接下来要做的是从这个zz数据框创建xts对象,如下所示:
> xx <- xts(zz[,2:6], order.by=as.POSIXct(zz[, 1], tz="", format="%Y-%m-%d %H:%M:%S"))
> head(xx)
OPEN HIGH LOW CLOSE VOLUME
2012-10-23 02:00:00 22 22 22 22 171
2012-10-23 03:00:00 22 22 22 22 171
2012-10-23 04:00:00 22 22 22 22 171
2012-10-23 05:00:00 22 22 22 22 171
2012-10-23 06:00:00 22 22 22 22 171
2012-10-23 07:00:00 22 22 22 22 171
> tail(xx)
OPEN HIGH LOW CLOSE VOLUME
2013-10-22 06:00:00 96 96 96 96 115
2013-10-22 07:00:00 96 96 96 96 115
2013-10-22 08:00:00 96 96 96 96 115
2013-10-22 09:00:00 96 96 96 96 115
2013-10-22 10:00:00 96 96 96 96 115
2013-10-22 11:00:00 96 96 96 96 118现在,看起来(在我看来) xts是正确的。在未命名的列1中有date+time,其余的列被quantmod中的chartCandle()正确命名。
下面是我尝试绘制它时发生的情况:
> candleChart(xx, name=tickerName, subset="last 3 weeks", bar.type = "ohlc")
Error in periodicity(x) : can not calculate periodicity of 1 observation
> candleChart(xx)
Error in periodicity(x) : can not calculate periodicity of 1 observation我不确定为什么第一个参数会是我的XTS对象的任何子集?另外,我发现:
> periodicity(xx)
Error in periodicity(xx) : can not calculate periodicity of 1 observation
> periodicity(xx)
Error in periodicity(xx) : can not calculate periodicity of 1 observation第二个调用看起来是正确的,但是我不确定为什么周期性(Xx)会认为我的xts中只有一个观察值?
发布于 2013-10-24 18:28:54
periodicity调用:
p <- median(diff(.index(x)))
if (is.na(p))
stop("can not calculate periodicity of 1 observation")如果x有1个观察值,或者如果索引中缺少值,则p可以为NA (因为median调用中没有na.rm=TRUE。
> xx <- xts(1:10, as.POSIXct(c(1:5,NA,7:10),origin='1970-01-01'))
> periodicity(xx)
Error in periodicity(xx) : can not calculate periodicity of 1 observation
> candleChart(xx)
Error in periodicity(x) : can not calculate periodicity of 1 observation当您将zz[, 1]转换为POSIXct时,索引中的NA可能与夏令时有关。
发布于 2019-01-13 05:00:37
我遇到了类似的问题。我的工作是将我的数据帧的时区设置为"UTC“。它没有夏令时,这是问题的根源。所以试一试
xx <- xts(zz[,2:6], order.by=as.POSIXct(zz[, 1], tz="UTC", format="%Y-%m-%d %H:%M:%S"))
https://stackoverflow.com/questions/19561697
复制相似问题