Hi:

This is more or less the same idea as the other respondent, but uses
function rle() instead.  The function:

findDates <- function(df) {
     u <- rle(df$Signals)
     n <- length(u$values)
     ends <- with(u, cumsum(lengths)[values == -1])
     begins <- with(u, cumsum(lengths)[values == 1] + 1)

   # Endpoint issues:
     if(u$values[1] == -1) begins <- c(1, begins)
     if(u$values[n] == 1) begins <- begins[-length(begins)]

   # generate output data frame

     data.frame(beginDate = df$date[begins],
                  endDate = df$date[ends])
  }

Using d as the name of your data frame, I coerced the date variable to be of
class Date with
d$date <- as.Date(d$date, format = '%Y-%m-%d') before calling the function.

> findDates(d)
   beginDate    endDate
1 2005-09-21 2005-09-28
2 2005-09-30 2005-10-28

A couple of stress tests:

d2 <- d[, 1:3]
d2$Signals <- c(rep(-1, 5), rep(1, 6), -1, rep(1, 3), rep(-1, 10), rep(1,
8), rep(-1, 4))
> findDates(d2)
 beginDate    endDate
1 2005-09-15 2005-09-21
2 2005-09-30 2005-09-30
3 2005-10-06 2005-10-19
4 2005-11-01 2005-11-04

d2$Signals <- c(rep(-1, 5), rep(1, 10), rep(-1, 2), rep(1, 20))
> findDates(d2)
   beginDate    endDate
1 2005-09-15 2005-09-21
2 2005-10-06 2005-10-07

I didn't test the function with dates of class time.series, ts or zoo, so
you may need to adapt it to your situation.

HTH,
Dennis

On Tue, Dec 7, 2010 at 9:42 AM, cameron <raymond.fu@invesco.com> wrote:

>
> i have time series of momentum signal.  I want to get the date of each of
> the
> "-1"  signal period.  for example , the first period of -1 signal begins on
> 2005-9-21 and ends on 2005-9-28.  2nd period of -1 signal begins on
> 2005-09-30 and ends on 2005-10-28.
>
>
> Thx
> Cameron
>
>     date        Px       200MA     Signals
> 2005-09-15   26.27  25.83865    1
> 2005-09-16   26.07  25.83275    1
> 2005-09-19   26.00  25.82730    1
> 2005-09-20   25.84  25.82035    1
> 2005-09-21   25.49  25.81115   -1
> 2005-09-22   25.34  25.80250   -1
> 2005-09-23   25.27  25.79205   -1
> 2005-09-26   25.27  25.78225   -1
> 2005-09-27   25.34  25.77355   -1
> 2005-09-28   25.67  25.76565   -1
> 2005-09-29   25.94  25.75920    1
> 2005-09-30   25.73  25.75230   -1
> 2005-10-03   25.50  25.74400   -1
> 2005-10-04   24.98  25.73410   -1
> 2005-10-05   24.67  25.72270   -1
> 2005-10-06   24.73  25.71100   -1
> 2005-10-07   24.59  25.69910   -1
> 2005-10-10   24.46  25.68635   -1
> 2005-10-11   24.41  25.67415   -1
> 2005-10-12   24.30  25.66090   -1
> 2005-10-13   24.59  25.64935   -1
> 2005-10-14   24.67  25.63890   -1
> 2005-10-17   24.53  25.62795   -1
> 2005-10-18   24.57  25.61710   -1
> 2005-10-19   25.09  25.60835   -1
> 2005-10-20   24.79  25.59840   -1
> 2005-10-21   24.78  25.58855   -1
> 2005-10-24   25.10  25.58070   -1
> 2005-10-25   25.03  25.57185   -1
> 2005-10-26   25.11  25.56375   -1
> 2005-10-27   24.85  25.55410   -1
> 2005-10-28   25.53  25.55040   -1
> 2005-10-31   25.70  25.54830    1
> 2005-11-01   25.96  25.54650    1
> 2005-11-02   26.46  25.54890    1
> 2005-11-03   26.44  25.55180    1
> 2005-11-04   26.66  25.55685    1
>
> -code
>
>        library(tseries)
>        library(timeSeries)
>
>        msft <- as.timeSeries(get.hist.quote(instrument="MSFT",
> start="1986-03-31",
> end="2008-09-10", quote=c("C"), provider="yahoo", retclass="zoo"))
>        msft <- cbind(msft,as.timeSeries(rollapply(as.zoo(msft
> ),width=200,mean,align="right")))
>        msft <- cbind(msft,NA)
>        msft[,3] <- (ifelse(msft[,1]>msft[,2],1,-1))
>
>        print(msft)
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/help-on-timeseries-tp3076866p3076866.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]

