如何从日内数据中估算出高价和低价?

时间:2016-12-19 17:27:33

标签: r xts hft

我有以下数据:

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

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

1 个答案:

答案 0 :(得分: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

(见How can I alter a time series (XTS or ZOO) in R?

选择的时间间隔为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