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?
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:
The example is from LMAX Protocol Reference Guide
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With