My problem is having to do with counting the frequency of an irregular timeseries containing tick-data. The problem starts where Joshua's excellent tips end here: http://quantivity.wordpress.com/2009/12/27/quote-arrival-frequency-distribution-for-tick-data/#comment-175
# create random bid/ask data
require(xts)
N <- 1e7
data <- 1.2945+rnorm(N)/1000
data <- cbind(data,data+runif(N)/1000)
colnames(data) <- c("bid","ask")
# create and order random times
times <- Sys.time()-N:1+rnorm(N)*100
times <- times[order(times)]
# create xts object from data and times
EURUSD <- xts(data, times)
# create quote frequency chart
plot(diff(endpoints(EURUSD,"minutes")),type='l')
My problem continues from here:
endPoints <- diff(endpoints(EURUSD,"minutes"))
Now when we have this frequency of the tick-data in endPoints how can this be added back to the original EURUSD xts? The problem is that the endPoints does not contain any timestamp or similar info to be able to add it back to a column in the EURUSD object. Also my attempts at trying to use to.minutes on the EURUSD has not worked because it seems to not always index in the same way.
As always would be very thankful for any tips!
Looks like I found a way to accomplish it. Not the most beatiful but it seems to work:
data <- EURUSD
#using the cut method to get the frequency
freqs <- data.frame(table(cut(index(data), breaks="min")))
#getting it back into an xts and merging with the original
freqs[,1] <- as.POSIXct(as.character(freqs[,1]), format = "%Y-%m-%d %H:%M:%s")
freqxts <- xts(freqs[,-1], order.by=freqs[,1])
datawithtickspeed <- merge(data, freqxts)