[R-SIG-Finance] xts and Sys.time() - very stange behaviour

soren wilkening me at censix.com
Wed Aug 1 18:22:26 CEST 2012


#I wasn't sure whether to post this to R.help, but since it involves xts() I
thought I would start here.
#I have attached a self explanatory code sample that does consistently
reproduce the problem on my #system (and it is driving me crazy)
#
#The code will generate some data based on Sys.time(), perform a test and if
it succeeds to reproduce the #problem, the loop will exit. Takes about 5
seconds on my system. So if the loop runs for a long time on #your machine,
your system is probably unaffected. you will have to interrupt it with
ctrl+c


require(xts)

options(digits.secs=3)

okok<-TRUE

while(okok){

initdate <- "2000-01-31 13:45:02.437" #some date
ob <- xts(as.matrix(t(c(0, NA, "aaaaa", "bbbb",  0, "ccccc",
as.character(as.POSIXct(initdate)), 1, 0))), order.by = as.POSIXct(initdate)
) 


#get some timestamps, with subsecond accuracy truncated to microseconds
t0 <- as.POSIXct( strftime(Sys.time(), "%Y-%m-%d %H:%M:%OS3") )  
Sys.sleep(0.3)
t1 <- as.POSIXct( strftime(Sys.time(), "%Y-%m-%d %H:%M:%OS3") )
Sys.sleep(0.7)
t2 <- as.POSIXct( strftime(Sys.time(), "%Y-%m-%d %H:%M:%OS3") )
Sys.sleep(0.2)


# use the timestamps to add rows to the 'ob'
order <- NULL

ordertime1 <- t0 + 1e-3
neworder <- xts(as.matrix(t(c(1, 2, 'aa', 'bb', 13.4, 'o', NA, NA, 0))),
order.by = (ordertime1))
if (is.null(order)) order <- neworder else order <- rbind(order, neworder)

ordertime2 <- t1 + 1e-3
neworder <- xts(as.matrix(t(c(2, 2, 'aa', 'bb', 13.4, 'o', NA, NA, 0))),
order.by = (ordertime2))
if (is.null(order)) order <- neworder else order <- rbind(order, neworder)

ordertime3 <- t2 + 1e-3
neworder <- xts(as.matrix(t(c(3, 2, 'aa', 'bb', 13.4, 'o', NA, NA, 0))),
order.by = (ordertime3))
if (is.null(order)) order <- neworder else order <- rbind(order, neworder)
tmpneworder <- neworder

ob <- rbind(ob,order)

key <- as.character(index(tmpneworder))

okok<- nrow( ob[ key ] ) == 1 # does access work ?
}

print(ob)
print(paste('problem: ob[\'',key,'\']  is empty, but should not be
!!',sep=''))

# ob should have 4 rows and each should be accessible by using the
timestamp, i.e.:  ob['2012-08-01 18:08:39.298'] or similar.  But for some
reason this wont work for the last row !!! ????
#
#Thanks for some help
# Soren



-----
http://censix.com
--
View this message in context: http://r.789695.n4.nabble.com/xts-and-Sys-time-very-stange-behaviour-tp4638714.html
Sent from the Rmetrics mailing list archive at Nabble.com.



More information about the R-SIG-Finance mailing list