Aqui una solución utilizando monthlyReturn
e (s)lapply
library(quantmod)
portfolio_monthly_returns=lapply(xts(df[,-1],order.by = df$Date),monthlyReturn)
portfolio_excess_returns <- lapply(portfolio_monthly_returns,Return.excess,
Rf = .0003)
sharpe_ratio_manual <- function(portfolio_excess_returns){
md=mean(portfolio_excess_returns)
sd=StdDev(portfolio_excess_returns)
is=round(
mean(portfolio_excess_returns) / StdDev(portfolio_excess_returns), 4
)
c(MD=md,SD=sd,IS=is)}
sapply(portfolio_excess_returns,sharpe_ratio_manual)
O resultado será similar a:
#> sapply(portfolio_excess_returns,sharpe_ratio_manual)
# .SXQR .SXTR .SXNR .SXMR .SXAR .SX3R
# MD 0.007662462 0.004811897 0.004427923 0.0009964127 0.008533315 0.007904365
# SD 0.044747675 0.051776959 0.055490708 0.0594352491 0.078777333 0.036180954
# IS 0.171200000 0.092900000 0.079800000 0.0168000000 0.108300000 0.218500000