Intro
I’ve been doing side project. One of the area I am interested in financial securities mostly stocks, ETFs, REITs, and options.
I came across congress financial security trading records. I cleaned the data and currently figuring out what the response / outcome column is.
I came across a few R packages along the way that have been useful for me for this endearvour and I’d like to share them. All these R packages was found through a youtube tutorial.
Packages
RQuantLib Package
This package is very useful for getting and finding all trading dates of the US market.
Example:
1
2
3
4
5
6
|
# 2023-11-15 - Wednesday
# 2023-11-12 - Sunday
RQuantLib::isBusinessDay(
calendar = "UnitedStates/NYSE",
dates = as.Date(c('2023-11-15','2023-11-12')))
# Output: [1] TRUE FALSE
|
Source: https://github.com/eddelbuettel/rquantlib
Of course Dirk Eddelbuettel is involved. The dude is a legend in R circle (and anything related to C++ and R crossover).
rvest Package
It’s a webscraping package.
I used this to grab the EPS and earning dates to create a response/outcome variable for congress trading dataset.
Credit for the code. quantRoom youtube.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
###
# get EPS by Ticker - includes Earning date
###
getEPS <- function(ticker) {
Sys.sleep(3)
url <- paste0(
"https://finance.yahoo.com/calendar/earnings/?symbol=",
ticker)
# read in page html
pg <- rvest::read_html(url)
# locate table
TABLE <-
pg %>%
rvest::html_nodes("table") %>%
rvest::html_table()
# row bind results and convert to data.frame
TABLE <- as.data.frame(t(do.call(rbind,TABLE[[1]])))
# remove timezone from Earnings Date
TABLE$`Earnings Date` <- gsub("EST","",TABLE$`Earnings Date`)
TABLE$`Earnings Date` <- gsub("EDT","",TABLE$`Earnings Date`)
# fix Earning Date/time
TABLE$`Earnings Date` <-
as.POSIXct(TABLE$`Earnings Date`,
format = "%b %d, %Y, %I %p",
tz = "EST")
# fix EPS
TABLE$`EPS Estimate` <- as.numeric(TABLE$`EPS Estimate`) %>%
suppressWarnings()
TABLE$`Reported EPS` <- as.numeric(TABLE$`Reported EPS`) %>%
suppressWarnings()
# fix earning surprise percentage
TABLE$`Surprise(%)` <- (
as.numeric(gsub("\\+","",TABLE$`Surprise(%)`)) %>%
suppressWarnings()
)/100
# return ALL
TABLE
}
EPS <- getEPS(ticker="ADM")
|
Source: https://rvest.tidyverse.org/
Credits
Pictures
- Bear & Baby https://pixabay.com/illustrations/toddler-baby-teddy-bear-cute-stars-8297939/
- Gifts https://pixabay.com/photos/gift-package-ribbon-parcel-444520/
Code tutorial
- quantRoom youtube.