<- function(symbol = "XBT",
xbt_reader timeframe = "1d",
count = "1000",
starttime = "",
reverse = 'false'){
<- "https://www.bitmex.com/api/v1/trade/bucketed?"
base = symbol
symbol = timeframe
timeframe = count
count = starttime
starttime
<- paste0(base,
url 'binSize=', timeframe,
'&partial=false&symbol=', symbol,
'&count=', count,
'&reverse=', reverse,
'&startTime=', starttime)
<- tibble(data = content(GET(url), "parsed")) %>%
result unnest_wider(data)
}
Exploring the recent Bitcoin crash with {tidyquant} and {gganimate}.
Bitcoin recently had a significant sell off, crashing 50% in a matter of hours. This undoubtly was due to the uncertainty in markets surrounding the economic impacts of COVID-19, and the consequent reduction in liquidity faced by many different assets. The volatile price action does however make for an interesting case study which this post will explore.
Introduction
Bitmex is one of the largest cryptocurrency exchanges where Bitcoin is traded. BitMEX provides a great API that can be used to gather all kinds of data from the site regarding trades and orders, and even allows you to build your own trading bot if you’re feeling confident in your programming ability to make a profitable algorithm… However this post will simply collect data from the most recent crash in price and explore what happened using some great R packages including tidyqaunt
(Dancho and Vaughan 2020) and gganimate
(Pedersen and Robinson 2020) as well as the usual suspects in the tidyverse
.
Getting the data
To obtain the necessary data, I first defined a quick function based on the information found on the BitMEX API explorer page (https://www.bitmex.com/api/explorer). This function barely scratches the surface at the possible requests available - but does get the data needed.
Next, I used this function to get the bucketed 5 minute trade data starting from the day before the crash.
<- xbt_reader(timeframe = "5m", starttime = "2020-03-11") %>%
xbt mutate(timestamp = as_datetime(timestamp))
Visualising the data
The price action can now be plotted using a combination of tidyqaunt
and gganimate
.
%>%
xbt mutate(label = "help") %>%
ggplot(aes(x = timestamp, y = close)) +
geom_candlestick(aes(open = open, high = high, low = low, close= close),
fill_up = "green",
fill_down = "red",
colour_up = "green",
colour_down = "red") +
scale_x_datetime(date_breaks = "12 hour", date_labels = "%H%M") +
scale_y_continuous(labels = scales::dollar) +
labs(y = "Price (USD)",
x = "Time") +
transition_time(timestamp) +
shadow_mark() +
theme_minimal()
As can be seen, this was a dramatic sell of with price dropping from almost 8000 USD to the low 3000 USD mark within a matter of hours, before recovery slightly to around the 5000 USD level.
DDoS attack
What makes this price action all the more intriguing is the reported DDoS attacks suffered by Bitmex on the 13th March. Bitmex reported two attacks, taking place at 02:16 UTC and 12:56 UTC. The first attack also happened to coincide with the lowest price during the drop which has caused some suspicion giving the timing of the attacks… However, speculation aside, these attacks can be clearly visualised using the trade data.
More information on the DDoS attacks can be found here: https://blog.bitmex.com/how-we-are-responding-to-last-weeks-ddos-attacks
I’ve adjusted the time interval to the lowest resolution possible (1 minute) to clearly see the effect of the DDoS attacks.
<- xbt_reader(timeframe = "1m", starttime = "2020-03-13") %>%
xbt_lower mutate(timestamp = as_datetime(timestamp)) %>%
filter(timestamp > "2020-03-13 11:30:00" & timestamp < "2020-03-13 13:30:00" |
> "2020-03-13 01:30:00" & timestamp < "2020-03-13 03:30:00") %>%
timestamp mutate(ddos = case_when(timestamp > "2020-03-13 01:30:00" & timestamp < "2020-03-13 03:30:00" ~ "First attack",
> "2020-03-13 11:30:00" & timestamp < "2020-03-13 13:30:00" ~ "Second attack",
timestamp
))
<- xbt_lower %>%
p1 ggplot(aes(x = timestamp, y = close)) +
geom_candlestick(aes(open = open, high = high, low = low, close= close),
fill_up = "green",
fill_down = "red",
colour_up = "green",
colour_down = "red") +
facet_wrap(~ddos, scales = "free") +
scale_y_continuous(labels = scales::dollar)+
labs(x = "", y = "Price (USD)") +
theme_minimal()
<- xbt_lower %>%
p2 ggplot(aes(timestamp, volume/1000)) +
geom_col() +
facet_wrap(~ddos, scales = "free") +
theme_minimal() +
labs(x = "Time", y = "Volume") +
theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)
/ p2 +
p1 plot_annotation(title = "Price data during the DDoS attacks")
The effect of each DDoS is clearly visible from the price data, leaving large gaps for approximately 15 minutes where very few to no trades were placed. Immediately following each attack was a period of extreme volatility, before price was able to stabilise to some degree.
These conditions would have expectationally difficult for many traders, although not all together unpredictable given the highly volatile nature of Bitcoin and the risk that comes with it…!
Summary
There are a several other avenues that could be interesting to explore here, such as visualising the order book liquidity during the crash compared to a more “stable” period of time, however I may save that for a follow-up post. I have also been playing with the Bitmex websocket connection which streams real time data from the exchange - I think a shiny app would be really cool to visualise this so may try something there as well.
Thanks for reading!