Make interactive graphs of financial time-series in five minutes with R (Bitcoin example)

We’ll use two packages: The package Quandl for programmatically accessing a variety of financial time-series, and the package dygraphs for lovely interactive graphs based on the eponymous Javascript library. If you don’t already have them, install them.

Below, we load the packages and run a search through the Quandl database to see what kind of variables we have to choose from. Feel free to explore your own interests and run the following example on a different variable. For this example, I’m interested in Bitcoin so I’ll use the Quandl.search() function to search for variables mentioning bitcoin. When you find a result you’re interested in, note the dataset code and the database code.

require(dygraphs)
require(Quandl)

search.results <- Quandl.search("bitcoin", silent=T)

head(search.results, 2) # Show the first 2 results
##         id dataset_code database_code                       name
## 1 11320897     BTC24EUR       BCHARTS Bitcoin Markets (btc24EUR)
## 2 11320898     BTC24USD       BCHARTS Bitcoin Markets (btc24USD)
##                                                                                                                                                                                                                                         description
## 1 Daily Bitcoin exchange rate (BTC vs. EUR) on Btc24. Updated daily at 6:00pm EST. Data is sourced from http://www.bitcoincharts.com . For other Bitcoin data and charts see our Bitcoin markets page (http://www.quandl.com/markets/bitcoin-data).
## 2 Daily Bitcoin exchange rate (BTC vs. USD) on Btc24. Updated daily at 6:00pm EST. Data is sourced from http://www.bitcoincharts.com . For other Bitcoin data and charts see our Bitcoin markets page (http://www.quandl.com/markets/bitcoin-data).
##               refreshed_at newest_available_date oldest_available_date
## 1 2016-01-14T23:01:25.970Z            2014-09-04            2012-05-09
## 2 2016-01-14T23:01:25.239Z            2013-04-13            2012-05-14
##                                                                    column_names
## 1 Date, Open, High, Low, Close, Volume (BTC), Volume (Currency), Weighted Price
## 2 Date, Open, High, Low, Close, Volume (BTC), Volume (Currency), Weighted Price
##   frequency        type premium database_id
## 1     daily Time Series   FALSE        8431
## 2     daily Time Series   FALSE        8431

Next, we’ll use the Quandl() function to import the time-series of interest. I’m selecting the BTC24USD dataset from the BCHARTS database, as revealed to me in the search results above. Set the type option to xts, which is just one of R’s widely-used formats for time-series variables. It plays well with the dygraph package.

bitcoin <- Quandl("BCHARTS/BTC24USD", type = "xts")

After running the above line, you should now have a dataframe object named bitcoin. To produce a nice, interactive plot, simply use the dygraph() function and specify the variable of interest from your dataframe. Give it a title, and run it.

dygraph(bitcoin$`Weighted Price`, main = "Bitcoin Exchange Rate (BTC vs. USD")


Pretty slick, eh? And you didn’t even touch a spreadsheet, let alone Javascript.

What would you like to do next? Let me know. Forecast future prices? Test a hypothesis about what drives the value up/down? Add additional time-series to one graph? Reply or email me.

Hat tip to Kyle Walker.


Related

comments powered by Disqus