gpt4 book ai didi

R生成一个1分钟的间隔时间序列

转载 作者:行者123 更新时间:2023-12-03 12:11:10 27 4
gpt4 key购买 nike

我想生成一个 1 分钟的间隔时间序列,然后粘贴到一个 xts 对象。基本上,我有一个像这样的逐个滴答的 dateTime 对象:

 [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"

我正在聚合我的 xts 系列(通过之前的报价)以使用 RTAQ 包函数获得 1 分钟(等距)间隔的时间系列:

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

问题是时间标签没有聚合,即聚合系列没有用 1 分钟间隔的时间对象标记。这部分是因为有几秒钟没有价格。为了获得等距的时间序列,这些函数用之前的报价来填补空白。

因此,我如何创建一个 1 分钟间隔的时间序列来获得人工的 1 分钟间隔时间序列?

谢谢。

最佳答案

出于兴趣,RTAQ 是否提供其他 R 包中没有的东西? 0.1 版是两年前发布的,所以它看起来像一个死项目。无论如何,您仍然可以使用 XTS 的 to.minute() 函数,因为 RTAQ 似乎使用 xts 对象。

这是我用来获取刻度并将其转换为条形图以及添加其他列(例如均值/标准差)的一些示例代码:

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

我使用 align.time.down 而不是 align.time,这样从 06:00:00 到 06:00:59.999 的报价会进入标记为“06:00:00”不是标有“06:01:00”的。这符合我的历史数据格式。如果你需要它的定义是:

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

最后,如果您有完整的分钟没有刻度,并且仍然希望在数据中为它们设置条形图,我会使用它(与上面相同的 k=60):

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)

seq.exclude_final_period.POSIXt函数定义如下:

#' 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_fromperiod_toPOSIXct 对象,描述您交易时段的开始和结束。

关于R生成一个1分钟的间隔时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11650996/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com