Black Monday Crash

[This article was first published on DataGeeek, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The Bank of Japan has raised the interest rates and Nikkei has crushed the worst since the Black Monday crash of October 1987. The S&P 500 and DAX have declined similarly, but the DAX has performed slightly better.

library(tidyverse)
library(tidyquant)

#Nikkei 225
df_nikkei225 <- 
  tq_get("^N225", to = "2024-08-05") %>% 
  tq_transmute(select = "close",
               mutate_fun = to.daily,
               col_rename = "nikkei225") 

#S&P 500
df_snp500 <- 
  tq_get("^GSPC", to = "2024-08-06") %>% 
  tq_transmute(select = "close",
               mutate_fun = to.daily,
               col_rename = "snp500") 

#DAX
df_dax <- 
  tq_get("^GDAXI", to = "2024-08-06") %>% 
  tq_transmute(select = "close",
               mutate_fun = to.daily,
               col_rename = "dax") 


#Merging all datasets
df_merged <- 
  df_nikkei225 %>% 
  left_join(df_snp500) %>% 
  left_join(df_dax) %>% 
  drop_na() 

#Index based on benchmark date (2024-07-05 = 100) 
df_index <- 
  df_merged %>%
  filter(date >= last(date) - months(1)) %>% 
  pivot_longer(cols = -date, names_to = "type") %>% 
  group_by(type) %>% 
  mutate(index = value / first(value)*100) %>% 
  ungroup()


#Compared chart of DAX, S&P 500, and Nikkei 225 for the last month

#Dataset for plotting labels
df_index_with_labels <- 
  df_index %>% 
  mutate(
    type = case_when(
      type == "nikkei225" ~ "Nikkei 225",
      type == "snp500" ~ "S&P 500",
      TRUE ~ "DAX"
    )
  ) 

df_index_with_labels %>% 
  ggplot(aes(date, index, col = type)) +
  geom_line(linewidth = 1.25) +
  geom_text(
    data = 
      df_index_with_labels |> 
      slice_tail(n = 1, by = type),
    aes(label = type),
    hjust = 0,
    vjust = 0,
    family = "Bricolage Grotesque",
    size = 5,
    position = position_jitter(width= -1, height= 1)
  ) +
  theme_minimal(
    base_size = 16, 
    base_family = "Bricolage Grotesque"
  ) +
  theme(
    panel.grid.minor = element_blank(),
    legend.position = 'none'
  ) +
  scale_color_manual(
    values = c("darkgreen", "darkred", "darkblue")
  ) +
  labs(
    x = element_blank(), 
    y = element_blank(),
    title = "Black Monday"
  ) +
  scale_x_date(expand = expansion(mult = c(0, .2))) +
  scale_y_continuous(
    labels = scales::label_number(accuracy = 1)
  )

To leave a comment for the author, please follow the link and comment on their blog: DataGeeek.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)