我正在尝试测试日内交易到临时VWAP的恢复。我已经编译了一个(希望)可复制的示例,以显示我所做的事情。目前,我使用rollapply,但是这会基于观察将VWAP计算应用于滚动窗口。理想情况下,我想使用滚动时间窗口,例如10分钟。

我的示例(基于刻度数据):

library(quantmod)
library(xts)

#Create xts object from tick data frame

my.xts <- xts(my.frame[,-1], order.by=my.frame[,1])
my.xts <- my.xts["T10:00/T10:30"]


> my.xts
                    PRICE QUANTITY
2011-08-31 10:00:14 28724      538
2011-08-31 10:00:22 28724      462
2011-08-31 10:00:22 28724      500
2011-08-31 10:00:47 28701      500
2011-08-31 10:01:10 28725      500
2011-08-31 10:01:18 28728     1000
2011-08-31 10:01:30 28702      500
2011-08-31 10:01:30 28701      500
2011-08-31 10:01:45 28701     2590
2011-08-31 10:01:45 28701     1275
2011-08-31 10:01:46 28701      635
2011-08-31 10:01:53 28697      821
2011-08-31 10:02:02 28700     3379
2011-08-31 10:02:02 28700     1621
2011-08-31 10:03:26 28709     1000
2011-08-31 10:03:38 28709     1000
2011-08-31 10:03:38 28709      997
2011-08-31 10:04:37 28699      500
2011-08-31 10:04:37 28700     2500
2011-08-31 10:04:50 28676      500
2011-08-31 10:05:01 28676      519
2011-08-31 10:05:01 28676      500
2011-08-31 10:05:03 28700     2500
2011-08-31 10:05:03 28700     2000
2011-08-31 10:05:42 28681      197
2011-08-31 10:06:22 28699     2535
2011-08-31 10:06:22 28700     1000
2011-08-31 10:06:22 28700     2500
2011-08-31 10:06:22 28700     2060
2011-08-31 10:06:31 28700     2500
2011-08-31 10:06:32 28700     2500
2011-08-31 10:06:33 28700     2500
2011-08-31 10:06:42 28700     1000
2011-08-31 10:06:42 28700      500
2011-08-31 10:06:43 28700      500
2011-08-31 10:06:58 28700     1000
2011-08-31 10:07:02 28700     1000
2011-08-31 10:07:04 28700     1000
2011-08-31 10:07:04 28700     1000
2011-08-31 10:07:05 28700     1000
2011-08-31 10:07:05 28700       90
2011-08-31 10:07:05 28700      910
2011-08-31 10:07:06 28700       90
2011-08-31 10:07:29 28700        8
2011-08-31 10:07:40 28700        3
2011-08-31 10:07:44 28681      590
2011-08-31 10:07:46 28680     1000
2011-08-31 10:07:47 28676     1167
2011-08-31 10:07:56 28699      350
2011-08-31 10:08:01 28675     1000
2011-08-31 10:08:14 28694      240
2011-08-31 10:08:14 28694      518
2011-08-31 10:08:15 28675      861
2011-08-31 10:08:15 28663      657
2011-08-31 10:09:20 28689     1000
2011-08-31 10:09:59 28696      500
2011-08-31 10:10:10 28694      100
2011-08-31 10:10:51 28698     1000
2011-08-31 10:10:51 28698     1000
2011-08-31 10:11:02 28700      374
2011-08-31 10:11:15 28701      500
2011-08-31 10:11:52 28724      500
2011-08-31 10:11:52 28725      900
2011-08-31 10:11:52 28728     1000
2011-08-31 10:11:52 28740     2500
2011-08-31 10:11:52 28740      300
2011-08-31 10:11:52 28749     1800
2011-08-31 10:11:52 28750      250
2011-08-31 10:11:52 28750      200
2011-08-31 10:11:52 28750     6497
2011-08-31 10:11:52 28750     2103
2011-08-31 10:12:02 28750     8950
2011-08-31 10:12:07 28750     4500
2011-08-31 10:12:50 28750     4450
2011-08-31 10:12:50 28750      550
2011-08-31 10:12:56 28750      478
2011-08-31 10:12:56 28750      522
2011-08-31 10:12:56 28750      492
2011-08-31 10:13:02 28750       61
2011-08-31 10:13:16 28750     4439
2011-08-31 10:13:44 28750     1000
2011-08-31 10:13:47 28750     1000
2011-08-31 10:13:48 28750     1000
2011-08-31 10:13:49 28750     1000
2011-08-31 10:13:49 28750      439
2011-08-31 10:14:25 28750     2061
2011-08-31 10:14:25 28750     1000
2011-08-31 10:14:26 28751      374
2011-08-31 10:14:46 28750      500
2011-08-31 10:15:31 28750     1000
2011-08-31 10:15:49 28775        1
2011-08-31 10:15:52 28775      454
2011-08-31 10:16:47 28752     1000
2011-08-31 10:17:00 28750     1069
2011-08-31 10:17:30 28750      500
2011-08-31 10:17:38 28744      821
2011-08-31 10:17:42 28744      179
2011-08-31 10:17:42 28776     1000
2011-08-31 10:17:44 28779      590
2011-08-31 10:17:46 28780      700
2011-08-31 10:17:46 28780     3000
2011-08-31 10:18:06 28754      590
2011-08-31 10:18:06 28753      700
2011-08-31 10:18:06 28751      500
2011-08-31 10:18:06 28751       10
2011-08-31 10:18:06 28751      990
2011-08-31 10:18:39 28750     1000
2011-08-31 10:18:39 28750     2000
2011-08-31 10:19:11 28748     1000
2011-08-31 10:19:13 28749     1077
2011-08-31 10:19:22 28750      500
2011-08-31 10:19:45 28768      600
2011-08-31 10:19:45 28768     2244
2011-08-31 10:19:58 28750      500
2011-08-31 10:19:58 28750      500
2011-08-31 10:19:58 28750       97
2011-08-31 10:19:58 28750      403
2011-08-31 10:19:58 28750      197
2011-08-31 10:20:06 28750       10
2011-08-31 10:20:18 28750      293
2011-08-31 10:20:18 28750      500
2011-08-31 10:20:19 28750      500
2011-08-31 10:20:19 28750      500
2011-08-31 10:20:19 28750      207
2011-08-31 10:20:51 28750      293
2011-08-31 10:21:04 28730     3000
2011-08-31 10:21:09 28725     1000
2011-08-31 10:21:09 28725     1029
2011-08-31 10:21:10 28720      500
2011-08-31 10:21:35 28700      525
2011-08-31 10:21:35 28700      475
2011-08-31 10:21:38 28725       50
2011-08-31 10:21:38 28745      350
2011-08-31 10:22:40 28713      350
2011-08-31 10:22:58 28700      327
2011-08-31 10:22:58 28700      373
2011-08-31 10:23:09 28720     2029
2011-08-31 10:23:18 28720       50
2011-08-31 10:23:26 28720      150
2011-08-31 10:23:38 28700       50
2011-08-31 10:23:54 28700      762
2011-08-31 10:23:54 28700       38
2011-08-31 10:23:57 28700      112
2011-08-31 10:23:57 28700     2500
2011-08-31 10:24:00 28700     3388
2011-08-31 10:24:11 28700     1534
2011-08-31 10:24:11 28700     3466
2011-08-31 10:24:11 28700     1706
2011-08-31 10:24:14 28700      794
2011-08-31 10:24:26 28700     2288
2011-08-31 10:24:40 28719     2000
2011-08-31 10:24:48 28703       15
2011-08-31 10:24:50 28737     1000
2011-08-31 10:24:50 28737       30
2011-08-31 10:25:35 28731     1077
2011-08-31 10:25:38 28735      200
2011-08-31 10:25:38 28737     2232
2011-08-31 10:25:50 28737      100
2011-08-31 10:25:54 28737     1288
2011-08-31 10:25:57 28725      829
2011-08-31 10:26:07 28710     1000
2011-08-31 10:26:16 28700    10000
2011-08-31 10:26:36 28701      485
2011-08-31 10:26:36 28700     1947
2011-08-31 10:26:44 28701     1068
2011-08-31 10:26:44 28700     1432
2011-08-31 10:26:45 28700     4121
2011-08-31 10:26:45 28700     1566
2011-08-31 10:26:45 28700      794
2011-08-31 10:26:45 28700     2524
2011-08-31 10:26:45 28700      991
2011-08-31 10:26:45 28700     2244
2011-08-31 10:26:53 28680      250
2011-08-31 10:26:58 28680      750
2011-08-31 10:27:09 28676      200
2011-08-31 10:27:19 28700      500
2011-08-31 10:27:19 28700     1500
2011-08-31 10:27:32 28675      300
2011-08-31 10:27:42 28699     1000
2011-08-31 10:27:42 28700       30
2011-08-31 10:27:53 28675      700
2011-08-31 10:28:03 28657        6
2011-08-31 10:28:15 28698      500
2011-08-31 10:28:15 28699      530
2011-08-31 10:30:34 28699      470
2011-08-31 10:30:34 28700     1854
2011-08-31 10:30:34 28700     1000
2011-08-31 10:30:34 28700      500
2011-08-31 10:30:35 28700      191
2011-08-31 10:30:40 28699      150
2011-08-31 10:30:41 28700     1663
2011-08-31 10:30:48 28700     1030
2011-08-31 10:30:51 28700      824
2011-08-31 10:30:57 28700      843


#####################################
#        Create VWAP data       #
#####################################

func.vwap <- function(my.data)
{
     vwap <- sum(my.data[,1]*my.data[,2])/sum(my.data[,2])
}

my.DayVWAP <- func.vwap(my.xts)

my.test <- rollapply(my.xts,20,func.vwap, by.column=FALSE)

根据James的建议添加了by.column = FALSE来修复rollapply中的错误。

如果我没有提供足够的信息,请告诉我。

提前致谢
埃德

最佳答案

将参数by.column=FALSE添加到您的rollapply语句中:

my.test <- rollapply(my.xts,20,func.vwap,by.column=FALSE)

08-19 23:09