Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OrderBook DB Design

Tags:

finance

orders

The OrderBook is filled with buy and sell orders. Updates, new orders may execute trades.

I can't seem to find any examples of implementation.

We could give each order an id, and checkExecute orders one by one. But we want something that will scale to thousands of active orders.

We can sort the prices to get the orders that will be executed (because they overlap). But we have to be careful and execute only in the order in which the orders were received :)

Thoughts?

like image 581
EthernetCable Avatar asked Oct 31 '11 21:10

EthernetCable


2 Answers

  • we have to be careful and execute only in the order in which the orders were received

Market data is usually modeled in a time series fashion, hence it is naturally ordered by a timestamp which goes up to as far as a picosecond precision at the moment.

Depending on what tech you rely on, if you don't mind to pay for it, I would go with OneTick, that is a time series DB that already has a built in order book with a book depth / price levels, CEP and much more.

In case you'd like to build it yourself ( or rely on free products ), take a look at OpenTSDB which is an open source, time series DB with a LGPLv3+ license. It'd be slower than OneTick of course, but since you just need thousands (OneTick handles billions) depending on your speed requirements it may work.

As to the OrderBook data model it would depend on how exactly are you going to use the book: e.g. do you care about multiple price levels? Top of the book? Do you need to match bids/asks to the actual orders, etc..

But you can start with designing a schema that would record OrderBook events such as:

enter image description here

The example is from LMAX Protocol Reference Guide

like image 157
tolitius Avatar answered Oct 21 '22 04:10

tolitius


Python's OrderedDict can serve as a foundation for a scalable OrderBook. You would want one ordered dictionary for each price-level so that orders will match according to price/time priority.

like image 26
Raymond Hettinger Avatar answered Oct 21 '22 03:10

Raymond Hettinger