Skip to content

Instantly share code, notes, and snippets.

@dodger487
Created March 22, 2017 14:47
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 dodger487/39db8573df775f1adbd04783d0808b9f to your computer and use it in GitHub Desktop.
Save dodger487/39db8573df775f1adbd04783d0808b9f to your computer and use it in GitHub Desktop.
A brief look at election's effect on S&P 500
# Chris Riederer
# 2017-03-21
# Look at election's impact on stock market
library("MASS")
library(quantmod)
library(dplyr)
library(ggplot2)
# Pull in S&P data from Yahoo Finance
getSymbols("^GSPC", src="yahoo")
# Convert to friendly dataframe
df <- as.data.frame(GSPC) %>%
mutate(date = rownames(.),
raw_change = GSPC.Close - GSPC.Open,
change = raw_change / GSPC.Open)
# What do interesting days look like?
df %>% filter(date == "2016-11-07")
df %>% filter(date == "2016-11-08")
df %>% filter(date == "2016-11-09")
df %>% filter(date == "2017-03-21")
df %>%
filter(date > "2015") %>%
ggplot(aes(x=change, fill = date > "2016-11-08")) +
geom_histogram(bins=200) +
geom_vline(xintercept=0.01487, color="red") +
geom_vline(xintercept=-0.01483619, color="blue") +
scale_x_continuous("Percent Change") +
ggtitle("S&P 500 Performance (Red is Nov 9 2017)")
df %>%
ggplot(aes(x=change)) +
geom_histogram(bins=200) +
geom_vline(xintercept=0.01487, color="green") +
geom_vline(xintercept=0.0147244, color="red") +
geom_vline(xintercept=-0.01483619, color="blue") +
scale_x_continuous("Percent Change") +
ggtitle("S&P 500 Movements Since 2007")
fitdistr(df$change, "normal")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment