|
| 1 | +#' @title Get all market symbols' metadata --deprecated-- |
| 2 | +#' |
| 3 | +#' @param retries A `numeric` value to specify the number of retries in case of failure (optional - default `3`). |
| 4 | +#' |
| 5 | +#' @return A `data.table` with metadata |
| 6 | +#' |
| 7 | +#' @details |
| 8 | +#' |
| 9 | +#' TODO: this function needs to be updated to v2 of the API. |
| 10 | +#' |
| 11 | +#' # --------------- |
| 12 | +#' For more information see documentation: [KuCoin - get-symbols-list-deprecated](https://docs.kucoin.com/#get-symbols-list-deprecated) |
| 13 | +#' |
| 14 | +#' @examples |
| 15 | +#' # import library |
| 16 | +#' library("kucoin") |
| 17 | +#' |
| 18 | +#' # get all symbols' most recent metadata |
| 19 | +#' metadata <- kucoin::get_market_metadata() |
| 20 | +#' |
| 21 | +#' # quick check |
| 22 | +#' metadata |
| 23 | +#' |
| 24 | +#' @export |
| 25 | + |
| 26 | +get_market_metadata.deprecated <- function(retries = 3) { |
| 27 | + # get server response |
| 28 | + response <- httr::RETRY( |
| 29 | + verb = "GET", |
| 30 | + url = get_base_url(), |
| 31 | + path = get_paths("symbols-deprecated"), |
| 32 | + times = retries |
| 33 | + ) |
| 34 | + |
| 35 | + # analyze response |
| 36 | + response <- analyze_response(response) |
| 37 | + |
| 38 | + # parse json result |
| 39 | + parsed <- jsonlite::fromJSON(httr::content(response, "text", encoding = "UTF-8")) |
| 40 | + |
| 41 | + # tidy the parsed data |
| 42 | + results <- data.table::data.table(parsed$data, check.names = FALSE) |
| 43 | + |
| 44 | + # seems that the only thing that changes in colnames is they are made to snake_case |
| 45 | + # https://github.com/dereckdemezquita/kucoin/issues/1 |
| 46 | + # Error in setnames(x, value) : |
| 47 | + # Can't assign 14 names to a 17 column data.table |
| 48 | + # colnames(results) <- c( |
| 49 | + # "symbol", "quote_max_size", "enable_trading", "price_increment", |
| 50 | + # "fee_currency", "base_max_size", "base_currency", "quote_currency", |
| 51 | + # "market", "quote_increment", "base_min_size", "quote_min_size", |
| 52 | + # "name", "base_increment" |
| 53 | + # ) |
| 54 | + |
| 55 | + colnames(results) <- to_snake_case(colnames(results)) |
| 56 | + |
| 57 | + # since we are not sure to get the same data from the api forever |
| 58 | + # I will programmatically modify what we receive rather than set colnames manually |
| 59 | + |
| 60 | + # will no longer re-order the table |
| 61 | + # common_cols <- c( |
| 62 | + # "symbol", "name", "enable_trading", |
| 63 | + # "base_currency", "quote_currency", |
| 64 | + # "market", # TOOD: added market column; might remove later |
| 65 | + # "base_min_size", "quote_min_size", |
| 66 | + # "base_max_size", "quote_max_size", |
| 67 | + # "base_increment", "quote_increment", |
| 68 | + # "price_increment", "fee_currency" |
| 69 | + # ) |
| 70 | + # data.table::setcolorder(results, c(common_cols, setdiff(colnames(results), common_cols))) |
| 71 | + |
| 72 | + numeric_cols <- c( |
| 73 | + "base_min_size", "quote_min_size", "base_max_size", |
| 74 | + "quote_max_size", "base_increment", "quote_increment", |
| 75 | + "price_increment", "price_limit_rate", "min_funds" |
| 76 | + ) |
| 77 | + |
| 78 | + # sandbox api does not have "min_funds" column |
| 79 | + # filter out columns that are not in the data; warn user that they are not in the data |
| 80 | + numeric_missing_cols <- setdiff(numeric_cols, colnames(results)) |
| 81 | + |
| 82 | + if (length(numeric_missing_cols) > 0) { |
| 83 | + rlang::warn(stringr::str_interp("The following columns are not in the data: ${collapse(numeric_missing_cols)}")) |
| 84 | + |
| 85 | + # keep only columns that are in the data |
| 86 | + numeric_cols <- numeric_cols[!numeric_cols %in% numeric_missing_cols] |
| 87 | + } |
| 88 | + |
| 89 | + logical_cols <- c("is_margin_enabled", "enable_trading") |
| 90 | + |
| 91 | + logical_missing_cols <- setdiff(logical_cols, colnames(results)) |
| 92 | + |
| 93 | + if (length(logical_missing_cols) > 0) { |
| 94 | + rlang::warn(stringr::str_interp("The following columns are not in the data: ${collapse(logical_missing_cols)}")) |
| 95 | + |
| 96 | + # keep only columns that are in the data |
| 97 | + logical_cols <- logical_cols[!logical_cols %in% logical_missing_cols] |
| 98 | + } |
| 99 | + |
| 100 | + ## ----------------- |
| 101 | + results[, (numeric_cols) := lapply(.SD, as.numeric), .SDcols = numeric_cols] |
| 102 | + # results[, colnames(results)[6:12] := lapply(.SD, as.numeric), .SDcols = 6:12] |
| 103 | + |
| 104 | + results[, c("symbol", "name") := lapply(.SD, prep_symbols, revert = TRUE), .SDcols = c("symbol", "name")] |
| 105 | + # results[, colnames(results)[1:2] := lapply(.SD, prep_symbols, revert = TRUE), .SDcols = 1:2] |
| 106 | + |
| 107 | + results[, (logical_cols) := lapply(.SD, as.logical), .SDcols = logical_cols] |
| 108 | + |
| 109 | + # data.table::setorder(results, base_currency, quote_currency) |
| 110 | + data.table::setorder(results, symbol, fee_currency) |
| 111 | + |
| 112 | + # return the result |
| 113 | + return(results[]) |
| 114 | +} |
0 commit comments