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: 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
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
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
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
RaRb_multi_portfolio %>%
tq_performance(Ra = Ra, Rb = NULL, performance_fun = SharpeRatio.annualized, scale = 10)
## # A tibble: 10 x 2
## # Groups: portfolio [10]
## portfolio `AnnualizedSharpeRatio(Rf=0%)`
## <int> <dbl>
## 1 1 1.10
## 2 2 1.38
## 3 3 1.21
## 4 4 1.25
## 5 5 1.43
## 6 6 1.34
## 7 7 1.31
## 8 8 1.42
## 9 9 1.38
## 10 10 1.36
Hint: Use dplyr::arrange().
RaRb_multi_portfolio %>%
tq_performance(Ra = Ra, Rb = NULL, performance_fun = SharpeRatio.annualized, scale = 10) %>%
arrange(desc(`AnnualizedSharpeRatio(Rf=0%)`))
## # A tibble: 10 x 2
## # Groups: portfolio [10]
## portfolio `AnnualizedSharpeRatio(Rf=0%)`
## <int> <dbl>
## 1 5 1.43
## 2 8 1.42
## 3 2 1.38
## 4 9 1.38
## 5 10 1.36
## 6 6 1.34
## 7 7 1.31
## 8 4 1.25
## 9 3 1.21
## 10 1 1.10
Hint: Make your argument using the calculated Sharpe
THE BEST WEIGHTING SCHEME WOULD BE 5 BECAUSE IT HAS THE HIGHEST RATIO OF 1.43.
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.4278 0.1974 0.2117 0.3758 0.2419 0.2493 0.3471 0.2623
## Alpha 0.0162 0.0086 0.0142 0.0135 0.0099 0.0131 0.0128 0.0106
## AnnualizedAlpha 0.2120 0.1084 0.1838 0.1752 0.1257 0.1689 0.1645 0.1352
## Beta 1.9394 1.3099 1.1216 1.7704 1.3987 1.2832 1.6736 1.4379
## Beta- 0.3796 0.8605 0.8608 0.6477 0.8380 0.8389 0.7243 0.8252
## Beta+ 2.7701 1.5211 1.4867 2.4861 1.7472 1.7233 2.3214 1.8514
## Correlation 0.6219 0.7925 0.5769 0.6957 0.7948 0.6764 0.7241 0.7830
## Correlationp-value 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## InformationRatio 0.9707 1.1111 0.7894 1.1248 1.2600 1.0382 1.1908 1.2732
## R-squared 0.3867 0.6281 0.3329 0.4840 0.6317 0.4576 0.5243 0.6131
## TrackingError 0.4407 0.1776 0.2682 0.3341 0.1920 0.2401 0.2915 0.2061
## TreynorRatio 0.3429 0.3317 0.4002 0.3463 0.3425 0.3791 0.3491 0.3474
## [,9] [,10]
## portfolio 9.0000 10.0000
## ActivePremium 0.2667 0.3146
## Alpha 0.0126 0.0115
## AnnualizedAlpha 0.1622 0.1465
## Beta 1.3632 1.6030
## Beta- 0.8265 0.7752
## Beta+ 1.8357 2.1481
## Correlation 0.7140 0.7606
## Correlationp-value 0.0000 0.0000
## InformationRatio 1.1432 1.2492
## R-squared 0.5099 0.5785
## TrackingError 0.2333 0.2519
## TreynorRatio 0.3696 0.3442
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.