Suppose that you consider investing in two stocks: Microsoft and General Electric. As a prudent investor, you analyze the historical performance of the stocks during the period of 1990-01-01 to 2017-12-31.

Q1 Import tidyquant and tidyverse packages.

library(tidyquant)
library(tidyverse)

Q2 Import stock prices of Microsoft and General Electric for the last 20 years.

Hint: Add group_by(symbol) at the end of the code so that calculations below will be done per stock.

from = today() - years(20)
stock <- tq_get(c("GE","MSFT"), get = "stock.prices", from = from) %>%
 group_by(symbol)
stock
## # A tibble: 10,066 x 8
## # Groups:   symbol [2]
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 GE     1999-04-05  36.1  36.8  36.0  36.7 12254700     19.1
##  2 GE     1999-04-06  36.5  36.6  36.0  36.3 14510500     18.8
##  3 GE     1999-04-07  36.6  36.7  35.8  36.7 13840600     19.0
##  4 GE     1999-04-08  36.4  36.7  36.0  36.5 15213400     18.9
##  5 GE     1999-04-09  36.5  36.7  35.8  36.0 13662800     18.7
##  6 GE     1999-04-12  35.5  37.3  35.5  37.3 18080700     19.4
##  7 GE     1999-04-13  36.5  37.6  36.5  37.5 20243500     19.5
##  8 GE     1999-04-14  37.5  37.5  36.5  36.6 15441200     19.0
##  9 GE     1999-04-15  36.4  36.5  35.3  36.1 20533000     18.7
## 10 GE     1999-04-16  35.8  36.0  35.2  35.7 15925700     18.5
## # ... with 10,056 more rows

Q3 Calculate quarterly returns, and save the result under returns_quarterly.

Hint: Take the adjusted variable from Stocks, and calculate quarterly returns using tq_transmute(), instead of tq_mutate(), which is used when periodicity changes. Another difference between the two is that tq_transmute() returns only newly-created columns while tq_mutate() adds new columns to existing variables.

returns_quarterly <- stock %>% tq_transmute(select = close, mutate_fun = periodReturn, period = "quarterly")
returns_quarterly
## # A tibble: 162 x 3
## # Groups:   symbol [2]
##    symbol date       quarterly.returns
##    <chr>  <date>                 <dbl>
##  1 GE     1999-06-30          -0.0142 
##  2 GE     1999-09-30           0.0492 
##  3 GE     1999-12-31           0.305  
##  4 GE     2000-03-31           0.00565
##  5 GE     2000-06-30           0.0217 
##  6 GE     2000-09-29           0.0908 
##  7 GE     2000-12-29          -0.171  
##  8 GE     2001-03-30          -0.127  
##  9 GE     2001-06-29           0.171  
## 10 GE     2001-09-28          -0.241  
## # ... with 152 more rows

Note that there are a variety of functions available with the mutate_fun argument: See above for the result of tq_transmute_fun_options(). Note that tq_mutate allows to calculate returns using quantmod::periodReturn. Google the quantmod package manual for more information on the periodReturn function.

Q4 Create a density plot for returns of both stocks.

Hint: Refer to the ggplot2 cheatsheet. Look for geom_density under One Variable. Use the fill argument to create the plot per each stock.

returns_quarterly %>% ggplot(aes(x = quarterly.returns, fill = symbol)) + geom_density(alpha = 0.3)

Q5 Which of the two stocks appears to have higher risk?

Hint: Risk is measured by the extent of volatility in returns. It has little to do with how high or low of returns are expected.

GE is more concentrated in the middle of the chart but it also has higher tails in both ends of the chart.

Q6 Which of the two stocks would provide the larger expected return adjusted to risk (in terms of standard deviation)?

Hint: Returns adjusted to risk is reflected in Sharpe Ratio. Use tidyquant:: tq_performance to compute Sharpe Ratio. The function returns Sharpe Ratios adjusted to three different types of risks: 1) standard deviation, 2) expected shortfall (5% worst loss), and 3) value at risk (the largest loss at the 95% confidence level).

returns_quarterly %>% tq_performance(Ra = quarterly.returns, performance_fun = SharpeRatio)
## # A tibble: 2 x 4
## # Groups:   symbol [2]
##   symbol `ESSharpe(Rf=0%,p=95~ `StdDevSharpe(Rf=0%,p=~ `VaRSharpe(Rf=0%,p=~
##   <chr>                  <dbl>                   <dbl>                <dbl>
## 1 GE                   -0.0139                 -0.0317              -0.0188
## 2 MSFT                  0.0795                  0.152                0.101