如何在 xts::to.period 中指定 OHLCV 列名?
how to specify OHLCV column names in xts::to.period?
在xts
中进行期间聚合时,如何指定哪些列构成OHLCV?此外,它丢失了我原始数据中的一列 ("window")。
也许有一种方法可以为 to.period
提供自己的聚合函数 - 将不胜感激。
> head(to.period(spy,period="minutes",k=5, indexAt="startof"),5)
spy.Open spy.High spy.Low spy.Close spy.Volume
2016-05-19 06:30:00 60 204.20 204.09 204.02 537530
2016-05-19 06:35:00 60 204.32 204.16 204.23 482436
2016-05-19 06:40:00 60 204.50 204.38 204.39 441800
2016-05-19 06:45:00 60 204.53 204.31 204.20 579161
2016-05-19 06:50:00 60 204.20 203.86 203.72 849998
> head(spy,10)
window open high low close volume
2016-05-19 06:30:00 60 204.030 204.09 203.900 203.91 144840
2016-05-19 06:31:00 60 203.900 204.20 203.900 204.20 94846
2016-05-19 06:32:00 60 204.200 204.23 204.110 204.19 68895
2016-05-19 06:33:00 60 204.180 204.30 204.160 204.18 110701
2016-05-19 06:34:00 60 204.160 204.16 204.020 204.10 118248
2016-05-19 06:35:00 60 204.100 204.16 204.010 204.06 78303
2016-05-19 06:36:00 60 204.060 204.20 204.040 204.19 67314
2016-05-19 06:37:00 60 204.200 204.33 204.140 204.33 147779
2016-05-19 06:38:00 60 204.320 204.33 204.130 204.27 109549
2016-05-19 06:39:00 60 204.270 204.34 204.230 204.24 79491
包 quantmod
中的函数 OHLCV
and/or OHLC
可以帮助您快速选择正确的列:
library(quantmod)
getSymbols("SPY")
# Something like your data:
SPY <- cbind(window = 60, SPY)
# Now correctly select the OHLCV columns:
SPY <- to.period(OHLCV(SPY), period = "months")
tail(SPY)
# OHLCV(SPY).Open OHLCV(SPY).High OHLCV(SPY).Low OHLCV(SPY).Close OHLCV(SPY).Volume
# 2016-03-31 195.01 206.87 194.45 205.52 2323306500
# 2016-04-29 204.35 210.92 203.09 206.33 1910635600
# 2016-05-31 206.92 210.69 202.78 209.84 1831962200
# 2016-06-30 209.12 212.52 198.65 209.48 2612406900
# 2016-07-29 209.48 217.54 207.06 217.12 1648453700
# 2016-08-04 217.19 217.65 214.25 216.41 264076600
# You might want to use the `name` argument to create syntactically valid names
SPY <- to.period(OHLCV(SPY), period = "months", name = "SPY")
tail(SPY)
# SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume
# 2016-03-31 195.01 206.87 194.45 205.52 2323306500
# 2016-04-29 204.35 210.92 203.09 206.33 1910635600
# 2016-05-31 206.92 210.69 202.78 209.84 1831962200
# 2016-06-30 209.12 212.52 198.65 209.48 2612406900
# 2016-07-29 209.48 217.54 207.06 217.12 1648453700
# 2016-08-05 217.19 218.23 214.25 218.18 335650500
注意在标签 "Open"、"High" 等中包含多个列,因为 OHLC 可能会 return 超过 4 列。将 OHLC 列显式重新标记为 "Open"、"High"、"Low"、"Close" 并进行明显的列选择(但输入时间更长)
to.period(SPY[, c("Open", "High", "Low", "Close")], period = "months")
有关如何使用您自己的自定义聚合函数的示例,请参阅 ?period.apply
。
在xts
中进行期间聚合时,如何指定哪些列构成OHLCV?此外,它丢失了我原始数据中的一列 ("window")。
也许有一种方法可以为 to.period
提供自己的聚合函数 - 将不胜感激。
> head(to.period(spy,period="minutes",k=5, indexAt="startof"),5)
spy.Open spy.High spy.Low spy.Close spy.Volume
2016-05-19 06:30:00 60 204.20 204.09 204.02 537530
2016-05-19 06:35:00 60 204.32 204.16 204.23 482436
2016-05-19 06:40:00 60 204.50 204.38 204.39 441800
2016-05-19 06:45:00 60 204.53 204.31 204.20 579161
2016-05-19 06:50:00 60 204.20 203.86 203.72 849998
> head(spy,10)
window open high low close volume
2016-05-19 06:30:00 60 204.030 204.09 203.900 203.91 144840
2016-05-19 06:31:00 60 203.900 204.20 203.900 204.20 94846
2016-05-19 06:32:00 60 204.200 204.23 204.110 204.19 68895
2016-05-19 06:33:00 60 204.180 204.30 204.160 204.18 110701
2016-05-19 06:34:00 60 204.160 204.16 204.020 204.10 118248
2016-05-19 06:35:00 60 204.100 204.16 204.010 204.06 78303
2016-05-19 06:36:00 60 204.060 204.20 204.040 204.19 67314
2016-05-19 06:37:00 60 204.200 204.33 204.140 204.33 147779
2016-05-19 06:38:00 60 204.320 204.33 204.130 204.27 109549
2016-05-19 06:39:00 60 204.270 204.34 204.230 204.24 79491
包 quantmod
中的函数 OHLCV
and/or OHLC
可以帮助您快速选择正确的列:
library(quantmod)
getSymbols("SPY")
# Something like your data:
SPY <- cbind(window = 60, SPY)
# Now correctly select the OHLCV columns:
SPY <- to.period(OHLCV(SPY), period = "months")
tail(SPY)
# OHLCV(SPY).Open OHLCV(SPY).High OHLCV(SPY).Low OHLCV(SPY).Close OHLCV(SPY).Volume
# 2016-03-31 195.01 206.87 194.45 205.52 2323306500
# 2016-04-29 204.35 210.92 203.09 206.33 1910635600
# 2016-05-31 206.92 210.69 202.78 209.84 1831962200
# 2016-06-30 209.12 212.52 198.65 209.48 2612406900
# 2016-07-29 209.48 217.54 207.06 217.12 1648453700
# 2016-08-04 217.19 217.65 214.25 216.41 264076600
# You might want to use the `name` argument to create syntactically valid names
SPY <- to.period(OHLCV(SPY), period = "months", name = "SPY")
tail(SPY)
# SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume
# 2016-03-31 195.01 206.87 194.45 205.52 2323306500
# 2016-04-29 204.35 210.92 203.09 206.33 1910635600
# 2016-05-31 206.92 210.69 202.78 209.84 1831962200
# 2016-06-30 209.12 212.52 198.65 209.48 2612406900
# 2016-07-29 209.48 217.54 207.06 217.12 1648453700
# 2016-08-05 217.19 218.23 214.25 218.18 335650500
注意在标签 "Open"、"High" 等中包含多个列,因为 OHLC 可能会 return 超过 4 列。将 OHLC 列显式重新标记为 "Open"、"High"、"Low"、"Close" 并进行明显的列选择(但输入时间更长)
to.period(SPY[, c("Open", "High", "Low", "Close")], period = "months")
有关如何使用您自己的自定义聚合函数的示例,请参阅 ?period.apply
。