Skip to main content

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

Project description

fastlob | Fast Limit-Order-Book in Python

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).

Installation

The package is now available on PyPI, you can simply install it using

pip install fastlob

Otherwise, one can install it from source

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

Testing

To run the tests and check that everything is okay, run make test or python3 -m unittest discover test.

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.

# examples/basics.py

import time, logging

from fastlob import Orderbook, OrderParams, OrderSide, OrderType

logging.basicConfig(level=logging.INFO) # maximum logging

lob = Orderbook(name='ABCD') # init lob

lob.start() # start background processes

# every order must be created this way 
params = OrderParams(
    side=OrderSide.BID, # is it a buy or sell order
    price=123.32, quantity=3.42, # by default runs at 2 digits decimal precision
    otype=OrderType.GTD, # good-till-date order
    expiry=time.time() + 120 # order will expire in two minutes
    # since order is GTD, expiry must be set to some future timestamp
)

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

result = lob(params) # let the book process the order
assert result.success() # result object can be used to see various infos about the order execution

# order uuid is used to query our order after it's been placed
status, quantity_left = lob.get_order_status(result.orderid())
print(f'Current order status: {status.name}, quantity left: {quantity_left}.\n')

lob.render() # pretty-print the book

lob.stop() # stop the background processes

In the notebook simulate.ipynb you will find an example of simulating the arrival of many orders using different distributions.

Contributing

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

Please do not hesitate to contact me, or directly submit a pull request if you'd like to contribute.

Lines Count

  92 fastlob/engine/engine.py
   3 fastlob/engine/__init__.py
  18 fastlob/utils/utils.py
   1 fastlob/utils/__init__.py
  82 fastlob/result/result.py
   1 fastlob/result/__init__.py
  79 fastlob/order/params.py
   1 fastlob/order/__init__.py
  97 fastlob/order/order.py
  56 fastlob/enums/enums.py
   1 fastlob/enums/__init__.py
  20 fastlob/consts/consts.py
   1 fastlob/consts/__init__.py
  99 fastlob/limit/limit.py
   1 fastlob/limit/__init__.py
 140 fastlob/side/side.py
   1 fastlob/side/__init__.py
   5 fastlob/__init__.py
 461 fastlob/lob/orderbook.py
  83 fastlob/lob/utils.py
   1 fastlob/lob/__init__.py
1243 total

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.11.tar.gz (76.1 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.11-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastlob-0.0.11.tar.gz
  • Upload date:
  • Size: 76.1 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.11.tar.gz
Algorithm Hash digest
SHA256 31458e335880fbf1266845ad9a69f76a2ec23398548905dcd33e815da773da16
MD5 da557b1ca3b46e844d4588337f6e6502
BLAKE2b-256 b73ffa7328cc4cb5340799873e96e2af0852fc2af24ea51477d17261e8a17f81

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastlob-0.0.11.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.11-py3-none-any.whl.

File metadata

  • Download URL: fastlob-0.0.11-py3-none-any.whl
  • Upload date:
  • Size: 19.8 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.11-py3-none-any.whl
Algorithm Hash digest
SHA256 04c2a1739b7ee285d9da125b0442eb6890c64d657c8460052999f593ab399c90
MD5 b702404ab3dd4b023d80c6ad08dc17fa
BLAKE2b-256 f718ef9c667d6593b5a43c95d9e592a26a53242c513a7999bf4b23e82fd4d8e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastlob-0.0.11-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