Free Coin Data: Batch download coin data using R

Will use the crypto compare API. The data is free. We will download daily data.

Required packages


Next download the full coin list from crypto compare and save the list to a data frame

# Obtain coin list
response = fromJSON("")
df =$Data, fill=TRUE))

The format is in a list, for ease we will save it to a .csv

# Write coin list to .csv
write.csv(df,file="D:/R Projects/Final Scripts/BitCoin Scripts/coin_list.csv")

Load the list of symbols from the .csv file and save the symbols in a vector

# Load Tickets From Coin List <- read.csv("D:/R Projects/Final Scripts/BitCoin Scripts/coin_list.csv", header=TRUE, stringsAsFactors = FALSE)
tickers <- c($Symbol)

Clean the list

# Those symbols with * at the end of their name
# Remove *
tickers <- gsub("[*]", "", tickers)

Next we write a loop to loop through the ticker symbol vector and download each symbol to .csv format. As an extra bonus, we will also plot the close price and save the plot to our output folder.

# Download Daily Data 
# Loop for downloading all .csvs 
# Save each coin close plot
# Add error catching to loop
# Add completion time of each iteration
for (i in 1:length(tickers)) {
  next.coin <- tickers[i] # next coin in ticker vector
coin_daily <- fromJSON(paste0("",next.coin,"&tsym=USD&allData=true&e=CCCAGG"))
df <- data.frame("time" = coin_daily$Data$time, "close" = coin_daily$Data$close,"open" = coin_daily$Data$open,"high" = coin_daily$Data$high,"low" = coin_daily$Data$low,"volumefrom" = coin_daily$Data$volumefrom,"volumeto" = coin_daily$Data$volumeto)
# Save plot to output to folder
# Set path, set png plot size
# ggplot 2 to create line plot 
mytitle = paste0(next.coin) # Close graphic device before next plot
p <- ggplot(df, aes(time)) +
  geom_line(aes(y=close), colour="red") +
  ggtitle(mytitle, subtitle = "") +
  theme(plot.title = element_text(hjust=0.5),plot.subtitle =element_text(hjust=0.5))
ggsave(path="D:/R Projects/Final Scripts/BitCoin Scripts/daily_plot",paste(next.coin,".png"))
# Save each file to .csv
write.csv(df,paste0(file="D:/R Projects/Final Scripts/BitCoin Scripts/data/",next.coin,".csv"))
ptm0 <- proc.time()
ptm1=proc.time() - ptm0
cat('\n','Iteration',i,'took', time, "seconds to complete")
  }, error = function(e) { print(paste("i =", i, "failed:")) })

Author: Andrew Bannerman

Integrity Inspector. Quantitative Analysis is a favorite past time.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s