[R-SIG-Finance] Speed optimization on minutes distribution calculation

Brian G. Peterson brian at braverock.com
Tue Jun 16 02:00:15 CEST 2009


It seems that the slow part is all the character string manipulation.  
This would be slow in almost any programming language.   Honestly, I am 
always annoyed by useless axes in charts that simply count from 1 to n.  
A time axis at least has some real meaning, and avoids the useless 
rewriting of character strings.

You should be able to get a meaningful, readable axis using the 
periodicity() function in xts without the string manipulation.

Regards,

    - Brian

Wind wrote:
> 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)
>>     
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>   


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



More information about the R-SIG-Finance mailing list