Skip to content

Commit

Permalink
Adapt crypto_history to new coinlist setting, create fake "ranknow", …
Browse files Browse the repository at this point in the history
…adapt much more tidyverse-notation.
  • Loading branch information
sstoeckl committed Mar 21, 2019
1 parent 35645c5 commit b96315b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
51 changes: 25 additions & 26 deletions R/crypto_history.R
Original file line number Diff line number Diff line change
Expand Up @@ -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'
#'
Expand All @@ -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
#'
Expand All @@ -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, ]
Expand All @@ -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)
}
10 changes: 7 additions & 3 deletions R/crypto_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#' @importFrom tibble tibble
#' @importFrom jsonlite fromJSON
#' @importFrom lubridate today ymd
#'
#' @importFrom dplyr left_join mutate
#'
#' @examples
#' \dontrun{
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion man/crypto_history.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b96315b

Please sign in to comment.