Code Monkey home page Code Monkey logo

pybook's Introduction

PyBook

A limit order book and matching engine written in Python.

Design

Written as a proof-of-concept for achieveing great average-case performance, the bid and ask order books are implemented as separate AVL trees. Inspired by the HFT orderbook writeup by WK Selph [1], the limit levels are stored as nodes inside the trees, with each node itself being a doubly-linked list of orders, sorted chronologically.

Performance

The design of the Order and Limit classes make assumptions about the format of transactions and are currently based off modified market data released by RGM Advisors [2]. Processing ~1.2 million transactions takes ~5 seconds on a Intel Core i5-6200U CPU @ 2.8GHz, for an average of ~240,000 transactions per second!

Running

Sample transaction data is included in data/pricer.in.gz. From the root of the repository, run the following commands:

  1. gzip -k -d data/pricer.in.gz (or similar depending on the OS)
  2. python3 src/pybook.py < data/pricer.in

After processing the transactions, a summary is printed: output

Debugging

Included in pybook.py is a method validate(tree) that will recursively verify the correctness (balance factor, parent/child pointers, height, limit levels) of a given AVL tree. Inside the main method, two lines #assert(validate(book.sell_tree)) and #assert(validate(book.buy_tree)) can be uncommented to verify the AVL tree after every transaction. Note, this incurs a significant performance cost, lowering the average processing speed to ~3000 transactions per second!

References

[1] https://web.archive.org/web/20110219163448/http://howtohft.wordpress.com/2011/02/15/how-to-build-a-fast-limit-order-book/

[2] https://web.archive.org/web/20161116104649/http://rgmadvisors.com/problems/orderbook/

(Both of the original websites are no longer available at their original location)

pybook's People

Contributors

abhathal avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.