# Analyzing Bitcoin in R in the year 2022 - Part 1

Pub: 14.11.22

In Finance.

Tags: coding r bitcoin analysis .

Retrieving Bitcoin transaction data

Bitcoin data is easily available e.g. via (http://api.bitcoincharts.com/v1/csv/). Specifically I used the file bitstampUSD.csv.gz which contains transactions on the Bitstamp exchange. At the time of writing the file size is a 474 MB download and 2.60 GB when unzipped.

Reading the data into R is simple, although it does take awhile due to the file size.

setwd('C:/MyFileLocation/')


Setup the data in a time series format for ease of use and structure it in a typical OHLCV (Open, High, Low, Close, Volume) format:

colnames(rawdata) <- c('timesec', 'USD', 'Volume')
sec_to_date = function(sec) {
as.POSIXct(sec, origin = '1970-01-01', tz = 'UTC')
}
Sys.setenv(TZ = "UTC")
rawdata$datetime <- sec_to_date(rawdata[,1]) rawdata <- xts(x = rawdata[,2:3], order.by = rawdata$datetime, tz = "UTC")
#Open
startpoints <- function (x, on = "months", k = 1) {
head(endpoints(x, on, k) + 1, -1)
}
rawdailyopen <- rawdata$USD[startpoints(rawdata, "days")] tclass(rawdailyopen) <- "Date" #High rawdailymax <- period.apply(rawdata$USD, INDEX = endpoints(rawdata, "days"), FUN = max)
tclass(rawdailymax) <- "Date"
#Low
rawdailymin <- period.apply(rawdata$USD, INDEX = endpoints(rawdata, "days"), FUN = min) tclass(rawdailymin) <- "Date" #Close rawdailyclose <- rawdata$USD[endpoints(rawdata, "days")]
tclass(rawdailyclose) <- "Date"
#Volume
rawdailyvol <- period.apply(rawdata\$Volume, INDEX = endpoints(rawdata, "days"), FUN = sum)
tclass(rawdailyvol) <- "Date"
.index(rawdailyopen) <- .index(rawdailyclose)
rawdailyohlc <- merge.xts(rawdailyopen, rawdailymax, rawdailymin, rawdailyclose, rawdailyvol)
colnames(rawdailyohlc) <- c("Open", "High", "Low", "Close", "Volume")


Last time we did the analysis with data up to approximately to the end of the year 2017. We now have five more years of data in 2022.

dailyret <- dailyReturn(Cl(rawdailyohlc))
store <- table.Stats(dailyret["/2017-12-29"])
store <- cbind(store, table.Stats(dailyret["2018-01-01/"]))
store <- cbind(store, table.Stats(dailyret))
colnames(store) <- c('2011-2017', '2017-2022', '2011-2022')
store


Summary statistics for daily returns of Bitcoin during two different subperiods and the full sample:

                2011-2017 2017-2022 2011-2022
Observations    2279.0000 1779.0000 4060.0000
NAs                0.0000    0.0000    0.0000
Minimum           -0.4852   -0.3898   -0.4852
Quartile 1        -0.0110   -0.0165   -0.0138
Median             0.0024    0.0010    0.0018
Arithmetic Mean    0.0048    0.0009    0.0031
Geometric Mean     0.0034    0.0001    0.0020
Quartile 3         0.0201    0.0186    0.0194
Maximum            0.5613    0.1945    0.5613
SE Mean            0.0011    0.0009    0.0007
LCL Mean (0.95)    0.0027   -0.0009    0.0016
UCL Mean (0.95)    0.0069    0.0028    0.0045
Variance           0.0027    0.0016    0.0022
Stdev              0.0518    0.0400    0.0471
Skewness           0.3622   -0.4100    0.1890
Kurtosis          18.5765    7.3008   17.1983


It is also relevant because Bitcoin is going through it's third largest drawdown in history.

table.Drawdowns(dailyret, top = 5, digits = 3, geometric = TRUE)


The third drawdown is the one that is ongoing since 2021-11-09 and is still not over. It is approaching the longest drawdowns that started in 2013 and 2017.

        From     Trough         To  Depth Length To Trough Recovery
1 2013-12-05 2015-01-14 2017-02-23 -0.849   1174       403      771
2 2017-12-17 2018-12-15 2020-11-30 -0.834   1080       364      716
3 2021-11-09 2022-11-09       <NA> -0.765    372       366       NA
4 2013-04-10 2013-07-06 2013-11-05 -0.710    210        88      122
5 2011-09-26 2011-10-20 2012-01-05 -0.630     84        18       66


We could consider the following questions:

• Is the distribution of daily / monthly Bitcoin returns different?
• Is bitcoin more or less correlated with the SP500 than it used to be?
• Do the new return dynamics indicate a different portfolio allocation?