-
Notifications
You must be signed in to change notification settings - Fork 12
/
get_quote_crypto.R
41 lines (32 loc) · 1.14 KB
/
get_quote_crypto.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
#' Get a crypto currency quote from RobinHood
#'
#' @param RH object class RobinHood
#' @param symbol (string) cryto currency symbol such as BTC, ETH, DOGE
#' @import httr magrittr
#' @export
#' @examples
#' \dontrun{
#' # Login in to your RobinHood account
#' RH <- RobinHood("username", "password")
#'
#' get_quote_crypto(RH, "BTC")
#'}
get_quote_crypto <- function(RH, symbol) {
# Check if RH is valid
RobinHood::check_rh(RH)
# Get IDs for cryptocurrency
currency_pairs <- RobinHood::api_currency_pairs(RH)
# Adjust symbol to require only the crypto symbol rather than the -USD addition
symbol <- paste(symbol, "-USD", sep = "")
# If symbol not in return the list of crypto quotes
if (!(symbol %in% currency_pairs$symbol)) {
return(cat("No quote found for given symbol"))
}
# Filter to requested currency pair
currency <- as.character(currency_pairs[currency_pairs$symbol == symbol, "id"])
# Quotes URL
quote_url <- paste(RobinHood::api_endpoints(endpoint = "forex"), currency, "/", sep = "")
# Get last price
quotes <- RobinHood::api_quote_crypto(RH, quote_url)
return(quotes)
}