[R] Time and xts
rnick
nikos.rachmanis at gmail.com
Tue Jan 18 22:36:12 CET 2011
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)
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"))
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
entrytrade<-function(k)
{
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])
{
entrytrade(x)
}
}
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
--
View this message in context: http://r.789695.n4.nabble.com/Time-and-xts-tp3219825p3224058.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list