Charting Historical Cryptocurrency Data with R

ZEN Chart

In this example, we’ll use R to retrieve historical crypto currency data from the CryptoCompare API. We will then add a few technical indicators. Finally, we’ll create a chart to display our data. Once you understand this process, you’ll be able to work with a multitude of freely available API’s.

CryptoCompare API: https://min-api.cryptocompare.com/

Step 1: Acquire JSON data and convert to a data frame.

First, install and load the jsonlite package. This is used to read the JSON data from the CryptoCompare API.

install.packages('jsonlite')
require(jsonlite)

If you copy and paste the example from CryptoCompare into your browser, you’ll see a text output of the JSON data they provide.

https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=10

You provide the symbol to look up, BTC, and the unit of measure in which to display the value, such as USD. You can also limit the number of results to return. For this API call the units are in days. A limit of 10 would return the past 10 days of data in the format below:

{“time”:1532044800,”close”:7333.93,”high”:7676.55,”low”:7279.34,”open”:7477.1,”volumefrom”:80874.93,”volumeto”:603034414.79}

“time” is the timestamp in UNIX time.
“close” is the latest price at the end of the period.
“high” is the highest price in the period.
“low” is the lowest price in the period.
“open” is the starting price in the period.
“volumefrom” is the volume in the “from” symbol you chose. (BTC in this example)
“volumeto” is the volume in the “to” symbol you chose. (USD in this example)

The R function below will retrieve the historical data for any symbol on CryptoCompare and return it as a data frame.

getHistorical <- function(symbol, daysBack = 60) { symbol <- toupper(symbol) if (daysBack < 30) {daysBack = 30} cryptoCompare <- fromJSON(paste0('https://min-api.cryptocompare.com/data/histoday?aggregate=1&e=CCCAGG&extraParams=CryptoCompare&fsym=', symbol,'&limit=', daysBack, '&tryConversion=false&tsym=BTC'), flatten=TRUE) cryptoQuote <- as.data.frame(cryptoCompare[4]) if(nrow(cryptoQuote)< 30) {return('Failed')} names(cryptoQuote) <- c('unixtime','Close','High','Low','Open','volumeFrom','Volume') cryptoQuote$posixDate <- .POSIXct((cryptoQuote$unixtime), tz="UTC") cryptoQuote <- cryptoQuote %>%
select(Open, High, Low, Close, Volume, posixDate)
return(cryptoQuote)
}

To run the function, simply call it as shown below, with the symbol of your choice.
zenData <- getHistorical('zen', 90)

At this point, we have enough data that we could create a simple price chart for our crypto, but we're going to take it a step further by adding some technical indicators.

Step 2: Add Technical Indicators

Using the TTR package, we can easily add technical indicators to our historical crypto data. Let's add a 5-day Exponential Moving Average (EMA) and the Relative Strength Index (RSI). The TTR package has most of the major technical indicators, but we'll start with these two for now.

require(TTR)
zenData$EMA5Day <- EMA(zenData$Close, n=5) zenData$RSI <- RSI(zenData$Close, n = 14)

That's it. The TTR package takes care of all of the heavy lifting.

Step 3: Chart Our Data

To chart our crypto data, we'll use the ggplot2 package from the tidyverse. Rather than simply installing the ggplot2 package alone, we'll install and load the tidyverse package which includes ggplot2 as well as dplyr.

require(tidyverse)

# Price Chart
ggplot(zenData, aes(posixDate, Close)) + geom_line(size = 1) +
geom_line(aes(posixDate, zenData$EMA5Day), color = 'red')

The first line above creates the price chart. Setting size to 1 makes the line a bit thicker than the default. (Why the default isn't 1, I have no idea.)

The second line draws the 5-day EMA on the same chart. This works because the scales are the same. Unfortunately, the RSI is on a scale from 0 to 100. So, we'll need to put it in a separate chart.

# RSI Chart
ggplot(zenData, aes(posixDate, RSI)) + geom_line() +
labs(x = '', y = 'RSI') +
geom_line(aes(posixDate, 80), color = 'red') +
geom_line(aes(posixDate, 50), color = 'yellow') +
geom_line(aes(posixDate, 20), color = 'green')

The first line above draws the RSI line. Then, we add labels with the "labs" function. Finally, we add reference lines at 80, 50, and 20 to highlight when the RSI is in overbought or oversold territory.

Finally, to put it all together, we'll use the gridExtra package to show both charts in a single plot window.

# Set our charts to variables.
priceChart <- ggplot(zenData, aes(posixDate, Close)) + geom_line(size = 1) + geom_line(aes(posixDate, zenData$EMA5Day), color = 'red') # RSI Chart rsiChart <- ggplot(zenData, aes(posixDate, RSI)) + geom_line() + labs(x = '', y = 'RSI') + geom_line(aes(posixDate, 80), color = 'red') + geom_line(aes(posixDate, 50), color = 'yellow') + geom_line(aes(posixDate, 20), color = 'green') require(gridExtra) # Display both charts in a single plot. grid.arrange(priceChart, rsiChart)

We could add labels and clean things up a bit, but this gets you most of the way there. With the code above you should be able to take it the rest of the way.

Leave a Reply

Your email address will not be published. Required fields are marked *