# Load packages
# Core
library(tidyverse)
library(tidyquant)
Visualize and examine changes in the underlying trend in the performance of your portfolio in terms of Sharpe Ratio.
Choose your stocks.
from 2012-12-31 to present
symbols <- c("PLTR", "IBM", "BLK", "TSM", "SLB")
prices <- tq_get(x = symbols,
get = "stock.prices",
fro = "2012-12-31",
to = "2024-12-31")
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] "BLK" "IBM" "PLTR" "SLB" "TSM"
weights <- c(0.4, 0.2, 0.2, 0.1, 0.1)
weights
## [1] 0.4 0.2 0.2 0.1 0.1
w_tbl <- tibble(symbols, weights)
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "months")
rfr <- 0.00045
portfolio_SharpeRatio_tbl_Apply <- portfolio_returns_tbl %>%
tq_performance(Ra = portfolio.returns,
performance_fun = SharpeRatio,
Rf = rfr,
FUN ="StdDev")
portfolio_SharpeRatio_tbl_Apply
## # A tibble: 1 × 1
## `StdDevSharpe(Rf=0%,p=95%)`
## <dbl>
## 1 0.155
Calculate_rolling_Sharperatio_Apply <- function(data) {
rolling_SR_Apply <- SharpeRatio(R = data,
Rf = rfr,
FUN = "StdDev")
return(rolling_SR_Apply)
}
window <- 24
rolling_sr_tbl_apply <- portfolio_returns_tbl %>%
tq_mutate(select = portfolio.returns,
mutate_fun = rollapply,
width = window,
FUN = Calculate_rolling_Sharperatio_Apply,
col_rename = "rolling_sr") %>%
select(-portfolio.returns) %>%
na.omit()
rolling_sr_tbl_apply
## # A tibble: 119 × 2
## date rolling_sr
## <date> <dbl>
## 1 2014-12-31 0.330
## 2 2015-01-30 0.203
## 3 2015-02-27 0.257
## 4 2015-03-31 0.202
## 5 2015-04-30 0.217
## 6 2015-05-29 0.186
## 7 2015-06-30 0.213
## 8 2015-07-31 0.124
## 9 2015-08-31 0.0776
## 10 2015-09-30 0.0161
## # ℹ 109 more rows
rolling_sr_tbl_apply %>%
ggplot(aes(x = date, y = rolling_sr)) +
geom_line(color = "cornflowerblue") +
labs(x = NULL, y = "Rolling Sharpe Ratio") +
annotate(geom = "text",
x = as.Date("2016-06-01"), y = 0.5,
label = " This portfolio has been pretty volatile over time.",
color = "red", size = 4)
How has your portfolio performed over time? Provide dates of the structural breaks, if any. The Code Along Assignment 9 had one structural break in November 2016. What do you think the reason is?
My portfolios risk adjusted return has been very up and down over time. At the start, the Sharpe Ratio was a little over .30. It dropped to under 0 briefly, meaning for the level of risk, there was negative return. After, it skyrocketed to .60 which is very good, but fell shortly after back down below 0 at the start of 2019. It remained below 0 through the heat of Covid, and began to crawl back up as we the economy began opening up around the start of 2021. After hovering around .30 for nearly 2 years, it plunged below 0 yet again. At the start of 2024, it began to climb sharply above the .40 threshold. All in all, this portfolio is risky, but can provide investors with great risk adjusted return at the right time. I believe the portfolio in the Code Along assignment had a structural break in November 2016 because of the election. Typically, when the election comes around, many different stocks fluctuate based upon candidates policies. In this case, Hillary Clinton was the strong favorite for quite some time, yet was upset. That may be the reason stock performance when taking risk into account dropped sharply during that period.