R 将每日数据与刻度数据合并 [英] R merge daily data with tick data
问题描述
感谢指向 na.locf (Darren) 的指针,更新示例和结果如下:
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?
示例:
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 天波动率)并将其应用于逐笔报价以进行分析.谢谢你的建议.
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.
推荐答案
R Cookbook 中的 14.5 和 14.6 项演示了使用 merge
(与 all=T
或 all=F
取决于目的),na.locf
和 zoo
与 seq
到生成一整套日期(以涵盖一个或另一个符号没有数据的日期).我使用相同的方法在没有刻度的几分钟内创建空白的 100 万条柱线,因此我认为它也适用于合并每日和刻度数据.
Items 14.5 and 14.6 in R Cookbook demonstrate merging monthly inflation data with daily IBM data, using merge
(with all=T
or all=F
depending on purpose), na.locf
and zoo
with seq
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.
这篇关于R 将每日数据与刻度数据合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!