# Load packages
# Core
library(tidyverse)
library(tidyquant)
Visualize expected returns and risk to make it easier to compare the performance of multiple assets and portfolios.
Choose your stocks.
from 2012-12-31 to 2017-12-31
symbols <- c("MSFT", "DPZ", "AAPL")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2020-12-31",
to = "2025-12-31")
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "quarterly",
type = "log") %>%
slice(-1) %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AAPL" "DPZ" "MSFT"
#weights
weights <- c(0.34, 0.33, 0.33)
weights
## [1] 0.34 0.33 0.33
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 3 × 2
## symbols weights
## <chr> <dbl>
## 1 AAPL 0.34
## 2 DPZ 0.33
## 3 MSFT 0.33
# ?tq_portfolio
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "quarters")
portfolio_returns_tbl
## # A tibble: 18 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2021-03-31 -0.0205
## 2 2021-06-30 0.165
## 3 2021-09-30 0.0333
## 4 2021-12-31 0.193
## 5 2022-03-31 -0.140
## 6 2022-06-30 -0.156
## 7 2022-09-30 -0.102
## 8 2022-12-30 0.0276
## 9 2023-03-31 0.128
## 10 2023-06-30 0.120
## 11 2023-09-29 -0.0266
## 12 2023-12-29 0.128
## 13 2024-03-28 0.0615
## 14 2024-06-28 0.105
## 15 2024-09-30 -0.0362
## 16 2024-12-31 0.0117
## 17 2025-03-31 -0.0468
## 18 2025-06-18 0.0328
portfolio_sd_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
tq_performance(Ra = portfolio.returns,
performance_fun = table.Stats) %>%
select(Stdev) %>%
mutate(tq_sd = round(Stdev, 3)*100)
portfolio_sd_tidyquant_builtin_percent
## # A tibble: 1 × 2
## Stdev tq_sd
## <dbl> <dbl>
## 1 0.102 10.2
#Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)
portfolio_mean_tidyquant_builtin_percent
## [1] 0.02651494
# Expected Returns vs Risk
sd_mean_tbl <- asset_returns_tbl %>%
group_by(asset) %>%
tq_performance(Ra = returns,
performance_fun = table.Stats) %>%
select(Mean = ArithmeticMean, Stdev) %>%
ungroup() %>%
mutate(Stdev = Stdev * 100,
Mean = Mean * 100) %>%
# Add portfolio sd
add_row(tibble(asset = "Portfolio",
Mean = portfolio_mean_tidyquant_builtin_percent * 100,
Stdev = portfolio_sd_tidyquant_builtin_percent$tq_sd))
sd_mean_tbl
## # A tibble: 4 × 3
## asset Mean Stdev
## <chr> <dbl> <dbl>
## 1 AAPL 2.32 14.2
## 2 DPZ 1.16 14.6
## 3 MSFT 4.49 12.5
## 4 Portfolio 2.65 10.2
sd_mean_tbl %>%
ggplot(aes(x = Stdev, y = Mean, color = asset)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = asset))
rolling_sd_tbl <- portfolio_returns_tbl %>%
tq_mutate(select = portfolio.returns,
mutate_fun = rollapply,
width = 4,
FUN = sd,
col_rename = "rolling_sd") %>%
na.omit() %>%
select(date, rolling_sd)
rolling_sd_tbl
## # A tibble: 15 × 2
## date rolling_sd
## <date> <dbl>
## 1 2021-12-31 0.103
## 2 2022-03-31 0.152
## 3 2022-06-30 0.164
## 4 2022-09-30 0.164
## 5 2022-12-30 0.0832
## 6 2023-03-31 0.128
## 7 2023-06-30 0.107
## 8 2023-09-29 0.0748
## 9 2023-12-29 0.0760
## 10 2024-03-28 0.0712
## 11 2024-06-28 0.0680
## 12 2024-09-30 0.0724
## 13 2024-12-31 0.0610
## 14 2025-03-31 0.0690
## 15 2025-06-18 0.0381
In terms of positioning the Portfolio I’ve put together is not very substantial by itself sitting far behind the stocks itself that said unlike DPZ the risk is rather middle of the road making it a more gradual expansion of wealth.
I honestly think that I would invest my money into individual stocks, most likely MSFT and AAPL, largely because MSFT is like the portfolio but more valued and AAPL is more stable than DPZ alone.