R 将每日数据与刻度数据合并
感谢您指向 na.locf(Darren),更新了示例和结果如下:
我有刻度数据,我已将其汇总到每日数据中,以便计算每日波动性。现在我已经创建了每日波动率,我想再次将每日数据与报价数据合并。但是,我怀疑由于每日数据和分时数据的索引差异,合并仍然是“空”。
如何将每日数据与报价数据合并?
示例:
AGL.xts <- xts(AGL_Frame[,-1], order.by=AGL_Frame[,1])
AGL.xts
Close
2012-01-19 16:46:11 32376
2012-01-19 16:46:32 32377
2012-01-19 16:46:32 32376
2012-01-19 16:46:42 32376
2012-01-19 16:46:42 32376
2012-01-19 16:46:42 32376
2012-01-19 16:46:45 32376
2012-01-19 16:46:48 32351
2012-01-19 16:46:54 32351
2012-01-19 16:46:57 32351
2012-01-19 16:46:57 32351
2012-01-19 16:47:14 32351
2012-01-19 16:47:14 32351
2012-01-19 16:47:19 32350
2012-01-19 16:47:32 32349
2012-01-19 16:47:32 32349
my.sample1 <- to.daily(AGL.xts[,1],1,'daily')
my.sample1
daily.Open daily.High daily.Low daily.Close
2011-12-01 17:00:27 31000 31479 30685 31350
2011-12-05 17:00:28 31225 31700 31015 31645
2011-12-06 17:00:22 31290 31626 31126 31500
2011-12-07 17:00:12 31550 31840 31215 31366
2011-12-08 17:00:09 31350 31875 31200 31200
2011-12-12 17:00:25 31093 31245 30310 30310
2011-12-13 17:00:24 30333 30767 30100 30430
2011-12-14 17:00:12 30210 30500 29575 29700
2011-12-19 17:00:03 29900 30005 29633 29679
my.AGL.roc <- ROC(my.sample1[,4])
my.AGL.sd <- apply.rolling(my.AGL.roc, FUN="sd", width=5)*sqrt(252)
my.AGL.sd
calcs
2011-12-05 17:00:28 NA
2011-12-06 17:00:22 NA
2011-12-07 17:00:12 NA
2011-12-08 17:00:09 NA
2011-12-12 17:00:25 0.2195421
2011-12-13 17:00:24 0.1966806
2011-12-14 17:00:12 0.2240305
2011-12-19 17:00:03 0.2327860
2011-12-20 17:00:28 0.2878848
2011-12-21 17:00:18 0.2275700
2011-12-22 17:00:12 0.2462184
2011-12-28 17:00:00 0.1633643
2011-12-29 17:00:20 0.1800739
2012-01-03 17:00:25 0.4068977
2012-01-04 17:00:13 0.3699694
2012-01-05 17:00:04 0.4014607
2012-01-09 17:00:05 0.4049482
2012-01-10 17:00:17 0.3934479
2012-01-11 17:00:07 0.2391906
2012-01-12 17:00:01 0.2328756
2012-01-16 17:00:02 0.2165803
2012-01-17 17:00:22 0.1910748
2012-01-18 17:00:19 0.1347729
2012-01-19 17:00:09 0.1198476
merged <- merge(AGL.xts,my.AGL.sd)
merged <- na.locf(merged)
merged
Close Calcs
2012-01-12 12:03:49 31920 0.2391906
2012-01-12 12:03:52 31920 0.2391906
2012-01-12 12:03:54 31920 0.2391906
2012-01-12 12:03:56 31941 0.2391906
2012-01-12 12:04:19 31910 0.2391906
2012-01-12 12:04:21 31910 0.2391906
2012-01-12 12:04:22 31909 0.2391906
2012-01-12 12:04:22 31903 0.2391906
2012-01-12 12:04:22 31910 0.2391906
2012-01-12 12:04:23 31910 0.2391906
2012-01-12 12:04:28 31910 0.2391906
2012-01-12 12:04:28 31910 0.2391906
2012-01-12 12:04:32 31910 0.2391906
2012-01-12 12:04:32 31910 0.2391906
2012-01-12 12:04:33 31909 0.2391906
2012-01-12 12:04:33 31910 0.2391906
2012-01-12 12:04:33 31910 0.2391906
2012-01-12 12:04:33 31910 0.2391906
2012-01-12 12:04:33 31910 0.2391906
2012-01-12 12:04:38 31901 0.2391906
这实现了我使用每日指标(在本例中为 5 天波动率)并将其应用于逐笔报价以进行分析的目标。谢谢你的建议。
Thanks for the pointer to na.locf (Darren), updated example and results below:
I have tick data, which I have rolled into daily data, in order to calc daily volatility. Now that I have created the daily volatility, I would like to merge the daily data with the tick data again. However, I suspect the merge remains "empty" due to the index differences of the daily and tick data.
How would one merge the daily data with tick data?
Example:
AGL.xts <- xts(AGL_Frame[,-1], order.by=AGL_Frame[,1])
AGL.xts
Close
2012-01-19 16:46:11 32376
2012-01-19 16:46:32 32377
2012-01-19 16:46:32 32376
2012-01-19 16:46:42 32376
2012-01-19 16:46:42 32376
2012-01-19 16:46:42 32376
2012-01-19 16:46:45 32376
2012-01-19 16:46:48 32351
2012-01-19 16:46:54 32351
2012-01-19 16:46:57 32351
2012-01-19 16:46:57 32351
2012-01-19 16:47:14 32351
2012-01-19 16:47:14 32351
2012-01-19 16:47:19 32350
2012-01-19 16:47:32 32349
2012-01-19 16:47:32 32349
my.sample1 <- to.daily(AGL.xts[,1],1,'daily')
my.sample1
daily.Open daily.High daily.Low daily.Close
2011-12-01 17:00:27 31000 31479 30685 31350
2011-12-05 17:00:28 31225 31700 31015 31645
2011-12-06 17:00:22 31290 31626 31126 31500
2011-12-07 17:00:12 31550 31840 31215 31366
2011-12-08 17:00:09 31350 31875 31200 31200
2011-12-12 17:00:25 31093 31245 30310 30310
2011-12-13 17:00:24 30333 30767 30100 30430
2011-12-14 17:00:12 30210 30500 29575 29700
2011-12-19 17:00:03 29900 30005 29633 29679
my.AGL.roc <- ROC(my.sample1[,4])
my.AGL.sd <- apply.rolling(my.AGL.roc, FUN="sd", width=5)*sqrt(252)
my.AGL.sd
calcs
2011-12-05 17:00:28 NA
2011-12-06 17:00:22 NA
2011-12-07 17:00:12 NA
2011-12-08 17:00:09 NA
2011-12-12 17:00:25 0.2195421
2011-12-13 17:00:24 0.1966806
2011-12-14 17:00:12 0.2240305
2011-12-19 17:00:03 0.2327860
2011-12-20 17:00:28 0.2878848
2011-12-21 17:00:18 0.2275700
2011-12-22 17:00:12 0.2462184
2011-12-28 17:00:00 0.1633643
2011-12-29 17:00:20 0.1800739
2012-01-03 17:00:25 0.4068977
2012-01-04 17:00:13 0.3699694
2012-01-05 17:00:04 0.4014607
2012-01-09 17:00:05 0.4049482
2012-01-10 17:00:17 0.3934479
2012-01-11 17:00:07 0.2391906
2012-01-12 17:00:01 0.2328756
2012-01-16 17:00:02 0.2165803
2012-01-17 17:00:22 0.1910748
2012-01-18 17:00:19 0.1347729
2012-01-19 17:00:09 0.1198476
merged <- merge(AGL.xts,my.AGL.sd)
merged <- na.locf(merged)
merged
Close Calcs
2012-01-12 12:03:49 31920 0.2391906
2012-01-12 12:03:52 31920 0.2391906
2012-01-12 12:03:54 31920 0.2391906
2012-01-12 12:03:56 31941 0.2391906
2012-01-12 12:04:19 31910 0.2391906
2012-01-12 12:04:21 31910 0.2391906
2012-01-12 12:04:22 31909 0.2391906
2012-01-12 12:04:22 31903 0.2391906
2012-01-12 12:04:22 31910 0.2391906
2012-01-12 12:04:23 31910 0.2391906
2012-01-12 12:04:28 31910 0.2391906
2012-01-12 12:04:28 31910 0.2391906
2012-01-12 12:04:32 31910 0.2391906
2012-01-12 12:04:32 31910 0.2391906
2012-01-12 12:04:33 31909 0.2391906
2012-01-12 12:04:33 31910 0.2391906
2012-01-12 12:04:33 31910 0.2391906
2012-01-12 12:04:33 31910 0.2391906
2012-01-12 12:04:33 31910 0.2391906
2012-01-12 12:04:38 31901 0.2391906
This achieves my goal of using a daily indicator (5-day vol in this case) and applying it to ticks for analysis purposes. Thanks for the advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
R Cookbook 中的第 14.5 和 14.6 项演示了使用
merge
将每月通胀数据与每日 IBM 数据合并(使用all=T
或all=F
,具体取决于有意),na.locf
和zoo
与seq
生成一整套日期(以覆盖一个或另一个符号出现时的日期)没有数据)。我使用相同的方法创建了几分钟的空白 1m 条,其中没有蜱虫,所以我认为它也适用于合并每日和蜱虫数据。
Items 14.5 and 14.6 in R Cookbook demonstrate merging monthly inflation data with daily IBM data, using
merge
(withall=T
orall=F
depending on purpose),na.locf
andzoo
withseq
to generate a full set of dates (to cover dates when one or the other symbol has no data).I've used the same approach to create blank 1m bars for minutes where there were no ticks, so I think it will work for merging daily and tick data too.
不确定函数
apply.rolling
来自哪里,但它看起来像是一个滞后为 5 的滚动标准差?好吧,你已经看到了它的样子。由于
apply.rolling
的实现细节,calcs 中的前五行没有值。但我同意约书亚的观点......不确定你到底想在这里做什么......
not sure where the function
apply.rolling
comes from, but it looks like its a rolling standard deviation with a lag of 5?Well, you've got that it looks like. There are no values for the first five rows in calcs due to the implementation details of
apply.rolling
.But i'd agree with Joshua... not sure exactly what you're trying to do here...