<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/a-crazy-day-in-the-bitcoin-world/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://insightr.wordpress.com/2017/11/30/a-crazy-day-in-the-bitcoin-world/"> R – insightR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<div><strong>By Gabriel Vasconcelos</strong></div>
<div></div>
<div></div>
</div>
<p>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.</p>
<p><span></span></p>
<p>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 “<a rel="nofollow" href="https://www.mercadobitcoin.com.br/" target="_blank">Mercado Bitcoin</a> (MB) (Bitcoin Market)”. You can see more details in their github page <a rel="nofollow" href="https://github.com/MercadoBitcoin" target="_blank">here</a>. 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.</p>
<p>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).</p>
<table>
<thead>
<tr>
<th align="left">stat</th>
<th align="right">Nov.29</th>
<th align="right">Nov.27</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">Transactions</td>
<td align="right">31838.000</td>
<td align="right">6975.000</td>
</tr>
<tr>
<td align="left">Amount</td>
<td align="right">2583.511</td>
<td align="right">446.133</td>
</tr>
<tr>
<td align="left">Avg.Amount</td>
<td align="right">0.081</td>
<td align="right">0.064</td>
</tr>
<tr>
<td align="left">Avg.Price</td>
<td align="right">40063.902</td>
<td align="right">34274.563</td>
</tr>
<tr>
<td align="left">Min.Price</td>
<td align="right">33002.000</td>
<td align="right">33500.000</td>
</tr>
<tr>
<td align="left">Max.Price</td>
<td align="right">44980.000</td>
<td align="right">34965.000</td>
</tr>
</tbody>
</table>
<p>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.</p>
<p><img alt="plot of chunk unnamed-chunk-3" src="https://i0.wp.com/i.imgur.com/PnQRfIn.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img alt="plot of chunk unnamed-chunk-4" src="https://i2.wp.com/i.imgur.com/S0DDxi3.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Codes</h2>
<pre>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 &amp;lt; end){
  file = paste(&quot;https://www.mercadobitcoin.net/api/BTC/trades/&quot;,start,&quot;/&quot;,sep=&quot;&quot;)
  data &amp;lt;- fromJSON(file, flatten = TRUE)
  start = max(data$date) + 1
  datalist[[length(datalist) + 1]] = data
}
df_present = Reduce(&quot;rbind&quot;, datalist)
df_present = df_present %&amp;gt;% filter(date&amp;lt;=end)

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

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

# = 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(&quot;Nov.29&quot; = present,&quot;Nov.27&quot; = past), 3)
stat = data.frame(stat = c(&quot;Transactions&quot;,&quot;Amount&quot;,&quot;Avg.Amount&quot;,&quot;Avg.Price&quot;,&quot;Min.Price&quot;,&quot;Max.Price&quot;),
                stat)

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

df_past_min=df_past %&amp;gt;%
  mutate(day = 1 + day(date) - min(day(date)), hour = hour(date) ,min = minute(date)) %&amp;gt;%
  mutate(date = make_datetime(day = day, hour = hour,min = min, tz=&quot;BRST&quot;)) %&amp;gt;%
  group_by(date) %&amp;gt;% summarise(price = tail(price, 1), vol = sum(amount))

df_min = full_join(df_present_min, df_past_min,by=c(&quot;date&quot;)) %&amp;gt;% 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 = &quot;date&quot;)
df2 = melt(df_min[, c(1, 3, 5)], id.vars = &quot;date&quot;)

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

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

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

p0
grid.draw(rbind(ggplotGrob(p1), ggplotGrob(p2), size = &quot;first&quot;))
</pre>
<p> <a rel="nofollow" href="https://feeds.wordpress.com/1.0/gocomments/insightr.wordpress.com/780/" target="_blank"><img border="0" alt="" src="https://feeds.wordpress.com/1.0/comments/insightr.wordpress.com/780/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" data-recalc-dims="1"></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://insightr.wordpress.com/2017/11/30/a-crazy-day-in-the-bitcoin-world/"> R – insightR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/T3O1v9zSz4s" height="1"></a></div>