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)
# Asset1 .SXQR Asset2 .SXTR Asset3 Asset4.SXNR Asset5 .SXMR .SXAR .SX3R
#MD# MD 0.00512297007662462 0.03089434004811897 0.02469234004427923 0.028327680009964127 0.02017995008533315 0.007904365
#SD# SD 0.09343994044747675 0.26828429051776959 0.21592801055490708 0.241918590594352491 0.20906925078777333 0.036180954
#IS# IS 0.05480000171200000 0.11520000092900000 0.11440000079800000 0.117100000168000000 0.09650000108300000 0.218500000