# Load packages
library(tidyquant)
library(tidyverse)
from <- today() - years(5)
stock_returns_monthly <- c("TSLA", "NFLX", "AMZN") %>%
tq_get(get = "stock.prices",
from = from) %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Ra")
stock_returns_monthly
## # 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
baseline_returns_monthly <- "^IXIC" %>%
tq_get(get = "stock.prices",
from = from) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Rb")
baseline_returns_monthly
## # A tibble: 60 x 2
## date Rb
## <date> <dbl>
## 1 2016-05-31 0.0271
## 2 2016-06-30 -0.0213
## 3 2016-07-29 0.0660
## 4 2016-08-31 0.00990
## 5 2016-09-30 0.0189
## 6 2016-10-31 -0.0231
## 7 2016-11-30 0.0259
## 8 2016-12-30 0.0112
## 9 2017-01-31 0.0430
## 10 2017-02-28 0.0375
## # … with 50 more rows
#
stock_returns_monthly_multi <- stock_returns_monthly %>%
tq_repeat_df(n = 10)
stock_returns_monthly_multi
## # 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
# Assign weights to individual stocks
weights <- 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
)
stocks <- c("TSLA", "NFLX", "AMZN")
weights_table <- tibble(stocks) %>%
tq_repeat_df(n = 10) %>%
bind_cols(tibble(weights)) %>%
group_by(portfolio)
weights_table
## # A tibble: 30 x 3
## # Groups: portfolio [10]
## portfolio stocks weights
## <int> <chr> <dbl>
## 1 1 TSLA 0.8
## 2 1 NFLX 0.1
## 3 1 AMZN 0.1
## 4 2 TSLA 0.1
## 5 2 NFLX 0.8
## 6 2 AMZN 0.1
## 7 3 TSLA 0.1
## 8 3 NFLX 0.1
## 9 3 AMZN 0.8
## 10 4 TSLA 0.6
## # … with 20 more rows
# Aggregate a Portfolio using Vector of Weights
portfolio_returns_monthly <-
stock_returns_monthly_multi %>%
tq_portfolio(assets_col = symbol,
returns_col = Ra,
weights = weights_table,
col_rename = "Ra")
portfolio_returns_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
# Merging Ra and Rb
RaRb_multi_portfolio <- left_join(portfolio_returns_monthly ,
baseline_returns_monthly,
by = "date")
RaRb_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.0271
## 2 1 2016-06-30 -0.0515 -0.0213
## 3 1 2016-07-29 0.0890 0.0660
## 4 1 2016-08-31 -0.0682 0.00990
## 5 1 2016-09-30 -0.0166 0.0189
## 6 1 2016-10-31 0.000531 -0.0231
## 7 1 2016-11-30 -0.0462 0.0259
## 8 1 2016-12-30 0.102 0.0112
## 9 1 2017-01-31 0.164 0.0430
## 10 1 2017-02-28 -0.00158 0.0375
## # … with 590 more rows
# Sharpe Ratio
RaRb_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.35
## 3 3 1.55
## 4 4 1.40
## 5 5 1.50
## 6 6 1.60
## 7 7 1.47
## 8 8 1.54
## 9 9 1.59
## 10 10 1.52
Hint: Use dplyr::arrange().
RaRb_multi_portfolio %>%
tq_performance(Ra = Ra, Rb = NULL, performance_fun = SharpeRatio.annualized, scale = 12) %>%
arrange(desc(`AnnualizedSharpeRatio(Rf=0%)`))
## # A tibble: 10 x 2
## # Groups: portfolio [10]
## portfolio `AnnualizedSharpeRatio(Rf=0%)`
## <int> <dbl>
## 1 6 1.60
## 2 9 1.59
## 3 3 1.55
## 4 8 1.54
## 5 10 1.52
## 6 5 1.50
## 7 7 1.47
## 8 4 1.40
## 9 2 1.35
## 10 1 1.24
Hint: Make your argument using the calculated Sharpe
The portfolio with the highest Sharpe Ratio is portfolio 6, which was 20% weight to both TSLA and AMZN, and 60% weight to NFLX. This shows that Netflix performed better than the other two stocks.Netflix would’ve been the best performing stock out of the three and because it had the most weight in this portfolio, the Sharpe Ratio was the highest for the portfolio comparatively. (This portfolio is the one that gave the most weight to NFLX out of the 10)
Hint: Calculate Beta from the Capital Asset Pricing Model. Make your argument based on the calculated Beta.
RaRb_multi_portfolio <- left_join(portfolio_returns_monthly ,
baseline_returns_monthly,
by = "date")
RaRb_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.0271
## 2 1 2016-06-30 -0.0515 -0.0213
## 3 1 2016-07-29 0.0890 0.0660
## 4 1 2016-08-31 -0.0682 0.00990
## 5 1 2016-09-30 -0.0166 0.0189
## 6 1 2016-10-31 0.000531 -0.0231
## 7 1 2016-11-30 -0.0462 0.0259
## 8 1 2016-12-30 0.102 0.0112
## 9 1 2017-01-31 0.164 0.0430
## 10 1 2017-02-28 -0.00158 0.0375
## # … with 590 more rows
# Beta and Alpha
RaRb_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.4114 0.2048 0.1920 0.3614 0.2405 0.2339 0.3339 0.2571
## Alpha 0.0156 0.0140 0.0083 0.0130 0.0128 0.0096 0.0123 0.0123
## AnnualizedAlpha 0.2036 0.1809 0.1049 0.1678 0.1647 0.1210 0.1577 0.1574
## Beta 1.9160 1.1091 1.3033 1.7496 1.2692 1.3873 1.6545 1.3484
## Beta- 0.3796 0.8608 0.8605 0.6477 0.8389 0.8380 0.7243 0.8265
## Beta+ 2.7223 1.4593 1.5101 2.4442 1.6941 1.7257 2.2830 1.8054
## Correlation 0.6157 0.5714 0.7919 0.6890 0.6702 0.7914 0.7174 0.7077
## Correlationp-value 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## InformationRatio 0.9306 0.7597 1.0832 1.0770 0.9953 1.2155 1.1398 1.0950
## R-squared 0.3790 0.3265 0.6270 0.4748 0.4492 0.6262 0.5147 0.5009
## TrackingError 0.4421 0.2696 0.1773 0.3356 0.2416 0.1924 0.2929 0.2348
## TreynorRatio 0.3396 0.4004 0.3310 0.3433 0.3780 0.3411 0.3464 0.3681
## [,9] [,10]
## portfolio 9.0000 10.0000
## ActivePremium 0.2532 0.3057
## Alpha 0.0102 0.0126
## AnnualizedAlpha 0.1301 0.1615
## Beta 1.4245 1.5198
## Beta- 0.8252 0.7811
## Beta+ 1.8256 2.0994
## Correlation 0.7783 0.7164
## Correlationp-value 0.0000 0.0000
## InformationRatio 1.2238 1.1540
## R-squared 0.6058 0.5133
## TrackingError 0.2069 0.2649
## TreynorRatio 0.3457 0.3586
The most volatile weighting scheme would be portfolio number 1. This one gave 80% weight to Tesla, 10% to Amazon, and 10% to Netflix (0.10). This portfolio has a beta of 1.916, which is well above the rest of them. The higher the beta, the more riskier the stock or portfolio, however it can also result in greater returns. The rest of the portfolios all have betas above 1, which shows they are more sensitive to the market than others, however we can expect this from tech stocks, which make up our portfolios in this example. This also shows that out of the stocks, Tesla would be the most volatile, and this is the portfolio that the TSLA stock has the most weight in, therefore it would be the most volatile portfolio as well.
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.