to.hourly 添加打开和关闭列

to.hourly adding open and close columns

我有一个包含 OHLC 数据的 data.frame 对象:

head(data,3)
Timestamp           Open   High   Low    Close   Vol
2016-02-05 13:45:00 1161.9 1162.4 1161.7 1161.8  592
2016-02-05 13:50:00 1161.8 1163.2 1161.7 1162.5  643
2016-02-05 13:55:00 1162.5 1164.7 1162.1 1164.5 1072

然后我创建另一个 data.frame 提取高和低列:

x <- data[,c("High","Low")]

给出:

head(x,3)
Timestamp           High   Low
2016-02-05 13:45:00 1162.4 1161.7
2016-02-05 13:50:00 1163.2 1161.7
2016-02-05 13:55:00 1164.7 1162.1

然后转换为每小时:

x <- xts::to.hourly(x, indexAt='startof')  

以某种方式添加回 "Open" 和 "Close" 列,即使它们在 "x" 中不存在:

head(x,3)
Timestamp           x.Open x.High x.Low   x.Close
2016-02-05 13:45:00 1162.4 1164.7 1162.4  1164.7
2016-02-05 14:00:00 1167.2 1176.7 1167.1  1176.7
2016-02-05 15:00:00 1176.3 1176.3 1174.9  1176.2

Open 和 Close 列中的值好像来自 data 而不是 x,但是当我没有将 data 传递给它时,它是如何获得这些值的那个函数?

显然这里有一个简单的解决方法,就是(再次)删除打开和关闭列 post to.hourly 函数,但这是预期的行为,还是我真的错过了什么简单吗?

输出符合预期行为。您正在将 5 分钟小节缩短为每小时小节。 to.hourly 将尝试根据您的输入以较低的频率制作 OHLC 时间序列,而不仅仅是每小时的 HL 时间序列。

to.hourly 是包 xtsto.period 的包装器。根据 to.period 的文档:

Convert an OHLC or univariate object to a specified periodicity lower than the given data object. For example, convert a daily series to a monthly series, or a monthly series to a yearly one, or a one minute series to an hourly series.

The result will contain the open and close for the given period, as well as the maximum and minimum over the new period, reflected in the new high and low, respectively.

您没有显示 13:55:00 之后的 5 分钟柱线,因此 14:00:00 的每小时柱线 1167.2 开盘价是否有意义尚不清楚,因为您不知道,使用只是 HL 数据,无论是最高价还是最低价(用作开盘价的代理)。您必须查看源代码以了解在生成那些每小时柱线开盘价时所做的近似值(仅根据 HL 柱线数据无法从逻辑上判断是对还是错)。如果您正在处理日内柱数据,至少了解 HLC 会有所帮助,而不仅仅是 HL。