Q1 Import stock prices of Netflix, Tesla and Amazon for the last 5 years.

library(tidyquant)
library(tidyverse)

from <- today() - years(5)
stock_prices <- c("AMZN", "TSLA", "NFLX") %>%
    tq_get(get  = "stock.prices",
           from = from)
stock_prices
## # A tibble: 3,774 x 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 AMZN   2014-11-26  334.  335.  332.  334. 1983600     334.
##  2 AMZN   2014-11-28  336.  341.  336.  339. 3051800     339.
##  3 AMZN   2014-12-01  338.  341.  326.  326  4944900     326 
##  4 AMZN   2014-12-02  328.  328.  323.  326. 2790300     326.
##  5 AMZN   2014-12-03  326.  327.  314.  316. 5681000     316.
##  6 AMZN   2014-12-04  316.  319.  313.  317. 3290300     317.
##  7 AMZN   2014-12-05  317.  317.  311.  313. 3265200     313.
##  8 AMZN   2014-12-08  312.  317.  305.  307. 3639200     307.
##  9 AMZN   2014-12-09  303.  314.  301.  312. 4049500     312.
## 10 AMZN   2014-12-10  312   313.  305.  306. 3245900     306.
## # … with 3,764 more rows

Q2 Calculate monthly returns.

Hint: Use NASDAQ Compsite Index as baseline.

stock_returns_monthly <- stock_prices %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Ra")
stock_returns_monthly
## # A tibble: 183 x 3
## # Groups:   symbol [3]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 AMZN   2014-11-28  0.0152
##  2 AMZN   2014-12-31 -0.0835
##  3 AMZN   2015-01-30  0.142 
##  4 AMZN   2015-02-27  0.0723
##  5 AMZN   2015-03-31 -0.0212
##  6 AMZN   2015-04-30  0.134 
##  7 AMZN   2015-05-29  0.0177
##  8 AMZN   2015-06-30  0.0113
##  9 AMZN   2015-07-31  0.235 
## 10 AMZN   2015-08-31 -0.0434
## # … with 173 more rows

# Baseline Period Returns
baseline_returns_monthly <- "^IXIC" %>%
    tq_get(get  = "stock.prices",
           from = today() - years(5)) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
baseline_returns_monthly
## # A tibble: 61 x 2
##    date              Rb
##    <date>         <dbl>
##  1 2014-11-28  0.000900
##  2 2014-12-31 -0.0116  
##  3 2015-01-30 -0.0213  
##  4 2015-02-27  0.0708  
##  5 2015-03-31 -0.0126  
##  6 2015-04-30  0.00827 
##  7 2015-05-29  0.0260  
##  8 2015-06-30 -0.0164  
##  9 2015-07-31  0.0284  
## 10 2015-08-31 -0.0686  
## # … with 51 more rows

Q3 Aggregate.

Hint: Create 100 portfolios.

num_port = 100

stock_returns_monthly_multi <- stock_returns_monthly %>%
    tq_repeat_df(n = num_port)
stock_returns_monthly_multi
## # A tibble: 18,300 x 4
## # Groups:   portfolio [100]
##    portfolio symbol date            Ra
##        <int> <chr>  <date>       <dbl>
##  1         1 AMZN   2014-11-28  0.0152
##  2         1 AMZN   2014-12-31 -0.0835
##  3         1 AMZN   2015-01-30  0.142 
##  4         1 AMZN   2015-02-27  0.0723
##  5         1 AMZN   2015-03-31 -0.0212
##  6         1 AMZN   2015-04-30  0.134 
##  7         1 AMZN   2015-05-29  0.0177
##  8         1 AMZN   2015-06-30  0.0113
##  9         1 AMZN   2015-07-31  0.235 
## 10         1 AMZN   2015-08-31 -0.0434
## # … with 18,290 more rows

num_stocks <- stock_returns_monthly %>%
  distinct(symbol) %>%
  n_distinct()
num_stocks
## [1] 3

weights <- list()

set.seed(06182019)

for(i in 1:num_port){
  
  weights[[i]] = runif(num_stocks-1, 0, 1)
  
  weights[[i]] = append(weights[[i]], c(0, 1))
  
  weights[[i]] = sort(weights[[i]])
  
  weights[[i]] = diff(weights[[i]])
}
weights[[1]]
## [1] 0.06385721 0.32707606 0.60906672


weights <- unlist(weights)
weights
##   [1] 0.0638572143 0.3270760642 0.6090667215 0.6674315634 0.0835250188
##   [6] 0.2490434179 0.9486860994 0.0501059340 0.0012079666 0.3082285074
##  [11] 0.3311331158 0.3606383768 0.1122350958 0.6053920568 0.2823728474
##  [16] 0.5903503322 0.2067945255 0.2028551423 0.2308615067 0.0173753274
##  [21] 0.7517631659 0.4117328513 0.0418477936 0.5464193551 0.3082213739
##  [26] 0.2995600218 0.3922186042 0.5221402226 0.1136128758 0.3642469016
##  [31] 0.0857777279 0.6071222641 0.3071000080 0.1944349897 0.0029123276
##  [36] 0.8026526826 0.1962747315 0.0904070712 0.7133181973 0.1588861502
##  [41] 0.1306861357 0.7104277140 0.8440541434 0.1414887353 0.0144571213
##  [46] 0.7966482679 0.0351519424 0.1681997897 0.1807254998 0.6042723870
##  [51] 0.2150021133 0.2389462919 0.5176046358 0.2434490724 0.3562180211
##  [56] 0.3205262697 0.3232557091 0.4174840255 0.1583388411 0.4241771335
##  [61] 0.1504051252 0.3887717656 0.4608231091 0.4359975604 0.2236178115
##  [66] 0.3403846282 0.2422919362 0.5420307070 0.2156773568 0.2298394081
##  [71] 0.4745642522 0.2955963397 0.6258799869 0.0470416916 0.3270783215
##  [76] 0.0375024606 0.5680024794 0.3944950600 0.0067017451 0.4451984253
##  [81] 0.5480998296 0.1778505542 0.0676041283 0.7545453175 0.6894674809
##  [86] 0.1230664686 0.1874660505 0.2058625557 0.2131162025 0.5810212418
##  [91] 0.2013536913 0.3865516332 0.4120946755 0.0952859495 0.0685476365
##  [96] 0.8361664140 0.1381823656 0.7144982803 0.1473193541 0.3775856681
## [101] 0.2000978957 0.4223164362 0.0258709076 0.0040990973 0.9700299951
## [106] 0.2257158658 0.4868523953 0.2874317388 0.4520090686 0.0244057050
## [111] 0.5235852264 0.2803844183 0.6376286114 0.0819869703 0.0697063785
## [116] 0.0290425441 0.9012510774 0.2231925349 0.6202506609 0.1565568042
## [121] 0.0009567274 0.6347501962 0.3642930763 0.4652535468 0.5267369885
## [126] 0.0080094647 0.2455690326 0.5717535003 0.1826774671 0.3309030798
## [131] 0.3034736423 0.3656232778 0.2554464112 0.0708831192 0.6736704696
## [136] 0.1052531952 0.7828613615 0.1118854433 0.7046088497 0.0596986255
## [141] 0.2356925248 0.3761585231 0.1659869477 0.4578545291 0.3481856456
## [146] 0.3822073762 0.2696069782 0.5244447065 0.2792275760 0.1963277175
## [151] 0.6065907234 0.2213197036 0.1720895730 0.3427638044 0.5191903980
## [156] 0.1380457976 0.1276013840 0.4892495682 0.3831490478 0.1110758134
## [161] 0.2420915307 0.6468326559 0.0106872793 0.4357936298 0.5535190909
## [166] 0.1927190754 0.4503961648 0.3568847599 0.5162877000 0.4488447069
## [171] 0.0348675931 0.0060355372 0.2789414194 0.7150230433 0.2701046988
## [176] 0.4184351300 0.3114601711 0.2568394218 0.5781026899 0.1650578883
## [181] 0.1619223342 0.4994306306 0.3386470352 0.3610243672 0.0146180678
## [186] 0.6243575651 0.2348479216 0.0849322565 0.6802198219 0.5817097924
## [191] 0.1324886060 0.2858016016 0.1098506262 0.7657505460 0.1243988278
## [196] 0.6334049297 0.1678676454 0.1987274250 0.0912114033 0.7903820183
## [201] 0.1184065784 0.1932190827 0.1458225858 0.6609583315 0.4054246263
## [206] 0.2570355292 0.3375398444 0.3087824071 0.1639304662 0.5272871268
## [211] 0.6402869809 0.0111529650 0.3485600541 0.6183618691 0.0447616260
## [216] 0.3368765048 0.4133743953 0.2577889026 0.3288367020 0.5067144651
## [221] 0.0076246934 0.4856608415 0.7231894799 0.1317818118 0.1450287083
## [226] 0.5244801973 0.2174437300 0.2580760727 0.4000706738 0.3209665995
## [231] 0.2789627267 0.3616019287 0.6374677932 0.0009302781 0.3023019873
## [236] 0.1927107023 0.5049873104 0.1554450225 0.0488181734 0.7957368041
## [241] 0.0097467289 0.1565462507 0.8337070204 0.0703802700 0.8226276855
## [246] 0.1069920445 0.5283486014 0.1723249501 0.2993264485 0.2353959384
## [251] 0.4908058548 0.2737982068 0.9296928791 0.0003363329 0.0699707880
## [256] 0.6333949310 0.2909886991 0.0756163700 0.0715088164 0.2376923505
## [261] 0.6907988330 0.1826897927 0.3067737606 0.5105364467 0.0269268886
## [266] 0.8082488808 0.1648242306 0.1397872302 0.1511347455 0.7090780244
## [271] 0.4692959369 0.4335684299 0.0971356332 0.1340574222 0.7232535251
## [276] 0.1426890527 0.8350737332 0.0936780574 0.0712482093 0.0399914719
## [281] 0.8819474026 0.0780611255 0.1437206552 0.2424383720 0.6138409728
## [286] 0.1119870632 0.4740645280 0.4139484088 0.4601041367 0.2248874852
## [291] 0.3150083781 0.1636324150 0.2519571851 0.5844103999 0.3345076842
## [296] 0.4942367005 0.1712556153 0.7141235650 0.0222984271 0.2635780079


weights_table <-  stock_returns_monthly %>%
  distinct(symbol) %>%
    tq_repeat_df(n = num_port) %>%
    bind_cols(tibble(weights)) %>%
    group_by(portfolio)
weights_table
## # A tibble: 300 x 3
## # Groups:   portfolio [100]
##    portfolio symbol weights
##        <int> <chr>    <dbl>
##  1         1 AMZN   0.0639 
##  2         1 TSLA   0.327  
##  3         1 NFLX   0.609  
##  4         2 AMZN   0.667  
##  5         2 TSLA   0.0835 
##  6         2 NFLX   0.249  
##  7         3 AMZN   0.949  
##  8         3 TSLA   0.0501 
##  9         3 NFLX   0.00121
## 10         4 AMZN   0.308  
## # … with 290 more rows


portfolio_returns_monthly_multi  <-
  stock_returns_monthly_multi %>%
    tq_portfolio(assets_col  = symbol, 
                 returns_col = Ra, 
                 weights     = weights_table, 
                 col_rename  = "Ra")
portfolio_returns_monthly_multi 
## # A tibble: 6,100 x 3
## # Groups:   portfolio [100]
##    portfolio date             Ra
##        <int> <date>        <dbl>
##  1         1 2014-11-28 -0.0121 
##  2         1 2014-12-31 -0.0437 
##  3         1 2015-01-30  0.167  
##  4         1 2015-02-27  0.0562 
##  5         1 2015-03-31 -0.105  
##  6         1 2015-04-30  0.289  
##  7         1 2015-05-29  0.113  
##  8         1 2015-06-30  0.0542 
##  9         1 2015-07-31  0.168  
## 10         1 2015-08-31 -0.00989
## # … with 6,090 more rows

Q4 Calculate performance metrics, CAPM measures.

RaRb_multi_portfolio <- left_join(portfolio_returns_monthly_multi , 
                                   baseline_returns_monthly,
                                   by = "date")
RaRb_multi_portfolio
## # A tibble: 6,100 x 4
## # Groups:   portfolio [100]
##    portfolio date             Ra        Rb
##        <int> <date>        <dbl>     <dbl>
##  1         1 2014-11-28 -0.0121   0.000900
##  2         1 2014-12-31 -0.0437  -0.0116  
##  3         1 2015-01-30  0.167   -0.0213  
##  4         1 2015-02-27  0.0562   0.0708  
##  5         1 2015-03-31 -0.105   -0.0126  
##  6         1 2015-04-30  0.289    0.00827 
##  7         1 2015-05-29  0.113    0.0260  
##  8         1 2015-06-30  0.0542  -0.0164  
##  9         1 2015-07-31  0.168    0.0284  
## 10         1 2015-08-31 -0.00989 -0.0686  
## # … with 6,090 more rows

# Beta and Alpha
#RaRb_multi_portfolio %>%
#  tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM) %>%
#  t()


RaRb_multi_portfolio %>%
  tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM)
## # A tibble: 100 x 13
## # Groups:   portfolio [100]
##    portfolio ActivePremium  Alpha AnnualizedAlpha  Beta `Beta-` `Beta+`
##        <int>         <dbl>  <dbl>           <dbl> <dbl>   <dbl>   <dbl>
##  1         1         0.228 0.0159           0.208  1.37    1.44   1.32 
##  2         2         0.262 0.016            0.210  1.40    1.60   1.37 
##  3         3         0.256 0.0156           0.205  1.39    1.66   1.33 
##  4         4         0.213 0.0138           0.178  1.33    1.46   1.21 
##  5         5         0.134 0.0099           0.126  1.20    1.25   0.905
##  6         6         0.233 0.0143           0.186  1.36    1.55   1.25 
##  7         7         0.299 0.0198           0.266  1.45    1.58   1.53 
##  8         8         0.284 0.0181           0.24   1.43    1.58   1.47 
##  9         9         0.222 0.0144           0.186  1.35    1.47   1.25 
## 10        10         0.261 0.0162           0.213  1.40    1.57   1.38 
## # … with 90 more rows, and 6 more variables: Correlation <dbl>,
## #   `Correlationp-value` <dbl>, InformationRatio <dbl>, `R-squared` <dbl>,
## #   TrackingError <dbl>, TreynorRatio <dbl>

Q5 Find the portfolio that is least affected by adverse market movements.

Hint: Sort the portfolios based on the relevant measure you calcualted in Q4. Use dplyr::arrange(). Discuss your answer based on the measure you used to sort.

RaRb_multi_portfolio %>%
  tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM) %>%
  arrange(desc(Alpha))
## # A tibble: 100 x 13
## # Groups:   portfolio [100]
##    portfolio ActivePremium  Alpha AnnualizedAlpha  Beta `Beta-` `Beta+`
##        <int>         <dbl>  <dbl>           <dbl> <dbl>   <dbl>   <dbl>
##  1        35         0.311 0.0218           0.296  1.46    1.59    1.57
##  2        39         0.303 0.021            0.284  1.45    1.58    1.55
##  3        12         0.304 0.0203           0.273  1.46    1.59    1.54
##  4        32         0.293 0.0201           0.270  1.44    1.56    1.52
##  5        80         0.295 0.0199           0.267  1.45    1.57    1.52
##  6         7         0.299 0.0198           0.266  1.45    1.58    1.53
##  7        81         0.276 0.0195           0.260  1.43    1.53    1.48
##  8        28         0.289 0.0194           0.260  1.44    1.56    1.50
##  9        13         0.283 0.0189           0.252  1.43    1.56    1.48
## 10        62         0.293 0.0189           0.252  1.44    1.59    1.50
## # … with 90 more rows, and 6 more variables: Correlation <dbl>,
## #   `Correlationp-value` <dbl>, InformationRatio <dbl>, `R-squared` <dbl>,
## #   TrackingError <dbl>, TreynorRatio <dbl>

Portfolio 35 is least affected by adverse market movements because it contains the highest alpha of all of the produced portfolios.

Q6 What are the weights of the stocks in the portfolio that is least affected by adverse market movements?

Portfolio 35, AMZN 0.0258709076 2.6% TSLA 0.0040990973 0.4% NFLX 0.9700299951 97%

Q7 Calculate the Sharpe ratios with an annualized risk-free rate of 2% and a confidence interval of 0.99. Which portfolio would yield the greatest return per risk when VaR is used as the measure of risk?

Hint: Use SharpeRatio for the performance_fun argument, instead of SharpeRatio.annualized, which doesn’t work with the Rf and p arguments. Sort the results using dplyr::arrange().

stock_returns_monthly %>% 
    tq_performance(Ra              = Ra, 
                   performance_fun = SharpeRatio,
                   Rf              = 0.02/12, 
                   p               = 0.99)
## # A tibble: 3 x 4
## # Groups:   symbol [3]
##   symbol `ESSharpe(Rf=0.2%,p=… `StdDevSharpe(Rf=0.2%… `VaRSharpe(Rf=0.2%,p…
##   <chr>                  <dbl>                  <dbl>                 <dbl>
## 1 AMZN                  0.141                  0.353                 0.180 
## 2 TSLA                  0.0370                 0.0851                0.0438
## 3 NFLX                  0.193                  0.291                 0.193

Netflix has the highest VaRSharpe ratio with an annualized risk free rate of 0.2% and 99 % confidence. It’s VaRSharpe is 0.19 compared to Amazon’s 0.18 and Tesla’s 0.04. This means that Netflix contains the most return per risk.

Q8 Hide the messages, but display the code and its results on 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.