Rolling maximum value (rolling width) rollapply is not working
r, xts
Solution
`cummax` computes the maximum on an expanding (as opposed to rolling) window.
cummax(x)
# [,1]
# 2013-07-07 1.0
# 2013-07-08 1.1
# 2013-07-09 1.2
# 2013-07-10 1.3
# 2013-07-11 1.4
# 2013-07-12 1.4
# 2013-07-13 1.4
Problem
``` x<- xts(c(1, 1.1, 1.2, 1.3, 1.4, -.9, 1.2), Sys.Date()-7:1) ``` in `x` i want a column which comes up with below mentioned result. ``` x [,1] max high 2013-07-07 1.0 1.0 2013-07-08 1.1 1.1 2013-07-09 1.2 1.2 2013-07-10 1.3 1.3 2013-07-11 1.4 1.4 2013-07-12 0.9 1.4 2013-07-13 1.2 1.4 ``` if i use `rollapply function` i have to keep width fixed but i want maximum value till date for each date. `rollapply` is not working. i would like to use something like rollapply(x, [1:i], max) where i will represent no. of row. I've got the answer that to get max.high i should use cummax and its working very well. As i've been barred from putting new questions in this forum, i'm adding my question with this one. I don't know if it the right way, but i don't have other ways to do it. x<- xts(c(100,98,105,100,99,98,96,95,94,93,99,100,106), Sys.Date()-13:1) colnames(x)<- "value" x$max<- cummax(x$value) x$trade<- ifelse(x$max*0.95>=x$value,1,0) x value max trade trade1 2013-07-05 100 100 0 0 2013-07-06 98 100 0 0 2013-07-07 105 105 0 0 2013-07-08 100 105 0 0 2013-07-09 99 105 1 1 2013-07-10 98 105 1 0 2013-07-11 96 105 1 0 2013-07-12 95 105 1 0 2013-07-13 94 105 1 1 2013-07-14 93 105 1 0 2013-07-15 99 105 1 -1 2013-07-16 100 105 0 0 2013-07-17 106 106 0 -1 Till the column trade, my code works but i need another column trade1 which is based on the logic that trade1 column will be 1 if value is atleast less than 5% from column max, and then it will again give me 1 when value column is atleast than 10% from column max, and when the value is up by 5% or more from the value 94 (as the value column was 94 when trade1 column was 1), the trade1 column should give me -1 and again when column value is up by 10% or more from the value 99 (as again the value column was 99 when trade1 column was 1) If any one require more clarity i'll again try to do that. regards Anup