# 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("TSLA", "META", "XOM", "AAPL", "PG", "AMZN")
prices <- tq_get(x = symbols,
get. = "stock.prices",
from = "2012-12-31",
to = Sys.Date())
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
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AAPL" "AMZN" "META" "PG" "TSLA" "XOM"
# weights
weights <- c(0.25, 0.05, 0.15, 0.3, 0.1, 0.15)
weights
## [1] 0.25 0.05 0.15 0.30 0.10 0.15
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 6 × 2
## symbols weights
## <chr> <dbl>
## 1 AAPL 0.25
## 2 AMZN 0.05
## 3 META 0.15
## 4 PG 0.3
## 5 TSLA 0.1
## 6 XOM 0.15
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: 126 × 2
## date returns
## <date> <dbl>
## 1 2013-01-31 0.0357
## 2 2013-02-28 -0.0290
## 3 2013-03-28 0.00445
## 4 2013-04-30 0.0446
## 5 2013-05-31 0.0516
## 6 2013-06-28 -0.0166
## 7 2013-07-31 0.139
## 8 2013-08-30 0.0379
## 9 2013-09-30 0.0318
## 10 2013-10-31 0.0398
## # ℹ 116 more rows
# Define risk free rate
rfr <- 0.0003
portfolio_SharpeRatio_tbl <- portfolio_returns_tbl %>%
tq_performance(Ra = returns,
performance_fun = SharpeRatio,
RF = rfr,
FUN = "StdDev")
portfolio_SharpeRatio_tbl
## # A tibble: 1 × 1
## `StdDevSharpe(Rf=0%,p=95%)`
## <dbl>
## 1 0.305
# Create a custom function to calculate rolling SR
Calculate_rolling_SharpeRatio <- 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 = returns,
mutate_fun = rollapply,
width = window,
FUN = Calculate_rolling_SharpeRatio,
col_rename = "rolling_sr") %>%
select(-returns) %>%
na.omit()
rolling_sr_tbl
## # A tibble: 103 × 2
## date rolling_sr
## <date> <dbl>
## 1 2014-12-31 0.627
## 2 2015-01-30 0.552
## 3 2015-02-27 0.646
## 4 2015-03-31 0.591
## 5 2015-04-30 0.565
## 6 2015-05-29 0.534
## 7 2015-06-30 0.568
## 8 2015-07-31 0.552
## 9 2015-08-31 0.358
## 10 2015-09-30 0.317
## # ℹ 93 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("2018-06-01"), y = 0,
label = str_glue("This portfolio had both strong and
weak periods of performance since 2016."), color = "red", size = 5)
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?
Based on the plot, the portfolio has shown periods of both strong and weak risk-adjusted performance.
Dec 2014 - Dec 2015: The SR fluctuated from 0.626 and 0.272, indicating relatively strong risk-adjusted performance.
2016: The SR continued to vary, reaching a low of 0.074 in Nov. This period suggests a decline in risk-adjusted performance.
Jan 2017 - Aug 2017: The SR increased gradually, peaking at 0.519 in August, indicating an improvement in risk-adjusted performance.
Sep 2017 - Dec 2018: The SR had several fluctuations, but generally maintained a range above 0.2, suggesting a reasonably positive risk-adjusted performance.
Early 2019: The SR dropped significantly, with values below 0.2, indicating a decline in risk-adjusted performance.
Mid 2019 - Mid 2020: The SR remained relatively low, hovering around 0.1 to 0.3, indicating limited risk-adjusted performance.
Mid 2020 - Mid 2021: The SR experienced an improvement, reaching a high of 0.436 in May 2021, suggesting better risk-adjusted performance.
Late 2021 - Early 2022: The SR remained relatively stable, ranging between 0.4 and 0.5, indicating consistent risk-adjusted performance.
Mid 2022 - June 2023: the SR declined significantly, dropping to -0.021 in Dec 2022, indicating poor risk-adjusted performance.
Overall, the portfolio has shown periods of both strong and weak risk-adjusted performance.
The first drop from around Mid 2015 - 2017 could have been casue by several global events. The Chinese stock market crash in Mid 2015 caused global uncertainty and negatively impacted many stocks; in Early 2016 concerns about the global economic growth increased market volatility; and the presidential elections in Nov 2016 caused political uncertainties.
Then, the increase from 2017 - 2018 might have been caused by the positive market sentiment after the U.S. election due to expectations of pro-business policies, tax cuts, and deregulation; and by the favorable economic data (corporate earnings, low interest rates) in Mid 2017.
Reasons for the second drop from 2018 - Mid 2019 might be the escalation of the U.S.-China trade war, both countries imposing tariffs on each other’s goods, increasing uncertainty and potential negative impact on global trade.
The gradual increase from 2020 - 2022 could have been caused by the economic recovery from the COVID-19 pandemic, as economies started reopening and vaccination efforts gained momentum. As well as expansionary monetary policies in which central banks worldwide implemented monetary stimulus measure to support the economy during the pandemic.
The drop form 2022 - present can be attributed to market correction driven by concerns over economic growth, inflation, or interest rates. Additionally, sector-specific challenges (technological disruptions) could have impacted the performance of certain industries, such as the tech industry of which several assets of this portfolio fall in. Also, geopolitical tensions or macroeconomic events, such as trade disputes, could have created uncertainty and affected market sentiment.
The structural risk in terms of the Sharpe ratio is evident during periods of significant declines in the ratio, indicating poor risk-adjusted performance. These periods include the decline from mid-2015 to 2017, the drop from 2018 to mid-2019, and the significant decline from mid-2022 to the present.
The portfolio has shown periods of strong and weak risk-adjusted performance.
It experienced a drop from mid-2015 to 2017 due to the Chinese stock market crash, global economic concerns, and political uncertainties.
An increase from 2017 to 2018 was driven by positive market sentiment and favorable economic data.
A drop from 2018 to mid-2019 was influenced by the U.S.-China trade war.
The gradual increase from 2020 to 2022 was a result of economic recovery and expansionary monetary policies during the pandemic.
The recent drop can be attributed to market correction, sector-specific challenges, and geopolitical tensions.