john nicholas
2011-Jul-22 19:37 UTC
[R] Picking returns from particular days of the month from a zoo object
Hello, I would like to implement a "turn-of-the-month' trading strategy in R. Given a daily series of stock market return data as a zoo object, the strategy would go long (buy) four trading days before the end of the month, and sell the third trading day of the following month. How can I select these days, particularly the fourth day before and the third day after the turn of the month, from a zoo object? Thanks in advance, John B. Nicholas, Ph.D 650-315-9895 [[alternative HTML version deleted]]
Joshua Ulrich
2011-Jul-23 10:31 UTC
[R] Picking returns from particular days of the month from a zoo object
On Fri, Jul 22, 2011 at 2:37 PM, john nicholas <jbnichola at sbcglobal.net> wrote:> ?Hello, > > I would like to implement a "turn-of-the-month' trading strategy in R. > > Given a daily series of stock market return data as a zoo object, the strategy > would go long (buy) four trading days before the end of the month, and sell the > third trading day of the following month. > > How can I select these days, particularly the fourth day before and the third > day after the turn of the month, from a zoo object? >Here are two ways to do this using xts. The first approach creates a list of xts objects where each list element contains one month of data. Then it uses first() and last() to extract the 4th trading day prior to the end of the month. require(quantmod) getSymbols(SPY) x1 <- do.call(rbind, lapply(split(SPY, "months"), function(x) first(last(x,4)))) The second approach uses apply.monthly(), but that function always returns an object with index values that correspond to the last observation in the month. So we have to manually update the index to be 3 days prior. x2 <- apply.monthly(SPY, function(x) first(last(x,4))) index(x2) <- index(SPY)[endpoints(SPY, "months")[-1]-3]> Thanks in advance, > > John B. Nicholas, Ph.D > 650-315-9895 >HTH, -- Joshua Ulrich | FOSS Trading: www.fosstrading.com
Gabor Grothendieck
2011-Jul-23 12:46 UTC
[R] Picking returns from particular days of the month from a zoo object
On Fri, Jul 22, 2011 at 3:37 PM, john nicholas <jbnichola at sbcglobal.net> wrote:> > ?Hello, > > I would like to implement a "turn-of-the-month' trading strategy in R. > > Given a daily series of stock market return data as a zoo object, the strategy > would go long (buy) four trading days before the end of the month, and sell the > third trading day of the following month. > > How can I select these days, particularly the fourth day before and the third > day after the turn of the month, from a zoo object? >library(quantmod) # also brings in zoo # set up some test data getSymbols("IBM", return.class = "zoo") # get index in ibm to 3rd trading day in month # and 4th last trading day in month ym <- as.yearmon(time(IBM)) ithird <- c(tapply(seq_along(tt), ym, "[", 3)) ilast4 <- c(tapply(seq_along(tt), ym, function(x) x[length(x)-3])) Now IBM[ithird, ] and IBM[ilast4, ] give the subseries at the third and fourth last days of each month, respectively. time(IBM)[ithird] and time(IBM)[ilast4] are just the dates. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com