# 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("WMT", "TGT", "COST")
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 = "monthly",
type = "log") %>%
slice(-1) %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
asset_returns_tbl
## # A tibble: 180 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 COST 2013-01-31 0.0359
## 2 COST 2013-02-28 -0.00765
## 3 COST 2013-03-28 0.0465
## 4 COST 2013-04-30 0.0216
## 5 COST 2013-05-31 0.0138
## 6 COST 2013-06-28 0.00854
## 7 COST 2013-07-31 0.0601
## 8 COST 2013-08-30 -0.0458
## 9 COST 2013-09-30 0.0291
## 10 COST 2013-10-31 0.0243
## # ℹ 170 more rows
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "COST" "TGT" "WMT"
weights <- c(0.35, 0.3, 0.25)
weights
## [1] 0.35 0.30 0.25
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 3 × 2
## symbols weights
## <chr> <dbl>
## 1 COST 0.35
## 2 TGT 0.3
## 3 WMT 0.25
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "months" )
portfolio_returns_tbl
## # A tibble: 60 × 2
## date portfolio.returns
## <date> <dbl>
## 1 2013-01-31 0.0250
## 2 2013-02-28 0.0144
## 3 2013-03-28 0.0569
## 4 2013-04-30 0.0262
## 5 2013-05-31 -0.00611
## 6 2013-06-28 -0.000959
## 7 2013-07-31 0.0426
## 8 2013-08-30 -0.0645
## 9 2013-09-30 0.0167
## 10 2013-10-31 0.0215
## # ℹ 50 more rows
rfr <- 0.0003
portfolio_SharpeRatio_tbl <- portfolio_returns_tbl %>%
tq_performance(Ra = portfolio.returns,
performance_fun = SharpeRatio,
Rf = rfr,
FUN = "StdDev")
portfolio_SharpeRatio_tbl
## # A tibble: 1 × 1
## `StdDevSharpe(Rf=0%,p=95%)`
## <dbl>
## 1 0.213
calculate_rolling_SharpeRation <- function(data) {
rolling_SR <- SharpeRatio(R = data,
Rf = rfr,
FUN = "StdDev")
return(rolling_SR)
}
# Define window
window <- 24
# Transform data: calculate rolling sharpe ratio
rolling_sr_tbl <- portfolio_returns_tbl %>%
tq_mutate(select = portfolio.returns,
mutate_fun = rollapply,
width = window,
FUN = calculate_rolling_SharpeRation,
col_rename = "rolling_sr") %>%
select(-portfolio.returns) %>%
na.omit()
rolling_sr_tbl
## # A tibble: 37 × 2
## date rolling_sr
## <date> <dbl>
## 1 2014-12-31 0.317
## 2 2015-01-30 0.278
## 3 2015-02-27 0.297
## 4 2015-03-31 0.272
## 5 2015-04-30 0.186
## 6 2015-05-29 0.184
## 7 2015-06-30 0.160
## 8 2015-07-31 0.148
## 9 2015-08-31 0.168
## 10 2015-09-30 0.166
## # ℹ 27 more rows
rolling_sr_tbl %>%
ggplot(aes(x = date, y = rolling_sr)) +
geom_line(color = "cornflowerblue") +
# Labeling
labs(x = NULL, y = "Rolling Sharpe Ratio") +
annotate(geom = "text",
x = as.Date("2016-06-01"), y = 0.5,
label = "This portfolio did quiet well up until mid-way of 2016.",
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?
Overtime my portfolio did not perform as well as I had hoped. It started out strong being above 0.3 but quickly dropped a quarter of the way through 2015. It then continued to drop slightly until the end of 2015 reaching a low of 0.1, to rise back up to 0.3 a quarter of the way through 2016. Towards mid 2016 is when we see the structural break going from 0.3 to -0.1 by the beginning of 2017. From here is seems the portfolio is making its way back into the positives as of the beginning of 2018.
I think that the reason Code Along 9 has one structural break is because a problem arose in the market. Code along 9 had a relatively high sharpe ratio, which means it can have huge returns when things are going well, but as soon as a problem arises, the sharpe ratio may crash. Which is exactly what I think happened in code along 9, everything was going very well but something must've happened in the market for the return rate to drop, causing the sharpe ratio to do the same. This could be anything from a market crash to an economically caused issue.