I designed my own function called SharpeRatio(data)
Where data is an nx2 matrix.
The function works fine for a given matrix dat, however when I try to use rollapply(dat, 20, SharpeRatio)
I get the following error: Error in dat[, 1] : incorrect number of dimensions
The following is the function definition:
SharpeRatio <- function(dat){
Returns = dat[,1]
RiskFree = dat[,2]
ER = (Returns - RiskFree)/100
Volatility = sd(Returns/100)
return((exp(mean(log(1+ER))) - 1)/Volatility)
}
rollapply
works by column unless the by.column=FALSE
argument is used so try this:
rollapply(dat, 20, SharpeRatio, by.column = FALSE)