Skip to content

Instantly share code, notes, and snippets.

@iangow
Created May 7, 2016 14:45
Show Gist options
  • Save iangow/7c88670ce1bccb6decc5159b16967003 to your computer and use it in GitHub Desktop.
Save iangow/7c88670ce1bccb6decc5159b16967003 to your computer and use it in GitHub Desktop.
Code to illustrate calculation of returns in R
Sys.setenv(PGHOST="iangow.me", PGDATABASE="crsp")
devtools::source_url(paste0("https://raw.githubusercontent.com/iangow/",
"acct_data/master/code/getEventReturnsDaily.R"))
sql <- "
SELECT DISTINCT permno, comnam, gvkey, rdq
FROM crsp.stocknames AS a
INNER JOIN crsp.ccmxpf_linktable AS b
ON a.permno=b.lpermno
INNER JOIN comp.fundq
USING (gvkey)
WHERE siccd=3571 AND datadate BETWEEN '2009-09-01' AND '2009-12-31'
LIMIT 100"
library(RPostgreSQL)
pg <- dbConnect(PostgreSQL())
events <- dbGetQuery(pg, sql)
rs <- dbDisconnect(pg)
rets <- getEventReturns(events$permno, events$rdq, days.before=0, days.after=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment