[R-SIG-Finance] Extracting Interval-Based Data From Zoo Series
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Dec 10 16:49:33 CET 2007
On Dec 10, 2007 5:57 AM, Rory Winston <rory.winston at gmail.com> wrote:
> Hi all
>
> I have a few series with high-frequency tick data. The series is recorded at
> millisecond resolution, and a data file can span anything from a few hours
> to a calendar month.
>
> Here is an example of one file:
>
> 2007-10-23 00:00:36.139|1.419|1.4192
> 2007-10-23 00:00:42.629|1.4191|0
> 2007-10-23 00:01:03.109|1.419|0
> 2007-10-23 00:01:08.109|0|1.4191
>
> I load the file in as follows:
>
> eurusd <- read.zoo(file="data/eurusd_EBS_11_07.dat",
> sep="|", FUN=as.POSIXct,
> col.names=c("time","bid","ask"))
>
> # Replace zero ticks with NA, and thus locf
> coredata(eurusd)[,"bid"][coredata(eurusd)[,"bid"]==0] = NA
> coredata(eurusd)[,"ask"][coredata(eurusd)[,"ask"]==0] = NA
> eurusd <- na.locf(eurusd)
>
> I have a couple of questions:
>
> 1. I can see that as.POSIXct() can deal with millisecond resolution, but zoo
> doesnt seem to use the millis portion of the timestamp as an index variable.
> I tried to use a function as follows:
>
> convertDate <- function(x) {
> # 2007-11-23 14:48:43.140
> as.POSIXct(strptime(x, "%Y-%m-%d %H:%M:%OS"))
> }
>
> and then used this as the FUN argument in read.zoo(), but it still complains
> that index values must be unique. Is it possible to use a high-resolution
> index (milliseconds or an even finer recorded granularity)?
>
> 2. What I would like to do with the data, is extract and aggregate
> sub-series. So for the tick data above, if I wanted to check for diurnal
> seasonality, I would like to be able to extract as a daily series, the
> interarrival times of the ticks. I can do this in a primitive way by:
>
> # Extract data for 12/11/07
> ts <- eurusd[as.Date(time(eurusd))=="2007-11-12"]
>
> plot(time(ts)[2:length(time(ts))], diff(time(ts)), type='h',
> main="EBS EUR/USD Tick Interarrival Times\n12-11-2007",
> xlab="Time", ylab="Interrarival Time (sec)")
>
>
> Is it possible to do this in a neater way? I tried using rollapply() to get
> a daily rolling window, but wasnt successful.
>
Try creating a zoo object of times and then plot its diff with plot.zoo:
tt <- time(eurusd)
ttz <- zoo(as.vector(tt), tt)
plot(diff(ttz))
More information about the R-SIG-Finance
mailing list