[R-SIG-Finance] xts objects and speed

Brian G. Peterson brian at braverock.com
Tue Jun 19 11:46:03 CEST 2012


On 06/18/2012 09:40 PM, E D wrote:
> Hi everyone,
>
> I'm trying to use R to do some analysis of intraday data.
> Currently I have quotes for one symbol (every 1 min, from 930am to 4pm) and
> about a month and a half of data, and I'm computing things such as the
> trailing 1 hour and 3 hour maximums and minimums for that ticker.
> I'm making use of commands such as max(last(myXTS[1:t],'3 hours')) for
> every t, and I'm finding it rather slow as it takes about 60s to run for
> 12500 observations on a modern workstation.
>
> Am I making improper or unoptimized use of the xts functions? Could someone
> give me a few points to speed up that kind of calculation? (If the answer
> is "I know this great time series library in C"... that's ok.)
>
> Thanks in advance.

You didn't provide a reproducible example, so you run the risk

Your email suggests that you are doing something like:

for (t in 180:nrow(myXTS)) {
	out<-max(last(myXTS[1:t],'3 hours'))
}

which will indeed be painfully slow.

In the hopes of eliciting a little more detail, here's a reproducible 
example...

#######################

# on a very slow web browsing desktop at home
# (as opposed to a 'modern workstation'
# on which I do real work):


require(TTR)

#?runMax

# construct a one-minute index
idx<-seq.POSIXt(from=as.POSIXct('2012-05-01'),
		to=as.POSIXct('2012-06-18'),by='min')

# construct a fake time series
myXTS<-xts(cumsum(rnorm(length(idx))),order.by=idx)

myXTS<-myXTS['T09:30/T16:00'] #subset by time

outs<-vector(length=nrow(myXTS))
system.time(
   for (t in 180:nrow(myXTS)) {
      outs[t]<-max(last(myXTS[1:t],'3 hours'))
   }
)

#    user  system elapsed
# 127.092   0.020 127.137

system.time(
	outf<-runMax(myXTS,180)
)

#   user  system elapsed
#  0.032   0.000   0.032

######################

Cheers,

    - Brian

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list