# Load packages
library(tidyquant)
library(tidyverse)
from <- today() - years(5)
stock_returns_monthly <- 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")
stock_returns_monthly
## # A tibble: 183 x 3
## # Groups: symbol [3]
## symbol date Ra
## <chr> <date> <dbl>
## 1 TSLA 2016-04-29 0
## 2 TSLA 2016-05-31 -0.0728
## 3 TSLA 2016-06-30 -0.0491
## 4 TSLA 2016-07-29 0.106
## 5 TSLA 2016-08-31 -0.0970
## 6 TSLA 2016-09-30 -0.0376
## 7 TSLA 2016-10-31 -0.0309
## 8 TSLA 2016-11-30 -0.0421
## 9 TSLA 2016-12-30 0.128
## 10 TSLA 2017-01-31 0.179
## # ... with 173 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: 61 x 2
## date Rb
## <date> <dbl>
## 1 2016-04-29 0
## 2 2016-05-31 0.0362
## 3 2016-06-30 -0.0213
## 4 2016-07-29 0.0660
## 5 2016-08-31 0.00990
## 6 2016-09-30 0.0189
## 7 2016-10-31 -0.0231
## 8 2016-11-30 0.0259
## 9 2016-12-30 0.0112
## 10 2017-01-31 0.0430
## # ... with 51 more rows
#
stock_returns_monthly_multi <- stock_returns_monthly %>%
tq_repeat_df(n = 10)
stock_returns_monthly_multi
## # A tibble: 1,830 x 4
## # Groups: portfolio [10]
## portfolio symbol date Ra
## <int> <chr> <date> <dbl>
## 1 1 TSLA 2016-04-29 0
## 2 1 TSLA 2016-05-31 -0.0728
## 3 1 TSLA 2016-06-30 -0.0491
## 4 1 TSLA 2016-07-29 0.106
## 5 1 TSLA 2016-08-31 -0.0970
## 6 1 TSLA 2016-09-30 -0.0376
## 7 1 TSLA 2016-10-31 -0.0309
## 8 1 TSLA 2016-11-30 -0.0421
## 9 1 TSLA 2016-12-30 0.128
## 10 1 TSLA 2017-01-31 0.179
## # ... with 1,820 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", "AMZN", "NFLX")
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 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
# 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: 610 x 3
## # Groups: portfolio [10]
## portfolio date Ra
## <int> <date> <dbl>
## 1 1 2016-04-29 0
## 2 1 2016-05-31 -0.0347
## 3 1 2016-06-30 -0.0516
## 4 1 2016-07-29 0.0886
## 5 1 2016-08-31 -0.0675
## 6 1 2016-09-30 -0.0161
## 7 1 2016-10-31 0.00120
## 8 1 2016-11-30 -0.0463
## 9 1 2016-12-30 0.101
## 10 1 2017-01-31 0.163
## # ... with 600 more rows
# Merging Ra and Rb
RaRb_multi_portfolio <- left_join(portfolio_returns_monthly ,
baseline_returns_monthly,
by = "date")
RaRb_multi_portfolio
## # A tibble: 610 x 4
## # Groups: portfolio [10]
## portfolio date Ra Rb
## <int> <date> <dbl> <dbl>
## 1 1 2016-04-29 0 0
## 2 1 2016-05-31 -0.0347 0.0362
## 3 1 2016-06-30 -0.0516 -0.0213
## 4 1 2016-07-29 0.0886 0.0660
## 5 1 2016-08-31 -0.0675 0.00990
## 6 1 2016-09-30 -0.0161 0.0189
## 7 1 2016-10-31 0.00120 -0.0231
## 8 1 2016-11-30 -0.0463 0.0259
## 9 1 2016-12-30 0.101 0.0112
## 10 1 2017-01-31 0.163 0.0430
## # ... with 600 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.25
## 2 2 1.56
## 3 3 1.36
## 4 4 1.42
## 5 5 1.61
## 6 6 1.51
## 7 7 1.49
## 8 8 1.61
## 9 9 1.55
## 10 10 1.55
Hint: Use dplyr::arrange().
# Sharpe Ratio
sharpe_ratio <- RaRb_multi_portfolio %>%
tq_performance(Ra = Ra, Rb = NULL, performance_fun = SharpeRatio.annualized, scale = 12)
arrange(sharpe_ratio, desc(`AnnualizedSharpeRatio(Rf=0%)`))
## # A tibble: 10 x 2
## # Groups: portfolio [10]
## portfolio `AnnualizedSharpeRatio(Rf=0%)`
## <int> <dbl>
## 1 5 1.61
## 2 8 1.61
## 3 2 1.56
## 4 9 1.55
## 5 10 1.55
## 6 6 1.51
## 7 7 1.49
## 8 4 1.42
## 9 3 1.36
## 10 1 1.25
Hint: Make your argument using the calculated Sharpe
Both portfolio 5 and 8 have a sharpe ratio of 1.61, making them the top performing portfolios. Portfolio 5 had a weight of 20% TSLA, 60% AMZN, and 20% NFLX. Portfolio 8 had a weight of 25% TSLA, 50% AMZN, and 25% NFLX.
Hint: Calculate Beta from the Capital Asset Pricing Model. Make your argument based on the calculated Beta.
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.4102 0.1952 0.2072 0.3612 0.2362 0.2424 0.3342 0.2551
## Alpha 0.0155 0.0086 0.0140 0.0130 0.0097 0.0129 0.0123 0.0104
## AnnualizedAlpha 0.2024 0.1076 0.1816 0.1681 0.1233 0.1659 0.1585 0.1321
## Beta 1.9166 1.3061 1.1156 1.7490 1.3888 1.2725 1.6538 1.4256
## Beta- 0.3888 0.8608 0.8610 0.6543 0.8389 0.8397 0.7291 0.8265
## Beta+ 2.7186 1.5075 1.4671 2.4390 1.7220 1.6960 2.2776 1.8217
## Correlation 0.6200 0.7921 0.5728 0.6948 0.7939 0.6722 0.7232 0.7819
## Correlationp-value 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## InformationRatio 0.9436 1.1067 0.7719 1.0994 1.2438 1.0123 1.1661 1.2534
## R-squared 0.3844 0.6274 0.3281 0.4827 0.6303 0.4519 0.5231 0.6114
## TrackingError 0.4348 0.1764 0.2684 0.3285 0.1899 0.2394 0.2866 0.2035
## TreynorRatio 0.3374 0.3306 0.3977 0.3418 0.3404 0.3763 0.3451 0.3448
## [,9] [,10]
## portfolio 9.0000 10.0000
## ActivePremium 0.2587 0.3039
## Alpha 0.0124 0.0111
## AnnualizedAlpha 0.1588 0.1417
## Beta 1.3505 1.5857
## Beta- 0.8277 0.7785
## Beta+ 1.8050 2.1088
## Correlation 0.7106 0.7604
## Correlationp-value 0.0000 0.0000
## InformationRatio 1.1161 1.2281
## R-squared 0.5049 0.5782
## TrackingError 0.2318 0.2474
## TreynorRatio 0.3667 0.3408
Based on calculated beta, portfolio 1 is the most volatile with a beta of 1.9166. Since a beta greater than 1 indicates that the security’s price tends to be more volatile than the market, this is the most volatile portfolio out of the others. Its weighting scheme is 80% TSLA, 10% AMZN, and 10% NFLX.
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.