[英]R xts Time series, subset by last workday of the week
通過to.weekly
將時間序列轉換為每周OHLC。 每周收盤價應該是本周的最后一個交易日 - 無論是周四還是周三。
例如,2014年7月4日(美國假期)是在星期五,因此那天沒有交易。 以下代碼返回6/27(星期五),7/3(星期四收盤)和7/11(星期五)的觀察結果。
getSymbols("SPY")
Cl(to.weekly(SPY))['2014-06-27/2014-07-11']
# SPY.Close
# 2014-06-27 195.82
# 2014-07-03 198.20
# 2014-07-11 196.61
如果您的數據僅包含工作日(沒有周末),那么您可以使用:
require(quantmod)
getSymbols("SPY")
spy <- do.call(rbind, lapply(split(SPY, "weeks"), last))
table(weekdays(index(spy)))
# Friday Thursday
# 436 17
如果您的數據包含周末,那么您需要在執行split
, lapply
, rbind
之前刪除它們。
data(sample_matrix)
x <- as.xts(sample_matrix)
x <- x[.indexwday(x) %in% 1:5]
y <- do.call(rbind, lapply(split(x, "weeks"), last))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.