-
Notifications
You must be signed in to change notification settings - Fork 12
/
get_tickers.R
63 lines (48 loc) · 1.75 KB
/
get_tickers.R
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#' Download all financial instruments available on RobinHood
#'
#' Get a dataframe of all listed companies including ticker symbols and tradeability indicators.
#'
#' @param RH object of class RobinHood
#' @param add_fundamentals (logical) if TRUE then return fundamental data (long run time)
#' @import httr magrittr
#' @export
#' @examples
#' \dontrun{
#' # Login in to your RobinHood account
#' RH <- RobinHood("username", "password")
#'
#' get_tickers(RH)
#'}
get_tickers <- function(RH, add_fundamentals = FALSE) {
# Check if RH is valid
RobinHood::check_rh(RH)
# Call the tickers api to retrieve all stock symbols
tickers <- RobinHood::api_tickers(RH)
# limit columns and only return tradeable and searchable symbols
tickers <- tickers[
tickers$rhs_tradability == "tradable" & tickers$state == "active",
c("symbol", "country", "name", "list_date")]
# If fundamentals are requested
if (add_fundamentals == TRUE) {
# Create string of symbols to get fundamentals on
symbols <- tickers$symbol
# Stopwatch
start_time <- proc.time()
cat("Getting additional investment fundamentals...")
fundamentals <- data.frame()
# Get fundamentals on each symbol, with a 1 second delay every 50 calls
for (i in 1:length(symbols)) {
x <- RobinHood::get_fundamentals(RH, symbols[i])
fundamentals <- rbind(fundamentals, x)
if (i %in% seq(0, 15000, 50)) {
profvis::pause(1)
}
}
# Stopwatch
end_time <- proc.time() - start_time
cat("..........COMPLETE (", round(end_time[3] / 60, 2), "minutes)")
# Join tickers and fundamentals
tickers <- dplyr::left_join(tickers, fundamentals, by = "symbol")
}
return(tickers)
}