# 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("JPM", "QQQ", "F", "META", "NKE")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2025-12-31",
to = "2026-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", "return"))
# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "F" "JPM" "META" "NKE" "QQQ"
#weights
weights <- c(0.25, 0.25, 0.2, 0.2, 0.1)
weights
## [1] 0.25 0.25 0.20 0.20 0.10
w_tbl <- tibble(symbols, weights)
# ?tq_portfolio
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = return,
weights = w_tbl,
rebalace_on = "months")
portfolio_returns_tbl
## # A tibble: 10 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2026-01-30 0.0138
## 2 2026-02-27 -0.0202
## 3 2026-03-31 -0.119
## 4 2026-04-30 0.0250
## 5 2026-05-29 0.109
## 6 2026-06-30 -0.0880
## 7 2026-07-31 0.0272
## 8 2026-08-31 -0.00698
## 9 2026-09-30 -0.0242
## 10 2026-10-19 0.00179
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.063 0.063
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <-
mean(portfolio_returns_tbl$portfolio.returns)
portfolio_mean_tidyquant_builtin_percent
## [1] -0.008217807
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.
My portfolio should have a good balance between making money and managing risk because it spreads my money across five stocks. One stock might earn a higher return, but it could also have a bigger chance of losing money. I would rather invest in the portfolio because it spreads out my risk instead of putting all my money into one stock.