Featured image of post Useful Financial R packages I've come across

Useful Financial R packages I've come across

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

  1. Bear & Baby https://pixabay.com/illustrations/toddler-baby-teddy-bear-cute-stars-8297939/
  2. Gifts https://pixabay.com/photos/gift-package-ribbon-parcel-444520/

Code tutorial

  1. quantRoom youtube.
comments powered by Disqus

Desiderata by Max Ehrmann

Go placidly amid the noise and the haste, and remember what peace there may be in silence. As far as possible without surrender be on good terms with all persons. Speak your truth quietly and clearly; and listen to others, even to the dull and the ignorant, they too have their story. Avoid loud and aggressive persons, they are vexations to the spirit.

If you compare yourself with others, you may become vain or bitter; for always there will be greater and lesser persons than yourself. Enjoy your achievements as well as your plans. Keep interested in your own career, however humble; it is a real possession in the changing fortunes of time.

Exercise caution in your business affairs, for the world is full of trickery. But let not this blind you to what virtue there is; many persons strive for high ideals, and everywhere life is full of heroism. Be yourself. Especially do not feign affection. Neither be cynical about love; for in the face of all aridity and disenchantment it is as perennial as the grass. Take kindly the counsel of the years, gracefully surrendering the things of youth.

Nurture strength of spirit to shield you in sudden misfortune. But do not distress yourself with dark imaginings. Many fears are born of fatigue and loneliness. Beyond a wholesome discipline, be gentle with yourself. You are a child of the universe, no less than the trees and the stars; you have a right to be here. And whether or not it is clear to you, no doubt the universe is unfolding as it should.

Therefore, be at peace with God, whatever you conceive Him to be. And whatever your labors and aspirations in the noisy confusion of life, keep peace in your soul. With all its sham, drudgery and broken dreams; it is still a beautiful world. Be cheerful.

Strive to be happy.

Built with Hugo
Theme Stack designed by Jimmy