# Load packages
# Core
library(tidyverse)
library(tidyquant)
Collect individual returns into a portfolio by assigning a weight to each stock
Choose your stocks.
from 2020-12-31 to 2026-10-04
symbols <- c("TSM", "MU", "NOW", "MRVL", "PLTR")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2020-12-31",
to = "2026-10-04")
prices
## # A tibble: 7,225 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 TSM 2020-12-31 110. 110. 108. 109. 4909500 99.5
## 2 TSM 2021-01-04 111. 114. 110. 112. 11262100 102.
## 3 TSM 2021-01-05 112. 115. 112. 113. 10583600 103.
## 4 TSM 2021-01-06 114. 116. 113. 116. 10609300 105.
## 5 TSM 2021-01-07 119. 123. 118. 121. 13556100 111.
## 6 TSM 2021-01-08 126. 126. 117. 119. 18976800 108.
## 7 TSM 2021-01-11 120. 124. 119. 123. 12006800 112.
## 8 TSM 2021-01-12 125. 125. 122. 123 14163200 112.
## 9 TSM 2021-01-13 124. 125. 118 119. 20650500 109.
## 10 TSM 2021-01-14 123. 135. 122. 126. 37125400 115.
## # ℹ 7,215 more rows
asset_returns_tbl1 <- 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 <- asset_returns_tbl1 %>% distinct(asset) %>% pull
symbols
## [1] "MRVL" "MU" "NOW" "PLTR" "TSM"
weight <- c(0.15, 0.20, 0.30, 0.25, 0.10)
weight
## [1] 0.15 0.20 0.30 0.25 0.10
weight_tbl <- tibble(symbols, weight)
weight_tbl
## # A tibble: 5 × 2
## symbols weight
## <chr> <dbl>
## 1 MRVL 0.15
## 2 MU 0.2
## 3 NOW 0.3
## 4 PLTR 0.25
## 5 TSM 0.1
library(PerformanceAnalytics)
portfolio_returns_tbl1 <-
asset_returns_tbl1 %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = weight_tbl,
rebalance_on = "quarters")
portfolio_returns_tbl1
## # A tibble: 24 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2021-03-31 0.0134
## 2 2021-06-30 0.0801
## 3 2021-09-30 -0.0232
## 4 2021-12-31 0.0616
## 5 2022-03-31 -0.196
## 6 2022-06-30 -0.318
## 7 2022-09-30 -0.135
## 8 2022-12-30 -0.0633
## 9 2023-03-31 0.207
## 10 2023-06-30 0.272
## # ℹ 14 more rows
view(portfolio_returns_tbl1)
Scatterplot
portfolio_returns_tbl1 %>%
ggplot(mapping = aes(x = date, y = portfolio.returns)) +
geom_point(color = "cornflowerblue") +
scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
labs(y = " quartlery returns",
x = NULL,
title = "Portfolio Returns Scatter")
Histogram
portfolio_returns_tbl1 %>%
ggplot(mapping = aes(x = portfolio.returns)) +
geom_histogram(fill = "cornflowerblue", binwidt = 0.05) +
labs( x = "returns",
title = "Portfolio Returns Distribution")
Histogram & Density Distribution
portfolio_returns_tbl1 %>%
ggplot(mapping = aes(x = portfolio.returns)) +
geom_histogram(fill = "cornflowerblue", binwidt = 0.05, color = "violet") +
geom_density() +
scale_x_continuous(breaks = seq(-0.50, 0.50, by = 0.05),
labels = scales::percent_format()) +
labs(x = "returns",
y = "distribution",
title = "Histogram & Density")
What return should you expect from the portfolio in a typical quarter?
Based upon the histogram, the most common return and therefore the return to expect in a typical quarter is 7.75% to 9.99%