[R-SIG-Finance] cut range from time objects

G See gsee000 at gmail.com
Sun Jun 23 17:45:27 CEST 2013


Hi Dominykas

I would try the below as my first attempt.

library(xts)
library(timeSeries)
Sys.setenv(TZ="GMT")
temp <- xts(1:84, timeCalendar(m=1, d=rep(1:7,each=12),h = seq(0,23,2)))

# Monday through Thursday
mt <- temp[.indexwday(temp) %in% 1:4]

# Friday 00:00:00/20:00:00
f <- temp[.indexwday(temp) == 5L]["T00:00:00/T20:00:00"]

# Sunday 20:00:00/24:00:00
s <- temp[.indexwday(temp) == 0L]["T20:00:00.000/T23:59:59.999"]

out <- rbind(mt, f, s)

# check answer by adding a column with weekday (0-6 starting on Sunday)
colnames(out) <- "temp" # have to have a column name to do the next step
out$wday <- .indexwday(out)
# Now you can look at the output and see that the only times where wday is 0
# are 20:00 or later, and the only times where wday is 5 are 20:00 or earlier
out

Hope this helps,
Garrett



On Sun, Jun 23, 2013 at 10:02 AM, Dominykas Grigonis
<dominykasgrigonis at gmail.com> wrote:
> Dear all,
>
> I have come across the issue and I hope you will be able to help me.
>
> I have minute data and unfortunately it has weekends in it and unfortunately the values on weekend are not 0s and not NAs, but rather last trading price of friday. I want to cut the ranges out as it slows down strategy testing.
>
> Say we have xts object with timeDate index
> temp <- xts(1:84, timeCalendar(m=1, d=rep(1:7,each=12),h = seq(0,23,2)))
>
>
> I want to cut rows from friday 20:00 to sunday 20:00.
> I started writing function, but it just does not seem right…
>
> filtertime <- function(x, rwd <- "5/2", rt <- "0800/1400"){
>     is.within   <- function(x){
>         value   <- hour(x)*60+minute(x)
>         (value > range[1] && value < range[2])}
>     rwd     <- as.numeric(strsplit(rwd,"/")[[1]])
>     rt      <- as.numeric(strsplit(rt,"/")[[1]])
>     rt      <- (rt%/%100)*60+(rt%%100)
>     time    <- index(x)
>     if(rwd[1]>rwd[2]){ wsq <- c((rwd[1]:7), (1:rwd[2]))
>     }else{wsq <- rwd[1]:rwd[2]}
>     wweek   <- sapply(time, function(x){x[all(dayOfWeek(x) != wsq)]})
>     wweek   <- x[all(dayOfWeek(x) != wsq)]
>
> }
> I am not looking for help on this function, I hope someone could suggest some efficient way to solve my problem as I am working on 1 year 1minute data.
> Thank you in advance.
>
> Kind regards,--
> Dominykas Grigonis
>
>
>         [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.



More information about the R-SIG-Finance mailing list