Pergunta

I would like to generate a 1 min spaced time sequence to paste then to a xts object. Basically, I've got a tick-by-tick dateTime object like that :

 [1] "2010-02-02 08:00:03 CET" "2010-02-02 08:00:04 CET" "2010-02-02 08:00:04 CET" "2010-02-02 08:00:04 CET" "2010-02-02 08:00:04 CET"
 [6] "2010-02-02 08:00:04 CET" "2010-02-02 08:00:04 CET" "2010-02-02 08:00:05 CET" "2010-02-02 08:00:05 CET" "2010-02-02 08:00:05 CET"

I'm aggregating my xts series (by previous tick) to get a 1 min (equally)-spaced time series using an RTAQ package function :

price_1m<-aggregatets(price,FUN="previoustick",k=1,on="minutes")

The problem is that the time label is not aggregated that is the aggregated series is not labeled by a 1 min spaced time-object. This is due is part to the fact that there are seconds with no prices. To get a equally spaced time series, the functions fills the blanks with the previous tick price.

Thus, how can i create a 1 min spaced time sequence to get an artificial 1 min spaced time sequence?

Thanks.

Foi útil?

Solução

Out of interest does RTAQ offer anything not in another R package? Version 0.1 was released over two years ago, so it looks like a Dead Project. Anyway, you can still use XTS's to.minute() function, as it appears RTAQ use xts objects.

Here is some sample code I use to take ticks and convert into bars, as well as adding other columns such as mean/sd:

k=60
...
bars=to.period(x,k,period="secs")
colnames(bars)=c("Open","High","Low","Close")
ep=endpoints(x,"secs",k)
bars$Volume=period.apply(x,ep,length)
bars$mean=period.apply(x,ep,mean)
bars$sd=period.apply(x,ep, function(x){apply(x,2,sd)})
align.time(bars,k)  #Do this last

Instead of align.time I use align.time.down, so that ticks from 06:00:00 to 06:00:59.999 go into a bar labelled "06:00:00" not one labelled "06:01:00". This matches the historical data format I have. If you need it the definition is:

align.time.down=function(x,n){index(x)=index(x)-n;align.time(x,n)}

Finally, if you have whole minutes with no ticks, and still want a bar in your data for them, I use this (same k=60 as above):

full_index=do.call("c",mapply(
    seq.exclude_final_period.POSIXt,period_from,period_to,by=k,SIMPLIFY=F
    ))
bars=merge(bars,xts(,full_index),all=TRUE)

The seq.exclude_final_period.POSIXt function is defined as follows:

#' Helper for process_one_day_of_ticks(); intended as a
#' replacement for seq.POSIXt (which will not exclude the final period).
#' @internal Use from=from+by instead of to=to-by to exclude the
#      first period instead.
seq.exclude_final_period.POSIXt=function(from,to,by){
to=to-by    #Make the final entry exclusive
seq(from,to,by)
}

period_from and period_to are POSIXct objects, that describe the start and end of your trading session.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top