如何以累积方式将 xts 转换为较低频率 [英] How to transform xts to lower frequency in a cumulative way

查看:31
本文介绍了如何以累积方式将 xts 转换为较低频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以累积方式将 xts 时间序列数据转换为较低的周期性.

I am trying to convert xts time series data to lower periodicity in a cumulative way.

例如,对来自 xts 包的样本数据 (sample_matrix) 使用 to.weekly 我得到:

For example, using to.weekly on sample data (sample_matrix) from xts package I get this:

library(xts)
data(sample_matrix)
to.weekly(as.xts(sample_matrix), name="")

> to.weekly(as.xts(sample_matrix), name="")
              .Open    .High     .Low   .Close
2007-01-08 50.03978 50.42188 49.95041 49.98806
2007-01-15 49.99489 50.68583 49.80454 50.48912
.....

我希望能够使用稍微不同的函数 to.weekly.cumulative 来代替返回:

I would like to be able to use a slightly different function to.weekly.cumulative that would instead return this:

> to.weekly.cumulative(as.xts(sample_matrix), name="")
              .Open    .High     .Low   .Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.03978 50.42188 49.95041 50.39767
2007-01-04 50.03978 50.42188 49.95041 50.33236
2007-01-05 50.03978 50.42188 49.95041 50.33459
2007-01-06 50.03978 50.42188 49.95041 50.18112
2007-01-07 50.03978 50.42188 49.95041 49.99185
2007-01-08 50.03978 50.42188 49.95041 49.98806
2007-01-09 49.99489 49.99489 49.80454 49.91333
2007-01-10 49.99489 50.13053 49.80454 49.97246
2007-01-11 49.99489 50.23910 49.80454 50.23910
2007-01-12 49.99489 50.35980 49.80454 50.28519
2007-01-13 49.99489 50.48000 49.80454 50.41286
2007-01-14 49.99489 50.62395 49.80454 50.60145
2007-01-15 49.99489 50.68583 49.80454 50.48912
....

此函数不会仅返回端点的数据,而是返回 xts 对象中所有行的数据.例如,我想要获得的是每日柱状图(或 1 分钟柱状图的 15 分钟柱状图)中的每周柱状图,这样我就可以每天(或每分钟)获得当前(从那天/分钟开始)的发展每周(15 分钟)条形图.所以,在星期一我会得到一个每周的酒吧,就像他们在星期一一样开盘、高、低、收盘,周二开盘将从周一开始,收盘将从周二开始,如果高于周一高点,高点将是周二高点如果低于周一低点,低点将是周二低点......周五我将从周一开盘,高点为全周高点,低点为全周低点,周五收盘.如果我使用 xts 的 to.weekly 函数,周五的数据应该是一样的.

This function would not return the data only for the endpoints but for all the rows in the xts object. For example, what I am trying to get is a weekly bars from daily bars (or 15 minute bars from 1 minute bars) in such a way that for every day (or minute) I get the current (from that day/minute) development of the weekly (15 minute) bar. So, on Monday I would get as a weekly bar Open, High, Low, Close exactly as they were on Monday, on Tuesday Open will be from Monday, Close will be from Tuesday, and High would be Tuesday High if greater than Monday High and Low would be Tuesday Low if lower than Monday Low ... and on Friday I would get Open from Monday, High as High of the whole week, Low the low of the whole week and Close Close of Friday. Data fro Friday should be the same if I would use to.weekly function from xts.

所以,基本上,这不仅仅是端点处的数据(如 xts to.weekly 所做的),而是原始 xts 对象周期性可用的所有时间步长.所以不知何故,这是一部关于每周酒吧发展的电影(每天我都会到达每周结束时每周酒吧的位置).

So, basically, this is not just the data at the endpoints (like xts to.weekly does) but all along the time steps available at the periodicity of the original xts object. So somehow a movie of the development of weekly bar through days (on each day I get where the weekly bar stands on close of each week).

怎么做(如何写函数 to.weekly.cumulative?)?

How to do that (How to write function to.weekly.cumulative?)?

高度赞赏有关如何执行此操作的示例.

Examples on how to do this highly appreciated.

试图根据 DWin 评论进行更多解释.

Tried to explain a bit more based on DWin comment.

推荐答案

按周"拆分,对每周的数据应用自定义函数,然后再绑定结果

Split on "weeks", apply a custom function to each week's data, then rbind the results

to.weekly.cumulative <- function(xts.obj, name="") {
     out <- do.call(rbind, 
                   lapply(split(xts.obj, 'weeks'), 
                       function(x) cbind(rep(first(x[,1]), NROW(x[,1])), 
                                   cummax(x[,2]), cummin(x[,3]), x[,4])))
     colnames(out) <- paste(name, c("Open", "High", "Low", "Close"), sep=".")
     out
}

> library(quantmod)
> data(sample_matrix)
> myxts <- as.xts(sample_matrix)


> head(to.weekly.cumulative(myxts), 15)
              .Open    .High     .Low   .Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.03978 50.42188 49.95041 50.39767
2007-01-04 50.03978 50.42188 49.95041 50.33236
2007-01-05 50.03978 50.42188 49.95041 50.33459
2007-01-06 50.03978 50.42188 49.95041 50.18112
2007-01-07 50.03978 50.42188 49.95041 49.99185
2007-01-08 50.03555 50.10363 49.96971 49.98806
2007-01-09 50.03555 50.10363 49.80454 49.91333
2007-01-10 50.03555 50.13053 49.80454 49.97246
2007-01-11 50.03555 50.23910 49.80454 50.23910
2007-01-12 50.03555 50.35980 49.80454 50.28519
2007-01-13 50.03555 50.48000 49.80454 50.41286
2007-01-14 50.03555 50.62395 49.80454 50.60145
2007-01-15 50.61724 50.68583 50.47359 50.48912
2007-01-16 50.61724 50.73731 50.47359 50.67835

这篇关于如何以累积方式将 xts 转换为较低频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆