A crazy day in the Bitcoin World

[This article was first published on R – insightR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

By Gabriel Vasconcelos

Today, November 29, 2017 was a crazy day in the Bitcoin world and the craziness is still going on as I write this post. The price range was of thousands of Dollars in a few hours. Bitcoins were today the main topic in all discussion groups I participate. Some people believe we are in the middle of a giant bubble and are very skeptical about Bitcoins intrinsic value and other people believe cryptocurrencies are the future and are already counting on a price of hundreds of thousands of dollars in a few years. I am no expert and I have no idea which group is right, but I hope it is the second because I really like the Bitcoin idea as the money of the future.

My objective here is to have a brief look at what happened with Bitcoins prices and transactions today (2017-11-29) compared to two days ago (2017-11-27), which was an ordinary day given the current levels of Bitcoin prices and volume. I will use data available by a Brazilian Bitcoin exchange called “Mercado Bitcoin (MB) (Bitcoin Market)”. You can see more details in their github page here. The prices are in Brazilian Real, which trades today for approximately 31 cents of US Dollar. Since some readers may be only interested in the results, I will put all codes to download the data and generate the plots in the end of the post.

First I will show a small table that demonstrates how volume and prices changed from one day to the other. The table shows that transactions and the number of Bitcoins traded increased something close 500% and the average amount of Bitcoins in each transaction also increased. The price range was of less than 2000 BRL in November 27th compared to more than 10000 BRL today (November 29th).

stat Nov.29 Nov.27
Transactions 31838.000 6975.000
Amount 2583.511 446.133
Avg.Amount 0.081 0.064
Avg.Price 40063.902 34274.563
Min.Price 33002.000 33500.000
Max.Price 44980.000 34965.000

The boxplot helps us to understand how the prices were distributed. The distance between the 25% and the 75% percentiles of today covers much more than the entire range of prices from November 27th. If you are more interested in the dynamics of prices and volume you should look at the figure below the boxplot. It shows how prices and volume behaved across the days (the data is coerced to minutes). Both figures show that November 29th was clearly an unusual day in the Bitcoin world.

plot of chunk unnamed-chunk-3

plot of chunk unnamed-chunk-4

Codes

library(jsonlite)
library(tidyverse)
library(lubridate)
library(reshape2)
library(grid)
library(scales)

## == The data is stored in JSON  == ##
## == We can only download 1000 trades each time == ##
## == The while is to download all trades in the day == ##
## == Dates are in UNIX time == ##
## == Data from the last 24h (now it is 23:17 - 2017-11-29) == ##
start = 1512004273 - 86400
end = 1512004273
datalist = list()
while(start < end){
  file = paste("https://www.mercadobitcoin.net/api/BTC/trades/",start,"/",sep="")
  data <- fromJSON(file, flatten = TRUE)
  start = max(data$date) + 1
  datalist[[length(datalist) + 1]] = data
}
df_present = Reduce("rbind", datalist)
df_present = df_present %>% filter(date<=end)

## == Data from the same time 2 days before == ##
start = 1512004273 - 86400 * 2
end = 1512004273 - 86400 * 1
datalist = list()
while(start < end){
  file = paste("https://www.mercadobitcoin.net/api/BTC/trades/",start,"/",sep="")
  data <- fromJSON(file, flatten = TRUE)
  start = max(data$date) + 1
  datalist[[length(datalist) + 1]] = data
}
df_past = Reduce("rbind", datalist)
df_past = df_past %>% filter(date <= end)

## = adjust date = ##
df_past$date = as.POSIXct(df_past$date, origin = "1970-01-01")
df_present$date = as.POSIXct(df_present$date, origin = "1970-01-01")

# = statistics = #
past = c(nrow(df_past), sum(df_past$amount, na.rm = TRUE), mean(df_past$amount, na.rm = TRUE), mean(df_past$price), range(df_past$price))
present = c(nrow(df_present), sum(df_present$amount, na.rm = TRUE), mean(df_present$amount, na.rm = TRUE), mean(df_present$price), range(df_present$price))
stat = round(data.frame("Nov.29" = present,"Nov.27" = past), 3)
stat = data.frame(stat = c("Transactions","Amount","Avg.Amount","Avg.Price","Min.Price","Max.Price"),
                stat)

## =  make data by minute = ##
df_present_min = df_present %>%
  mutate(day = 1 + day(date) - min(day(date)), hour = hour(date), min = minute(date)) %>%
  mutate(date = make_datetime(day = day, hour = hour,min = min,tz = "BRST")) %>%
  group_by(date) %>% summarise(price = tail(price, 1) ,vol = sum(amount))

df_past_min=df_past %>%
  mutate(day = 1 + day(date) - min(day(date)), hour = hour(date) ,min = minute(date)) %>%
  mutate(date = make_datetime(day = day, hour = hour,min = min, tz="BRST")) %>%
  group_by(date) %>% summarise(price = tail(price, 1), vol = sum(amount))

df_min = full_join(df_present_min, df_past_min,by=c("date")) %>% arrange(date)
df_min$price.x = na.locf(df_min$price.x, na.rm = FALSE)
df_min$price.y = na.locf(df_min$price.y, na.rm = FALSE)

## = Plots = ##
df1 = melt(df_min[, c(1, 2, 4)], id.vars = "date")
df2 = melt(df_min[, c(1, 3, 5)], id.vars = "date")

p0 = ggplot() +
  geom_boxplot(data = df1, aes(variable, value)) + labs(x="Day", y="Price") +
  scale_x_discrete(labels = c("Nov. 29", "Nov. 27"))

p1 = ggplot() +
  geom_line(data = df1, aes(x = date, y = value, color = factor(variable, labels = c("Nov. 29", "Nov. 27")))) +
  labs(x = "Time",y = "Price") + guides(color = guide_legend(title = "Day")) +
  scale_x_datetime(labels = date_format("%H:%M"))

p2 = ggplot() +
  geom_line(data = df2,aes(x = date,y = value,color = factor(variable, labels=c("Nov. 29", "Nov. 27")))) +
  labs(x = "Time", y = "Volume") + guides(color = guide_legend(title = "Day")) +
  scale_x_datetime(labels = date_format("%H:%M"))

p0
grid.draw(rbind(ggplotGrob(p1), ggplotGrob(p2), size = "first"))

To leave a comment for the author, please follow the link and comment on their blog: R – insightR.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)