get.price=function(coin='BTC',value='USD',time.frame=NULL){
time.frame=tolower(time.frame)
options(warn=-1)
if(length(coin)==1){
#single coin
if(length(time.frame)==0){
#current price
lnk=paste0('https://min-api.cryptocompare.com/data/price?fsym=',coin,'&tsyms=',paste0(value,collapse = ','))
x=jsonlite::fromJSON(readLines(lnk))
}else{
#historical price
if(time.frame %in% c('minute','hour','day')){
lnk=paste0('https://min-api.cryptocompare.com/data/v2/histo',time.frame,'?fsym=',coin,'&tsym=',value,'&limit=2000')
x=jsonlite::fromJSON(readLines(lnk))
x=data.table::data.table(x$Data$Data)
x=data.table::data.table(
t= as.POSIXct(x$time,origin='1970-01-01'),
h= x$high,
l= x$low,
o= x$open,
c= x$clos,
vol= x$volumeto)
}else{
stop('please make sure the time.frame is either: minute, hour, or day')
}
}
}else{
#multiple ocins
if(!is.null(time.frame))message('no historical day available for multiple coins...')
lnk=paste0('https://min-api.cryptocompare.com/data/pricemulti?fsyms=',paste0(coin,collapse = ','),'&tsyms=',paste0(value,collapse = ','))
x=jsonlite::fromJSON(readLines(lnk))
}
options(warn=0)
return(x)
}
get.market.cap <- function(n=100,value='USD'){
options(warn = -1)
steps <- ceiling(n/100)-1
dbOut <- data.table()
for(page in 0:steps){
lnk <- paste0('https://min-api.cryptocompare.com/data/top/mktcapfull?limit=100&tsym=',value,'&page=',page)
x <- jsonlite::fromJSON(readLines(lnk))$Data
# x.raw <- as.data.table(x$RAW)[,c(6,38,39)]
x.raw <- as.data.table(x$RAW[[value]])
x.raw <- x.raw[,.(PRICE,SUPPLY,MKTCAP)]
names(x.raw) <- c('price','supply','marketcap')
selection <- data.table(
name = as.data.table(x$CoinInfo)$FullName,
ticker = as.data.table(x$CoinInfo)$Name,
type = as.data.table(x$CoinInfo)$ProofType,
algo = as.data.table(x$CoinInfo)$Algorithm,
max.supply = as.data.table(x$CoinInfo)$MaxSupply
)
selection <- cbind(
selection,
x.raw
)
dbOut <- rbind(dbOut,selection)
}
dbOut[,rank:=1:nrow(dbOut)]
dbOut[,value:=value]
dbOut=data.table(dbOut[,c('rank','name','ticker','type','algo','price','value','supply','marketcap','max.supply')])
dbOut=head(dbOut,n)
options(warn=0)
return(dbOut)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.