# 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("NVDA", "MSFT", "AMD", "INTL")
prices <- tq_get(x    = symbols, 
                 get  = "stock.prices",
                 from = "2017-12-31",
                 to   = "2023-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"))

asset_returns_tbl
## # A tibble: 73 × 3
##    asset date        returns
##    <chr> <date>        <dbl>
##  1 AMD   2018-06-29  0.400  
##  2 AMD   2018-09-28  0.723  
##  3 AMD   2018-12-31 -0.515  
##  4 AMD   2019-03-29  0.324  
##  5 AMD   2019-06-28  0.174  
##  6 AMD   2019-09-30 -0.0465 
##  7 AMD   2019-12-31  0.459  
##  8 AMD   2020-03-31 -0.00832
##  9 AMD   2020-06-30  0.146  
## 10 AMD   2020-09-30  0.444  
## # ℹ 63 more rows

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AMD"  "INTL" "MSFT" "NVDA"
# weights
weights <- c(0.35, 0.35, 0.15, 0.15)
weights
## [1] 0.35 0.35 0.15 0.15
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AMD        0.35
## 2 INTL       0.35
## 3 MSFT       0.15
## 4 NVDA       0.15

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: 23 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2018-06-29           0.156  
##  2 2018-09-28           0.302  
##  3 2018-12-31          -0.309  
##  4 2019-03-29           0.181  
##  5 2019-06-28           0.0673 
##  6 2019-09-30          -0.00133
##  7 2019-12-31           0.225  
##  8 2020-03-31           0.0146 
##  9 2020-06-30           0.145  
## 10 2020-09-30           0.214  
## # ℹ 13 more rows

5 Plot: Portfolio Histogram and Density

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

What return should you expect from the portfolio in a typical quarter? A typical quarter for this spread of stocks is typically in profit, we have a bigger the count in profits, and a bigger percentage return on the positive side. We must also look at the risk of having this weight, yes the profits are there, but we can also quite clearly see that the loses can be major for some quarters. I used some of the same stocks as i did last week for data and last week we saw that some of these stocks carry risk and have recorded big looses, but also big profits, that is why we are seeing such a big gap between profit and loss.