Aggregating price data to different time horizon in R data.table

六眼飞鱼酱① 提交于 2019-12-03 17:37:50

You can use the endpoints function (which is written in C) from xts on POSIXt vectors. endpoints finds the position of the last element of a certain time period. By convention, 1:05 would not be included in the same bar as 1:00. So, the data that you provided dput for (which is different than the printed data above it) will have 2 bars.

Assuming dt is your data.table:

library(data.table)
library(xts)

setkey(dt, t)  # make sure the data.table is sorted by time.
ep <- endpoints(dt$t, "minutes", 5)[-1] # remove the first value, which is 0
dt[ep, grp:=seq_along(ep)]              # create a column to group by
dt[, grp:=na.locf(grp, fromLast=TRUE)]  # fill in NAs

dt[, list(t=last(t), open=open[1], high=max(high), low=min(low), 
          close=last(close), volume=sum(volume)), by=grp]

   grp                   t    open   high     low   close volume
1:   1 2009-05-01 01:04:00 0.89467 0.8951 0.89457 0.89500    403
2:   2 2009-05-01 01:05:00 0.89500 0.8950 0.89486 0.89488     36
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!