# Core
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.1 ✔ stringr 1.5.2
## ✔ ggplot2 4.0.0 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyquant)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## ── Attaching core tidyquant packages ─────────────────────── tidyquant 1.0.11 ──
## ✔ PerformanceAnalytics 2.0.8 ✔ TTR 0.24.4
## ✔ quantmod 0.4.28 ✔ xts 0.14.1── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
## ✖ zoo::as.Date() masks base::as.Date()
## ✖ zoo::as.Date.numeric() masks base::as.Date.numeric()
## ✖ dplyr::filter() masks stats::filter()
## ✖ xts::first() masks dplyr::first()
## ✖ dplyr::lag() masks stats::lag()
## ✖ xts::last() masks dplyr::last()
## ✖ PerformanceAnalytics::legend() masks graphics::legend()
## ✖ quantmod::summary() masks base::summary()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
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
symbols <- c("NVDA", "INTC", "GOOG", "AMD", "AAPL")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2027-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 <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AAPL" "AMD" "GOOG" "INTC" "NVDA"
## [1] "AAPL" "AMD" "GOOG" "INTC" "NVDA"
weights <- c(0.20, 0.15, 0.15, 0.30, 0.25)
weights
## [1] 0.20 0.15 0.15 0.30 0.25
## [1] 0.20 0.15 0.15 0.30 0.25
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 × 2
## symbols weights
## <chr> <dbl>
## 1 AAPL 0.2
## 2 AMD 0.15
## 3 GOOG 0.15
## 4 INTC 0.3
## 5 NVDA 0.25
## # A tibble: 5 × 2
## symbols weights
## <chr> <dbl>
## 1 AAPL 0.2
## 2 AMD 0.15
## 3 GOOG 0.15
## 4 INTC 0.3
## 5 NVDA 0.25
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "quarters")
## Warning in check_weights(weights, assets_col, map, x): Sum of weights does not
## equal 1.
## Warning in Return.portfolio.geometric(R = R, weights = weights, wealth.index =
## wealth.index, : The weights for one or more periods do not sum up to 1:
## assuming a return of 0 for the residual weights
portfolio_returns_tbl
## # A tibble: 52 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2013-03-28 0.0241
## 2 2013-06-28 0.123
## 3 2013-09-30 0.0403
## 4 2013-12-31 0.122
## 5 2014-03-31 0.0270
## 6 2014-06-30 0.117
## 7 2014-09-30 0.0245
## 8 2014-12-31 0.00478
## 9 2015-03-31 0.000522
## 10 2015-06-30 -0.0364
## # ℹ 42 more rows
## # A tibble: 52 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2013-03-28 0.0241
## 2 2013-06-28 0.123
## 3 2013-09-30 0.0403
## 4 2013-12-31 0.122
## 5 2014-03-31 0.0270
## 6 2014-06-30 0.117
## 7 2014-09-30 0.0245
## 8 2014-12-31 0.00478
## 9 2015-03-31 0.000522
## 10 2015-06-30 -0.0364
## # ℹ 42 more rows
portfolio_returns_tbl %>%
ggplot(mapping = aes(x = portfolio.returns)) +
geom_histogram(fill = "cornflowerblue", binwidth = 0.01) +
geom_density() +
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?
With my returns ranging from -40% to 40% I should expect around -5% and
23% returns on my portfolio as that is the highest distribution area and
has a spike of density in those markers as shown in the curve of the
plot.
I can also expect to have a positive return more often than not as there
is more distribution above the 0% on the graph showing a typical return
of around 10 percent on average.