Analyzing digital currency in R

Introduction:

One of the groundbreaking innovations in technology is the development of cryptocurrencies. However, the popularity of digital currencies as an investment is up for debate and one of the reason for this is the high volatility observed in digital currency market. Yermack (2018) investigates digital currency market and compares the volatility in cryptocurrency with foreign exchange market.

In the present blog post i provide a quick overview of crypto package available in R which can be used to analyze the digital currency market. Packages such as ggplot2, dplyr, and tidyquant can be used in conjunction with crypto package to perform an in depth analysis of the crypto market. Even though this post is inspired by Yermack (2018) it is by no  means an attempt at replication of the results.

Data:

#####################################
#load the libraries
#####################################
library(crypto)
library(ggplot2)
library(tidyquant)

####################################
# get all traded crypto
####################################
all_crypto <- getPrices()
head(all_crypto)

The getPrices() function from the crypto package will extract the latest prices of all the digital currencies. In addition to the latest prices the function downloads 14 columns which provides additional information. The head() function will provide a quick summary of the data downloaded. At the time of writing this post there were 1664 digital currencies. The crypto package extracts data from the https://coinmarketcap.com .

Extracting historical prices of Coins:

Now, in order to extract historical prices of specific coins we can use the getCoins() function. The getCoins() function uses the name of the coin or the symbol to extract historical prices. We can also specify an argument such as a start date to extract prices from a specific start date.  In the following example we would extract the prices of five different digital currencies and create a plot to examine the trend and volatility over time.

#####################################
#get Data
#####################################
btc <- getCoins("Bitcoin")
ltc <- getCoins("Litecoin")
eth <- getCoins("Ethereum")
bch <- getCoins("Bitcoin Cash")

btc %>% ggplot(aes(x=date, y =close)) +
  geom_line()+
  theme_minimal()+
  labs(title = "Bitcoin Trend (USD)",
       subtitle = "Source: https://coinmarketcap.com/")

Following plot shows the historical trend in Bitcoin since 2013. Bitoin reached its highest peak towards the end of 2017 and has been declining since.

bitcoin_final
Trend in bitcoin since 2013

We can also plot all the digital currencies on the same plot to observe the relative volatility. In order to do that we will combine all the individual data frames into one big data frame. The rbind() function can be used to combine individual data frames.

crypto <- rbind(btc,ltc,eth,bch)
#######################################
#plotting the crypto data
#######################################
ggplot(crypto,aes(x=date, y =close, colour = symbol)) +
  geom_line()+
  scale_colour_manual(name ="Tickers",values=c("#F8766D", "#A3A500","#00BF7D", "#00B0F6","#E76BF3"))+
  theme_minimal()+
  labs(ylab = "close (USD)",title = "Cryptocurrency Trend",
       subtitle = "Source: https://coinmarketcap.com/",
       caption = "@opendataandr")

Following plot provides a comparative view. We observe that Bitcoin definitely dominates the digital currency market.  In order to get a better view of the trend in other cryptocurrency we should remove the Bitcoin and this will show the movements and volatility in other digital currencies.

crypto
Trend in the Digital Currency

Comparing volatility in the Cryptocurrencies with FX market:

One of the metrics observed by investors is volatility of an instrument. Hence, we compare the volatility in the digital currency with that of foreign exchange market. Specifically we compare volatility, since the beginning of 2018, in the Euro and GBP with digital currencies.

The tidyquant package is used to extract the EUR/USD and GBP/USD exchange rates.

########################################################
#calculating volatility of top 3 coins with other assets
########################################################
btc <- getCoins("BTC",start_date = '20180101') %>% mutate(pct = Delt(close))
eth <- getCoins("ETH",start_date = '20180101') %>% mutate(pct = Delt(close))
xrp <- getCoins("XRP",start_date = '20180101') %>% mutate(pct = Delt(close))
###########################
# calculate the volatility:
###########################
btc_vl = sd(btc$pct, na.rm = TRUE)
eth_vl <- sd(eth$pct, na.rm = TRUE)
xrp_vl <- sd(xrp$pct, na.rm = TRUE)

#####################################
# Estimate the volatility of FX market:
#####################################
eur_usd <- tq_get("EUR/USD",get = "exchange.rates",from = "2018-01-01") %>% mutate(pct = Delt(exchange.rate))
eurusd_vl <- sd(eur_usd$pct, na.rm = TRUE)
gbp_usd <- tq_get("GBP/USD",get = "exchange.rates",from = "2018-01-01") %>% mutate(pct = Delt(exchange.rate))
gbpusd_vl <- sd(gbp_usd$pct, na.rm = TRUE)

vol.data = data.frame(comp = c("btc","eth","xrp","eurusd","gbpusd"), vol = c(btc_vl,eth_vl,xrp_vl,eurusd_vl,gbpusd_vl))

##################################
#plot the data 
##################################
ggplot(vol.data, aes(x= reorder(comp,vol), y = vol, fill = vol))+
geom_bar(stat = "identity")+
theme_bw()+
guides(fill = FALSE)+
labs(x = "", y = "Standard Deviation", title ="Comparison of Volatility in Crypto Currency and FX",
subtitle="Standard Deviation of daily percentage change (USD)")

We observe that the volatility in the digital currencies is much higher than the volatility observed in the FX market. The highly volatile nature of digital currencies makes them a risky investment.

fx_crytpo
Comparison of Volatility between Cryptocurrencies and FX market

What Next?

The main aim of writing this post was to introduce readers to the crypto package and its functionalities. We have only performed some simple analysis however one can go a step further and calculate moving averages or add trend lines.The tidyquant package provides many useful functions to extract equity prices and economic data which can be used to perform comparative analysis, summary statistics, create portfolios.

References:

  1. Yermack  (2018) : http://www.nber.org/reporter/2018number1/yermack.html

 

Leave a Reply

WordPress.com.

Up ↑

%d bloggers like this: