-
Notifications
You must be signed in to change notification settings - Fork 24
/
crypto_list.R
121 lines (117 loc) · 5.93 KB
/
crypto_list.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#' Retrieves name, CMC id, symbol, slug, rank, an activity flag as well as activity dates on CMC for all coins
#'
#' This code uses the web api. It retrieves data for all historic and all active coins and does not require an 'API' key.
#'
#' @param only_active Shall the code only retrieve active coins (TRUE=default) or include inactive coins (FALSE)
#' @param add_untracked Shall the code additionally retrieve untracked coins (FALSE=default)
#'
#' @return List of (active and historically existing) cryptocurrencies in a tibble:
#' \item{id}{CMC id (unique identifier)}
#' \item{name}{Coin name}
#' \item{symbol}{Coin symbol (not-unique)}
#' \item{slug}{Coin URL slug (unique)}
#' \item{rank}{Current rank on CMC (if still active)}
#' \item{is_active}{Flag showing whether coin is active (1), inactive(0) or untracked (-1)}
#' \item{first_historical_data}{First time listed on CMC}
#' \item{last_historical_data}{Last time listed on CMC, *today's date* if still listed}
#'
#' @importFrom tibble as_tibble
#' @importFrom jsonlite fromJSON
#' @importFrom dplyr bind_rows mutate rename arrange distinct
#'
#' @examples
#' \dontrun{
#' # return all coins
#' active_list <- crypto_list(only_active=TRUE)
#' all_but_untracked_list <- crypto_list(only_active=FALSE)
#' full_list <- crypto_list(only_active=FALSE,add_untracked=TRUE)
#'
#' # return all coins active in 2015
#' coin_list_2015 <- active_list %>%
#' dplyr::filter(first_historical_data<="2015-12-31",
#' last_historical_data>="2015-01-01")
#' }
#'
#' @name crypto_list
#'
#' @export
#'
crypto_list <- function(only_active=TRUE, add_untracked=FALSE) {
# get current coins
active_url <- paste0("https://web-api.coinmarketcap.com/v1/cryptocurrency/map")
active_coins <- jsonlite::fromJSON(active_url)
coins <- active_coins$data %>% tibble::as_tibble() %>% dplyr::mutate(dplyr::across(c(first_historical_data,last_historical_data),as.Date))
if (!only_active){
inactive_url <- paste0("https://web-api.coinmarketcap.com/v1/cryptocurrency/map?listing_status=inactive")
inactive_coins <- jsonlite::fromJSON(inactive_url)
date_cols <- inactive_coins$data %>%
select_if(is.character) %>%
select(where(~ !any(is.na(as.Date(., format = "%Y-%m-%d", tryFormats = c("%Y-%m-%d", "%Y/%m/%d"))))))
data_formatted <- inactive_coins$data %>%
mutate(across(all_of(names(date_cols)), ~ as.Date(., format = "%Y-%m-%d", tryFormats = c("%Y-%m-%d", "%Y/%m/%d"))))
coins <- dplyr::bind_rows(coins,
data_formatted %>% tibble::as_tibble() %>% dplyr::arrange(id))
}
if (add_untracked){
untracked_url <- paste0("https://web-api.coinmarketcap.com/v1/cryptocurrency/map?listing_status=untracked")
untracked_coins <- jsonlite::fromJSON(untracked_url)
date_cols <- untracked_coins$data %>%
select_if(is.character) %>%
select(where(~ !any(is.na(as.Date(., format = "%Y-%m-%d", tryFormats = c("%Y-%m-%d", "%Y/%m/%d"))))))
data_formatted <- untracked_coins$data %>%
mutate(across(all_of(names(date_cols)), ~ as.Date(., format = "%Y-%m-%d", tryFormats = c("%Y-%m-%d", "%Y/%m/%d"))))
coins <- dplyr::bind_rows(coins,
data_formatted %>% tibble::as_tibble() %>% dplyr::arrange(id))
}
return(coins %>% dplyr::select(id:last_historical_data) %>% dplyr::distinct() %>% dplyr::arrange(id))
}
#' Retrieves name, CMC id, symbol, slug, rank, an activity flag as well as activity dates on CMC for all coins
#'
#' This code uses the web api. It retrieves data for all historic and all active exchanges and does not require an 'API' key.
#'
#' @param only_active Shall the code only retrieve active exchanges (TRUE=default) or include inactive coins (FALSE)
#' @param add_untracked Shall the code additionally retrieve untracked exchanges (FALSE=default)
#'
#' @return List of (active and historically existing) exchanges in a tibble:
#' \item{id}{CMC exchange id (unique identifier)}
#' \item{name}{Exchange name}
#' \item{slug}{Exchange URL slug (unique)}
#' \item{is_active}{Flag showing whether exchange is active (1), inactive(0) or untracked (-1)}
#' \item{first_historical_data}{First time listed on CMC}
#' \item{last_historical_data}{Last time listed on CMC, *today's date* if still listed}
#'
#' @importFrom tibble as_tibble
#' @importFrom jsonlite fromJSON
#' @importFrom dplyr bind_rows mutate rename arrange distinct
#'
#' @examples
#' \dontrun{
#' # return all exchanges
#' ex_active_list <- exchange_list(only_active=TRUE)
#' ex_all_but_untracked_list <- exchange_list(only_active=FALSE)
#' ex_full_list <- exchange_list(only_active=FALSE,add_untracked=TRUE)
#' }
#'
#' @name exchange_list
#'
#' @export
#'
exchange_list <- function(only_active=TRUE, add_untracked=FALSE) {
# get current coins
active_url <- paste0("https://web-api.coinmarketcap.com/v1/exchange/map")
active_exchanges <- jsonlite::fromJSON(active_url)
exchanges <- active_exchanges$data %>% tibble::as_tibble() %>% dplyr::mutate(dplyr::across(c(first_historical_data,last_historical_data),as.Date))
if (!only_active){
inactive_url <- paste0("https://web-api.coinmarketcap.com/v1/exchange/map?listing_status=inactive")
inactive_exchanges <- jsonlite::fromJSON(inactive_url)
exchanges <- dplyr::bind_rows(exchanges,
inactive_exchanges$data %>% tibble::as_tibble() %>% dplyr::mutate(dplyr::across(c(first_historical_data,last_historical_data),as.Date))) %>% dplyr::arrange(id)
}
if (add_untracked){
untracked_url <- paste0("https://web-api.coinmarketcap.com/v1/exchange/map?listing_status=untracked")
untracked_exchanges <- jsonlite::fromJSON(untracked_url)
exchanges <- dplyr::bind_rows(exchanges,
untracked_exchanges$data %>% tibble::as_tibble() %>% dplyr::mutate(dplyr::across(c(first_historical_data,last_historical_data),as.Date),is_active=-1)) %>% dplyr::arrange(id)
}
return(exchanges %>% dplyr::select(id:last_historical_data) %>% dplyr::distinct() %>% dplyr::arrange(id))
}