library(tidyquant)
library(tidyverse)

Q1 Import stock prices of NASDAQ Compsite Index, Microsoft and Walmart for the last 30 years.

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

# Import stock prices
from = today() - years(30)
Stocks <- 
  tq_get(c("^IXIC", "WMT", "MSFT"), get = "stock.prices", from = from) %>%
  group_by(symbol)
Stocks
## # A tibble: 22,677 x 8
## # Groups:   symbol [3]
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 ^IXIC  1990-04-24  422.  422.  419.  419. 126790000     419.
##  2 ^IXIC  1990-04-25  420   421.  419.  421. 121710000     421.
##  3 ^IXIC  1990-04-26  422.  422   419.  421. 115930000     421.
##  4 ^IXIC  1990-04-27  421.  421.  418.  418  116010000     418 
##  5 ^IXIC  1990-04-30  418.  420.  417   420. 105790000     420.
##  6 ^IXIC  1990-05-01  422   422.  421.  422. 124130000     422.
##  7 ^IXIC  1990-05-02  423.  424.  422.  424. 143260000     424.
##  8 ^IXIC  1990-05-03  425   427.  424.  425. 160850000     425.
##  9 ^IXIC  1990-05-04  427.  429.  426.  429. 136810000     429.
## 10 ^IXIC  1990-05-07  429.  432.  429.  431. 122690000     431.
## # … with 22,667 more rows

Q2 Calculate yearly returns.

Hint: Take the adjusted variable from Stocks, and calculate yearly returns using ***tq_transmute().

# Calculate returns. 
returns_annually <- 
  Stocks %>%
    tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "yearly")
returns_annually
## # A tibble: 93 x 3
## # Groups:   symbol [3]
##    symbol date       yearly.returns
##    <chr>  <date>              <dbl>
##  1 ^IXIC  1990-12-31        -0.108 
##  2 ^IXIC  1991-12-31         0.569 
##  3 ^IXIC  1992-12-31         0.155 
##  4 ^IXIC  1993-12-31         0.147 
##  5 ^IXIC  1994-12-30        -0.0320
##  6 ^IXIC  1995-12-29         0.399 
##  7 ^IXIC  1996-12-31         0.227 
##  8 ^IXIC  1997-12-31         0.216 
##  9 ^IXIC  1998-12-31         0.396 
## 10 ^IXIC  1999-12-31         0.856 
## # … with 83 more rows

Q3 Which of the three stocks has the highest expected yearly return?

Hint: Take returns_yearly and pipe it to summarise. Calculate the mean yearly returns.

returns_annually %>% 
  summarise(returns_avg = mean(yearly.returns))
## # A tibble: 3 x 2
##   symbol returns_avg
##   <chr>        <dbl>
## 1 ^IXIC        0.137
## 2 MSFT         0.275
## 3 WMT          0.160

Q4 Calculate standard deviation of the yearly returns. Which of the three stocks is the riskiest in terms of standard deviation?

Hint: Take returns_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute sd (standard deviation).

# Compute standard deviation
returns_annually %>%
    tq_performance(Ra = yearly.returns,
                   Rb = NULL,
                   performance_fun = sd)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol  sd.1
##   <chr>  <dbl>
## 1 ^IXIC  0.278
## 2 WMT    0.328
## 3 MSFT   0.403

Q5 Is the standard deviation appropriate measure of risk for the three stocks? Calculate skewness and kurtosis, and discuss them in your answer.

Hint: when the return distribution is not normal, the standard deviation is not an appropriate measure of risk. One can use skewness and kurtosis to detect non-normal returns. Take returns_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute skewness. Do the same for kurtosis.

# Compute skewness
returns_annually %>%
    tq_performance(Ra = yearly.returns,
                   Rb = NULL, 
                   performance_fun = skewness)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 ^IXIC       0.179
## 2 WMT         1.42 
## 3 MSFT        0.272

# Compute kurtosis
returns_annually %>%
    tq_performance(Ra = yearly.returns,
                   Rb = NULL, 
                   performance_fun = kurtosis)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol kurtosis.1
##   <chr>       <dbl>
## 1 ^IXIC       0.304
## 2 WMT         1.52 
## 3 MSFT        0.424
# density plot
returns_annually %>%
  ggplot(aes(x = yearly.returns, fill = symbol)) +
  geom_density(alpha = 0.3)

Q6 Which of the three stocks poses greater downside risk? Calculate HistoricalES(95%), HistoricalVaR(95%), and SemiDeviation, and discuss them in your answer.

Hint: Take returns_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute table.DownsideRisk.

# Retrieve performance metrics
returns_annually %>%
    tq_performance(Ra = yearly.returns,
                   Rb = NULL, 
                   performance_fun = table.DownsideRisk) %>%
  t()
##                                           [,1]      [,2]      [,3]     
## symbol                                    "^IXIC"   "WMT"     "MSFT"   
## DownsideDeviation(0%)                     "0.1244"  "0.0829"  "0.1448" 
## DownsideDeviation(MAR=0.833333333333333%) "0.1279"  "0.0866"  "0.1475" 
## DownsideDeviation(Rf=0%)                  "0.1244"  "0.0829"  "0.1448" 
## GainDeviation                             "0.2050"  "0.3157"  "0.3227" 
## HistoricalES(95%)                         "-0.3991" "-0.2471" "-0.5362"
## HistoricalVaR(95%)                        "-0.3541" "-0.2197" "-0.3317"
## LossDeviation                             "0.1599"  "0.0885"  "0.2388" 
## MaximumDrawdown                           "0.6718"  "0.3206"  "0.6285" 
## ModifiedES(95%)                           "-0.4068" "-0.5226" "-0.4907"
## ModifiedVaR(95%)                          "-0.2974" "-0.2183" "-0.3419"
## SemiDeviation                             "0.1891"  "0.1707"  "0.2663"

Q7 Which of the three stocks would you choose? Calculate the Sharpe ratios with an annualized risk-free rate of 2% and a default confidence interval of 0.95.

Hint: Make your argument based on the three Sharpe Ratios.

returns_annually %>%
    tq_performance(Ra = yearly.returns,
                   Rb = NULL,
                   performance_fun = SharpeRatio,
                   Rf = 0.02) 
## # A tibble: 3 x 4
## # Groups:   symbol [3]
##   symbol `ESSharpe(Rf=2%,p=95%… `StdDevSharpe(Rf=2%,p=95… `VaRSharpe(Rf=2%,p=95…
##   <chr>                   <dbl>                     <dbl>                  <dbl>
## 1 ^IXIC                   0.287                     0.420                  0.392
## 2 WMT                     0.267                     0.426                  0.639
## 3 MSFT                    0.519                     0.633                  0.745

Q7.a Repeat Q7 but at a confidence interval of 0.99. Does it change your answer in Q7?

Hint: Make your argument based on the three Sharpe Ratios.

returns_annually %>%
    tq_performance(Ra = yearly.returns,
                   Rb = NULL,
                   performance_fun = SharpeRatio,
                   p = 0.99, Rf = 0.02) 
## # A tibble: 3 x 4
## # Groups:   symbol [3]
##   symbol `ESSharpe(Rf=2%,p=99%… `StdDevSharpe(Rf=2%,p=99… `VaRSharpe(Rf=2%,p=99…
##   <chr>                   <dbl>                     <dbl>                  <dbl>
## 1 ^IXIC                   0.205                     0.420                  0.243
## 2 WMT                     0.140                     0.426                  1.13 
## 3 MSFT                    0.364                     0.633                  0.428

Q8 Hide the messages and the code, but display results of the code from the webpage.

Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.

Q9 Display the title and your name correctly at the top of the webpage.

Q10 Use the correct slug.