Bitcoin Performance and Statistical Tools

Richard Nguyen

2024-04-06

## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
## Rows: 1048575 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (8): Timestamp, Open, High, Low, Close, Volume_(BTC), Volume_(Currency),...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Table of Contents

  1. Histogram of Daily Returns
    1. Code for Histogram
  2. Price of Bitcoin Over Time
    1. Code for Price of Bitcoin Over Time
  3. 3d Scatter Plot
    1. Code for 3d Scatter Plot
  4. Statistics: Standard Deviation and Moving Averages

Histogram of Daily BTC Price Change

Code for Bitcoin Daily Price Changes

btc_data$PriceChange <- c(NA, diff(btc_data$Close))

# Remove NA values
btc_data1 <- btc_data[!is.infinite(btc_data$PriceChange) & 
                        !is.na(btc_data$PriceChange), ]

# Calculate quantiles
quantiles <- quantile(btc_data1$PriceChange, probs = c(0.01, 0.99), 
                      na.rm = TRUE)

# Plot histogram
ggplot(btc_data1, aes(x = PriceChange)) +
  geom_histogram(binwidth = 1, fill = "#6D9EC1", color = "black") +
  labs(title = "Histogram of Bitcoin Daily Price Changes",
       x = "Daily Price Change",
       y = "Frequency") +
  xlim(quantiles[1], quantiles[2]) +  # Apply
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Price of Bitcoin Over Time

Code for Price of Bitcoin Over Time

btc_plot <- ggplot(btc_data, aes(x = Date, y = Close)) +
  geom_line(color = "#00BFC4") +
  scale_y_log10() +
  labs(title = "Price of Bitcoin Over Time",
       x = "Date",
       y = "Closing Price (USD)") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1), 
        (axis.title.y = element_text(angle = 90, vjust = 0.5)))

print(btc_plot)

3d Plot of BTC Date, Volume, and Price

Code 3d Scatter Plot

btc_data$Date <- as.POSIXct(btc_data$Timestamp, origin = "1970-01-01",
                            tz = "UTC")

threeDplot <- plot_ly(data = btc_data, x = ~Date, y = ~Close, 
                      z = ~`Volume_(BTC)`, 
                      type = 'scatter3d', mode = 'markers',
        marker = list(size = 2, color = ~Close, colorscale = 'Viridis', 
                      opacity = 0.8)) %>%
  layout(title = "3D Time Series Plot of Bitcoin Prices and Volume",
         scene = list(xaxis = list(title = 'Date'),
                      yaxis = list(title = 'Close Price'),
                      zaxis = list(title = 'Volume (BTC)')))
                      
threeDplot

Standard Deviation

Standard deviation (\(\sigma\)) is a measure of the amount of variation or dispersion in a set of values. It is calculated using the following formula:

\[ \sigma = \sqrt{\frac{1}{N-1} \sum_{i=1}^{N} (x_i - \overline{x})^2} \]

Where: \(N\) is the number of observations, \(x_i\) is each individual observation, \(\overline{x}\) is the sample mean.

Standard Deviation and Bitcoin

By applying the standard deviation formula to Bitcoin’s daily price changes, we can assess its volatility over a specified period. A higher standard deviation indicates greater price volatility, implying more risk, and potentially more reward, for investors.

The standard deviation of Bitcoin’s daily returns \(\sigma_{\text{returns}}\) can be calculated as:

\[ \sigma_{\text{returns}} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{N} (r_i - \bar{r})^2} \]

Where: \(r_i\) represents the daily return on day \(i\), \(\bar{r}\) is the average daily return, \(N\) is the total number of days.

Moving Averages

Another useful statistical tool to analyze Bitcoin is the moving average (MA) is used to smooth out short-term fluctuations and highlight longer-term trends or cycles. The formula for a simple moving average is:

\[ MA_t = \frac{1}{n}\sum_{i=0}^{n-1} P_{t-i} \]

Where: \(MA_t\) is the moving average at time \(t\), \(n\) is the number of time periods in the moving average, \(P_{t-i}\) is the price at time \(t-i\).