# Load packages
# Core
library(tidyverse)
library(tidyquant)
Collect individual returns into a portfolio by assigning a weight to each stock
Choose your stocks.
from 2012-12-31 to 2017-12-31
symbols <- c("TSLA", "GM", "F", "VWAGY", "HMC")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2017-12-31" )
asset_returns_tbl <- prices %>%
group_by (symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log") %>%
slice(-1) %>%
ungroup()%>%
set_names(c("asset", "date", "returns"))
asset_returns_tbl
## # A tibble: 300 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 F 2013-01-31 0.00734
## 2 F 2013-02-28 -0.0266
## 3 F 2013-03-28 0.0419
## 4 F 2013-04-30 0.0417
## 5 F 2013-05-31 0.142
## 6 F 2013-06-28 -0.0135
## 7 F 2013-07-31 0.0931
## 8 F 2013-08-30 -0.0417
## 9 F 2013-09-30 0.0411
## 10 F 2013-10-31 0.0198
## # … with 290 more rows
# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "F" "GM" "HMC" "TSLA" "VWAGY"
# weights
weight <- c(0.25, 0.25, 0.2, 0.2, 0.1)
weight
## [1] 0.25 0.25 0.20 0.20 0.10
w_tbl <- tibble(symbols, weight)
w_tbl
## # A tibble: 5 × 2
## symbols weight
## <chr> <dbl>
## 1 F 0.25
## 2 GM 0.25
## 3 HMC 0.2
## 4 TSLA 0.2
## 5 VWAGY 0.1
#?tq_portfolio
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "months")
portfolio_returns_tbl
## # A tibble: 60 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2013-01-31 0.0262
## 2 2013-02-28 -0.0429
## 3 2013-03-28 0.0298
## 4 2013-04-30 0.118
## 5 2013-05-31 0.175
## 6 2013-06-28 0.00125
## 7 2013-07-31 0.102
## 8 2013-08-30 0.0132
## 9 2013-09-30 0.0656
## 10 2013-10-31 -0.00922
## # … with 50 more rows
portfolio_sd_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
tq_performance(Ra = portfolio.returns,
performance_fun = table.Stats) %>%
select(Stdev) %>%
mutate(tq_sd = round(Stdev, 4))
portfolio_sd_tidyquant_builtin_percent
## # A tibble: 1 × 2
## Stdev tq_sd
## <dbl> <dbl>
## 1 0.0531 0.0531
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)
portfolio_mean_tidyquant_builtin_percent
## [1] 0.01019433
sd_mean_tbl <- asset_returns_tbl %>%
group_by(asset) %>%
tq_performance(Ra = returns,
performance_fun = table.Stats) %>%
select(Mean = ArithmeticMean, Stdev) %>%
ungroup() %>%
# Add portfolio sd
add_row(tibble(asset = "Portfolio",
Mean = portfolio_mean_tidyquant_builtin_percent,
Stdev = portfolio_sd_tidyquant_builtin_percent$tq_sd))
sd_mean_tbl
## # A tibble: 6 × 3
## asset Mean Stdev
## <chr> <dbl> <dbl>
## 1 F 0.0031 0.0545
## 2 GM 0.0086 0.061
## 3 HMC 0 0.0532
## 4 TSLA 0.037 0.145
## 5 VWAGY -0.0012 0.094
## 6 Portfolio 0.0102 0.0531
sd_mean_tbl %>%
ggplot(aes(x = Stdev, y = Mean, color = asset)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = asset))
rolling_sd_table <- portfolio_returns_tbl %>%
tq_mutate(select = portfolio.returns,
mutate_fun = rollapply,
width = 24,
FUN = sd,
col_rename = "rolling_sd") %>%
na.omit() %>%
select(date, rolling_sd)
rolling_sd_table
## # A tibble: 37 × 2
## date rolling_sd
## <date> <dbl>
## 1 2014-12-31 0.0588
## 2 2015-01-30 0.0599
## 3 2015-02-27 0.0603
## 4 2015-03-31 0.0607
## 5 2015-04-30 0.0570
## 6 2015-05-29 0.0458
## 7 2015-06-30 0.0462
## 8 2015-07-31 0.0420
## 9 2015-08-31 0.0445
## 10 2015-09-30 0.0434
## # … with 27 more rows
rolling_sd_table %>%
ggplot(aes(x = date, y = rolling_sd)) +
geom_line(color = "cornflowerblue") +
#Formatting
scale_y_continuous(labels = scales::percent_format()) +
#Labeling
labs(x = NULL,
y = NULL,
title = "24-Month Rolling Volatility") +
theme(plot.title = element_text(hjust = 0.5))
How should you expect your portfolio to perform relative to its assets in the portfolio? Would you invest all your money in any of the individual stocks instead of the portfolio? Discuss both in terms of expected return and risk.
The mean, or expected return, and the standard deviation, or risk, is plotted. HMC, F, and GM is low return and low risk, with low variability. VWAGY is a negative expected return with moderate risk, and TSLA is high return with high risk, plotted in the top right corner. Portfolio has a higher expected return but low risk. If we were to look at the vertical line of HMC, F, and Portfolio, we would go with Portfolio as there is the same risk but higher expected return. I would invest in Portfolio as there is low risk with moderate expected return, GM, F, HMC, and VWAGY are not worth investing all in alone, and TSLA has a very high risk.The volatility, or risk, in returns is not constant, and had an extreme fall in 2015, and then it went back up in 2016, and has been steadily decreasing since about the middle of 2016.