[R] Loop too slow for Bid calc - BUT cannot figure out how to do with matrix
Duncan Murdoch
murdoch.duncan at gmail.com
Mon Oct 4 23:47:43 CEST 2010
On 04/10/2010 5:29 PM, rivercode wrote:
> 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:
I would use loops, but something like this:
lastrow <- Bids[1,]
time <- [ some starting time ]
for (i in 2:400000) {
thisrow <- Bids[i,]
while (thisrow[,2] > time) {
output lastrow for time
time <- time + 1
}
lastrow <- thisrow
}
I don't see how that would be too slow, but if it was, just rewrite it in C.
Duncan Murdoch
>
> 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
>
More information about the R-help
mailing list