I have an xts sequence of stock trade events that I want to process to generate 1 minute OHLC time series. For instance this set of trades:
Timestamp Price Size
9:30:00.123 12.32 200
9:30.00.532 12.21 100
9:30.32.352 12.22 500
9:30.45.342 12.35 200
Should result in the 9:30:00 record:
Timestamp Open High Low Close
9:30:00 12.32 12.35 12.21 12.35
The way I approached this is to split the original trade series by the minute:
myminseries = do.call(rbind, lapply(split(mytrades, "minutes"), myminprocessing))
This produce the records I want but there is a problem: if the stock doesn't have any trade in a given minute, I will miss that minute record entirely. What I want instead is to have an all 0s record for the missing trades minute. For example, if there isn't any trade at 9:31:00 I should have:
Timestamp Open High Low Close
9:30:00 12.32 12.35 12.21 12.35
9:31:00 0 0 0 0
9:32:00 12.40 12.42 12.38 12.42
How can I backfill the 1 minute series? Or should I use a completely different approach than split()?