Skip to content

Aggregating tick-by-tick data to seconds

4 messages · Costas Vorlow, R. Michael Weylandt, G See +1 more

#
If you have a column labelled "Volume", to.period will sum it: e.g.,

library(xts)
data(sample_matrix)

s <- as.xts(sample_matrix)

s <- cbind(s, Volume = round(exp(runif(NROW(s),2,3))*10000))

head(s)
head(to.weekly(s))

Cheers,
Michael
On Sat, Sep 15, 2012 at 4:57 PM, Costas Vorlow <costas.vorlow at gmail.com> wrote:
#
You can use `period.apply` (along with `endpoints()`) to apply any
function you like over non-overlapping time periods.

In this case, you want to sum columns 2 and 3 separately, so use
`period.apply()` with `FUN=colSums` on just the 2nd and 3rd columns.

    period.apply(test[, 2:3], endpoints(test, 'secs'), FUN=colSums)

Then you can merge that with the results of `to.period`

    > cbind(to.period(test[, 1], name="test"), period.apply(test[,
-1], endpoints(test, 'secs'), FUN=colSums))
                                test.Open test.High test.Low
test.Close   up down
    2012-09-12 16:30:00    144.39    144.39   144.38     144.38 5903  100

You could also use `period.sum()` on each column separately.

HTH,
Garrett
On Sat, Sep 15, 2012 at 10:57 AM, Costas Vorlow <costas.vorlow at gmail.com> wrote:
#
?period.sum

HTH 
Jeff

Jeffrey Ryan    |    Founder    |    jeffrey.ryan at lemnica.com

www.lemnica.com
On Sep 15, 2012, at 10:57 AM, Costas Vorlow <costas.vorlow at gmail.com> wrote: