library(quantmod)

library(tidyverse)

library(lubridate)
raw_df = getSymbols("^GSPC",auto.assign = FALSE,
                  from = "1990-01-01", to = "2022-01-01")
spy_df = tibble(date = index(raw_df),
                adj_price = coredata(raw_df)[,"GSPC.Adjusted"],
                price = coredata(raw_df)[,"GSPC.Close"]) %>% 
  arrange(date) %>% 
  mutate(across(contains("price"),  ~. / lag(.) - 1,
         .names = "ret_{col}")) %>% 
  filter(complete.cases(.))
extreme_preformance = spy_df %>% 
  select(date, ret_adj_price) %>% 
  slice_max(order_by = ret_adj_price,n = 20) %>% 
  mutate(category = "best_days") %>% 
  bind_rows(spy_df %>%
              select(date, ret_adj_price) %>%
              slice_min(order_by = ret_adj_price,n = 20) %>%
              mutate(category = "worst_days"))
extreme_preformance %>%
  mutate(date = as.character(date)) %>% 
  ggplot(aes(tidytext::reorder_within(date, ret_adj_price,
                                      category),ret_adj_price)) + 
  geom_col() + 
  facet_wrap(~category, scales = "free") + 
  scale_y_continuous(labels = scales::percent_format()) + 
  tidytext::scale_x_reordered() + 
  xlab(NULL) + ylab(NULL) + 
  theme(axis.text.x = element_text(angle = 90))
summary_df = tibble(days = c(0,1,2,3,5,10,20)) %>% 
  mutate(excluding_bad_days_return = map_dbl(days,
                                             function(temp_day){

  temp_return = spy_df %>% 
    slice_max(order_by = ret_adj_price,n = -1 * temp_day) %>%
    arrange(date) %>% 
    group_by(year = year(date)) %>% 
    slice(-1) %>% 
    summarise(annual_ret = prod(1 + ret_adj_price) - 1, .groups = "drop") %>% 
    pull(annual_ret) %>% 
    mean()

  return(temp_return)


})) %>% 
  mutate(excluding_good_days_return = map_dbl(days,
                                              function(temp_day){

  temp_return = spy_df %>% 
    slice_min(order_by = ret_adj_price,n = -1 * temp_day) %>%
    arrange(date) %>% 
    group_by(year = year(date)) %>% 
    slice(-1) %>% 
    summarise(annual_ret = prod(1 + ret_adj_price) - 1, .groups = "drop") %>% 
    pull(annual_ret) %>% 
    mean()

  return(temp_return)


}))
summary_df %>% 
  pivot_longer(-days) %>% 
  ggplot(aes(x = as_factor(days), y = value)) + 
  geom_col() + 
  scale_y_continuous(labels = scales::percent_format()) + 
  geom_text(aes(x = as_factor(days), y = value,
                label = round(value * 100,2)),
            vjust = -1) +
  ylim(c(NA,0.16)) + 
  facet_wrap(~name) + 
  xlab(NULL) + ylab(NULL)


MichaelGurkov/LearningMaterials documentation built on July 9, 2022, 5:17 p.m.