From b96315ba109d7ca108f3d0ec7f3bd35bafa0e246 Mon Sep 17 00:00:00 2001 From: Diffform Date: Thu, 21 Mar 2019 23:07:54 +0100 Subject: [PATCH] Adapt crypto_history to new coinlist setting, create fake "ranknow", adapt much more tidyverse-notation. --- NAMESPACE | 2 ++ R/crypto_history.R | 51 +++++++++++++++++++++---------------------- R/crypto_list.R | 10 ++++++--- man/crypto_history.Rd | 2 +- 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 5df7202..233b35b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -29,6 +29,8 @@ importFrom(dplyr,"summarise") importFrom(dplyr,'%>%') importFrom(dplyr,'arrange') importFrom(dplyr,'mutate') +importFrom(dplyr,left_join) +importFrom(dplyr,mutate) importFrom(dplyr,select) importFrom(grDevices,'rgb') importFrom(httr,"GET") diff --git a/R/crypto_history.R b/R/crypto_history.R index b6d744f..6cd85dc 100644 --- a/R/crypto_history.R +++ b/R/crypto_history.R @@ -31,11 +31,11 @@ #' This is the main function of the crypto package. If you want to retrieve #' ALL coins then do not pass a argument to crypto_history(), or pass the coin name. #' -#' @importFrom dplyr '%>%' 'mutate' 'arrange' +#' @importFrom dplyr '%>%' 'mutate' 'arrange' 'left_join' "group" "ungroup" "slice" #' @importFrom tidyr 'replace_na' #' @importFrom crayon 'make_style' #' @importFrom grDevices 'rgb' -#' @importFrom tibble 'tibble' 'as_tibble' +#' @importFrom tibble 'tibble' 'as_tibble' 'rowid_to_column' #' @importFrom cli 'cat_bullet' #' @importFrom lubridate 'mdy' #' @@ -53,7 +53,7 @@ #' #' # Retrieve 2015 history for all 2015 crypto currencies #' coin_list_2015 <- crypto_list(start_date_hist="20150101",end_date_hist="20151231",date_gap="months") -#' 2015_coins <- crypto_history(coins = coin_list_2015, start_date = "20150101", end_date="20151231") +#' coins_2015 <- crypto_history(coins = coin_list_2015, start_date = "20150101", end_date="20151231") #' } #' @name crypto_history #' @@ -75,7 +75,7 @@ crypto_history <- function(coins = NULL, limit = NULL, start_date = NULL, end_da message("XRP: rK59semLsuJZEWftxBFhWuNE6uhznjz2bK", appendLF = TRUE) message("\n") # only if no coins are provided - if (is.null(coins)) coins <- crypto_list(coin, start_date, end_date, coin_list) + if (is.null(coins)) coins <- crypto_list(coin=NULL, start_date, end_date, coin_list) if (!is.null(limit)) coins <- coins[1:limit, ] @@ -94,32 +94,31 @@ crypto_history <- function(coins = NULL, limit = NULL, start_date = NULL, end_da loop_data[[i]] <- scraper(to_scrape$attributes[i], to_scrape$slug[i], sleep) } - results <- do.call(rbind, loop_data) %>% tibble::as.tibble() + results <- do.call(rbind, loop_data) %>% tibble::as_tibble() if (length(results) == 0L) stop("No data currently exists for this crypto currency.", call. = FALSE) - market_data <- merge(results, coin_names, by = "slug") - colnames(market_data) <- c("slug", "date", "open", "high", "low", "close", "volume", - "market", "symbol", "name", "ranknow") - market_data <- market_data[c("slug", "symbol", "name", "date", "ranknow", "open", - "high", "low", "close", "volume", "market")] - market_data$date <- lubridate::mdy(market_data$date, locale = platform_locale()) + market_data <- results %>% left_join(coin_names, by = "slug") + colnames(market_data) <- c("date", "open", "high", "low", "close", "volume", + "market", "slug", "symbol", "name") + # calculate fake ranknow based on markt cap on the last date in the dataset + market_data %>% dplyr::group_by(symbol) %>% dplyr::arrange(desc(date)) %>% dplyr::slice(1) %>% dplyr::ungroup() %>% + tibble::rowid_to_column("ranknow") %>% dplyr::select(slug,ranknow) - market_data[, 5:11] <- apply(market_data[, 5:11], 2, function(x) gsub(",", "", - x)) - market_data[, 7:11] <- apply(market_data[, 7:11], 2, function(x) gsub("-", "0", - x)) - market_data$volume <- market_data$volume %>% tidyr::replace_na(0) %>% as.numeric() - market_data$market <- market_data$market %>% tidyr::replace_na(0) %>% as.numeric() - market_data[, 5:11] <- apply(market_data[, 5:11], 2, function(x) as.numeric(x)) - market_data <- na.omit(market_data) - - market_data <- market_data %>% dplyr::mutate(close_ratio = (close - low)/(high - - low) %>% round(4) %>% as.numeric(), spread = (high - low) %>% round(2) %>% - as.numeric()) - - market_data$close_ratio <- market_data$close_ratio %>% tidyr::replace_na(0) - history_results <- market_data %>% dplyr::arrange(ranknow, date) + history_results <- market_data %>% + # create fake ranknow + dplyr::left_join(market_data %>% dplyr::group_by(symbol) %>% dplyr::arrange(desc(date)) %>% dplyr::slice(1) %>% dplyr::ungroup() %>% + tibble::rowid_to_column("ranknow") %>% dplyr::select(slug,ranknow), by="slug") %>% + dplyr::select(slug,symbol,name,date,ranknow,open,high,low,close,volume,market) %>% + dplyr::mutate(date=lubridate::mdy(date, locale = platform_locale())) %>% + dplyr::mutate_at(vars(open,high,low,close,volume,market),~gsub(",","",.)) %>% + dplyr::mutate_at(vars(high,low,close,volume,market),~gsub("-","0",.)) %>% + dplyr::mutate_at(vars(open,high,low,close,volume,market),~as.numeric(tidyr::replace_na(.,0))) %>% + dplyr::mutate(close_ratio = (close - low)/(high - low) %>% round(4) %>% as.numeric(), + spread = (high - low) %>% round(2) %>% as.numeric()) %>% + dplyr::mutate_at(vars(close_ratio),~as.numeric(tidyr::replace_na(.,0))) %>% + dplyr::group_by(symbol) %>% + dplyr::arrange(ranknow,desc(date)) return(history_results) } diff --git a/R/crypto_list.R b/R/crypto_list.R index 2c7384a..8b9c59e 100644 --- a/R/crypto_list.R +++ b/R/crypto_list.R @@ -25,7 +25,7 @@ #' @importFrom tibble tibble #' @importFrom jsonlite fromJSON #' @importFrom lubridate today ymd -#' +#' @importFrom dplyr left_join mutate #' #' @examples #' \dontrun{ @@ -64,9 +64,13 @@ } } coins <- out_list + # always get list for data validation + json <- "https://s2.coinmarketcap.com/generated/search/quick_search.json" + out_list_recent <- jsonlite::fromJSON(json) + # validate name & slug via symbol from recent list + coins <- coins %>% dplyr::left_join(out_list_recent %>% select(symbol,name,slug) %>% rename(slug_main=slug, name_main=name),by="symbol") %>% + mutate(name=ifelse(is.na(name_main),name,name_main),slug=ifelse(is.na(slug_main),slug,slug_main)) %>% select(symbol, name, slug, hist_date) if (is.null(end_date_hist)){ - json <- "https://s2.coinmarketcap.com/generated/search/quick_search.json" - out_list_recent <- jsonlite::fromJSON(json) coins <- rbind(out_list,out_list_recent %>% select(name,symbol,slug) %>% dplyr::mutate(hist_date=lubridate::today())) } } else { diff --git a/man/crypto_history.Rd b/man/crypto_history.Rd index 6357331..046cc6a 100644 --- a/man/crypto_history.Rd +++ b/man/crypto_history.Rd @@ -57,6 +57,6 @@ all_coins <- crypto_history(start_date = '20180101') # Retrieve 2015 history for all 2015 crypto currencies coin_list_2015 <- crypto_list(start_date_hist="20150101",end_date_hist="20151231",date_gap="months") -2015_coins <- crypto_history(coins = coin_list_2015, start_date = "20150101", end_date="20151231") +coins_2015 <- crypto_history(coins = "coin_list_2015", start_date = "20150101", end_date="20151231") } }