# Load packages
# Core
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.0 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(tidyquant)
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
##
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
##
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## Attaching package: 'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
##
##
## Attaching package: 'PerformanceAnalytics'
##
## The following object is masked from 'package:graphics':
##
## legend
##
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(scales)
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
Collect individual returns into a portfolio by assigning a weight to each stock
Three stocks: “VIR”, “MSFT”, “GE”,
from 2012-12-31 to 2017-12-31
symbols <- c("WMT", "MSFT", "GE")
prices <- tq_get(x = symbols,
get = "stock.prices",
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 <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "GE" "MSFT" "WMT"
#Weights
weights <- c(0.25, 0.25, 0.5)
weights
## [1] 0.25 0.25 0.50
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 3 × 2
## symbols weights
## <chr> <dbl>
## 1 GE 0.25
## 2 MSFT 0.25
## 3 WMT 0.5
# tq_portfolio
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "quarter" )
## Warning: `spread_()` was deprecated in tidyr 1.2.0.
## Please use `spread()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
portfolio_returns_tbl
## # A tibble: 20 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2013-03-28 0.0948
## 2 2013-06-28 0.0523
## 3 2013-09-30 0.00127
## 4 2013-12-31 0.107
## 5 2014-03-31 -0.00432
## 6 2014-06-30 0.00595
## 7 2014-09-30 0.0363
## 8 2014-12-31 0.0618
## 9 2015-03-31 -0.0524
## 10 2015-06-30 -0.0294
## 11 2015-09-30 -0.0500
## 12 2015-12-31 0.0889
## 13 2016-03-31 0.0669
## 14 2016-06-30 0.0178
## 15 2016-09-30 0.0151
## 16 2016-12-30 0.0210
## 17 2017-03-31 0.0280
## 18 2017-06-30 0.0180
## 19 2017-09-29 0.0147
## 20 2017-12-29 0.0757
portfolio_returns_tbl %>%
ggplot(mapping = aes(x = portfolio.returns)) +
geom_histogram(fill = "yellow 1", binwidth = .01,) +
geom_density() +
#Formatting
scale_x_continuous(labels = scales::percent_format())+
labs(x = "returns",
y = "distribution",
title = "Portfolio Histogram and Density")
# What return should you expect from the portfolio in a typical
quarter?
You can expect a return between 1.25% and 2.5% in a typical quarter