Gradient Trader Part 2: Visualizing Bitcoin Order Book

Update: Nov. 17 2017: All algorithms on this page have been ported to Rust.

In this short post I will demonstrate how to use the order book. I am giving out visualization algorithms for free.

Visualization should lead to truth and understanding. There are different ways of visualizing the order book. We will start with the simplest one.

This is the supply-demand curve.

Order book on Bittrex:

As you can see in the simplest case, the order book is nothing but the transpose of the supply-demand curve zoomed in. This is a simple visualization - most traders can only catch a fleeting glimpse. Its utility is limited to spotting walls at any instant.

Now let’s work our way up towards better visualization.

Establish DB Connection

First, establish a connection to the database and retrieve orderbook updates over 1 hour.

The following is a trick to “proxy/forward” a db connection using ssh. Let’s say the database(dburl) is configured to only accept connections from secure-server, then it’s used to simply forward the port from localhost to dburl.

Although the exchange may send other fields such as trade id, update type(create, delete, partial fill) and some exchange-specific order types, the above is the minimum set of fields to reconstruct an order book.

Visualize Individual Orders

Visualizing order cancellation/creation is the lowest level one can go about visualization. The x-axis is time, y-axis is size of order. It provides insights into the activities of individual market participants.

defplot_events(df,trades_df=None,min_price=None,max_price=None,log_scale=True):ifmin_price:df=df[(df["price"]>min_price)]iftrades_dfisnotNone:trades_df=trades_df[(trades_df["price"]>min_price)]ifmax_price:df=df[(df["price"]<max_price)]iftrades_dfisnotNone:trades_df=trades_df[(trades_df["price"]<max_price)]fig=plt.figure(figsize=(24,18))ax=fig.add_subplot(111)iftrades_dfisnotNone:plt.title("Trades And Cancellation")plt.legend(["Trades","Cancellation"])plt.scatter(trades_df["ts"],trades_df["price"],s=trades_df["size"],color="#00ffff")plt.scatter(df["ts"],df["price"],s=df["size"]/30,color="#ffff00")else:plt.scatter(df["ts"],df["price"],s=df["size"],color="#ffff00")plt.legend("Cancelled")plt.title("Cancellation")iflog_scale:ax.set_yscale('log')plot_events(cancelled,trades_df=trades,min_price=0.00499,max_price=0.00529,log_scale=False)plt.show()

This is a demonstration of some visualization algorithms using matplotlib. As you can see, they are fairly straightforward to implement.

In the future I plan on porting such visualization to react-stockcharts so I can interactively explore data. The algorithms are already here, what is left to be done is changing matplotlib calls to d3. If you would like to collaborate with me on this, please email me.