Load the Data

Let’s load the data from Yahoo using QuantMod.

library(quantmod)
getSymbols("^GSPC", src="yahoo")
## [1] "^GSPC"

Plot the Index

It’s a nice sanity check.

Calculate Sharpe Ratio

library(PerformanceAnalytics)
# 10-year Treasury bond rate (assume constant).
riskFreeRate <- 0.0092
# Calculate log returns
R.SPY <- Return.calculate(xts(GSPC), method="log")
meanLogReturn <- mean(R.SPY$GSPC.Adjusted, na.rm = TRUE)*256
sdLogReturn <- sd(R.SPY$GSPC.Adjusted, na.rm = TRUE)*sqrt(256)
sharpe <- (meanLogReturn - riskFreeRate) / sdLogReturn
# Final result.
round(sharpe, 2)
## [1] 0.29