to.minutes 使用自定义端点 [英] to.minutes using custom endpoints

查看:28
本文介绍了to.minutes 使用自定义端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用从上午 9:50 开始的日内数据,并希望将其转换为 20 分钟的时间间隔,因此第一个时间段为 09:50 至 10:09:59,第二个时间段为从 10:10 到 10:29:59 等等.但是 xts 包中的 to.minutes() 似乎将其固定到小时,并且在 09 有时间条:59:59 和 10:19:59 等等...即它是 10 分钟后...我知道它可能不是一个常规请求...但无论如何这样做,以便它具有正确的端点,即基于第一个时间戳?

I am using intra-day data that starts at 9:50am and would like to convert it into 20 minute time intervals so the first period would be from 09:50 to 10:09:59 and the second time period would be from 10:10 to 10:29:59 etc. However to.minutes() from the xts package seems to fix it onto the hours and the has time bars at 09:59:59 and 10:19:59 etc...i.e. it is 10 minutes out... i know its probably not a regular request...but is there anyway of doing this so that it has the correct endpoints, i.e. basing it upon the frist timestamp?

对于奖励积分......有没有办法根据最终时间戳来做到这一点?(即生成从该时间戳向后的周期端点?

and for bonus points...is there a way to do do it based on the final time stamp? (i.e. generateing the period endpoints going backwards from that timestamp?

这是我的观点的例证,它是 10 分钟(从我想要的)

here is an illustration of my point about it being 10 minutes out (from what I want)

x <- xts(rnorm(24*60*60), as.POSIXct(format(paste(Sys.Date(),'09:50')))-((24*60*60):1))
head(x)
x1 <- to.minutes(x, 20)
head(x1)

我可以想出一种方法来通过使用拆分、剪切、lapplys do.calls 和 rbinds....与现有解决方案的比较...

I can think of a way to correct this by using splits, cuts, lapplys do.calls and rbinds....but I would basically be re-creating an OHLC object...and feel that it might be inefficient in comparison to existing solutions...

推荐答案

我最近遇到了类似的挑战(在下午 5 点开始之前拆分外汇数据).从您的测试数据开始:

I had a similar challenge recently (splitting FX data by the 5pm day start). Starting with your test data:

library(xts)
set.seed(42)
x <- xts(rnorm(24*60*60), as.POSIXct(format(paste(Sys.Date(),'09:50')))-((24*60*60):1))

将其向后移动 10 分钟,进行拆分,然后将拆分的数据向前移动 10 分钟:

Move it back 10 minutes, do the split, then move the split data forward 10 minutes:

offset <- 600
index(x) <- index(x) - offset
x1 <- to.minutes(x, 20)
index(x1) <- index(x1) + offset

(注意,这会破坏 x;要么在副本上工作,要么在之后执行 index(x) <- index(x) + offset).x1 看起来像:

(NB. this corrupts x; either work on a copy or also do index(x) <- index(x) + offset afterwards). x1 looks like:

                        x.Open   x.High     x.Low    x.Close
2012-10-06 10:09:59  1.3709584 3.495304 -3.371739  0.4408241
2012-10-06 10:29:59 -0.7465165 3.584659 -2.828475  0.5938161
2012-10-06 10:49:59  1.3275046 3.174520 -3.199558 -0.6273660
...
2012-10-07 09:09:59 -0.83742490 3.103466 -3.251721 -1.093380
2012-10-07 09:29:59 -0.48464537 3.228048 -3.113351 -1.572931
2012-10-07 09:49:59  1.90503697 3.420940 -3.505207  2.832325

神奇数字 600 的出现是因为您的最后一个滴答声距离前一个 20 分钟的界限是 600 秒.以下是动态计算的方法:

The magic number of 600 came because your last tick was 600 seconds from the previous 20 minute boundary. Here is how you calculate it dynamically:

offset <- ( as.integer(last(index(x))) %% 1200 ) + 1

as.integer 将最后一个刻度的时间转换为 secs-since-1970 形式.(如果时间戳中有毫秒,请使用 as.numeric.)%%1200 向下舍入到 20 分钟边界.最后,+1 是因为 to.minutes 将 XX:XX:00 视为一个小节的 start,而不是 end 前一栏.

as.integer converts the time of the last tick into secs-since-1970 form. (Use as.numeric if you have milliseconds in your timestamps.) %%1200 rounds down to a 20 minute boundary. Finally, the +1 is because to.minutes treats XX:XX:00 as the start of one bar, not the end of the previous bar.

这篇关于to.minutes 使用自定义端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆