# Load packages
# Core
library(tidyverse)
library(tidyquant)
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("NKE", "NFLX", "AAPL")
prices <- tq_get(x = symbols,
from = "2012-12-31",
to = "2017-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
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AAPL" "NFLX" "NKE"
# weights
weights <- c(0.25, 0.25, 0.25)
weights
## [1] 0.25 0.25 0.25
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 3 × 2
## symbols weights
## <chr> <dbl>
## 1 AAPL 0.25
## 2 NFLX 0.25
## 3 NKE 0.25
# ?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: 20 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2013-03-28 0.169
## 2 2013-06-28 0.0213
## 3 2013-09-30 0.177
## 4 2013-12-31 0.106
## 5 2014-03-31 -0.0357
## 6 2014-06-30 0.119
## 7 2014-09-30 0.0631
## 8 2014-12-31 -0.0262
## 9 2015-03-31 0.0920
## 10 2015-06-30 0.136
## 11 2015-09-30 0.0259
## 12 2015-12-31 0.0196
## 13 2016-03-31 -0.0215
## 14 2016-06-30 -0.0852
## 15 2016-09-30 0.0507
## 16 2016-12-30 0.0565
## 17 2017-03-31 0.123
## 18 2017-06-30 0.0195
## 19 2017-09-29 0.0349
## 20 2017-12-29 0.0862
portfolio_returns_tbl %>%
ggplot(mapping = aes(x = portfolio.returns)) +
geom_histogram(fill = "cornflowerblue", binwidth = 0.02) +
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?
Based off of the histogram and density chart you should expect mostly positive returns from the portfolio, as 16/20 of the total values are above 0%, which is 80% so you should be able to expect mostly positive returns quarterly, however that is not guarenteed as there are a 4 points that fall below the 0% mark which would mean you lose money on your investment. A majority of the returns fall right inbetween the 0%-10% range (11/20), which simply means that more often than not, the return on the quarter will fall in that sector which is still positive however not so far to the right that it makes me want to invest half my savings, especially because there are just about the same ammount of values in the 10-20% (5/20) range as the -10-0% range (4/20), which means although a majority of the time you get a little positive return (0-10% (11/20)) and there is a chance for you to get in the 10-20% range (5/20), however you have just about the same chances (4/10) as getting in the -10-0% range (as the 10-20%), and although these returns arent terrible I would still advise to find different stocks with more consistant returns and a higher density towards the 10-20% range, as the most dense area is just right above the 0% mark which is too close for comfort.