Weathering the Storm

R
quant
Timeseries comparison and the impact of Covid-19 on the financial markets by sector
Author

Carl Goodwin

Published

August 2, 2020

Modified

June 29, 2024

Two meerkats cuddle under an umbrella shielded from the pouring rain

In 2020, Covid-19 began battering financial markets now further impacted by the war in Ukraine. Which sectors are faring best?

I’ll compare each sector in the S&P 500 with the overall market. Baselining each at zero as of February 19th, we’ll see which were the first to recover lost ground.

theme_set(theme_bw())

pal_name <- "wesanderson::Moonrise2"

pal <- paletteer_d(pal_name)

display_palette(pal, pal_name)

symbols <-
  c(
    "SPY",
    "XLV",
    "XLK",
    "XLE",
    "XLF",
    "XLC",
    "XLI",
    "XLY",
    "XLP",
    "XLRE",
    "XLU",
    "XLB"
  )

from <- "2020-02-19"

from_formatted <- date_parse(from, format = "%Y-%m-%d") |> 
  date_format(format = "%b %d, %Y")
eod_sectors <-
  tq_get(symbols, from = from) |>
  mutate(
    norm_close = adjusted / first(adjusted) - 1,
    type = if_else(symbol == "SPY", "Market", "Sector"),
    sector = case_match(
      symbol,
      "SPY"  ~ "S&P 500",
      "XLB"  ~ "Materials",
      "XLE"  ~ "Energy",
      "XLU"  ~ "Utilities",
      "XLI"  ~ "Industrical",
      "XLRE" ~ "Real Estate",
      "XLV"  ~ "Health",
      "XLK"  ~ "Technology",
      "XLF"  ~ "Financial",
      "XLC"  ~ "Communication",
      "XLY"  ~ "Consumer Discretionary",
      "XLP"  ~ "Consumer Staples",
      .default = "Other"
    ), .by = symbol
  ) |>
  drop_na()

Perhaps not too surprising to see that Tech led the way back from Covid. But with the further impact of the situation in Ukraine, the Energy sector is now the strongest performer relative to February 2020. Comms, with all that home-working, benefited initially during the lockdown, but has faded since.

eod_sectors |>
  mutate(
    sector = str_wrap(sector, 12),
    sector = fct_reorder(sector, norm_close, last, .desc = TRUE)
  ) |>
  ggplot(aes(date, norm_close, colour = sign(norm_close))) +
  geom_hline(yintercept = 0, linetype = "dashed", colour = "grey80") +
  geom_line() +
  facet_wrap(~sector) +
  scale_colour_gradient(low = pal[2], high = pal[1]) +
  scale_y_continuous(labels = label_percent()) +
  labs(
    title = "S&P 500 Sector Impact of Covid-19 & Ukraine",
    subtitle = glue("Relative to {from_formatted}"),
    x = NULL, y = NULL, colour = NULL
  ) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "none")

R Toolbox

Summarising below the packages and functions used in this post enables me to separately create a toolbox visualisation summarising the usage of packages and functions across all posts.

Package Function
base c[1], library[9], sign[1]
clock date_format[1], date_parse[1]
conflicted conflict_prefer_all[1], conflict_scout[1], conflicts_prefer[1]
dplyr case_match[1], first[1], if_else[1], mutate[2]
forcats fct_reorder[1]
ggfoundry display_palette[1]
ggplot2 aes[1], element_text[1], facet_wrap[1], geom_hline[1], geom_line[1], ggplot[1], labs[1], scale_colour_gradient[1], scale_y_continuous[1], theme[1], theme_bw[1], theme_set[1]
glue glue[1]
paletteer paletteer_d[1]
scales label_percent[1]
stringr str_wrap[1]
tidyquant tq_get[1]
tidyr drop_na[1]
usedthese used_here[1]