HODL-dar: A Cryptocurrency Risk Analysis Tool
05 September 2021
Abstract:
In this post we take a look at the risk factors behind four major cryptocurrencies as well as those behind a large cap index and gold. To communicate our message we use R to build a Dash React.js-based app with interactive Plotly.js graphs and daily-updated data. The scripts and dependency environment are then containerized using Docker and deployed via Git to the Heroku cloud hosting service.
The app is available at: https://crypto-risk-analysis.herokuapp.com
Note: Heroku stopped maintaining free dynamos starting in September of 2022, and it is no longer being maintained.
Background:
Cryptocurrency. The word is almost incapable of drawing a neutral response, whether it be fanaticism, scorn, or plain confusion. What once was seen as a technophile’s novelty ten years ago is now the talk of major banks and national governments. Cryptocurrencies are now readily purchasable through digital brokers that offer modern apps; a far cry from the days of Mt. Gox. While they currently offer a way to transfer money across borders at a much lower cost, they also draw fears of crypto-colonialism. Regardless of opinion, the buzz created around the concept of a decentralized monetary system has led to widespread speculation. This in turn has led to what appears to be either a general unawareness or an outright disregard for the risks of placing, what to a good number, may be money well missed in a time of crisis. Whenever we invest we know there is a chance we may lose our principal in a vague sense. While firmly believing that you should make the best decision for your given situation (and definitely don’t take advice from strangers on the internet... ahem), this is easier said than done when the investor has few ways to readily quantify the risk of a potential or current investment. Unfortunately investments are a bit like wars, they are a lot easier to start than they are to stop. Everybody seems to put all of their effort into researching what to invest into, but very few give consideration on when they are going to get out. There may be no single agreed upon method with which to gauge every aspect of risk contained within an investment, but providing tools to quickly gauge certain risk factors offers a perspective of risk that will hopefully serve to educate the public. Just to be 100% clear: None of this is financial advice in any way. For some reason, I feel the need to clarify this to people willing to put their life savings into an asset based on a dog from a meme.
Computing Environment:
Containerization:Docker
Debian system-level dependencies:
apt-transport-https
build-essential
curl
gfortran
libatlas-base-dev
libbz2-dev
libcairo2
libicu-dev
liblzma-dev
libpango-1.0-0
libpangocairo-1.0-0
libpcre3-dev
libtcl8.6
libtiff5
libtk8.6
libx11-6
libxt6
locales
tzdata
zlib1g-dev
libgmp10
libgmp3-dev
libmpfr-dev
libssl-dev
pandoc
libxml2-dev
libv8-3.14-dev
libsasl2-dev
ca-certificates
file
gnupg2
lsb-release
automake
fontconfig
libpcre2-dev
perl
software-properties-common
sudo
wget
zlib1g-dev
libssl-dev
libcurl4-gnutls-dev
libquantlib0*
git-core
libatlas3-base
R version 3.5.2:
dashBootstrapComponents 0.12.2
dashHtmlComponents 1.0.3
dashCoreComponents 1.10.0
dash 0.5.0
plotly 4.9.3
ggplot2 3.2.0
QRM 0.4-31
timeSeries 3062.100
timeDate 3043.102
numDeriv 2016.8-1.1
mvtnorm 1.1-0
Matrix 1.2-15
gsl 2.1-6
MASS 7.3-51.1
moments 0.14
dplyr 0.8.3
tidyr 0.8.3
quantmod 0.4.18
TTR 0.23-4
xts 0.11-2
zoo 1.8-6
Data:
SPY: SPDR S&P 500 ETF Trust. 500 largest market cap weighted US common stocks based on the S&P 500 index.
GLD: SPDR Gold Shares. A trust holding gold bullion that is generally valued by the daily spot price of gold, less fund expenses.
BTC-USD: Price in US Dollars of Bitcoin.
ETH-USD: Price in US Dollars of Ethereum.
XRP-USD: Price in US Dollars of XRP.
DOGE-USD: Price in US Dollars of Dogecoin.
Data sourcing and cleaning:
The R package quantmod gives us an API to Yahoo Finance data. Data for the maximum duration of the daily closing price for each of the underlyings is updated daily and pulled into a dataframe in R. Cryptocurrencies do not have a closing time per se as trading operates more like the schedules of FOREX markets (although retail FOREX brokers are closed during weekends), but in this case the market close of the US markets was used as there is generally higher liquidity during these trading hours.
Data was filtered to the ten most recent years for all underlyings. The data was transposed to Tidy data format. The date column was changed to a date format. Rows with NA values were deleted (primarily weekends where there was data for cryptocurrencies but SPY and GLD were untradeable).
Next we created a dataframe consisting of the daily log differences of the underlyings. We repeat this process to make dataframes of the log difference on a weekly, monthly, quarterly, and annual periodicity.
Containerization and Deployment:
Dash is a React.js based application that has APIs for both R and Python (for R programmers it is incorporated with the Fiery framework). It quickly allows us to share data as well as allowing us to incorporate HTML, CSS, and Bootstrap to make our dashboards more customized and professional in appearance. Plotly.js is an interactive graphing system that allows users to zoom, orbit, pan, draw, unselect layers, and download snapshots of the graphs. Plotly contains an API for both R and Python (R programmers already familiar with the ggplot2 library should find it pretty intuitive).
Dash is a production-stable framework. Deploying apps to the Heroku Cloud Service (now owned by Salesforce) can be done for free with the free version of Dash; deploying to either AWS or Azure requires an enterprise license. Heroku does not natively support R, but the team at Plotly have created a base Docker image to help us deploy our Dashboard.
It is worth taking a moment to pause and go over what Docker and containerization means. When we run our R script locally, we are in our home. We have arranged all of our system requirements, we have the version of R that matches the countertops, and the packages we need tucked away nicely in a bookshelf. We take it for granted when we run our script that everything just works. What happens when we need to move to a new home, say a cloud service? The most logical thing to do would be to pack all of the things we need into boxes, throw it in the back of a moving van, and unpack it at the new home. If we wanted to be really efficient, we could even write a set of instructions on where to put things when we get to our new abode (i.e. “Put the books in the bookshelf.”). This is pretty much how Docker works. When we deploy to our Heroku environment, all we have is a blank Debian OS.
Our Dockerfile is our set of instructions on where to put stuff in our new home. The first layer we need to build on top of our OS are our system-level dependencies. R (and its packages) require certain system-level dependencies in order to be able to do things like plot our graphs, run all that fun javascript for our app's front-end, or perform the linear algebra needed to run the regressions on our Market Betas. After adding this first layer we then need to install all of the R packages needed to run our app. In order to do this we create an R script that runs during the build (init.R). This script installs all of the R packages we need. The last part of our dockerfile is a command telling the instance to run our R script containing our app.
If you have used the AWS CLI or Git before the Heroku CLI is fairly intuitive. After you create an account you can push the directory containing the scripts to Heroku (It takes awhile to build the image). Your Github repository can also be directly integrated to Heroku deployment. This CI/CD workflow greatly facilitates an Agile work environment and allows the project to be scaled to a larger working group as well as shared across multiple projects for access to useful code snippets. Each developer can edit scripts and run the application locally for testing and feedback while using Github to manage version control and group contributions (forks, merges, etc.). The Docker image ensures that no matter where it is developed, the application will work in the production environment. Whenever the working group has finished testing changes they have made in their local development environment, they can push their changes to the Github repository. Whenever changes have passed QA tests the updated scripts can be pushed to the client-facing Heroku environment via either the Heroku Git-based CLI or directly through Github integrated deployment. Continuous Integration, the cloud environment, and an Agile workflow all work to allow us to continuously improve our application for our clients in manageable bites while reducing downtime and allowing us to acheive scaleability. To anyone who hasn’t already, I highly reccomend the book The Phoenix Project for a really intuitive way of learning more about these concepts.
Exploratory Data Analysis:
Over just the last year Bitcoin has increased by roughly six times in value only to reduce to half its peak shortly after. To say it is volatile would be an understatement.
There is not a way to type into words the sigh I just let out. Dogecoin was priced at about 3 thousandths of a cent in February, then soared to 68.4 cents. I honestly have no idea how Elon Musk isn't in prison right now.
When we look at Ethereum after looking at the others, we can see that Dogecoin had two major spikes in price around the time of its rise in popularity in social media. Bitcoin seems to experience a more gradual peak in price preceding this, followed by a downturn around the same time that Dogecoin experienced a sharp fall in price. Ethereum also experienced a similar effect.
The price for XRP also experienced an extreme upturn in price in late April of 2021 similar to that shown by Dogecoin followed by a similar downturn. All of the cryptocurrencies have recovered to some extent from this downturn, with XRP’s recovery being more similar to that of Bitcoin and Ethereum.
Looking at two other well-defined asset classes for contrast, in the same time period large cap stocks have trended steadily upward while the price of gold has had some volatility but overall has decreased.
Looking at the quarterly aggregated log returns seems to indicate that all of the cryptocurrencies have seen an increase in volatility over the last year, and are in general more volatile than large cap stocks or gold. Despite recent volatility increases, it appears that overall volatility has decreased for Ethereum since 2016 and in XRP since 2018. Dogecoin has experienced two major spikes in volatility over the last five years. Bitcoin seems to maintain generally constant swings in price.
The market risk factor, or Beta, compares the one day moves of the underlying to the one day moves in the market (the S&P 500 in practice). XRP has historically shown the strongest correlation to the market, but in reality all of these cryptocurrencies are strongly correlated to the market. Bitcoin has historically demonstrated the lowest correlation to the market in one day moves, but this is still a correlation of 0.696.
Value-at-Risk is one way to gauge risk. We take the one day moves and create a kernel density estimate of the losses. The Value-at-Risk is set to a 95% tolerance, meaning historically there was a 5% chance on any given day of lossing that amount of the investment amount. Losses are given in terms of log losses. Log differences are roughly equivalent to percentage when the value is below 1 (i.e. a value of 0.1 would represent a historic 5% chance on any day of suffering a los of at least 10% of your investment). The log approaches 1 but never reaches it, therefore values near or higher than 1 should be viewed as “losing nearly everything” as a log value is only and approximation of a percentage.
Estimated Shortfall is a concept that builds on Value-at-Risk that attempts to provide a more realistic perspective to downside loss. This is because downturns are frequently quick and violent events that show up in the far ends of our long and skinny distribution tails. Estimated Shortfall accounts for this by taking the mean of the tail remaining beyond our 95% threshhold. This can be roughly interpreted as “historically when a 5% likelihood of downside was exceeded, on average the investment loss X% in value.” Gold exhibits the smallest historic daily Expected Shortfall of 1.9% while Dogecoin exhibits the largest historic daily Expected Shortfall of 19.8%.
Disclaimer:
The content of this article and related application are for education purposes only and do not constitute financial advice. Miguel Sebastián de la Mata does not provide recommendations. Those looking for investment advice should seek out a registered professional. Miguel Sebastián de la Mata and any parties affiliated with him or the contents of this blog or related application are not responsible for investment actions taken by viewers and this content should not be used as a basis for investment trades.