A crazy day in the Bitcoin World

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(zoo)
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 * 3
end = 1512004273 - 86400 * 2
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"))
Advertisements
This entry was posted in R and tagged , , , , , . Bookmark the permalink.

9 Responses to A crazy day in the Bitcoin World

  1. Pingback: A crazy day in the Bitcoin World – Cloud Data Architect

  2. Pingback: A crazy day in the Bitcoin World – Mubashir Qasim

  3. Eran says:

    consider using the better box plot version, found here:
    https://eranraviv.com/r-tips-tricks-boxplots/

    Like

  4. Leo Torres says:

    I would love to get this working on my machine I am getting this error when I run your script
    Error in mutate_impl(.data, dots) :
    Evaluation error: CCTZ: Unrecognized tzone: “BRST”.
    Error in mutate_impl(.data, dots) :
    Evaluation error: CCTZ: Unrecognized tzone: “BRST”.
    Error in full_join(df_present_min, df_past_min, by = c(“date”)) :
    object ‘df_present_min’ not found
    Error in na.locf(df_min$price.x, na.rm = FALSE) :
    object ‘df_min’ not found
    Error in na.locf(df_min$price.y, na.rm = FALSE) :
    object ‘df_min’ not found
    Error in melt(df_min[, c(1, 2, 4)], id.vars = “date”) :
    object ‘df_min’ not found
    Error in melt(df_min[, c(1, 3, 5)], id.vars = “date”) :
    object ‘df_min’ not found
    Error in fortify(data) : object ‘df1’ not found
    Error in fortify(data) : object ‘df1’ not found
    Error in fortify(data) : object ‘df2’ not found
    Error: object ‘p0’ not found
    Error in plot_clone(plot) : object ‘p1’ not found

    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))
    Error in mutate_impl(.data, dots) :
    Evaluation error: CCTZ: Unrecognized tzone: “BRST”.

    Like

  5. rshah says:

    I tried many times and everytime, I am having this error;
    Error in grid.draw(rbind(ggplotGrob(p1), ggplotGrob(p2), size = “first”)) :
    could not find function “grid.draw”
    Error in ggplot() : could not find function “ggplot”

    Like

  6. Ujjwal says:

    thanks, that was nice.
    I realized that my R version was on 3.2.0 and was failing to run the code. just updated to 3.2.5 and all the code ran smoothly (had to include the library ggplot2)

    Thanks,
    Ujjwal

    Like

  7. Jose Antonio says:

    La cotización de los Bitcoins es algo que está en constante auge, solo hay que ver que su mercado se negocia cada segundo del día. Por esta característica, hay muchísima comunidad detrás.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s