-
Notifications
You must be signed in to change notification settings - Fork 12
/
api_tickers.R
60 lines (44 loc) · 1.48 KB
/
api_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
#' RobinHood API: Tickers
#'
#' Backend function called by get_ticker. Returns a data frame of all instruments listed
#' on RobinHood.
#'
#' @param RH object of class RobinHood
#' @import httr magrittr
#' @export
api_tickers <- function(RH) {
cat("Getting stock ticker data from RobinHood.com...")
# Stopwatch
start_time <- proc.time()
# URL and token
url <- RobinHood::api_endpoints("instruments")
token <- paste("Bearer", RH$api_response.access_token)
# GET call
dta <- GET(url,
add_headers("Accept" = "application/json",
"Content-Type" = "application/json",
"Authorization" = token))
httr::stop_for_status(dta)
# Format return
dta <- RobinHood::mod_json(dta, "fromJSON")
output <- dta$results
# Cycle through the pages of tickers until all have been pulled
while (length(dta$`next`) > 0) {
# URL
url <- dta$`next`
# GET call
dta <- GET(url,
add_headers("Accept" = "application/json",
"Content-Type" = "application/json",
"Authorization" = token))
httr::stop_for_status(dta)
# Format return
dta <- RobinHood::mod_json(dta, "fromJSON")
output <- rbind(output, dta$results)
profvis::pause(.25)
}
# Stopwatch
end_time <- proc.time() - start_time
cat("..........COMPLETE (", round(end_time[3] / 60, 2), "minutes)\n")
return(output)
}