Skip to main content

Fast & minimalist fixed decimal precision limit-order-book (LOB) implementation in pure Python.

Project description

fastlob | Fast Python Limit-Order-Book

Fast & minimalist fixed decimal precision limit-order-book (LOB) implementation in pure Python with almost no dependencies.


Package currently in development, bugs are expected.

The goal is to build an efficient easy to use package with a clean and comprehensible API.

We aim to keep it minimalist and simple, while keeping reasonable performances (for a pure Python implementation). We intend the final project to contain no more than ~1000 lines of code.

We implement three types of orders: FOK, GTC and GTD. Every order is defined as a limit order, but will be executed as a market order if its price matches the best (bid or ask) limit price in the book.

In the case of GTD orders, the book only supports whole seconds for the order expiry (order can not be set to expire in 3.8 seconds, it will be rounded to 4).

Usage

This book runs at a fixed decimal precision through the Python decimal package. The precision can be set via the PYLOB_DECIMAL_PRECISION environment variable, the default value is 2.

To be able to use this project, clone the repository and install the requirements:

git clone git@github.com:mrochk/pylob.git
cd pylob
pip install -r requirements.txt

To run tests, use make test or python3 -m unittest discover test.

Placing an order

# example.py
import time
import fastlob as lob

book = lob.OrderBook(
    name='My Order-Book',
    log_level=logging.WARNING # default logging level, change to INFO or WARNING to increase or reduce
)

book.start()

# every order must be created this way 
order_params = lob.OrderParams(
    side=lob.OrderSide.BID,
    price=123.32, # by default runs at 2 digits decimal precision
    quantity=3.4,
    otype=lob.OrderType.GTD, # good-till-date order
    expiry=time.time() + 120 # expires in two minutes
)

# -> at this point an exception will be raised if invalid attributes are provided

result = book(order_params) # let the book process the order

assert result.success() # result object can be used to see various infos about the order execution

order_id = result.order_id() # unique id is used to query our order after it's been placed
status, quantity_left = book.get_order_status(order_id)
print(f'Current status of the order: {status.name}, quantity left: {quantity_left}.\n')

print(book.view()) # pretty-print the book

book.stop()

Lines count:

   94 fastlob/engine/engine.py
    1 fastlob/engine/__init__.py
   18 fastlob/utils/utils.py
    1 fastlob/utils/__init__.py
   65 fastlob/order/params.py
    1 fastlob/order/__init__.py
  161 fastlob/order/order.py
   52 fastlob/enums/enums.py
    1 fastlob/enums/__init__.py
   64 fastlob/orderbook/result.py
  484 fastlob/orderbook/orderbook.py
    0 fastlob/orderbook/__init__.py
   19 fastlob/consts/consts.py
    1 fastlob/consts/__init__.py
  140 fastlob/limit/limit.py
    1 fastlob/limit/__init__.py
  162 fastlob/side/side.py
    1 fastlob/side/__init__.py
    4 fastlob/__init__.py
 1270 total

TODOs:

As mentioned earlier, this package is still in early development, and contributions are more than welcome.

The main tasks that have to be done are:

  • More and better testing for edge cases. In fact, some tests have to be rewritten too.
  • Benchmarking / profiling to have an idea of the performance, and see where it's slow.
  • Some parts probably need to be rewritten in a cleaner way.
  • Publish the package on pypi.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastlob-0.0.3.tar.gz (144.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastlob-0.0.3-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file fastlob-0.0.3.tar.gz.

File metadata

  • Download URL: fastlob-0.0.3.tar.gz
  • Upload date:
  • Size: 144.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fastlob-0.0.3.tar.gz
Algorithm Hash digest
SHA256 7faa6f4a7e7b4ae8bbc38ed7eae1f465a807318a42993ce33b79ff94da211e22
MD5 4bbc845962a5cb714abd09a48798a057
BLAKE2b-256 a6dae619d38c1d33eb52687fd196e5da73d388f53d84dde19bb77324cf83ade0

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastlob-0.0.3.tar.gz:

Publisher: python-publish.yml on mrochk/fastlob

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastlob-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: fastlob-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fastlob-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c2ba4ed282712c84df1299d521a8dbd87c2a9462a4d39bfc896177b54dfbef57
MD5 044ced1dcb5b1a720c603f7e7a1df61e
BLAKE2b-256 7a3ffbe4fe33455ab21ca533066faa047765521d8ee5a9c2e34fb437b113782b

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastlob-0.0.3-py3-none-any.whl:

Publisher: python-publish.yml on mrochk/fastlob

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page