I need to do previous tick aggregation on my tick data set for 5 minute intervals. Please note what I want to do is analogous to aggregateTrades() function in highfrequency package. But I need to solve this problem without using highfrequency package due to some other data handling issues. This is my data set:
dput(tt)
structure(c(1371.25, NA, 1373.95, NA, NA, 1373, NA, 1373.95,
1373.9, NA, NA, 1374, 1374.15, NA, 1374, 1373.85, 1372.55, 1374.05,
1374.15, 1374.75, NA, NA, 1375.9, 1374.05, NA, NA, NA, NA, NA,
NA, NA, 1375, NA, NA, NA, NA, NA, 1376.35, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 1376.25, NA, 1378, 1376.5, NA, NA, NA, 1378,
1378, NA, NA, 1378.8, 231.9, 231.85, NA, 231.9, 231.85, 231.9,
231.8, 231.9, 232.6, 231.95, 232.35, 232, 232.1, 232.05, 232.05,
232.05, 231.5, 231.3, NA, NA, 231.1, 231.1, 231.1, 231, 231,
230.95, 230.6, 230.6, 230.7, 230.6, 231, NA, 231, 231, 231.45,
231.65, 231.4, 231.7, 231.3, 231.25, 231.25, 231.4, 231.4, 231.85,
231.75, 231.5, 231.55, 231.35, NA, 231.5, 231.5, NA, 231.5, 231.25,
231.15, 231, 231, 231, 231.05, NA), .indexCLASS = c("POSIXct",
"POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "Asia/Calcutta", tzone = "Asia/Calcutta", index = structure(c(1459481850,
1459482301, 1459482302, 1459482303, 1459482304, 1459482305, 1459482306,
1459482307, 1459482309, 1459482310, 1459482311, 1459482312, 1459482314,
1459482315, 1459482316, 1459482317, 1459482318, 1459482319, 1459482320,
1459482321, 1459482322, 1459482323, 1459482324, 1459482326, 1459482328,
1459482329, 1459482330, 1459482331, 1459482332, 1459482336, 1459482337,
1459482338, 1459482339, 1459482342, 1459482344, 1459482346, 1459482347,
1459482348, 1459482349, 1459482350, 1459482351, 1459482354, 1459482355,
1459482356, 1459482357, 1459482358, 1459482359, 1459482362, 1459482363,
1459482364, 1459482369, 1459482370, 1459482371, 1459482372, 1459482373,
1459482378, 1459482379, 1459482380, 1459482382, 1459482388), tzone = "Asia/Calcutta", tclass = c("POSIXct",
"POSIXt")), .Dim = c(60L, 2L), .Dimnames = list(NULL, c("A",
"B")), class = c("xts", "zoo"))
This is my code for previous tick aggregation:
ag.5min.tt<-tt%>%filter(as.Date(index(tt)))%>%lapply(aggregate(by=cut(format(index(tt), format = "%H:%M:%S"), breaks = "5 mins", Fun=tail)))
What I am trying to do with the above code is to make 5 minute intervals each day for the prices of A and B. But I am getting error. Please suggest how to fix this error:
Error in UseMethod("filter_") :
no applicable method for 'filter_' applied to an object of class "c('xts', 'zoo')"
Thanks.
Edit: Converting the xts object to dataframe:
tt<-as.data.frame(tt)
tt<-data.frame(Time=rownames(tt), coredata(tt))
ag.5min.tt<-tt%>% filter(as.Date(index(tt)))%>%lapply(aggregate(by=cut(format(index(tt), format = "%H:%M:%S"), breaks = "5 mins", Fun=tail)))
New error:
Error in eval(substitute(expr), envir, enclos) :
filter condition does not evaluate to a logical vector.
Edit: attempt:
tt$Time<- as.POSIXct(tt$Time, format="%Y-%m-%d %H:%M:%S")
ag.5min.tt<-tt%>% group_by(Time==as.Date(tt$Time))%>%lapply(aggregate(by=cut(format(tt$Time, format = "%H:%M:%S"), breaks = "5 mins", Fun=tail)))
Error:
Error in cut.default(format(tt$Time, format = "%H:%M:%S"), breaks = "5 mins", :
'x' must be numeric
In addition: Warning message:
In eval(substitute(expr), envir, enclos) :
Incompatible methods ("Ops.POSIXt", "Ops.Date") for "=="
The result would look like this. Every five minute time stamp will have values for that particular time stamp or if there is an NA that time stamp will have last non- NA value for the stocks A and B
time A B
1 2016-04-01 09:00:00 NA NA
2 2016-04-01 09:05:00 NA NA
3 2016-04-01 09:10:00 NA NA
4 2016-04-01 09:15:00 1371.25 231.90
5 2016-04-01 09:20:00 1376.35 231.55