# Sharpe Ratio of ETFs in R

Suppose I want to calculate the Sharpe ratio given a risk free rate of 0.05 for a portfolio consisting of assets from 500 ETFs. How can I do so in R given the data I've collected thus far in my R code? The following is a part of my code:

#To extract ETF-prices for each column
prices <- data.frame(ETF=ETF,row.names = dates)
head(prices)
# Function for calculating continous returns & log returns
returnscalc <- function(x){
diff(x)/x[-length(x)]
}
returns <- apply(prices, 2, returnscalc)
head(returns)
# Function for calculating geometric mean
geomAveCalc <- function(x){
(prod((1+x)))^(1/length(x))-1
}

weeklymean <- apply(returns, 2, geomAveCalc)
yearlymean <- matrix((1+weeklymean)^52-1)
yearlystd <-  sqrt (52 * apply(returns, 2, var))
yearlycov <- 52 * cov(returns)


where ETF is the data extracted from the .csv file containing the ETFs data.

Some help will be deeply appreciated.

## 1 Answer

try:

library(PerformanceAnalytics)
SharpeRatio.annualized(Returns, Rf = 0.05, scale = 252, geometric = TRUE)