填充 xts 股票价格对象中缺失的数据
我有 1 分钟的日内价格数据,其中缺少数据点。因此我想填补它们。
我阅读了以下帖子中的建议并尝试了类似的过程: R:填充时间序列中缺失的日期?
在我的如果缺失的数据点是第一笔交易,即 09:31:00。
> head(s)
AMR.Open AMR.High AMR.Low AMR.Close AMR.Volume AMR.WAP AMR.hasGaps AMR.Count
2010-09-10 09:32:00 6.08 6.10 6.07 6.10 298 6.087 0 39
2010-09-10 09:33:00 6.10 6.14 6.10 6.14 274 6.122 0 70
2010-09-10 09:34:00 6.14 6.15 6.13 6.13 472 6.133 0 96
2010-09-10 09:35:00 6.13 6.14 6.13 6.13 291 6.133 0 68
2010-09-10 09:36:00 6.13 6.13 6.11 6.11 548 6.123 0 97
2010-09-10 09:37:00 6.11 6.11 6.11 6.11 67 6.110 0 26
> na.locf(s, xout=seq(as.POSIXct(head(index(s), 1) - 60), as.POSIXct(tail(index(s), 1)), by="1 min")) -> ss
> head(ss)
AMR.Open AMR.High AMR.Low AMR.Close AMR.Volume AMR.WAP AMR.hasGaps AMR.Count
2010-09-10 09:32:00 6.08 6.10 6.07 6.10 298 6.087 0 39
2010-09-10 09:33:00 6.10 6.14 6.10 6.14 274 6.122 0 70
2010-09-10 09:34:00 6.14 6.15 6.13 6.13 472 6.133 0 96
2010-09-10 09:35:00 6.13 6.14 6.13 6.13 291 6.133 0 68
2010-09-10 09:36:00 6.13 6.13 6.11 6.11 548 6.123 0 97
2010-09-10 09:37:00 6.11 6.11 6.11 6.11 67 6.110 0 26
正如您在上面看到的,返回的对象未按预期填充。
您可以在下面看到我正确指定了开始和结束时间。
> as.POSIXct(head(index(s), 1) - 60)
[1] "2010-09-10 09:31:00 EDT"
> as.POSIXct(tail(index(s), 1))
[1] "2010-09-10 16:00:00 EDT"
>
这可能是因为日期范围指定了时区,而原始 POSIX 索引没有指定时区吗?我尝试通过指定 tz="" 来删除 tz,但这并没有删除它。话虽这么说,时区可能只是一个转移注意力的话题。
如果有人有兴趣测试,我将数据保存为 rda(二进制)格式:
http:// /www.speedyshare.com/files/28576853/test.rda
感谢帮助。
I have 1 minute intraday price data which has missing data points. As such I want to fill them.
I read through the suggestions in the following post and tried a similar procedure:
R: Filling missing dates in a time series?
In my case the missing data point is the first trade i.e. at 09:31:00.
> head(s)
AMR.Open AMR.High AMR.Low AMR.Close AMR.Volume AMR.WAP AMR.hasGaps AMR.Count
2010-09-10 09:32:00 6.08 6.10 6.07 6.10 298 6.087 0 39
2010-09-10 09:33:00 6.10 6.14 6.10 6.14 274 6.122 0 70
2010-09-10 09:34:00 6.14 6.15 6.13 6.13 472 6.133 0 96
2010-09-10 09:35:00 6.13 6.14 6.13 6.13 291 6.133 0 68
2010-09-10 09:36:00 6.13 6.13 6.11 6.11 548 6.123 0 97
2010-09-10 09:37:00 6.11 6.11 6.11 6.11 67 6.110 0 26
> na.locf(s, xout=seq(as.POSIXct(head(index(s), 1) - 60), as.POSIXct(tail(index(s), 1)), by="1 min")) -> ss
> head(ss)
AMR.Open AMR.High AMR.Low AMR.Close AMR.Volume AMR.WAP AMR.hasGaps AMR.Count
2010-09-10 09:32:00 6.08 6.10 6.07 6.10 298 6.087 0 39
2010-09-10 09:33:00 6.10 6.14 6.10 6.14 274 6.122 0 70
2010-09-10 09:34:00 6.14 6.15 6.13 6.13 472 6.133 0 96
2010-09-10 09:35:00 6.13 6.14 6.13 6.13 291 6.133 0 68
2010-09-10 09:36:00 6.13 6.13 6.11 6.11 548 6.123 0 97
2010-09-10 09:37:00 6.11 6.11 6.11 6.11 67 6.110 0 26
As you can see above the object returned is not filled as desired.
Below you can see that I correctly specified the start and end times.
> as.POSIXct(head(index(s), 1) - 60)
[1] "2010-09-10 09:31:00 EDT"
> as.POSIXct(tail(index(s), 1))
[1] "2010-09-10 16:00:00 EDT"
>
Could this be because the date range has a time-zone specified whereas the original POSIX index does not? I tried to remove the tz by specifiying tz="" but that does not remove it. That being said, the time-zone may be just a red herring.
I saved the data in rda (binary) format if anyone is interested in testing:
http://www.speedyshare.com/files/28576853/test.rda
Appreciate the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
na.locf
对数据进行操作,而不是对索引进行操作。如果您想向数据添加一行NA
,您需要创建一个合适的 xts 对象来rbind
到s
:na.locf
operates on the data, not the index. If you want to add a row ofNA
to the data, you would need to make a suitable xts object torbind
tos
: