library(tidyquant)
library(tidyverse)

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

# Import stock prices
from = today() - years(30)
Stocks <-
  tq_get(c("MSFT", "^IXIC", "WMT"), 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 MSFT   1989-11-07 0.538 0.572 0.536 0.562 131542400    0.363
##  2 MSFT   1989-11-08 0.570 0.587 0.569 0.578 106486400    0.373
##  3 MSFT   1989-11-09 0.583 0.587 0.573 0.582 111526400    0.375
##  4 MSFT   1989-11-10 0.583 0.590 0.582 0.587  59168000    0.379
##  5 MSFT   1989-11-13 0.587 0.613 0.582 0.612 178905600    0.395
##  6 MSFT   1989-11-14 0.616 0.620 0.597 0.601  95241600    0.388
##  7 MSFT   1989-11-15 0.602 0.620 0.599 0.615  94492800    0.397
##  8 MSFT   1989-11-16 0.618 0.620 0.599 0.608  68054400    0.392
##  9 MSFT   1989-11-17 0.609 0.613 0.602 0.603  31651200    0.389
## 10 MSFT   1989-11-20 0.602 0.602 0.587 0.601  56937600    0.388
## # … with 22,667 more rows

Q2 Calculate yearly returns.

# Calculate returns.
returns_yearly <-
  Stocks %>%
    tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "yearly")
returns_yearly
## # A tibble: 93 x 3
## # Groups:   symbol [3]
##    symbol date       yearly.returns
##    <chr>  <date>              <dbl>
##  1 MSFT   1989-12-29         0.0741
##  2 MSFT   1990-12-31         0.730 
##  3 MSFT   1991-12-31         1.22  
##  4 MSFT   1992-12-31         0.151 
##  5 MSFT   1993-12-31        -0.0556
##  6 MSFT   1994-12-30         0.516 
##  7 MSFT   1995-12-29         0.436 
##  8 MSFT   1996-12-31         0.883 
##  9 MSFT   1997-12-31         0.564 
## 10 MSFT   1998-12-31         1.15  
## # … with 83 more rows

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

returns_yearly %>% 
  summarise(returns_avg = mean(yearly.returns))
## # A tibble: 3 x 2
##   symbol returns_avg
##   <chr>        <dbl>
## 1 ^IXIC        0.134
## 2 MSFT         0.284
## 3 WMT          0.159

Microsoft has the highest yearly return at .2837, then Walmart at .1593, then Nasdaq Composite at .1337

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

# Compute standard deviation
returns_yearly %>%
    tq_performance(Ra = yearly.returns,
                   Rb = NULL, # Calculataing downside risk measures doesn't require Rb
                   performance_fun = sd)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol  sd.1
##   <chr>  <dbl>
## 1 MSFT   0.408
## 2 ^IXIC  0.277
## 3 WMT    0.326

# See options for the `performance_fun` argument
#tq_performance_fun_options()

Microsoft is the stock with the highest risk based off of standard deviation at .4084, then Walmart at .3256, then Nasdaq composite being the least risky at .2772.

Q5 Is the standard deviation appropriate measure of risk for the three stocks? Calculate skewness and kurtosis, and make your argument using them.

When you are calculation for standard deviation you are underestimating the downside risk. Microsoft is more balanced in terms of Skewness with .22, which means they are more likely to have positive returns, but not as likely as walmart. And Microsoft has the lowest value for kurtosis which would mean its has the least frequent extreme changes in positive and negative returns.

# Compute skewness
returns_yearly %>%
    tq_performance(Ra = yearly.returns,
                   Rb = NULL, # Calculataing downside risk measures doesn't require Rb
                   performance_fun = skewness)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 MSFT        0.228
## 2 ^IXIC       0.181
## 3 WMT         1.36

# Compute kurtosis
returns_yearly %>%
    tq_performance(Ra = yearly.returns,
                   Rb = NULL, # Calculataing downside risk measures doesn't require Rb
                   performance_fun = kurtosis)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol kurtosis.1
##   <chr>       <dbl>
## 1 MSFT        0.249
## 2 ^IXIC       0.362
## 3 WMT         1.35

Q6 Calculate VaR, a downside risk measure, with a confidence interval of 0.99. Which of the three stocks has the greatest downside risk in terms of VaR?

Hint: This is not to be confused with HistoricalVaR you calculated in class. Look for the right code, using tq_performance_fun_options().

# Retrieve performance metrics
returns_yearly %>%
    tq_performance(Ra = yearly.returns,
                   Rb = NULL, # Calculataing downside risk measures doesn't require Rb
                   performance_fun = VaR, p =.99) %>%
  t()
##        [,1]         [,2]         [,3]        
## symbol "MSFT"       "^IXIC"      "WMT"       
## VaR    "-0.5991879" "-0.4842999" "-0.1414682"

The stock with the greatest downside risk based on VaR is Microsoft with -.599. Walmart has the least downside risk at -.141

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

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

returns_yearly %>%
    tq_performance(Ra              = yearly.returns, 
                   performance_fun = SharpeRatio,
                   Rf              = 0.02, p =.99)
## # A tibble: 3 x 4
## # Groups:   symbol [3]
##   symbol `ESSharpe(Rf=2%,p=99… `StdDevSharpe(Rf=2%,p=… `VaRSharpe(Rf=2%,p=…
##   <chr>                  <dbl>                   <dbl>                <dbl>
## 1 MSFT                   0.373                   0.646                0.440
## 2 ^IXIC                  0.196                   0.410                0.235
## 3 WMT                    0.139                   0.428                0.985

I would choose Microsoft because they have the highest ES Sharpe ratio at .3727, and STD DEv Sharpe ration at .645

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.