在 R 中导入与管理金融数据
Joshua Ulrich
Quantitative Analyst & quantmod Co-Author and Maintainer

xts 索引是 UTC 下自 1970-01-01 起的秒数merge() 使用内部索引merge() 结果采用第一个对象的时区datetime <- as.POSIXct("2017-01-18 10:00:00", tz = "UTC")
london <- xts(1, datetime, tzone = "Europe/London")
tokyo <- xts(1, datetime, tzone = "Asia/Tokyo")
merge(london, tokyo)
london tokyo
2017-01-18 10:00:00 1 1
merge(tokyo, london)
tokyo london
2017-01-18 19:00:00 1 1
head(dc_trades)
Price
2016-01-16 08:00:58 20.85
2016-01-16 08:01:56 20.85
2016-01-16 08:03:35 20.85
2016-01-16 08:07:44 20.84
2016-01-16 08:45:58 20.85
2016-01-16 08:46:49 20.85
datetimes <- seq(from = as.POSIXct("2016-01-16 08:00"),
to = as.POSIXct("2016-01-17 18:00"),
by = "1 min")
regular_xts <- xts(, order.by = datetimes)
merged_xts <- merge(dc_trades, regular_xts)
head(merged_xts)
Price
2016-01-16 08:00:00 NA
2016-01-16 08:00:58 20.85
2016-01-16 08:01:00 NA
2016-01-16 08:01:56 20.85
2016-01-16 08:02:00 NA
2016-01-16 08:03:00 NA
# 所有观测应为 NA
all(is.na(merged_xts["2016-01-16 19:00/2016-01-17 07:00"]))
TRUE
# xts 按时段子集
merged_trade_day <- merged_xts["T08:00/T18:00"]
# 现在没有观测
nrow(merged_trade_day["2016-01-16 19:00/2016-01-17 07:00"])
0
split()-lapply()-rbind() 范式:使用 xts/zoo 操作时间序列# 用 split() 将数据分为不重叠的块
trade_day_list <- split(merged_trade_day, "days")
# 对每个块用 lapply() 应用函数
filled_trade_day_list <- lapply(trade_day_list, na.locf)
# 用 do.call() 和 rbind() 合并各块
filled_trade_day <- do.call(rbind, filled_trade_day_list)
to.period() 汇总稠密日内数据period:新频率(如秒、小时、天等)k:每条新观测包含的周期数head(dc_price)
DC.Price
2016-01-16 00:00:07 20.84224
2016-01-16 00:00:08 20.84225
2016-01-16 00:00:08 20.84225
2016-01-16 00:00:11 20.84225
2016-01-16 00:00:25 20.84224
2016-01-16 00:00:44 20.84224
xts_5min <- to.period(dc_price, period = "minutes", k = 5)
head(xts_5min, n = 4)
dc_price.Open dc_price.High dc_price.Low dc_price.Close
2016-01-16 00:03:49 20.84224 20.84227 20.84140 20.84160
2016-01-16 00:09:50 20.84160 20.84160 20.84156 20.84156
2016-01-16 00:14:57 20.84156 20.84156 20.84154 20.84154
2016-01-16 00:19:23 20.84154 20.84154 20.83206 20.83211
xts_aligned <- align.time(xts_5min, n = 60 * 5)
head(xts_aligned, n = 4)
dc_price.Open dc_price.High dc_price.Low dc_price.Close
2016-01-16 00:05:00 20.84224 20.84227 20.84140 20.84160
2016-01-16 00:05:00 20.84160 20.84160 20.84156 20.84156
2016-01-16 00:15:00 20.84156 20.84156 20.84154 20.84154
2016-01-16 00:20:00 20.84154 20.84154 20.83206 20.83211
在 R 中导入与管理金融数据