[R] Basic Help with Zoo objects and trading days

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jan 29 23:16:36 CET 2011


On Sat, Jan 29, 2011 at 2:03 PM, john nicholas <jbnichola at sbcglobal.net> wrote:
> All,
>
> I have been just recently working with zoo objects for trading systems.
>
> Can someone please help with these basic questions?
>
> Given a daily time series downloaded using get.hist.quote() from the tseries
> package, ie......
>
> startDate= as.Date("2000-01-01")
> endDate= as.Date("2011-01-29")
> frequency= 'd'
>
> s= get.hist.quote('IWF', start= startDate, end= endDate,
>      compression= 'd', quote= "AdjClose")
>
> tail(s,30)
>           AdjClose
> 2010-12-16    56.85
> 2010-12-17    56.95
> 2010-12-20    57.00
> 2010-12-21    57.32
> 2010-12-22    57.37
> 2010-12-23    57.30
> 2010-12-27    57.30
> 2010-12-28    57.29
> 2010-12-29    57.43
> 2010-12-30    57.34
> 2010-12-31    57.26
> 2011-01-03    57.80
> 2011-01-04    57.55
> 2011-01-05    57.87
> 2011-01-06    57.89
> 2011-01-07    57.81
> 2011-01-10    57.88
> 2011-01-11    58.13
> 2011-01-12    58.60
> 2011-01-13    58.59
> 2011-01-14    58.94
> 2011-01-18    59.20
> 2011-01-19    58.64
> 2011-01-20    58.36
> 2011-01-21    58.33
> 2011-01-24    58.79
> 2011-01-25    58.84
> 2011-01-26    59.24
> 2011-01-27    59.36
> 2011-01-28    58.23
>
> How can I extract a time series (a zoo object) containing the price of only the
> last trading day of each week, ie.:
>
> 2010-12-17    56.95
> 2010-12-23    57.30
> 2010-12-31    57.26
> 2011-01-07    57.81
> 2011-01-14    58.94
> 2011-01-21    58.33
> 2011-01-28    58.23
>
> Similarly how can I extract a zoo object of only the last trading day of each
> month, ie.
>
> 2010-12-31    57.26
>
> Of course in most cases the last trading day is different from the last calendar
> day, and I would like to preserve the actual date of the last trading day.
>
> Finally, how can I extract a time series of the prices of only the days of
> options expiration in a particular cycle, such as the third friday of Mar, June,
> Sept. and Dec.?
>
> Any help is greatly appreciated.
>

Here are a few ways:

1. In get.hist.quote call use compression = "w" rather than compression = "d" .

2. use apply.weekly in xts where s is from your post:

   library(xts)
   apply.weekly(s, tail, 1)

3. Use the following where nextfri is a function that appears in
vignette("zoo-quickref") and s is from your post:

   nextfri <- function(x) 7 * ceiling(as.numeric(x - 5 + 4)/7) + as.Date(5 - 4)

   # and this
   aggregate(s, nextfri, tail, 1)

   # or this
   s[!duplicated(nextfri(time(s)), fromLast = TRUE)]

Note that the compression = "w" solution labels each week by using the
Monday of that week.  The apply.weekly and duplicated solutions label
each week by the date of the last present data point in that week.
The aggregate solution labels each point by the Friday of that week.

You also might want to look at getSymbols in the quantmod package.

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



More information about the R-help mailing list