Skip to content

Instantly share code, notes, and snippets.

@mkiang
Created February 15, 2020 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkiang/99bec17c8f6d0d110c8b8a7bfc4ae686 to your computer and use it in GitHub Desktop.
Save mkiang/99bec17c8f6d0d110c8b8a7bfc4ae686 to your computer and use it in GitHub Desktop.
Distribution of inter-peak days of the DJIA
library(tidyverse)
library(tidyquant)
source(
"https://raw.githubusercontent.com/mkiang/disproportionate_prescribing/master/code/mk_nytimes.R"
)
getSymbols("^DJI")
DJI <- as_tibble(DJI) %>%
mutate(
date = index(DJI),
year = lubridate::year(date),
day_index = 1:n()
)
DJI$peak_day <- 0
for (i in 1:NROW(DJI)) {
if (i == 1) {
current_peak <- as.numeric(DJI$DJI.Close[1])
next()
}
if (as.numeric(DJI$DJI.Close[i]) > current_peak) {
DJI$peak_day[i] <- 1
current_peak <- as.numeric(DJI$DJI.Close[i])
}
}
peak_days <- DJI %>%
filter(peak_day == 1) %>%
mutate(inter_peak_days = day_index - lag(day_index)) %>%
mutate(inter_peak_trunc = ifelse(inter_peak_days > 20, 21, inter_peak_days))
p1 <- ggplot(peak_days,
aes(x = inter_peak_trunc)) +
geom_histogram(bins = 20) +
scale_x_continuous(
"Market days since last ^DJI 'record high'",
expand = c(0, 0),
breaks = seq(0, 20, 5),
labels = c("0", "5", "10", "15", "20+"),
limits = c(0, 20.5)
) +
scale_y_continuous("Frequency",
expand = c(0, 0),
breaks = c(0, 20, 40)) +
facet_wrap( ~ year) +
mk_nytimes()
ggsave(
"./dji_inter_peak_times.jpg",
p1,
width = 6,
height = 5,
dpi = 300,
scale = 1
)
mean(peak_days$inter_peak_days, na.rm = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment