Borrowing the data from agstudy's answer (and therefore making 1 min bars, not 5-mins), I would use this:
dat.xts <- as.xts(dat.xts) ## otherwise you get an error align.time
## since (dat.xts is a zoo object)
bars <- period.apply(dat.xts,
endpoints(dat.xts,"secs",60),
function(xx){
ticks=coredata(xx$price)
c( first(ticks),max(ticks), min(ticks),
last(ticks), sum(xx$volume), sum(xx$price_volume) )
})
colnames(bars) <- c("Open","High","Low","Close","Volume","Price*Volume")
align.time(bars,60)
I.e. period.apply()
is given an xts object holding the ticks for a given 1 minute period. I use first
, max
, min
and last
to make the OHLC data. (I have to use coredata
otherwise those functions complain.)
Then volume and price_volume are summed for each period.
After the main loop I assign column headings, and round up the timestamps. The output is:
Open High Low Close Volume Price*Volume
2013-11-11 14:56:00 31.19 31.19 31.19 31.19 11 34309
2013-11-11 14:57:00 31.16 31.16 31.15 31.15 443 1381515