/
get_trx_market_data_for_date.R
104 lines (89 loc) · 3.13 KB
/
get_trx_market_data_for_date.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#' Get TRX market data for a date
#'
#' Retrieves TRX market data for a specific date (price, market cap, high/low, etc.)
#'
#' @param date (character, Date or POSIXct): date to retrieve the data for.
#' Expected format: `%Y-%m-%d`. Please note that the minimal acceptable
#' `date` is `2017-11-09` as no data are available for earlier dates.
#' Attempts to retrieve data for earlier dates will fail with the
#' corresponding error. Attempts to request a future `date`,
#' for which no history exists yet, will fail as well.
#' @eval function_params(c("coingecko_vs_currencies", "max_attempts"))
#'
#' @return A tibble with as many rows as the length of `vs_currencies` and
#' the following columns:
#' * `date` (Date): same as `date`;
#' * `vs_currency` (character): same as `vs_currencies`;
#' * `market_cap` (double): market cap as of `date`;
#' * `total_trading_vol` (double): total trading volume on `date`;
#' * `price` (double): average price on `date`.
#'
#' @export
#'
#' @examples
#' r <- get_trx_market_data_for_date(date = Sys.Date() - 1)
#' print(r)
get_trx_market_data_for_date <- function(date,
vs_currencies = c("usd", "eur"),
max_attempts = 3L) {
validate_arguments(
arg_max_attempts = max_attempts,
arg_vs_currencies = vs_currencies
)
if (!(is.character(date) |
inherits(date, "POSIXct") |
inherits(date, "Date"))) {
rlang::abort("`date` must be a character, POSIXct or date value")
}
if (is.character(date)) {
date <- as.Date(date, format = "%Y-%m-%d")
if (is.na(date)) {
rlang::abort("`date` cannot be coerced to a Date value")
}
} else {
date <- as.Date(date)
}
if (date > Sys.Date()) {
rlang::abort("Cannot retrieve data for a future `date`. Check the `date` argument")
}
if (date < as.Date("2017-11-09")) {
rlang::abort("No data are available for dates before 2017-11-09. Check the `date` argument")
}
supported_currencies <- get_supported_coingecko_currencies(
max_attempts = max_attempts
)
if (!all(vs_currencies %in% supported_currencies)) {
rlang::abort(c(
"The following currencies are not currently supported:",
vs_currencies[!vs_currencies %in% supported_currencies]
))
}
date_split <- unlist(strsplit(as.character(date), split = "-"))
date_reversed <- paste(date_split[3],
date_split[2],
date_split[1],
sep = "-"
)
query_params <- list(
date = date_reversed,
localization = tolower(FALSE)
)
url <- build_get_request(
base_url = "https://api.coingecko.com",
path = c("api", "v3", "coins", "tron", "history"),
query_parameters = query_params
)
r <- api_request(url = url, max_attempts = max_attempts)$market_data
if (is.null(r)) {
message("No data are available for this `date`")
return(NULL)
}
result <- tibble::tibble(
date = as.Date(date),
vs_currency = vs_currencies,
market_cap = as.numeric(r$market_cap[vs_currencies]),
total_trading_vol = as.numeric(r$total_volume[vs_currencies]),
price = as.numeric(r$current_price[vs_currencies]),
)
return(result)
}