7 min read

Cryptocurrencies

Introduction

In this post we will examine the prices of the cryptocurrencies Bitcoin (BTC) and Litecoin (LTC). Our plots will use the powerful graphing package plotly and the OHLC (Open High Low Close) pricing data from Yahoo Finance for our analysis.

Once we have our data we will follow the guide from Plotly’s Website to create candlestick charts using the OHLC data.

Section 1: Setup

Our core packages for this project will be plotly and quantmod, however, we also have important dependencies such as the xts package. Below we load our packages.

library(plotly)
library(quantmod)
library(here)

With our packages and dependencies loaded we load up our data. Using the quantmod package and its function getSymbols we load data for Bitcoin, Litecoin, and also the Litecoin price in terms of Bitcoin from Yahoo as xts time series objects.

Notice that we use env=NULL, this allows us to store our variables without the dash “-” character. Since the data in our xts series will by default use “BTC-USD.Open” we also rename the columns in our xts series.

# load Bitcoin data
BTC <- getSymbols("BTC-USD", src='yahoo', env=NULL)
names(BTC) <- c("BTC.Open", "BTC.High", "BTC.Low",
                "BTC.Close", "BTC.Volume", "BTC.Adjusted")

# load Litecoin data
LTC <- getSymbols("LTC-USD", src='yahoo', env=NULL)
names(LTC) <- c("LTC.Open", "LTC.High", "LTC.Low",
                "LTC.Close", "LTC.Volume", "LTC.Adjusted")

# load Litecoin price in terms of Bitcoin
LTCBTC <- getSymbols("LTC-BTC", src='yahoo', env=NULL)
names(LTCBTC) <- c("LTCBTC.Open", "LTCBTC.High", "LTCBTC.Low",
                   "LTCBTC.Close", "LTCBTC.Volume", "LTCBTC.Adjusted")

With our OHLC time series loaded we also load data on Bitcoin and Litecoin halvings. This data was aggregated from various sources. A halving refers to the event when a cryptocurrency such as Bitcoin reduces the amount of new Bitcoin mined (or added to the blockchain) by 50%. In the case of Bitcoin and Litecoin the halving ensures there is a maximum amount of Bitcoin and Litecoin that will ever exist.

BTC_halvings <- read.csv(here('csv', 'crypto', 'july2021',
                              'BTC_halving_dates.csv'))
LTC_halvings <- read.csv(here('csv', 'crypto', 'july2021',
                              'LTC_halving_dates.csv'))

Section 2: Bitcoin

Bitcoin was invented in 2008 and became usable starting January 9, 2009 with the first block in its blockchain. Satoshi Nakamoto is credited with creating Bitcoin, however, it is unclear who this person really is.

We now examine the daily OHLC values of Bitcoin using plotly. Our code to create a daily candlestick chart is shown below.

# convert Bitcoin xts data to dataframe for plotly compatibility
df <- data.frame(Date=index(BTC),coredata(BTC))

# create main plotly candlestick chart
fig_main <- df %>% plot_ly(x = ~Date, type="candlestick",
          open = ~BTC.Open, close = ~BTC.Close,
          high = ~BTC.High, low = ~BTC.Low) 

# create volume plot to attach to candlestick chart
fig_vol <- df %>% plot_ly(x=~Date, y=~BTC.Volume, type='bar',
                          name="BTC Volume",
                          colors=c('#17BECF','#7F7F7F'))
fig_vol <- fig_vol %>% layout(yaxis=list(title="Volume"))

# shows text for Bitcoin's first halving
halving1 <- list(text = "First Halving",
          x = BTC_halvings$Date[2],
          y = 1.02,
          xref = 'x',
          yref = 'paper',
          xanchor = 'left',
          showarrow = FALSE
)

# use shapes to create a line for Bitcoin's first halving
halving_line1 <- list(type = line,
          x0 = BTC_halvings$Date[2],
          x1 = BTC_halvings$Date[2],
          y0 = 0,
          y1 = 1,
          xref = 'x',
          yref = 'paper',
          line = list(color = 'black',
                      width = 0.5)
)

# shows text for Bitcoin's second halving
halving2 <- list(text = "Second Halving",
          x = BTC_halvings$Date[3],
          y = 1.02,
          xref = 'x',
          yref = 'paper',
          xanchor = 'left',
          showarrow = FALSE
)

# use shapes to create a line for Bitcoin's second halving
halving_line2 <- list(type = line,
          x0 = BTC_halvings$Date[3],
          x1 = BTC_halvings$Date[3],
          y0 = 0,
          y1 = 1,
          xref = 'x',
          yref = 'paper',
          line = list(color = 'black',
                      width = 0.5)
)

# shows text for Bitcoin's third halving
halving3 <- list(text = "Third Halving",
          x = BTC_halvings$Date[4],
          y = 1.02,
          xref = 'x',
          yref = 'paper',
          xanchor = 'left',
          showarrow = FALSE
)

# use shapes to create a line for Bitcoin's third halving
halving_line3 <- list(type = line,
          x0 = BTC_halvings$Date[4],
          x1 = BTC_halvings$Date[4],
          y0 = 0,
          y1 = 1,
          xref = 'x',
          yref = 'paper',
          line = list(color = 'black',
                      width = 0.5)
)

# add halving text and lines to a list
halvings <- list(halving1, halving2, halving3)
halving_lines <- list(halving_line1, halving_line2, halving_line3)

# add halving lists to main plot
fig_main <- fig_main %>% layout(shapes=halving_lines,
                      annotations=halvings)

# combine main plot and volume plot into a complete final plot
fig <- subplot(fig_main, fig_vol, heights=c(0.7, 0.2), nrows=2,
               shareX=TRUE, titleY=TRUE)

# add title and remove range slider to final plot
# note: range slider was removed to allow vertical 
# zooming and not just horizontal zooming
fig <- fig %>% layout(title = "Bitcoin Daily OHLC Candlestick Chart",
                      xaxis = list(rangeslider = list(visible = FALSE)))

This next block of code displays our candlestick chart

# plot our daily candlestick chart
fig

With our daily candlestick chart created we decided to create a monthly chart as well. The code for this is shown below. This is done by using appropriate aggregate functions for each month based on whether we are working with Open, High, Low, Close, or Volume data. Note that we only do a monthly chart for Bitcoin in this post.

# create a function to correctly aggregate our data
# each type of data requires a different aggregate function
# for example: "Open" is aggregated by the function "head" with
# the argument "n=1" to select the first "Open" for the month
monthly_BTC <- function() {
    output <- merge(apply.monthly(BTC$BTC.Open, head, n=1),
                    apply.monthly(BTC$BTC.High, max, na.rm=TRUE),
                    apply.monthly(BTC$BTC.Low, min, na.rm=TRUE),
                    apply.monthly(BTC$BTC.Close, tail, n=1),
                    apply.monthly(BTC$BTC.Volume, sum, na.rm=TRUE))
    return(output)
}

# We apply our aggregation function to create our monthly data
BTC_monthly <- monthly_BTC()

# Here we follow similar steps to above to create a monthly
# instead of daily candlestick plot with plotly
# note: we do not include halving dates
df <- data.frame(Date=index(BTC_monthly),coredata(BTC_monthly))

fig_main <- df %>% plot_ly(x = ~Date, type="candlestick",
          open = ~BTC.Open, close = ~BTC.Close,
          high = ~BTC.High, low = ~BTC.Low) 

fig_vol <- df %>% plot_ly(x=~Date, y=~BTC.Volume, type='bar', name="BTC Volume",
                          colors=c('#17BECF','#7F7F7F'))
fig_vol <- fig_vol %>% layout(yaxis=list(title="Volume"))

fig <- subplot(fig_main, fig_vol, heights=c(0.7, 0.2), nrows=2,
               shareX=TRUE, titleY=TRUE)
fig <- fig %>% layout(title = "Bitcoin Monthly OHLC Candlestick Chart",
                      xaxis = list(rangeslider = list(visible = FALSE)))

With our plotly candlestick chart set up we now examine our monthly data.

# plot our daily candlestick chart
fig

Section 3: Litecoin

Litecoin is a cryptocurrency inspired by Bitcoin. It was created in 2011 and became usable on October 13, 2011. The creator of Litecoin is Charlie Lee a Google employee at the time.

The differences between Litecoin and Bitcoin are small. The primary differences are that the maximum number of Litecoin is 4 times that of Bitcoin due to shorter block times. Bitcoin creates a new block in the blockchain approximately every 10 minutes while Litecoin does this about every 2.5 minutes.

Below we set up our plotly candlestick chart for Litecoin daily OHLC prices.

We now plot our Litecoin data below:

# Litecoin candlestick chart
fig

Section 4: Litecoin in terms of Bitcoin Market

To finish things up we examine the price of Litecoin in terms of Bitcoin. Due to the lower dollar value of Litecoin in comparison to Bitcoin we can see that it only takes a small fraction of Bitcoin to buy a whole Litecoin. Note that each Bitcoin is divisible into 100 million smaller units often called a Satoshi. Litecoin is also divisible into 100 million smaller units sometimes referred to as a Litoshi.

We now build our candlestick chart below using the Yahoo data.

We plot our chart below.

# Litecoin in terms of Bitcoin OHLC candlestick daily chart.
fig

Note in our chart above that our volume is calculated in Bitcoins, not U.S. Dollars.

Conclusion

Based on the data we looked at this time, it would appear that over time Litecoin loses its value against Bitcoin while occasionally making large gains. Another point of interest is that when we compare the general shape of our Bitcoin daily data to our monthly data we actually see roughly the same shape. I think seeing the monthly instead of just the daily is useful, however, since we can see more definitively how prices can change over a larger unit of time. Looking at at a yearly candlestick chart for example could be useful for understanding the long term trends.

In terms of the plotly package and the associated candlestick charts, it is clear that this is a very powerful tool for making useful and interactive plots. In fact, the ability to zoom in and look at individual candles in our candlestick chart allows us to make full use of our daily data without having to aggregate to a larger unit of time or look at a static zoomed in interval. In addition it is great to be able to add the volume to our charts in a seamless manner.