使用 quantmod“to.weekly”聚合每日数据函数创建以周一而非周五结束的每周数据

发布于 2024-12-05 18:05:09 字数 1226 浏览 1 评论 0原文

我正在尝试使用 quantmod 中的“to.weekly”函数将每日股价数据(仅收盘价)汇总为每周股价数据。 xts 对象 foo 保存从 2011 年 1 月 3 日星期一到 2011 年 9 月 20 日星期一结束的股票每日股价数据。为了汇总这些每日数据,我使用了:

tmp <- to。 week(foo)

上述方法的成功之处在于,tmp 现在保存了一系列每周 OHLC 数据点(根据 quantmod 文档)。问题是该系列于 2011 年 1 月 3 日星期一开始,随后的每周也从星期一开始,例如 1 月 10 日星期一、1 月 17 日星期一等。我原本预计本周会默认在周五结束,因此每周系列于 1 月 7 日周五开始,并于 9 月 16 日周五结束。

我尝试过调整数据的开始和结束,并将“endof”或“startof”与indexAt参数一起使用,但我无法让它返回以周五结束的一周。

我很感激收到的任何见解。 (抱歉,我找不到任何方法来附加 dput 文件,因此数据显示在下面)

foo:

2011-01-03 2802
2011-01-04 2841
2011-01-05 2883
2011-01-06 2948
2011-01-07 2993
2011-01-10 2993
2011-01-11 3000
2011-01-12 3000
2011-01-13 3025
2011-01-14 2970
2011-01-17 2954
2011-01-18 2976
2011-01-19 2992
2011-01-20 2966
2011-01-21 2940
2011-01-24 2969
2011-01-25 2996
2011-01-26 2982
2011-01-27 3035
2011-01-28 3075
2011-01-31 3020

tmp:

           foo.Open foo.High foo.Low foo.Close
2011-01-03     2802     2802    2802      2802
2011-01-10     2841     2993    2841      2993
2011-01-17     3000     3025    2954      2954
2011-01-24     2976     2992    2940      2969
2011-01-31     2996     3075    2982      3020

I am trying to aggregate daily share price data (close only) to weekly share price data using the "to.weekly" function in quantmod. The xts object foo holds daily share price data for a stock starting from Monday 3 January 2011 and ending on Monday 20 September 2011. To aggregate this daily data I used:

tmp <- to.weekly(foo)

The above approach succeeds in that tmp now holds a series of weekly OHLC data points, as per the quantmod docs. The problem is that the series begins on Monday 3 January 2011 and each subsequent week also begins on Monday e.g. Monday 10 January, Monday 17 January and so on. I had expected the week to default to ending on Friday so that the weekly series started on Friday 7 January and ended on Friday 16 September.

I have experimented with adjusting the start and end of the data and using 'endof' or 'startof' together with the indexAt parameter but I cannot get it to return a week ending in Friday.

I am grateful for any insights received.
(Sorry, I could not find any way to attach dput file so data appears below)

foo:

2011-01-03 2802
2011-01-04 2841
2011-01-05 2883
2011-01-06 2948
2011-01-07 2993
2011-01-10 2993
2011-01-11 3000
2011-01-12 3000
2011-01-13 3025
2011-01-14 2970
2011-01-17 2954
2011-01-18 2976
2011-01-19 2992
2011-01-20 2966
2011-01-21 2940
2011-01-24 2969
2011-01-25 2996
2011-01-26 2982
2011-01-27 3035
2011-01-28 3075
2011-01-31 3020

tmp:

           foo.Open foo.High foo.Low foo.Close
2011-01-03     2802     2802    2802      2802
2011-01-10     2841     2993    2841      2993
2011-01-17     3000     3025    2954      2954
2011-01-24     2976     2992    2940      2969
2011-01-31     2996     3075    2982      3020

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪 2024-12-12 18:05:09

我想出了一些只产生收盘值的东西,也许可以进一步修改它以返回 OHLC 系列。

假设 foo 是一个 xts 对象,首先我们创建星期五的索引向量:

fridays = as.POSIXlt(time(foo))$wday == 5

然后我们在它前面加上 0

indx <- c(0, which(fridays))

并使用 period.apply

period.apply(foo, INDEX=indx, FUN=last)

结果:

          [,1]
2011-01-07 2993
2011-01-14 2970
2011-01-21 2940
2011-01-28 3075

I've come up with something yielding only Close values, perhaps it can be hacked further to return OHLC series.

Assuming that foo is an xts object, first we create the vector of indeces of Fridays:

fridays = as.POSIXlt(time(foo))$wday == 5

Then we prepend it with 0:

indx <- c(0, which(fridays))

And use period.apply:

period.apply(foo, INDEX=indx, FUN=last)

Result:

          [,1]
2011-01-07 2993
2011-01-14 2970
2011-01-21 2940
2011-01-28 3075
浪荡不羁 2024-12-12 18:05:09

对于周五(由于市场休市,偶尔会出现周四),请使用:

tmp = to.weekly(foo, indexAt = "endof") 

对于周一(由于市场休市,偶尔会出现周二),请使用:

tmp = to.weekly(foo, indexAt = "startof")`

或者,您可以创建一个包含日期的 Date 自定义向量与每周相关。例如,无论市场休市情况如何,强制每周与星期五相关联:

customIdx = seq(from = as.Date("2011-01-07"), by = 7, length.out = nrow(tmp))
索引(tmp) = 自定义Idx

For Fridays (with occasional Thursdays due to market closures), use:

tmp = to.weekly(foo, indexAt = "endof") 

For Mondays (with occasional Tuesdays due to market closures), use:

tmp = to.weekly(foo, indexAt = "startof")`

Or you can create a custom vector of Dates that contains the date to be associated with each week. For instance, to force every week to be associated with Friday regardless of market closures:

customIdx = seq(from = as.Date("2011-01-07"), by = 7, length.out = nrow(tmp))
index(tmp) = customIdx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文