# Load packages
# Core
library(tidyverse)
library(tidyquant)
Collect individual returns into a portfolio by assigning a weight to each stock
five stocks: “SPY”, “EFA”, “IJS”, “EEM”, “AGG”
from 2012-12-31 to 2017-12-31
symbols <- c("CRWD", "AMZN", "SHOP","TTD", "NVDA")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2021-01-01")
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log") %>%
slice(-1) %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AMZN" "CRWD" "NVDA" "SHOP" "TTD"
# weights
weights <- c(0.25, 0.25, 0.2, 0.2, 0.1)
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 × 2
## symbols weights
## <chr> <dbl>
## 1 AMZN 0.25
## 2 CRWD 0.25
## 3 NVDA 0.2
## 4 SHOP 0.2
## 5 TTD 0.1
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "months",
col_rename = "returns")
portfolio_returns_tbl
## # A tibble: 53 × 2
## date returns
## <date> <dbl>
## 1 2021-02-26 0.0378
## 2 2021-03-31 -0.0978
## 3 2021-04-30 0.110
## 4 2021-05-28 0.00183
## 5 2021-06-30 0.149
## 6 2021-07-30 -0.000161
## 7 2021-08-31 0.0648
## 8 2021-09-30 -0.0992
## 9 2021-10-29 0.105
## 10 2021-11-30 0.0333
## # ℹ 43 more rows
portfolio_kurt_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
tq_performance(Ra = returns,
performance_fun = table.Stats) %>%
select(Kurtosis)
portfolio_kurt_tidyquant_builtin_percent
## # A tibble: 1 × 1
## Kurtosis
## <dbl>
## 1 -0.0767
# Assign a value for window
window = 24
# Transform data: calculate 24 month rolling kurtosis
rolling_kurt_tbl <- portfolio_returns_tbl %>%
tq_mutate(select = returns,
mutate_fun = rollapply,
width = window,
FUN = kurtosis,
col_rename = "kurt") %>%
na.omit() %>%
select(-returns)
# Plot
rolling_kurt_tbl %>%
ggplot(aes(x = date, y = kurt)) +
geom_line(color = "cornflowerblue") +
# Formatting
scale_y_continuous(breaks = seq(-1, 4, 0.5)) +
scale_x_date(breaks = scales::pretty_breaks(n = 7)) +
theme(plot.title = element_text(hjust = 0.5)) +
# Labeling
labs(x = NULL,
y = "Kurtosis",
title = paste0("Rolling ", window, " Month Kurtosis"))
Has the downside risk of your portfolio increased or decreased over
time? Explain using the plot you created. You may also refer to the
skewness of the returns distribution you plotted in the previous
assignment.
My portfolios skewness was slightly above -.5. Meaning there was a tail to the left and the possibility of large negative returns. This means a negative kurtosis for my portfolio is bad. It shows frequent large losses for my portfolio. With a low kurtosis, and a negative Skewness, my portfolio has the chance to have frequent large losses.