滚动应用xts。我可以输出窗口中最大值的时间吗?

发布于 2024-12-07 13:53:45 字数 1741 浏览 0 评论 0原文

我正在通过 Quantmod 研究一些雅虎财务数据。

我如何不仅确定数据滚动窗口中的最高和最低价格,而且还确定这些高点和低点的确切时间戳?我已经尝试使用 rollapply 尝试 which.max() ,但这仅报告滚动窗口本身中值的 seq,而不是保存时间戳的行的 .index() 。

有人能提出解决方案吗?

下面是一个可重现的示例,以及我想要的一些示例输出...

> library(quantmod)
> getSymbols("BET.L")
xmin <- rollapply(BET.L$BET.L.Close,10,min, ascending = TRUE)
names(xmin) <- "MinClose"

xmax <- rollapply(BET.L$BET.L.Close,10,max, ascending = TRUE)
names(xmax) <- "MaxClose"

head(cbind(BET.L$BET.L.Close, as.xts(xmax), as.xts(xmin)),15)
           BET.L.Close MaxClose MinClose
2010-10-22     1550.00       NA       NA
2010-10-25     1546.57       NA       NA
2010-10-26     1545.00       NA       NA
2010-10-27     1511.26       NA       NA
2010-10-28     1490.00  1550.00     1395
2010-10-29     1435.00  1546.57     1381
2010-11-01     1447.00  1545.00     1347
2010-11-02     1420.00  1511.26     1347
2010-11-03     1407.00  1490.00     1347
2010-11-04     1395.00  1447.00     1347
2010-11-05     1381.00  1447.00     1347
2010-11-08     1347.00  1490.00     1347
2010-11-09     1415.00  1490.00     1347
2010-11-10     1426.00  1490.00     1347
2010-11-11     1430.00  1490.00     1347

以及我想要生成的输出类型如下所示:

           BET.L.Close MaxClose MinClose    MaxDate    MinDate
2010-10-22     1550.00       NA       NA      NA       NA
2010-10-25     1546.57       NA       NA      NA       NA
2010-10-26     1545.00       NA       NA      NA       NA
2010-10-27     1511.26       NA       NA      NA       NA
2010-10-28     1490.00  1550.00     1395 2010-10-22 2010-11-04
2010-10-29     1435.00  1546.57     1381 2010-10-25 2010-11-05

理想情况下,我采取的任何方法都必须考虑到常见的重复价格这一事实,在这种情况下,我将命令窗口获取第一个最大值和最后一个最小值。

I'm studying some yahoo finance data via quantmod.

How would I determine not only the Max and Min price over a rolling window of data, but also the exact Timestamp of those highs and lows? I have tried which.max() with rollapply but this only reports the seq of the value in the rolling window itself, and not the .index() of the row that holds the timestamp.

Can anyone suggest a solution?

a reproducible example is below, and some sample output I'd like to have...

> library(quantmod)
> getSymbols("BET.L")
xmin <- rollapply(BET.L$BET.L.Close,10,min, ascending = TRUE)
names(xmin) <- "MinClose"

xmax <- rollapply(BET.L$BET.L.Close,10,max, ascending = TRUE)
names(xmax) <- "MaxClose"

head(cbind(BET.L$BET.L.Close, as.xts(xmax), as.xts(xmin)),15)
           BET.L.Close MaxClose MinClose
2010-10-22     1550.00       NA       NA
2010-10-25     1546.57       NA       NA
2010-10-26     1545.00       NA       NA
2010-10-27     1511.26       NA       NA
2010-10-28     1490.00  1550.00     1395
2010-10-29     1435.00  1546.57     1381
2010-11-01     1447.00  1545.00     1347
2010-11-02     1420.00  1511.26     1347
2010-11-03     1407.00  1490.00     1347
2010-11-04     1395.00  1447.00     1347
2010-11-05     1381.00  1447.00     1347
2010-11-08     1347.00  1490.00     1347
2010-11-09     1415.00  1490.00     1347
2010-11-10     1426.00  1490.00     1347
2010-11-11     1430.00  1490.00     1347

and the type of output I would like to generate would look something like:

           BET.L.Close MaxClose MinClose    MaxDate    MinDate
2010-10-22     1550.00       NA       NA      NA       NA
2010-10-25     1546.57       NA       NA      NA       NA
2010-10-26     1545.00       NA       NA      NA       NA
2010-10-27     1511.26       NA       NA      NA       NA
2010-10-28     1490.00  1550.00     1395 2010-10-22 2010-11-04
2010-10-29     1435.00  1546.57     1381 2010-10-25 2010-11-05

Ideally any approach I take must cater for the fact of duplicate prices which is common, and in this case I would order my window to take the first of the max values, and the last of the min values.

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

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

发布评论

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

评论(2

情痴 2024-12-14 13:53:45

这个计划有一个很大的问题。 coredata 元素是一个矩阵,因此所有元素必须是无类的并且具有相同的模式。 xts 对象中不能包含 Date 类的对象,如果您坚持使用字符类,那么它将强制所有其他元素也是字符。因此,理解了这一点,仍然可以通过计算 which.max 结果,然后创建一个以 which.max 值作为偏移量的行号来执行某些操作,最后使用该结果作为对象的索引。 (很抱歉双重使用“which”和“index”。希望代码可以清楚地表达含义。)

xmin <- rollapply(BET.L$BET.L.Close,10,min)
names(xmin) <- "MinClose"

xmax <- rollapply(BET.L$BET.L.Close,10,max, ascending = TRUE)
names(xmax) <- "MaxClose"
head(dat <- cbind(BET.L$BET.L.Close, as.xts(xmax), as.xts(xmin)),15))

w.MaxDate <- rollapply(BET.L$BET.L.Close,10, which.max)
names(w.MaxDate) <- "w.maxdt"
dat <- cbind(dat, as.xts(w.MaxDate) )
dat<-cbind(dat,as.xts(seq.int(236), order.by=index(dat)))
> head(dat)
           BET.L.Close MaxClose MinClose       w.maxdt ..2
2010-10-22     1550.00       NA       NA            NA   1
2010-10-25     1546.57       NA       NA            NA   2
2010-10-26     1545.00       NA       NA            NA   3
2010-10-27     1511.26       NA       NA            NA   4
2010-10-28     1490.00  1550.00     1395             1   5
2010-10-29     1435.00  1546.57     1381             1   6
dat$maxdate <- xts( index(dat)[dat$..2-5+dat$BET.L.Close.1], order.by=index(dat))
> head(dat)
           BET.L.Close MaxClose MinClose       w.maxdt ..2 maxdate
2010-10-22     1550.00       NA       NA            NA   1      NA
2010-10-25     1546.57       NA       NA            NA   2      NA
2010-10-26     1545.00       NA       NA            NA   3      NA
2010-10-27     1511.26       NA       NA            NA   4      NA
2010-10-28     1490.00  1550.00     1395             1   5   14904
2010-10-29     1435.00  1546.57     1381             1   6   14907

所以我得到了日期的整数表示形式。只需查看输入向量的头部,您就可以看到它们是正确的值:

> head(index(dat)[dat$..2-5+dat$w.maxdt])
[1] NA           NA           NA           NA           "2010-10-22" "2010-10-25"

There is a big problem with this plan. The coredata element is a matrix, hence all elements must be classless and of the same mode. You cannot have objects of Date class in an xts object, and if you insist on a character class then it will force all of the other elements to also be character. So with that understood it's still possible to do something by calculating the which.max result, then creating a row-number from which that which.max-value is an offset, and finally using that result as an index into the index of the object. (Sorry for the double uses of "which" and "index". Hope the meaning is clear from the code.)

xmin <- rollapply(BET.L$BET.L.Close,10,min)
names(xmin) <- "MinClose"

xmax <- rollapply(BET.L$BET.L.Close,10,max, ascending = TRUE)
names(xmax) <- "MaxClose"
head(dat <- cbind(BET.L$BET.L.Close, as.xts(xmax), as.xts(xmin)),15))

w.MaxDate <- rollapply(BET.L$BET.L.Close,10, which.max)
names(w.MaxDate) <- "w.maxdt"
dat <- cbind(dat, as.xts(w.MaxDate) )
dat<-cbind(dat,as.xts(seq.int(236), order.by=index(dat)))
> head(dat)
           BET.L.Close MaxClose MinClose       w.maxdt ..2
2010-10-22     1550.00       NA       NA            NA   1
2010-10-25     1546.57       NA       NA            NA   2
2010-10-26     1545.00       NA       NA            NA   3
2010-10-27     1511.26       NA       NA            NA   4
2010-10-28     1490.00  1550.00     1395             1   5
2010-10-29     1435.00  1546.57     1381             1   6
dat$maxdate <- xts( index(dat)[dat$..2-5+dat$BET.L.Close.1], order.by=index(dat))
> head(dat)
           BET.L.Close MaxClose MinClose       w.maxdt ..2 maxdate
2010-10-22     1550.00       NA       NA            NA   1      NA
2010-10-25     1546.57       NA       NA            NA   2      NA
2010-10-26     1545.00       NA       NA            NA   3      NA
2010-10-27     1511.26       NA       NA            NA   4      NA
2010-10-28     1490.00  1550.00     1395             1   5   14904
2010-10-29     1435.00  1546.57     1381             1   6   14907

So I got you the integer representation of the date. You can see that they are the correct values by just looking at the head of the input vector:

> head(index(dat)[dat$..2-5+dat$w.maxdt])
[1] NA           NA           NA           NA           "2010-10-22" "2010-10-25"
临风闻羌笛 2024-12-14 13:53:45

我认为您可以向原始 xts 对象添加一个表示日期的数字列,然后使用 rollapply。查看下面示例中的 xminmax

require(quantmod)
getSymbols("BET.L")

## add Date as numeric
BET.L$dt <- as.numeric(format(index(BET.L), "%Y%m%d"))
xmin <- rollapply(BET.L, 10, align='r', by.column = FALSE,
    FUN = function(dw)
        return(dw[which.min(dw[,'BET.L.Close']), c('BET.L.Close', 'dt')])
    )
xmax <- rollapply(BET.L, 10, align='r', by.column = FALSE,
    FUN = function(dw)
        return(dw[which.max(dw[,'BET.L.Close']), c('BET.L.Close', 'dt')])
    )

xminmax <- cbind(xmin, xmax)
## to get back to dates use: 
##  as.Date(as.character(as.integer(xminmax$dt.xmin)), format = "%Y%m%d")

## left edge of data window
x_ldt <- rollapply(BET.L$dt, 10, align='r', function(dw) return(dw[1]))

结果:

head(xminmax)
           BET.L.Close.xmin  dt.xmin BET.L.Close.xmax  dt.xmax
2010-11-04             1395 20101104          1550.00 20101022
2010-11-05             1381 20101105          1546.57 20101025
2010-11-08             1347 20101108          1545.00 20101026
2010-11-09             1347 20101108          1511.26 20101027
2010-11-10             1347 20101108          1490.00 20101028
2010-11-11             1347 20101108          1447.00 20101101

I think you can add one more numeric column representing date to your original xts object and then use rollapply. Look at xminmax from example below:

require(quantmod)
getSymbols("BET.L")

## add Date as numeric
BET.L$dt <- as.numeric(format(index(BET.L), "%Y%m%d"))
xmin <- rollapply(BET.L, 10, align='r', by.column = FALSE,
    FUN = function(dw)
        return(dw[which.min(dw[,'BET.L.Close']), c('BET.L.Close', 'dt')])
    )
xmax <- rollapply(BET.L, 10, align='r', by.column = FALSE,
    FUN = function(dw)
        return(dw[which.max(dw[,'BET.L.Close']), c('BET.L.Close', 'dt')])
    )

xminmax <- cbind(xmin, xmax)
## to get back to dates use: 
##  as.Date(as.character(as.integer(xminmax$dt.xmin)), format = "%Y%m%d")

## left edge of data window
x_ldt <- rollapply(BET.L$dt, 10, align='r', function(dw) return(dw[1]))

Results:

head(xminmax)
           BET.L.Close.xmin  dt.xmin BET.L.Close.xmax  dt.xmax
2010-11-04             1395 20101104          1550.00 20101022
2010-11-05             1381 20101105          1546.57 20101025
2010-11-08             1347 20101108          1545.00 20101026
2010-11-09             1347 20101108          1511.26 20101027
2010-11-10             1347 20101108          1490.00 20101028
2010-11-11             1347 20101108          1447.00 20101101
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文