I am trying to create a function that buys an N period high. So if I have a vector:
x = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
I want to take the rolling 3 period high. This is how I would like the function to look
x = c(1, 2, 3, 4, 5, 5, 5, 3, 4, 5)
I am trying to do this on an xts object. Here is what I tried:
rollapplyr(SPY$SPY.Adjusted, width = 40, FUN = cummax)
rollapply(SPY$SPY.Adjusted, width = 40, FUN = "cummax")
rapply(SPY$SPY.Adjusted, width = 40, FUN = cummax)
The error I am receiving is:
Error in `dimnames<-.xts`(`*tmp*`, value = dn) :
length of 'dimnames' [2] not equal to array extent
Thanks in advance