library(tidyquant)
## Warning: package 'lubridate' was built under R version 4.0.5
library(tidyverse)

Q1 Get monthly returns of Tesla, Amazon, and Netflix for the last 5 years.

from <- today() - years(5)
monthly_returns <- c("TSLA", "AMZN", "NFLX") %>%
    tq_get(get  = "stock.prices",
           from = from) %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Ra")
monthly_returns
## # A tibble: 180 x 3
## # Groups:   symbol [3]
##    symbol date             Ra
##    <chr>  <date>        <dbl>
##  1 TSLA   2016-05-31 -0.0768 
##  2 TSLA   2016-06-30 -0.0491 
##  3 TSLA   2016-07-29  0.106  
##  4 TSLA   2016-08-31 -0.0970 
##  5 TSLA   2016-09-30 -0.0376 
##  6 TSLA   2016-10-31 -0.0309 
##  7 TSLA   2016-11-30 -0.0421 
##  8 TSLA   2016-12-30  0.128  
##  9 TSLA   2017-01-31  0.179  
## 10 TSLA   2017-02-28 -0.00770
## # ... with 170 more rows

Q2 Get monthly returns of NASDAQ for the same period as the baseline.

NAS_monthly <- "^GSPC" %>%
    tq_get(get  = "stock.prices",
           from = from) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
NAS_monthly
## # A tibble: 60 x 2
##    date              Rb
##    <date>         <dbl>
##  1 2016-05-31  0.00746 
##  2 2016-06-30  0.000911
##  3 2016-07-29  0.0356  
##  4 2016-08-31 -0.00122 
##  5 2016-09-30 -0.00123 
##  6 2016-10-31 -0.0194  
##  7 2016-11-30  0.0342  
##  8 2016-12-30  0.0182  
##  9 2017-01-31  0.0179  
## 10 2017-02-28  0.0372  
## # ... with 50 more rows

Q3 Aggregate for 10 portfolios with the following weighting schemes.

three_stock <- monthly_returns %>%
  tq_repeat_df(n = 10)
three_stock
## # A tibble: 1,800 x 4
## # Groups:   portfolio [10]
##    portfolio symbol date             Ra
##        <int> <chr>  <date>        <dbl>
##  1         1 TSLA   2016-05-31 -0.0768 
##  2         1 TSLA   2016-06-30 -0.0491 
##  3         1 TSLA   2016-07-29  0.106  
##  4         1 TSLA   2016-08-31 -0.0970 
##  5         1 TSLA   2016-09-30 -0.0376 
##  6         1 TSLA   2016-10-31 -0.0309 
##  7         1 TSLA   2016-11-30 -0.0421 
##  8         1 TSLA   2016-12-30  0.128  
##  9         1 TSLA   2017-01-31  0.179  
## 10         1 TSLA   2017-02-28 -0.00770
## # ... with 1,790 more rows
weight <- c(
  0.80, 0.10, 0.10,
  0.10, 0.80, 0.10,
  0.10, 0.10, 0.80,
  0.60, 0.20, 0.20,
  0.20, 0.60, 0.20,
  0.20, 0.20, 0.60,
  0.50, 0.25, 0.25,
  0.25, 0.50, 0.25,
  0.25, 0.25, 0.50,
  0.40, 0.40, 0.20)
stock <- c("TSLA", "AMZN", "NFLX")
three_stock_table <-  tibble(stock) %>%
    tq_repeat_df(n = 10) %>%
    bind_cols(tibble(weight)) %>%
    group_by(portfolio)
three_stock_table
## # A tibble: 30 x 3
## # Groups:   portfolio [10]
##    portfolio stock weight
##        <int> <chr>  <dbl>
##  1         1 TSLA     0.8
##  2         1 AMZN     0.1
##  3         1 NFLX     0.1
##  4         2 TSLA     0.1
##  5         2 AMZN     0.8
##  6         2 NFLX     0.1
##  7         3 TSLA     0.1
##  8         3 AMZN     0.1
##  9         3 NFLX     0.8
## 10         4 TSLA     0.6
## # ... with 20 more rows
portfolio_monthly  <-
  three_stock %>%
    tq_portfolio(assets_col  = symbol, 
                 returns_col = Ra, 
                 weights     = three_stock_table, 
                 col_rename  = "Ra")
portfolio_monthly
## # A tibble: 600 x 3
## # Groups:   portfolio [10]
##    portfolio date              Ra
##        <int> <date>         <dbl>
##  1         1 2016-05-31 -0.0456  
##  2         1 2016-06-30 -0.0515  
##  3         1 2016-07-29  0.0890  
##  4         1 2016-08-31 -0.0682  
##  5         1 2016-09-30 -0.0166  
##  6         1 2016-10-31  0.000531
##  7         1 2016-11-30 -0.0462  
##  8         1 2016-12-30  0.102   
##  9         1 2017-01-31  0.164   
## 10         1 2017-02-28 -0.00158 
## # ... with 590 more rows

Q4 Calcualte the Sharpe Ratio per portfolio.

multi_portfolio <- left_join(portfolio_monthly, 
                                   NAS_monthly,
                                   by = "date")
multi_portfolio
## # A tibble: 600 x 4
## # Groups:   portfolio [10]
##    portfolio date              Ra        Rb
##        <int> <date>         <dbl>     <dbl>
##  1         1 2016-05-31 -0.0456    0.00746 
##  2         1 2016-06-30 -0.0515    0.000911
##  3         1 2016-07-29  0.0890    0.0356  
##  4         1 2016-08-31 -0.0682   -0.00122 
##  5         1 2016-09-30 -0.0166   -0.00123 
##  6         1 2016-10-31  0.000531 -0.0194  
##  7         1 2016-11-30 -0.0462    0.0342  
##  8         1 2016-12-30  0.102     0.0182  
##  9         1 2017-01-31  0.164     0.0179  
## 10         1 2017-02-28 -0.00158   0.0372  
## # ... with 590 more rows
multi_portfolio %>%
  tq_performance(Ra = Ra, Rb = NULL, performance_fun = SharpeRatio.annualized, scale = 12)
## # A tibble: 10 x 2
## # Groups:   portfolio [10]
##    portfolio `AnnualizedSharpeRatio(Rf=0%)`
##        <int>                          <dbl>
##  1         1                           1.24
##  2         2                           1.55
##  3         3                           1.35
##  4         4                           1.40
##  5         5                           1.60
##  6         6                           1.50
##  7         7                           1.47
##  8         8                           1.59
##  9         9                           1.54
## 10        10                           1.53

Q5 Sort the portfolios in descending order of Sharpe Ratio.

Hint: Use dplyr::arrange().

multi_portfolio %>%
  tq_performance(Ra = Ra, Rb = NULL, performance_fun = SharpeRatio.annualized, scale = 12) %>%
  dplyr::arrange(desc(`AnnualizedSharpeRatio(Rf=0%)`))
## # A tibble: 10 x 2
## # Groups:   portfolio [10]
##    portfolio `AnnualizedSharpeRatio(Rf=0%)`
##        <int>                          <dbl>
##  1         5                           1.60
##  2         8                           1.59
##  3         2                           1.55
##  4         9                           1.54
##  5        10                           1.53
##  6         6                           1.50
##  7         7                           1.47
##  8         4                           1.40
##  9         3                           1.35
## 10         1                           1.24

Q6 Which weighting scheme would have performed the best?

Hint: Make your argument using the calculated Sharpe

I found that scheme five works the best because it has a rate of 1.60. Although, scheme eight is a really close second with a rate of 1.59

Q7 Which weighting scheme is most volatile?

Hint: Calculate Beta from the Capital Asset Pricing Model. Make your argument based on the calculated Beta.

multi_portfolio %>%
  tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM) %>%
  t()
##                      [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]
## portfolio          1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000
## ActivePremium      0.4993 0.2800 0.2928 0.4493 0.3218 0.3284 0.4218 0.3411
## Alpha              0.0292 0.0179 0.0236 0.0258 0.0199 0.0232 0.0246 0.0210
## AnnualizedAlpha    0.4131 0.2370 0.3227 0.3582 0.2674 0.3166 0.3384 0.2832
## Beta               1.8133 1.2144 0.9150 1.6288 1.2753 1.0955 1.5262 1.3021
## Beta-              1.5226 0.9928 0.8455 1.3538 1.0559 0.9500 1.2658 1.0816
## Beta+              3.0347 1.6204 1.4080 2.6668 1.8390 1.7107 2.4651 1.9401
## Correlation        0.5185 0.6567 0.4195 0.5708 0.6474 0.5148 0.5889 0.6331
## Correlationp-value 0.0000 0.0000 0.0008 0.0000 0.0000 0.0000 0.0000 0.0000
## InformationRatio   1.0721 1.3201 0.9830 1.2326 1.4029 1.1963 1.2998 1.4007
## R-squared          0.2689 0.4312 0.1760 0.3258 0.4191 0.2650 0.3468 0.4008
## TrackingError      0.4657 0.2121 0.2978 0.3645 0.2294 0.2745 0.3245 0.2435
## TreynorRatio       0.3588 0.3552 0.4854 0.3688 0.3710 0.4379 0.3756 0.3782
##                      [,9]   [,10]
## portfolio          9.0000 10.0000
## ActivePremium      0.3450  0.3908
## Alpha              0.0230  0.0227
## AnnualizedAlpha    0.3139  0.3095
## Beta               1.1867  1.4688
## Beta-              1.0084  1.2175
## Beta+              1.8576  2.2871
## Correlation        0.5543  0.6219
## Correlationp-value 0.0000  0.0000
## InformationRatio   1.2813  1.3632
## R-squared          0.3072  0.3868
## TrackingError      0.2693  0.2867
## TreynorRatio       0.4183  0.3691

Above was calculated through beta! Scheme one can be seen to be the most volatile because its beta is the highest, as well as it also has the largest difference between beta- and beta+.

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.