R XTS to.minutes5(),未转换为“I"预期的 [英] R XTS to.minutes5(), is not converting as "I" expected

查看:14
本文介绍了R XTS to.minutes5(),未转换为“I"预期的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些 1 分钟的数据转换为 5 分钟的数据,我发现第一次增量需要 4 分钟,然后继续做 5 分钟的增量.

Hi i'm converting some 1 min data to 5 min data, and i'm finding it does 4 mins for the first increment, then goes on to do 5 min increments after that.

我试过搞乱所有的indexAt"参数,但没有一个给我我想要的,从 5 开始,然后是 10、15、20 等等.

I've tried messing around with all the "indexAt" parameters but none give me what i want, which is starting from 5, then 10, 15, 20 etc.

我试过了

x5 <- to.minutes5(x)

x <- to.period(x,
          period = 'minutes', 
          k = 5, 
          OHLC = TRUE)

1 分钟数据

                     Open  High   Low Close Volume
2013-01-16 00:01:00 93.55 93.60 93.54 93.58      5
2013-01-16 00:02:00 93.59 93.60 93.58 93.58      5
2013-01-16 00:03:00 93.59 93.60 93.58 93.58      5
2013-01-16 00:04:00 93.58 93.58 93.57 93.57     12
2013-01-16 00:05:00 93.57 93.57 93.55 93.70     21
2013-01-16 00:06:00 93.56 93.56 93.56 93.56      5
2013-01-16 00:07:00 93.56 93.56 93.55 93.55      3
2013-01-16 00:08:00 93.55 93.55 93.55 93.55      2
2013-01-16 00:09:00 93.55 93.56 93.55 93.56      2
2013-01-16 00:10:00 93.56 93.56 93.56 93.56      1
2013-01-16 00:11:00 93.57 93.57 93.57 93.57      3

转换为 5 分钟后

                    clemtest.Open clemtest.High clemtest.Low clemtest.Close clemtest.Volume
2013-01-16 00:04:00         93.55         93.60        93.54          93.57              27
2013-01-16 00:09:00         93.57         93.57        93.55          93.56              33
2013-01-16 00:14:00         93.56         93.57        93.56          93.57               8
2013-01-16 00:19:00         93.56         93.58        93.51          93.53              77
2013-01-16 00:24:00         93.53         93.55        93.49          93.49             121
2013-01-16 00:29:00         93.49         93.51        93.49          93.51             121

计算是正确的,只是不是从前 5 分钟的数据开始,而是从 4 分钟开始,然后是 5 分钟.

the calculations are correct, its just not starting with the first 5 mins of data, it start with 4 mins, then goes onto 5 mins after that.

(使用 indexAt='startof' 给我正确的 5, 10, 15 ... 但是当检查条形图时,5 分钟数据代表那 5 分钟(例如,分钟 5 - 10)的开始,而不是分钟 0 -5)

(using indexAt='startof' gives me the correct, 5, 10, 15... but when inspecting the bar the 5 min data represents the start of that 5 min (eg min 5 - 10) not min 0 - 5)

这里是1min数据的尾部供参考.

Here is the tail of the 1min data for reference.

                         Open  High   Low Close Volume
2013-01-17 23:53:00 95.52 95.52 95.52 95.52      2
2013-01-17 23:55:00 95.51 95.52 95.51 95.52      2
2013-01-17 23:56:00 95.51 95.51 95.51 95.51      1
2013-01-17 23:57:00 95.52 95.52 95.52 95.52      1
2013-01-17 23:59:00 95.52 95.52 95.51 95.51      4
2013-01-18 00:00:00 95.51 95.51 95.51 95.51      8

推荐答案

这是您要查找的内容吗(仅使用 12 分钟的数据,因为这是您发布的全部内容)?

Is this what you're looking for (using just 12 minutes of data, as that is all you posted)?

x <- read.table(text ="
2013-01-16 00:01:00 93.55 93.60 93.54 93.58      5
2013-01-16 00:02:00 93.59 93.60 93.58 93.58      5
2013-01-16 00:03:00 93.59 93.60 93.58 93.58      5
2013-01-16 00:04:00 93.58 93.58 93.57 93.57     12
2013-01-16 00:05:00 93.57 93.57 93.55 93.70     21
2013-01-16 00:06:00 93.56 93.56 93.56 93.56      5
2013-01-16 00:07:00 93.56 93.56 93.55 93.55      3
2013-01-16 00:08:00 93.55 93.55 93.55 93.55      2
2013-01-16 00:09:00 93.55 93.56 93.55 93.56      2
2013-01-16 00:10:00 93.56 93.56 93.56 93.56      1
2013-01-16 00:11:00 93.57 93.57 93.57 93.57      3")

colnames(x) <- c("Date", "time",  "Open",  "High",   "Low", "Close", "Volume")


xt <- xts(x[, 3:7], order.by = as.POSIXct(paste0(x$Date, x$time, " ")) - 0.000001)



xt5 <- to.period(xt, period = "minutes", k =5)
xt5 <- align.time(xt5, n = 300)
xt5

#                     xt.Open xt.High xt.Low xt.Close xt.Volume
# 2013-01-16 00:05:00   93.55   93.60  93.54    93.70        48
# 2013-01-16 00:10:00   93.56   93.56  93.55    93.56        13
# 2013-01-16 00:15:00   93.57   93.57  93.57    93.57         3

在包含区间 ["2013-01-16 00:00:00", "2013-01-1600:04:59.99999"],你可以将底层时间减少一秒(一个稍微负的数量,这里说-0.000001),这样它就包含在前5分钟内间隔.

To include the bar with stamp "2013-01-16 00:05:00" in the 5 minute bar that includes the interval ["2013-01-16 00:00:00", "2013-01-16 00:04:59.99999"], you could reduce the underlying time by a tiny amount of a second (a slightly negative quantity, here say -0.000001), so that it is included in the first 5 minute interval.

我认为如果您决定说明 OHLC 柱状图数据上的时间戳是在柱状图的开头还是柱状图的结尾,我认为可以避免您的评论中的混淆.即2013-01-16 00:01:00"是指区间(2013-01-16 00:00:00至2013-01-16 00:00:59.999)或(2013-01-16)的OHLC00:01:00, 2013-01-16 00:01:59.999).在您的情况下,它位于栏的末尾(前一种情况).

I think the confusion in your comments is avoided if you make the decision of stating whether the timestamp on the OHLC bar data is at the start of the bar or the end of the bar. i.e. does "2013-01-16 00:01:00" mean the OHLC for the interval (2013-01-16 00:00:00 to 2013-01-16 00:00:59.999) or (2013-01-16 00:01:00, 2013-01-16 00:01:59.999). In your case, it is at the end of the bar (the former case).

将时间戳作为 OHLC 数据条的开始并不是一个好主意,因为当您将不同条间隔上的 xts 对象合并在一起时,它会引入前瞻偏差.

And the timestamp being the start of the bar for OHLC data isn't a good idea as it introduces look forward bias when you merge xts objects on different bar intervals together.

这篇关于R XTS to.minutes5(),未转换为“I"预期的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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