[R-SIG-Finance] Constructing suitable temporal subsamples using zoo and/or xts

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jan 28 20:04:24 CET 2012


On Sat, Jan 28, 2012 at 1:36 PM, Ted Byers <r.ted.byers at gmail.com> wrote:
> I am managing to confuse myself with what I am seeing in the docs, and
> results returned by Google, so I hope someone here can provide me a bit of
> clarity.
>
>
>
> I am working with tick data, from which  I have been constructing an xts
> object with a datetime field and a price field, from which I then construct
> one minute OHLC data.  I know I have to use zoo's function to interpolate
> missing data (since there are a few minutes through the data when nothing
> was traded, even for a high volume futures contract).
>
>
>
> I want to use rollapply on daily data to apply my analysis to daily data
> between 9:30AM EST and 4:30PM EST as that is when most of the trading of
> these contracts happen, but there I sporadic trading of these contracts
> outside those times which I would like to ignore.
>
>
>
> I have found rollapply easy to use (and that is why I need zoo's
> interpolation capability so it doesn't fail because of missing data), and
> have seen apply.daily.
>
>
>
> I thus expect I need two functions: 1) to do my analysis (already done and
> working fine - this returns a data.frame with all the variables I require in
> my output); 2) a function to invoke rollapply on a daily sample, and which I
> can pass to apply.daily in order to get results for all the days for which I
> have data.
>
>
>
> What I don't know how to do is how to tell rollapply that it is to work only
> on values with times between my start and end times, for whatever date's
> data it happens to get, or how to combine the results for each day into a
> single data.frame or zoo or xts object.
>
>
>
> I guess my question amounts to how I can do this in R, with the capabilities
> of zoo and/or xts, or do I have to resort to using a mix of SQL, Perl and R
> to get it done (i.e. make a driver script in Perl that gets a day's worth of
> data between the right times from my DB using SQL, and then passes that data
> to  my R script).
>

See ?window.zoo

or perhaps depending on specifics:

hm <- as.numeric(format(time(z), "%H%M"))
z[hm >= 930 & hm <= 1600)]

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-SIG-Finance mailing list