[R] Loop too slow for Bid calc - BUT cannot figure out how to do with matrix

rivercode aquanyc at gmail.com
Mon Oct 4 23:29:55 CEST 2010


Hi,

I am trying to create Bid/Ask for each second from a high volume stock and
the only way I have been able to solve this is using loops to create the
target matrix from the source tick data matrix.  Looping  is too slow and
not practical to use on multiple stocks. For example:

Bids Matrix (a real one is 400,000++ length):

Bid       Time
10.03    11:05:03.124
10.04    11:05:03.348
10.05    11:05:04.010

One Second Bid Matrix (Bid price for every second of the day):

Bid       Second
10.02   11:05:03
??        11:05:04    <----Last bid price before 11:05:04.xxx, which is
11.04 at 11:05:03.348 

The challenge is how to create the one second bid matrix, without looping
through the Bids Matrix to find the first timestamp that is greater than the
OneSecond  timestamp then getting the previous row price from
BidsMatrix...which would have been the bid at the beginning of that second.  

I am new to R, so need some help to do this “properly”. 

#  OneSecond.  Matrix above called “One Second Bid Matrix”
#  BidsMatrix.  Matrix above called “Bids Matrix”

bidrow = 1

# looping through each second
for (sec in 1:length(OneSecond$Second) )  
    {
	t = as.POSIXlt(onesec$Second[sec],origin = "1970-01-01")
	sec.onesec = as.numeric(format(t, "%H%M%S")) # convert date/time to format
HHMMSS as a number
			
	  # Find bid for second, which is the last bid before a change in the
second
	  for (r in bidrow:length(BidsMatrix$Price))
		{
                 # convert the BidsMatrix timestamp to number of format
%H%M%S 
  		 bidTS = unlist(strsplit(as.character(BidsMatrix$Time[r]),
split="\\."))[1] # remove milliseconds
		 bidTS = gsub(":", "", bidTS) # remove ":" from time
		 bidTS = as.numeric(bidTS) # convert to number
	  
				
	    if (bidTS > sec.onesec)  
		  {
		    onesec$Bid[sec] = bids$Price[r -1] # Price of previous bid
    		    bidrow = r  # save bidrow as starting point to find next bid.
		    break
		  } #if
		
		}# for
	
}# for

Hope this is clear and  thanks for your help.

Chris

-- 
View this message in context: http://r.789695.n4.nabble.com/Loop-too-slow-for-Bid-calc-BUT-cannot-figure-out-how-to-do-with-matrix-tp2955116p2955116.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list