彙總並結合日內資料

在 R 匯入與管理金融資料

Joshua Ulrich

Quantitative Analyst & quantmod Co-Author and Maintainer

時區!

1 Source: https://www.shutterstock.com/video/search/time-zone-clocks
在 R 匯入與管理金融資料

時區!

  • xts 的內部索引是自 UTC 的 1970-01-01 午夜起算的秒數
  • merge() 使用內部索引
  • merge() 的結果會採用第一個物件的時區
在 R 匯入與管理金融資料

時區!

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
在 R 匯入與管理金融資料

建立規律的日內資料

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
在 R 匯入與管理金融資料

建立規律的日內資料

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
在 R 匯入與管理金融資料

篩到交易時段

# 所有觀測值都應為 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
在 R 匯入與管理金融資料

以交易日填補遺漏值

# 用 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)
在 R 匯入與管理金融資料

彙總不規則的日內資料

  • to.period() 彙總稠密的日內資料
    • period:新週期性(例如秒、時、日等)
    • k:每筆新觀測涵蓋的週期數
在 R 匯入與管理金融資料

彙總不規則的日內資料(1)

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
在 R 匯入與管理金融資料
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 匯入與管理金融資料

一起來練習吧!

在 R 匯入與管理金融資料

Preparing Video For Download...