library(pacman)
p_load(BatchGetSymbols, dplyr, ggplot2, tidyr, lubridate, tsibble)
What would happen if you bought and held the trades Nancy Pelosi was legally required to declare? Despite having a month lag, the answer indicates Nancy is an exceptionally gifted trader.
df = read.csv("https://house-stock-watcher-data.s3-us-west-2.amazonaws.com/data/all_transactions.csv")
tickers = df %>% filter(type == "purchase", representative == "Hon. Nancy Pelosi") %>% select(ticker) %>% unlist
tickers = c("SPY", tickers)
first.date <- ymd("2016-01-01")
last.date <- Sys.Date()
freq.data <- 'daily'
l.out <- BatchGetSymbols(tickers = tickers,
first.date = first.date,
last.date = last.date,
freq.data = freq.data,
cache.folder = file.path(tempdir(), 'BGS_Cache'))
df_prices = l.out$df.tickers %>% transmute(ticker, price = price.close, date = ref.date)
ts_prices = as_tsibble(df_prices[-which(duplicated(df_prices)),], index = date, key = ticker)
ts_prices = fill_gaps(ts_prices) %>% tidyr::fill(price, .direction = "down")
df_trades = df %>% filter(type == "purchase",
representative == "Hon. Nancy Pelosi") %>%
select(disclosure_date, transaction_date, asset_description, ticker) %>%
mutate(disclosure_date = mdy(disclosure_date )) %>%
mutate(date1m = disclosure_date %m+% months(1),
date3m = disclosure_date %m+% months(3),
date6m = disclosure_date %m+% months(6)) %>% na.omit
df_profit = df_trades %>%
left_join(ts_prices, by = c("ticker", "disclosure_date" = "date")) %>%
mutate(price0m = price) %>% select(-price) %>%
left_join(ts_prices, by = c("ticker", "date1m" = "date")) %>%
mutate(price1m = price) %>% select(-price) %>%
left_join(ts_prices, by = c("ticker", "date3m" = "date")) %>%
mutate(price3m = price) %>% select(-price) %>%
left_join(ts_prices, by = c("ticker", "date6m" = "date")) %>%
mutate(price6m = price) %>% select(-price) %>%
mutate(gain1m = (price1m - price0m)/price0m,
gain3m = (price3m - price0m)/price0m,
gain6m = (price6m - price0m)/price0m)
df_trades_SPY = df_trades %>% select(disclosure_date:date6m) %>%
mutate(ticker = "SPY") %>%
left_join(ts_prices, by = c("ticker", "disclosure_date" = "date")) %>%
mutate(price0m = price) %>% select(-price) %>%
left_join(ts_prices, by = c("ticker", "date1m" = "date")) %>%
mutate(price1m = price) %>% select(-price) %>%
left_join(ts_prices, by = c("ticker", "date3m" = "date")) %>%
mutate(price3m = price) %>% select(-price) %>%
left_join(ts_prices, by = c("ticker", "date6m" = "date")) %>%
mutate(price6m = price) %>% select(-price) %>%
mutate(gain1m = (price1m - price0m)/price0m,
gain3m = (price3m - price0m)/price0m,
gain6m = (price6m - price0m)/price0m)
df_plot = data.frame(time = rep(1:3, 2),
Pct_Gain = c(c(mean(df_profit$gain1m, na.rm = T), mean(df_profit$gain3m, na.rm = T), mean(df_profit$gain6m, na.rm = T)),
c(mean(df_trades_SPY$gain1m, na.rm = T), mean(df_trades_SPY$gain3m, na.rm = T), mean(df_trades_SPY$gain6m, na.rm = T))),
Strategy = rep(c("Copying Pelosi", "SPY"), each = 3))
ggplot(df_plot, aes(x = time, y = Pct_Gain, color = Strategy)) +
geom_point() +
geom_line() +
theme_bw() +
xlab("Months after Trade") +
ylab("% Gains") + theme(legend.position = c(0.07, 0.93), legend.background = element_blank())
df_plot[order(df_plot$time), ]
sessionInfo()
## R version 4.1.2 (2021-11-01)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19042)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=English_United States.1252
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] tsibble_1.1.1 lubridate_1.8.0 tidyr_1.1.4
## [4] ggplot2_3.3.5 BatchGetSymbols_2.6.4 dplyr_1.0.7
## [7] rvest_1.0.2 pacman_0.5.1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.7 highr_0.9 pillar_1.6.4 bslib_0.3.1
## [5] compiler_4.1.2 jquerylib_0.1.4 xts_0.12.1 tools_4.1.2
## [9] digest_0.6.28 lattice_0.20-45 anytime_0.3.9 jsonlite_1.7.2
## [13] evaluate_0.14 lifecycle_1.0.1 tibble_3.1.5 gtable_0.3.0
## [17] pkgconfig_2.0.3 rlang_0.4.12 DBI_1.1.2 curl_4.3.2
## [21] yaml_2.2.1 xfun_0.27 fastmap_1.1.0 withr_2.4.2
## [25] httr_1.4.2 stringr_1.4.0 xml2_1.3.2 knitr_1.36
## [29] generics_0.1.1 vctrs_0.3.8 sass_0.4.0 grid_4.1.2
## [33] tidyselect_1.1.1 glue_1.4.2 R6_2.5.1 fansi_0.5.0
## [37] rmarkdown_2.11 farver_2.1.0 TTR_0.24.3 purrr_0.3.4
## [41] magrittr_2.0.1 scales_1.1.1 htmltools_0.5.2 ellipsis_0.3.2
## [45] quantmod_0.4.20 assertthat_0.2.1 colorspace_2.0-2 labeling_0.4.2
## [49] utf8_1.2.2 stringi_1.7.5 munsell_0.5.0 crayon_1.4.2
## [53] zoo_1.8-9