# Load packages

# Core
library(tidyverse)
library(tidyquant)

Goal

Collect individual returns into a portfolio by assigning a weight to each stock

Choose your stocks.

from 2012-12-31 to 2017-12-31

1 Import stock prices

symbols <- c("MSFT", "DPZ", "AAPL")

prices <- tq_get(x = symbols,
                 get = "stock.prices",
                 from = "2020-12-31",
                 to = "2025-12-31")

2 Convert prices to returns (quarterly)

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"))

3 Assign a weight to each asset (change the weigting scheme)

# 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

4 Build a portfolio

# ?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

5 Plot: Portfolio Histogram and Density

Scatterplot

portfolio_returns_tbl %>%
    
    ggplot(mapping = aes(x = date, y = portfolio.returns)) +
    geom_point(color = "cornflowerblue") +
    
    # Formatting
    scale_x_date(date_breaks = "1 year",
                 date_labels = "%Y") +
    
    # Labeling
    labs(y = "monthly returns",
         x = NULL,
         title = "Portfolio Returns Scatter")

Histogram

portfolio_returns_tbl %>%
    
    ggplot(mapping = aes(x = portfolio.returns)) +
    geom_histogram(fill = "cornflowerblue", binwidth = 0.005) +
    
    labs(x = "returns",
         title = "Portfolio Returns Distribution")

Histogram & Density plot

portfolio_returns_tbl %>%
    
    ggplot(mapping = aes(x = portfolio.returns)) +
    geom_histogram(fill = "cornflowerblue", binwidth = 0.01) +
    geom_density() +
    
    # Formatting
    scale_x_continuous(labels = scales::percent_format()) +
    
    labs(x = "returns",
         y = "distribution",
         title = "Portfolio Histogram & Density")

What return should you expect from the portfolio in a typical quarter?

It appears that at a quarterly rate that the portfolio is likely expected to make a return of 2% on average. Which using the 2025 metrics it could rise due to the current state of the stock market. That said it is likely only do be around 12% more if anything.

The largest possible loss of the portfolio would most like be around -18% whilst the highest return would be about 19%. That said the bulk of returns will most likely be between 3% and 12% returns, with 3 percent being more likely.