[R] Time and xts

Hugo Mildenberger Hugo.Mildenberger at web.de
Wed Jan 19 02:40:02 CET 2011

```Nicos,

there are certainly better / faster methods to compare a time range which I
simply don't know of, but the condensed code below should do it. Your main
problem was that you can't compare time(x) to 7:55, since the
latter expression denotes a sequence from 7 to 55 in R. Consequently, such
an expression compares time(x) with all integer numbers between 7 and 55:

>time(z[1]) <= 7:55
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[49] FALSE

>length(z[time(z) >= 2 && time(z) <= 7:55])
>[1] 0

Your while loop was therefore equivalent to

>m=vector("numeric",0);
>while(m) print(1);

Best

Hugo

-- snip --
library(zoo)
library(chron)

Lines<- "Date    Time    Open    High    Low     Close
1/2/2005        17:05   1.3546  1.3553  1.3546  1.35495
1/2/2005        17:10   1.3553  1.3556  1.3549  1.35525
1/2/2005        17:15   1.3556  1.35565 1.35515 1.3553
1/2/2005        17:25   1.355   1.3556  1.355   1.3555
3/14/2006       5:21    1.18895 2.18925 1.18835 1.1885
3/14/2006       7:56    2.18895 3.18925 1.18835 1.1885
2/13/2006       5:20    1.18895 1.18925 1.18835 1.1885"

z<- read.zoo(con<-textConnection(Lines), header = TRUE, index = list(1, 2), FUN = function(d,t) as.chron(paste(d, t), format = "%m/%d/%Y %H:%M"))
close(con);

entrytrade <- function(k) { k\$Open+k\$High }

#Note: A binary and operator (&) instead a logical (&&) is needed here
new <- entrytrade( z[hours(time(z)) >= 2 & ( hours(time(z)) + minutes(time(z))/100 <= 7.55 ) ] );

print(new);

--- snip ---

On Tuesday 18 January 2011 22:36:12 rnick wrote:
>
> Hi all,
>
> Gabor came up with a very nice of code for my request:
>
> Lines <- "Date    Time    Open    High    Low     Close
> 1/2/2005        17:05   1.3546  1.3553  1.3546  1.35495
> 1/2/2005        17:10   1.3553  1.3556  1.3549  1.35525
> 1/2/2005        17:15   1.3556  1.35565 1.35515 1.3553
> 1/2/2005        17:25   1.355   1.3556  1.355   1.3555
> 2/13/2006       5:20    1.18895 1.18925 1.18835 1.1885"
>
> library(zoo)
> library(chron)
>
>  FUN = function(d,t) as.chron(paste(d, t), format = "%m/%d/%Y %H:%M"))
>
> z\$New <- z\$Open + z\$High
> zz <- z[hours(time(z)) >= 2 & hours(time(z)) <= 8]
>
> The result is:
>
> > zz
>                    Open High  Low Close  New
> (02/13/06 05:20:00) 1.19 1.19 1.19  1.19 2.38
>
> However, my original code needs a for loop.
>
> Any ideas on how to identify the z positions where time is between 2:00:00
> and 07:55:00.
> Could i do it with the following structure?
>
>
> Lines<- "Date    Time    Open    High    Low     Close
> 1/2/2005        17:05   1.3546  1.3553  1.3546  1.35495
> 1/2/2005        17:10   1.3553  1.3556  1.3549  1.35525
> 1/2/2005        17:15   1.3556  1.35565 1.35515 1.3553
> 1/2/2005        17:25   1.355   1.3556  1.355   1.3555
> 2/13/2006       5:20    1.18895 1.18925 1.18835 1.1885"
>
> library(zoo)
> library(chron)
>
> z<- read.zoo(textConnection(Lines), header = TRUE, index = list(1, 2), FUN =
> function(d,t) as.chron(paste(d, t), format = "%m/%d/%Y %H:%M"))
>
> n=5
>
>
> {
>
>        new[k]<<-open[k]+high[k]
> }
>
> for (x in seq(from=1,to=n,by=1))
> {
> 	while (z[time(x)>=2 &&  time(x)<=7:55])
> 	{
> 	}
> }
>
> Based on what i have seen the problem lies in the for loop and more
> specifically in the while statement. That's the error i get:
>
> Error in while (z[time(x) >= 2 && time(x) <= 7:55]) { :
>   argument is of length zero
>
> Thanks
>

```