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.