Pergunta

I have the following xts object called trades:

trades = structure(c("37119712", "37119713", "37119714", "37119715", "37119716", 
"37119717", "37119720", "37119722", "37119724", "37119725", "37119726", 
"37119727", "37119732", "37119734", "37119735", "37119741", "37119742", 
"37119756", "37119774", "37119775", "37119834", "37119835", "37119836", 
"37119850", "37119851", "37119852", "37119853", "37119854", "37119855", 
"37119856", "BID", "BID", "BID", "BID", "BID", "ASK", "BID", 
"BID", "ASK", "BID", "BID", "ASK", "BID", "BID", "ASK", "BID", 
"BID", "ASK", "BID", "BID", "ASK", "ASK", "ASK", "BID", "BID", 
"ASK", "ASK", "ASK", "BID", "BID", "440.002", "440.002", "440.030", 
"440.983", "441.500", "440.000", "440.000", "440.000", "439.335", 
"440.998", "441.496", "439.335", "441.496", "441.496", "441.496", 
"441.490", "441.500", "439.736", "439.736", "441.400", "439.645", 
"439.643", "439.642", "441.400", "441.400", "441.400", "441.400", 
"440.960", "441.499", "441.500", "1.0000000", "8.7381500", "1.5481100", 
"0.0639222", "0.4908570", "3.1680000", "0.0473160", "0.7825840", 
"0.0108432", "0.0270000", "0.0263241", "0.0100000", "0.1060000", 
"0.1879220", "0.1150000", "0.1000000", "1.8170000", "0.0222000", 
"0.0234469", "2.4815500", "0.2730000", "0.0380000", "3.0852800", 
"0.5824950", "0.0100000", "0.0689000", "0.2188070", "0.0100000", 
"0.0705000", "0.8139830", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", 
"TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", 
"TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", 
"TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", 
"TRUE"), .Dim = c(30L, 5L), .Dimnames = list(NULL, c("id", "type", 
"price", "size", "api")), index = structure(c(1399673507, 1399673524, 
1399673524, 1399673524, 1399673524, 1399673531, 1399673553, 1399673565, 
1399673605, 1399673609, 1399673609, 1399673619, 1399673645, 1399673677, 
1399673686, 1399673721, 1399673721, 1399673764, 1399673810, 1399673810, 
1399674172, 1399674172, 1399674172, 1399674333, 1399674333, 1399674335, 
1399674353, 1399674368, 1399674368, 1399674368), tzone = "CET",
tclass = c("POSIXct", "POSIXt")), class = c("xts", "zoo"),
.indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"),
.indexTZ = "CET", tzone = "CET")

The time series is not equidistant. When I type the following command:

last(trades["/2014-05-10 00:26:08"], "5 minutes")

I would expext to get all data from interval (2014-05-10 00:26:08 less 5 minutes, 2014-05-10 00:26:08).

However, I'm getting following results:

                    id         type  price     size          api   
2014-05-10 00:15:21 "37119741" "BID" "441.490" " 0.10000000" "TRUE"
2014-05-10 00:15:21 "37119742" "BID" "441.500" " 1.81700000" "TRUE"
2014-05-10 00:16:04 "37119756" "ASK" "439.736" " 0.02220000" "TRUE"
2014-05-10 00:16:50 "37119774" "BID" "439.736" " 0.02344690" "TRUE"
2014-05-10 00:16:50 "37119775" "BID" "441.400" " 2.48155000" "TRUE"
2014-05-10 00:22:52 "37119834" "ASK" "439.645" " 0.27300000" "TRUE"
2014-05-10 00:22:52 "37119835" "ASK" "439.643" " 0.03800000" "TRUE"
2014-05-10 00:22:52 "37119836" "ASK" "439.642" " 3.08528000" "TRUE"
2014-05-10 00:25:33 "37119850" "BID" "441.400" " 0.58249500" "TRUE"
2014-05-10 00:25:33 "37119851" "BID" "441.400" " 0.01000000" "TRUE"
2014-05-10 00:25:35 "37119852" "ASK" "441.400" " 0.06890000" "TRUE"
2014-05-10 00:25:53 "37119853" "ASK" "441.400" " 0.21880700" "TRUE"
2014-05-10 00:26:08 "37119854" "ASK" "440.960" " 0.01000000" "TRUE"
2014-05-10 00:26:08 "37119855" "BID" "441.499" " 0.07050000" "TRUE"
2014-05-10 00:26:08 "37119856" "BID" "441.500" " 0.81398300" "TRUE" 

But typing this:

last(trades["/2014-05-10 00:26:08"], "2 minutes")

gives the correct results:

                    id         type  price     size          api   
2014-05-10 00:25:33 "37119850" "BID" "441.400" " 0.58249500" "TRUE"
2014-05-10 00:25:33 "37119851" "BID" "441.400" " 0.01000000" "TRUE"
2014-05-10 00:25:35 "37119852" "ASK" "441.400" " 0.06890000" "TRUE"
2014-05-10 00:25:53 "37119853" "ASK" "441.400" " 0.21880700" "TRUE"
2014-05-10 00:26:08 "37119854" "ASK" "440.960" " 0.01000000" "TRUE"
2014-05-10 00:26:08 "37119855" "BID" "441.499" " 0.07050000" "TRUE"
2014-05-10 00:26:08 "37119856" "BID" "441.500" " 0.81398300" "TRUE"

But then again, the command:

last(trades["/2014-05-10 00:26:08"], "120 seconds")

should give the same result as above, but it doesnt. Where's the catch??

Foi útil?

Solução

last.xts doesn't seem to work well with a character n if your data aren't regular. Maybe try something like this instead?

> trades[paste(end(trades)-60*5, end(trades), sep="/"),]
                    id         type  price     size        api   
2014-05-10 00:22:52 "37119834" "ASK" "439.645" "0.2730000" "TRUE"
2014-05-10 00:22:52 "37119835" "ASK" "439.643" "0.0380000" "TRUE"
2014-05-10 00:22:52 "37119836" "ASK" "439.642" "3.0852800" "TRUE"
2014-05-10 00:25:33 "37119850" "BID" "441.400" "0.5824950" "TRUE"
2014-05-10 00:25:33 "37119851" "BID" "441.400" "0.0100000" "TRUE"
2014-05-10 00:25:35 "37119852" "ASK" "441.400" "0.0689000" "TRUE"
2014-05-10 00:25:53 "37119853" "ASK" "441.400" "0.2188070" "TRUE"
2014-05-10 00:26:08 "37119854" "ASK" "440.960" "0.0100000" "TRUE"
2014-05-10 00:26:08 "37119855" "BID" "441.499" "0.0705000" "TRUE"
2014-05-10 00:26:08 "37119856" "BID" "441.500" "0.8139830" "TRUE"
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top