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.

# Import data
from = today() - years(20)
Stocks <- tq_get(c("MSFT", "GE"), get = "stock.prices", from = from) %>%
  group_by(symbol)
Stocks
## # 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 MSFT   1999-04-05  47.2  47.5  46.8  47.5 39848600     34.3
##  2 MSFT   1999-04-06  47.6  47.8  46.6  47.0 39674800     34.0
##  3 MSFT   1999-04-07  47.4  47.5  45.6  46.7 46910200     33.7
##  4 MSFT   1999-04-08  46.6  47.3  45.5  47.3 38637000     34.2
##  5 MSFT   1999-04-09  47.1  47.5  46.5  47.1 31816800     34.1
##  6 MSFT   1999-04-12  45.8  46.8  45.7  46.5 37858800     33.6
##  7 MSFT   1999-04-13  46.6  46.6  44.6  45.1 46746800     32.6
##  8 MSFT   1999-04-14  45.4  45.6  42.9  42.9 52944000     31.0
##  9 MSFT   1999-04-15  43.7  44.9  41.9  44.4 72594600     32.1
## 10 MSFT   1999-04-16  44.5  44.6  42.9  43.3 44774000     31.3
## # ... 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.

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.


returns_quarterly <-
  Stocks %>%
    tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "quarterly") 

returns_quarterly
## # A tibble: 162 x 3
## # Groups:   symbol [2]
##    symbol date       quarterly.returns
##    <chr>  <date>                 <dbl>
##  1 MSFT   1999-06-30          -0.0500 
##  2 MSFT   1999-09-30           0.00416
##  3 MSFT   1999-12-31           0.289  
##  4 MSFT   2000-03-31          -0.0899 
##  5 MSFT   2000-06-30          -0.247  
##  6 MSFT   2000-09-29          -0.246  
##  7 MSFT   2000-12-29          -0.281  
##  8 MSFT   2001-03-30           0.261  
##  9 MSFT   2001-06-29           0.335  
## 10 MSFT   2001-09-28          -0.299  
## # ... with 152 more rows

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.

Because GE has higher density in the middle and less on the sides they are less risky. This means that we have a better idea of where the quarterly returns are going to be compared to Microsoft, where Microsoft is more spreadout.

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 MSFT                 0.0954                   0.180                0.122 
## 2 GE                   0.01000                  0.0221               0.0135

Microsoft would have a much bigger return adjusted to risk, about 10 times that of GE (9.5% compared to .99%). This probably exist because although when looking at density Microsoft has more Risk, they also have more dense higher returns as you move across, and less dense negative returns.