2

我有以下数据:

dput(head(trade.wide,10))
structure(c(54.7, 54.5, 54.5, 54.6, 54.65, 54.6, 54.65, 54.65, 
54.65, 54.7), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct", 
"POSIXt"), tzone = "", class = c("xts", "zoo"), index = structure(c(1459482300, 
1459482302, 1459482305, 1459482306, 1459482307, 1459482308, 1459482312, 
1459482314, 1459482315, 1459482317), tzone = "", tclass = c("POSIXct", 
"POSIXt")), .Dim = c(10L, 1L), .Dimnames = list(NULL, "PRICE"))
dput(tail(trade.wide,10))
structure(c(84.15, 84.1, 84.1, 84.05, 84.1, 84.05, 84, 84.1, 
84.1, 84.2), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct", 
"POSIXt"), tzone = "", class = c("xts", "zoo"), index = structure(c(1472637583, 
1472637584, 1472637585, 1472637586, 1472637588, 1472637595, 1472637596, 
1472637597, 1472637598, 1472637600), tzone = "", tclass = c("POSIXct", 
"POSIXt")), .Dim = c(10L, 1L), .Dimnames = list(NULL, "PRICE"))

我试图从开始(9:15:00)开始估计 30 分钟内的高低。

df.OHLC<-to.period(trade.wide,period = "minutes", k=30, indexAt="startof")

这就是我得到的:

head(df.OHLC,10)
                    trade.wide.Open trade.wide.High trade.wide.Low trade.wide.Close
2016-04-01 09:15:00           54.70           54.85          54.05            54.65
2016-04-01 09:30:07           54.65           56.50          54.65            56.05
2016-04-01 10:00:02           56.15           56.15          55.75            55.85
2016-04-01 10:30:03           55.80           56.20          55.70            56.10
2016-04-01 11:00:12           56.10           56.35          55.75            55.75
2016-04-01 11:30:12           55.75           55.80          55.40            55.50
2016-04-01 12:00:20           55.50           55.70          55.45            55.60
2016-04-01 12:30:24           55.55           55.75          55.25            55.50
2016-04-01 13:00:10           55.50           56.40          55.35            55.90
2016-04-01 13:30:17           55.85           57.35          55.75            57.20

但是,它的时间戳为 09:15: 00、09:30:07、10:00:02 ……我需要的是09:15:00、09:45:00、10:15:00…… . 我也试过period.max()功能,但它也有类似的问题。

df.OHLC1<- do.call(rbind, lapply(split(trade.wide, "days"),function(x) period.max(x,endpoints(x,on= "minutes",k=30))))

head(df.OHLC1,10)
                     [,1]
2016-04-01 09:29:59 54.85
2016-04-01 09:59:56 56.50
2016-04-01 10:29:53 56.15
2016-04-01 10:59:59 56.20
2016-04-01 11:29:54 56.35
2016-04-01 11:59:52 55.80
2016-04-01 12:29:59 55.70
2016-04-01 12:59:54 55.75
2016-04-01 13:29:45 56.40
2016-04-01 13:59:59 57.35

tail(df.OHLC1)
                     [,1]
2016-08-31 13:29:59 86.55
2016-08-31 13:59:56 86.30
2016-08-31 14:29:59 85.85
2016-08-31 14:59:59 85.15
2016-08-31 15:29:58 84.90
2016-08-31 15:30:00 84.20

我想知道为什么这些功能不成比例地划分时间?请帮我解决这个问题。谢谢

4

1 回答 1

1

使用您的样本数据,我首先得到:

trade.wide <- readRDS("sample")
df.OHLC <- to.period(trade.wide, period = "minutes", k = 30, indexAt = "startof")
head(df.OHLC, n = 4)
##                     trade.wide.Open trade.wide.High trade.wide.Low trade.wide.Close
## 2016-04-01 05:45:00           54.70           54.85          54.05            54.65
## 2016-04-01 06:00:07           54.65           56.50          54.65            56.05
## 2016-04-01 06:30:02           56.15           56.15          55.75            55.85
## 2016-04-01 07:00:03           55.80           56.20          55.70            56.10

给出的时间戳实际上是间隔中出现的第一个时间戳trade.wide。您可以使用以下方法将它们与间隔的实际边界对齐align.time()

aligned <- align.time(df.OHLC, n = 30*60)
head(aligned, n = 4)
##                     trade.wide.Open trade.wide.High trade.wide.Low trade.wide.Close
## 2016-04-01 06:00:00           54.70           54.85          54.05            54.65
## 2016-04-01 06:30:00           54.65           56.50          54.65            56.05
## 2016-04-01 07:00:00           56.15           56.15          55.75            55.85
## 2016-04-01 07:30:00           55.80           56.20          55.70            56.1

aligned中,行由间隔结束时的时间标记,因为align.time向上舍入到下一个 30 分钟的 30 倍。如果要在间隔开始时用时间标记它们,则需要从时间戳中减去 30 分钟,如下所示:

index(aligned) <- index(aligned) - 30*60
head(aligned, n = 4)
##                     trade.wide.Open trade.wide.High trade.wide.Low trade.wide.Close
## 2016-04-01 05:30:00           54.70           54.85          54.05            54.65
## 2016-04-01 06:00:00           54.65           56.50          54.65            56.05
## 2016-04-01 06:30:00           56.15           56.15          55.75            55.85
## 2016-04-01 07:00:00           55.80           56.20          55.70            56.10

(请参阅如何在 R 中更改时间序列(XTS 或 ZOO)?

间隔选择为00:0000:3001:00等。如果您想要 、 、 等间隔00:1500:4501:15可以从时间戳中减去 15 分钟开始,然后按照与上述类似的过程进行操作:

index(trade.wide) <- index(trade.wide) - 15*60
df.OHLC <- to.period(trade.wide, period = "minutes", k = 30, indexAt = "startof")
aligned <- align.time(df.OHLC, n = 30*60)
index(aligned) <- index(aligned) - 15*60
head(aligned, n = 4)
##                     trade.wide.Open trade.wide.High trade.wide.Low trade.wide.Close
## 2016-04-01 05:45:00           54.70           55.65          54.05            55.65
## 2016-04-01 06:15:00           55.60           56.50          55.30            55.90
## 2016-04-01 06:45:00           55.85           56.20          55.70            55.85
## 2016-04-01 07:15:00           55.85           56.35          55.70            55.90
于 2016-12-19T18:40:21.213 回答