Pergunta

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.

Foi útil?

Solução

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.

Outras dicas

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...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top