创建常规时间序列

时间:2022-06-23 12:37:50

How can I create a regular time series of 5 minute OHLC data with volume and Number_Ticks summed? Here's what I've tried:

如何创建一个5分钟OHLC数据的常规时间序列,其中包含Volume和Number_Ticks的总和?这是我尝试过的:

trades <- 
structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
.Label = "26-10-2012", class = "factor"), Time = structure(1:8,
.Label = c("09:15", "09:16", "09:17", "09:18", "09:19", "09:20", "09:21",
"09:22", "09:23", "09:24", "09:25", "09:26", "09:27", "09:28", "09:29", 
"09:30", "09:31", "09:32", "09:33", "09:34", "09:35", "09:36", "09:37",
"09:38", "09:39", "09:40", "09:41", "09:42", "09:43", "09:44", "09:45",
"09:46", "09:47", "09:48", "09:49", "09:50", "09:51", "09:52", "09:53",
"09:54", "09:55", "09:56", "09:57", "09:58", "09:59", "10:00", "10:01",
"10:02", "10:03", "10:04", "10:05", "10:06", "10:07", "10:08", "10:09",
"10:10", "10:11", "10:12", "10:13", "10:14", "10:15", "10:16", "10:17",
"10:18", "10:19", "10:20", "10:21", "10:22", "10:23", "10:24", "10:25", 
"10:26", "10:27", "10:28", "10:29", "10:30", "10:31", "10:32", "10:33",
"10:34", "10:35", "10:36", "10:37", "10:38", "10:39", "10:40", "10:41",
"10:42", "10:43", "10:44", "10:45", "10:46", "10:47", "10:48", "10:49",
"10:50", "10:51", "10:52", "10:53", "10:54"), class = "factor"),
OPEN = c(5720, 5716, 5720.55, 5718.3, 5720.6, 5722, 5723.8, 5724.1),
HIGH = c(5720, 5720, 5721.75, 5720.5, 5722.9, 5727.9, 5726.5, 5725),
LOW = c(5711.25, 5715.85, 5717.05, 5718, 5720.2, 5722, 5723.75, 5721.5),
LAST_PRICE = c(5715.65, 5720, 5718.1, 5720, 5722, 5723.8, 5724.3, 5722.1),
NUMBER_TICKS = c(58L, 57L, 63L, 52L, 54L, 58L, 60L, 59L), VOLUME = c(5146L,
2251L, 2060L, 1368L, 742L, 1771L, 1672L, 1364L)), .Names = c("Date", "Time",
"OPEN", "HIGH", "LOW", "LAST_PRICE", "NUMBER_TICKS", "VOLUME"),
row.names = c("1", "2", "3", "4", "5", "6", "7", "8"), class = "data.frame")
dt_tm <-as.POSIXct(paste(trades[,1], trades[,2],sep=","), format="%d%m%y,%H%M")
cable <- xts(trades[,3:8], order.by=dt_tm)
to.minutes5(cable, indexAt='startof', name=NULL)
#      Open High    Low Close   Volume
# <NA> 5720 6120 5481.2  5917 28089073

1 个解决方案

#1


1  

Look at your dt_tm object. All the values are NA because you specified the format incorrectly.

看看你的dt_tm对象。所有值都是NA,因为您错误地指定了格式。

dt_tm <- as.POSIXct(paste(trades[,1], trades[,2]),
                    format="%d-%m-%Y %H:%M", tz="UTC")
cable <- xts(trades[,3:8], order.by=dt_tm)
colnames(cable)[4] <- "CLOSE"  # in case LAST_PRICE isn't recognized
to.minutes5(cable, indexAt='startof', name=NULL)
#                     Open   High     Low  Close Volume
# 2012-10-26 09:15:00 5720 5722.9 5711.25 5722.0  11567
# 2012-10-26 09:20:00 5722 5727.9 5721.50 5722.1   4807

There's no way to sum the number of ticks at the same time, but you can do it by renaming the column "Volume" and re-running to.minutes5.

无法同时对滴答数进行求和,但您可以通过重命名“Volume”列并重新运行to.minutes5来实现。

cable$VOLUME <- cable$NUMBER_TICKS
to.minutes5(cable, indexAt='startof', name=NULL)
#                     Open   High     Low  Close Volume
# 2012-10-26 09:15:00 5720 5722.9 5711.25 5722.0    284
# 2012-10-26 09:20:00 5722 5727.9 5721.50 5722.1    177

#1


1  

Look at your dt_tm object. All the values are NA because you specified the format incorrectly.

看看你的dt_tm对象。所有值都是NA,因为您错误地指定了格式。

dt_tm <- as.POSIXct(paste(trades[,1], trades[,2]),
                    format="%d-%m-%Y %H:%M", tz="UTC")
cable <- xts(trades[,3:8], order.by=dt_tm)
colnames(cable)[4] <- "CLOSE"  # in case LAST_PRICE isn't recognized
to.minutes5(cable, indexAt='startof', name=NULL)
#                     Open   High     Low  Close Volume
# 2012-10-26 09:15:00 5720 5722.9 5711.25 5722.0  11567
# 2012-10-26 09:20:00 5722 5727.9 5721.50 5722.1   4807

There's no way to sum the number of ticks at the same time, but you can do it by renaming the column "Volume" and re-running to.minutes5.

无法同时对滴答数进行求和,但您可以通过重命名“Volume”列并重新运行to.minutes5来实现。

cable$VOLUME <- cable$NUMBER_TICKS
to.minutes5(cable, indexAt='startof', name=NULL)
#                     Open   High     Low  Close Volume
# 2012-10-26 09:15:00 5720 5722.9 5711.25 5722.0    284
# 2012-10-26 09:20:00 5722 5727.9 5721.50 5722.1    177