# Comparing daily return and Sharpe ratio of stocks in R using Quantmod package

Risk adjusted returns can be used to compare the performance of each stock or you can assess the overall performance of your portfolio.

Sharpe Ratio for the whole portfolio is calculated as follows:

ARn= annualized return of the stock n
Wn = Weight of the stock n ( if you invested 20% in a stock, the weight would equal 0.2)
ρ​= Risk free return (based on average returns from low risk investments like treasury bonds)
​σavg= weighted average standard deviation of the stocks in the portfolio

If you want to calculate for each stock, you can do so using the following formula:

(Adj. Annualized Return – Risk Free Return)/ standard deviation of the return

In this example, I am going to compare two different portfolios containing stocks with EQUAL weights.

Portfolio 1 : American Express, Coca Cola, UPS, BASF

Portfolio 2 : AT&T, Bank of America, Tesco, Lufthansa

To make a meaningful comparison of the two portfolios, I am not going to include data after the Covid-19 crisis due to very high volatility. Instead, I will compare the performance during the period of Jan-Dec 2019.

R libraries:

library(quantmod)
library(PerformanceAnalytics)

Get daily adjusted returns for each stock symbol like the following example for UPS:

Merge the stocks under two different portfolios:

p1<- merge(AXP,KO,UPS,BASF, all=F)
p2<- merge(ATT,BAC,TCO,LHA, all=F)
colnames(p1) <- c(“AXP”,”KO”,”UPS”,”BASF”)
colnames(p2) <- c(“ATT”,”BAC”,”TCO”,”LHA”)

Drawing charts and getting Sharpe Ratios

charts.PerformanceSummary(p1, event.labels = T,main=”Portfolio 1″)
table.AnnualizedReturns(p1,scale=252,Rf=0.01/252)

charts.PerformanceSummary(p2,event.labels = T, main=”Portfolio 2″)
table.AnnualizedReturns(p2,scale=252,Rf=0.01/252)

Rf= Risk free return ( 1% in this case)

Results:

Comparing Sharpe Ratios of the two portfolios:

To do this, we simply find the average from P1 and P2.
Sharpe Ratio for Portfolio 1 is 117.5% and for Portfolio 2 is 119%. Therefore, the two portfolios actually carry roughly the same performance based on risk adjusted return. However, it should be noted that this does not imply the two portfolios would be equally profitable since we are not including other parameters like dividends. Sharpe ratio also does not tell a complete story with regards to risks because we are only looking at volatility of the returns based on SD here.

In addition, the value also depends on the period you are looking at. Lets take a look at the Tesla chart below. if you calculate the ratio based on the whole year, you would get a very different result than if you instead measure the period from June through Dec 2019 when the stock was going up sharply. During the first 6 months of the year, Tesla stock was going down continuously.