Introduction
In this paper we will conduct a thorough analysis for some of the main cryptocurrencies in 2021. This will involve several steps such as importing and accessing data through API’s, cleaning / manipulating the data and finally performing the necessary analysis expressed in various visualizations to obtain valuable insights.
Part 1: Accessing and Manipulating the Data
In order to perform the next steps, first we need to install and then load the following libraries:
library(tidyverse) #collection of R packages for data science
library(lubridate) #helps working with date and time
library(readxl) #easily reads excel data
library(highcharter) #customizable js charting library
library(tidyquant) #integrates resources for analyzing financial data
library(timetk) #tool for working with timeseries
library(tibbletime) # allows for the creation of time aware tibbles
library(quantmod) # development and testing for trading models
library(PerformanceAnalytics)
library(scales)
library(dplyr)
Now that we have the right libraries its time to import the cryptocurrencies we will analyze. The list of the major cryptocurrencies we have chosen is based on: a) US News b) Investopedia c) Coinbase
Out of this list we selected the following next to their symbols): Bitcoin USD -> BTC-USD; Ethereum USD -> ETH-USD; Litecoin USD -> LTC-USD; Cardano USD -> ADA-USD; Stellar USD -> XLM-USD;
Before importing the data for each of the abovementioned, we store the symbols in a vector (which will be later on utilized for represnting the data):
symbols <- c("BTC-USD","ETH-USD","LTC-USD","ADA-USD","XLM-USD")
In order to access the data we use the quantmod package, to access the data from Yahoo Finance.Current src methods available are: yahoo, google, MySQL, FRED, csv, RData, oanda, and av. Data is loaded without user assignment by default.
Then, we select the beginning and ending date for the historical data (we have chosen 3 years, encompassing even the newest cryptocurrencies).
Since the data imported returns an object with the opening price,closing price, adjusted price, daily high, daily low and daily volume we will seek to obtain the adjusted price from the price series. To have a better understanding, lets review the script which accomplishes the abovementioned:
prices <-getSymbols(symbols,
src ='yahoo',
from = "2018-12-31",to = "2021-06-03",
auto.assign = TRUE,
warnings = FALSE)%>%
map(~Ad(get(.)))%>% #returns the list by default
reduce(merge)%>% #merges the list in one xts object
`colnames<-`(symbols) #rename columns according to symbols
Lets review the top beginning values with the column names to check if the data is imported and ready for the following transformations.
head(prices, 5)
## BTC-USD ETH-USD LTC-USD ADA-USD XLM-USD
## 2018-12-31 3742.700 133.3683 30.46822 0.041063 0.112813
## 2019-01-01 3843.520 140.8194 31.97993 0.042547 0.115930
## 2019-01-02 3943.409 155.0477 33.43368 0.045258 0.119331
## 2019-01-03 3836.741 149.1350 32.02670 0.042682 0.113825
## 2019-01-04 3857.718 154.5819 32.40417 0.043812 0.115361
Part 2 - Log Returns and Simple Returns
Since all the data is properly imported its time to manipulate it, to better fit our analysis. The first step it to convert the daily prices to monthly log returns (The log return for a time period is the sum of the log returns of partitions of the time period. For example the log return for a year is the sum of the log returns of the days within the year.)
In order to accomplish that, we will use the quantmode package as follows:
prices_monthly <-to.monthly(prices, #converts daily to monthly
indexAt = "lastof", #index last day for returns
OHLC = FALSE) #"Open,High, Low, Close format"
## Warning in to.period(x, "months", indexAt = indexAt, name = name, ...): missing
## values removed from data
After performing this function we have one value per month, for each last day. Now we can use the log method to return the log for each value.
asset_returns_xts <-
Return.calculate(prices_monthly,
method = "log") %>%
na.omit()
Let’s verify that our operations have been properly performed.
head(asset_returns_xts, 5)
## BTC-USD ETH-USD LTC-USD ADA-USD XLM-USD
## 2019-01-31 -0.07917695 -0.21971527 0.03812259 -0.067727326 -0.31134667
## 2019-02-28 0.10868489 0.24472811 0.37897896 0.116676351 0.03416655
## 2019-03-31 0.06298889 0.03427236 0.27307008 0.480803574 0.23034305
## 2019-04-30 0.26492813 0.13622134 0.20099760 0.008565852 -0.07419444
## 2019-05-31 0.47156055 0.50278991 0.43304401 0.240153267 0.29062911
Since everything seems fine, we can go ahead and visualize the log return for each cryptocurrency. For this task we will use the highchart library and assign the asset returns for each symbol. The code output should appear as below:
highchart(type = "stock")%>%
hc_title(text = "Monthly Log Returns")%>%
hc_add_series(asset_returns_xts[, symbols[1]],
name = symbols[1])%>%
hc_add_series(asset_returns_xts[, symbols[2]],
name = symbols[2])%>%
hc_add_series(asset_returns_xts[, symbols[3]],
name = symbols[3])%>%
hc_add_series(asset_returns_xts[, symbols[4]],
name = symbols[4])%>%
hc_add_series(asset_returns_xts[, symbols[5]],
name = symbols[5])%>%
hc_add_theme(hc_theme_ffx()) %>%
hc_navigator(enabled = FALSE) %>%
hc_scrollbar(enabled = FALSE) %>%
hc_exporting(enabled = TRUE) %>%
hc_legend(enabled = TRUE)
One of the major advantages of log returns is that their returns are additive, making many of time series applications easier to calculate. However, lets see how simple returns look like in comparison:
asset_returns_xts <-
Return.calculate(prices_monthly,
method = "simple") %>%
na.omit()
Lets graph the results again, similarly to the log returns:
highchart(type = "stock")%>%
hc_title(text = "Monthly Simple Returns")%>%
hc_add_series(asset_returns_xts[, symbols[1]],
name = symbols[1])%>%
hc_add_series(asset_returns_xts[, symbols[2]],
name = symbols[2])%>%
hc_add_series(asset_returns_xts[, symbols[3]],
name = symbols[3])%>%
hc_add_series(asset_returns_xts[, symbols[4]],
name = symbols[4])%>%
hc_add_series(asset_returns_xts[, symbols[5]],
name = symbols[5])%>%
hc_add_theme(hc_theme_ffx()) %>%
hc_navigator(enabled = FALSE) %>%
hc_scrollbar(enabled = FALSE) %>%
hc_exporting(enabled = TRUE) %>%
hc_legend(enabled = TRUE)
We can distinguish the trends that most cryptocurrencies have in the 3 year span, and also see that log returns follow similar patterns to the simple returns visualized above.
However, given the density of all the lines combined, it would be much better to compare the values individually. For that reason we will assign a histogram to each cryptocurrency.
At first we will assign a highchart histogram function for each chart and histogram. There, we will assign to them the symbols, color, themes and legends. To get a better grasp of the structure lets review the script below:
hc_hist_fun <-function(n = 1, object, color){ #assign function to object and color
hc_hist <-hist(object[, symbols[n]], #include the symbols within hist object
breaks = 50,
plot = FALSE)
hchart(hc_hist, color = color)%>% #to each histogram, assign color
hc_title(text =paste #use the given text for Title in each symbol
(symbols[n],
"Log Returns Distribution",
sep = " "))%>%
hc_add_theme(hc_theme_ffx())%>%
hc_exporting(enabled = TRUE)%>%
hc_legend(enabled = FALSE)
}
Now we can proceed and review individually the returns in single histograms. We represent the value by the value given in each function as below:
hc_hist_fun(1, asset_returns_xts, "#8085e9")
Above we have a visual representation of the Bitcoin simple return based on the historical data provided. Lets look at the other cryptocurrencies:
hc_hist_fun(2, asset_returns_xts, "f15c80")
Given the high volatility in the cryptocurrencies it is hard to make definite conclusions, hence I will reserve my comments in the end of the analysis and provide a breakdown of my interpretations.
At the meantime lets take a look at the other cryptocurrencies:
hc_hist_fun(3, asset_returns_xts, "#e4d354")
(Note: At the meantime feel free to write notes and your own comment for each chart utilized):
hc_hist_fun(4, asset_returns_xts, "#2b908f")
hc_hist_fun(5, asset_returns_xts, "#91e8e1")