[R-SIG-Finance] Speed optimization on minutes distribution calculation
Wind
windspeedo99 at gmail.com
Mon Jun 15 18:04:53 CEST 2009
I want to plot the distribution of volume of the future CLN9 along
the 24 hours axis. The following codes could complete the task. But
it is very time consuming when sapply(mins,function(x)
{mean(hqm[which(format(index(hqm),"%H:%M")==x),5])}).
Any suggestion for codes with better performance would be highly appreciated.
The data hqm has been retrieved from IB via IBrokers.
> head(hqm[,5])
CLN9.Volume
2009-05-25 06:00:00 17
2009-05-25 06:01:00 2
2009-05-25 06:02:00 11
2009-05-25 06:03:00 26
2009-05-25 06:04:00 20
2009-05-25 06:05:00 5
> tail(hqm[,5])
CLN9.Volume
2009-06-15 21:51:00 1050
2009-06-15 21:52:00 807
2009-06-15 21:53:00 782
2009-06-15 21:54:00 385
2009-06-15 21:55:00 562
2009-06-15 21:56:00 423
>mins<-unlist(lapply(0:23,function(h){sapply(0:59,function(m){paste(sprintf("%02d",h),sprintf("%02d",m),sep=":")})}))
> head(mins)
[1] "00:00" "00:01" "00:02" "00:03" "00:04" "00:05"
> tail(mins)
[1] "23:54" "23:55" "23:56" "23:57" "23:58" "23:59"
>temp<-sapply(mins,function(x) {mean(hqm[which(format(index(hqm),"%H:%M")==x),5])})
> head(temp)
00:00 00:01 00:02 00:03 00:04 00:05
279.1333 284.9333 247.8667 176.3333 278.8667 179.0667
> tail(temp)
23:54 23:55 23:56 23:57 23:58 23:59
250.2667 312.7333 318.9333 210.8000 258.2000 232.8667
>plot(temp)
More information about the R-SIG-Finance
mailing list