Fast & minimalist limit-order-book (LOB) implementation in pure Python with almost no dependencies.
Project description
fastlob | Python Limit-Order-Book
Fast & minimalist limit-order-book (LOB) implementation in Python, with almost no dependencies.
Package currently in development, bugs are expected.
Functionalities:
- Place limit orders.
- Execute market orders.
- Orders can be Good-Till-Cancel (GTC), Fill-or-Kill (FOK) or Good-Till-Date (GTD).
- Cancel pending or partially filled orders.
- Query order status (pending, filled, partially filled, canceled...).
- Set custom tick size for price and quantities.
- Extract spread, midprice, volume, etc.
- Simulate on historical data.
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, in this case it will be rounded to 4, nearest integer).
We do not implement multi-threaded order processing yet, check out the corresponding issue for more infos.
Installation
The package 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 decimal precision (also called tick size) can be set via the FASTLOB_DECIMAL_PRECISION_PRICE and FASTLOB_DECIMAL_PRECISION_QTY environment variables, if not set it defaults to 2.
# examples/basics.py
import time, logging
from fastlob import Orderbook, OrderParams, OrderSide, OrderType
logging.basicConfig(level=logging.INFO) # maximum logging
lob = Orderbook(name='MYLOB', start=True) # init and start lob
# 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 you will find an example of simulating the arrival of many orders using different distributions.
In load_snapshot_updates you'll find an example of running the lob on historical data.
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, there are also various issues open on Github.
Lines Count
96 fastlob/engine/engine.py
3 fastlob/engine/__init__.py
22 fastlob/utils/utils.py
1 fastlob/utils/__init__.py
115 fastlob/result/result.py
1 fastlob/result/__init__.py
84 fastlob/order/params.py
1 fastlob/order/__init__.py
99 fastlob/order/order.py
66 fastlob/enums/enums.py
1 fastlob/enums/__init__.py
22 fastlob/consts/consts.py
1 fastlob/consts/__init__.py
122 fastlob/limit/limit.py
1 fastlob/limit/__init__.py
24 fastlob/side/utils.py
320 fastlob/side/side.py
1 fastlob/side/__init__.py
5 fastlob/__init__.py
546 fastlob/lob/orderbook.py
28 fastlob/lob/utils.py
1 fastlob/lob/__init__.py
1560 total
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastlob-0.0.13.tar.gz.
File metadata
- Download URL: fastlob-0.0.13.tar.gz
- Upload date:
- Size: 70.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a303316755296fd98faf717e6722629164b0dd9e1786c64b47306c263dbfbf3b
|
|
| MD5 |
227b71f2ef5a9b740e788a9a4f1c513f
|
|
| BLAKE2b-256 |
815ffa2a8d36fdec7bf2dbc0e3864aea8a99f4862b25638f50f93f0781ce79c4
|
Provenance
The following attestation bundles were made for fastlob-0.0.13.tar.gz:
Publisher:
python-publish.yml on mrochk/fastlob
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastlob-0.0.13.tar.gz -
Subject digest:
a303316755296fd98faf717e6722629164b0dd9e1786c64b47306c263dbfbf3b - Sigstore transparency entry: 197395709
- Sigstore integration time:
-
Permalink:
mrochk/fastlob@58fc208f4ca23b536271b7be251149d7e0f83215 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/mrochk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@58fc208f4ca23b536271b7be251149d7e0f83215 -
Trigger Event:
push
-
Statement type:
File details
Details for the file fastlob-0.0.13-py3-none-any.whl.
File metadata
- Download URL: fastlob-0.0.13-py3-none-any.whl
- Upload date:
- Size: 23.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dcf717ef13ef5a9b97347d76afde16a8018a9b3791cd15ef29031720d38fe05b
|
|
| MD5 |
c83542d0b1eabc3ce22ede6ec5be45b5
|
|
| BLAKE2b-256 |
361d6906a62df05bb595acbc8160335ee3c044ad6ca4c36c39b67a5ac16f1265
|
Provenance
The following attestation bundles were made for fastlob-0.0.13-py3-none-any.whl:
Publisher:
python-publish.yml on mrochk/fastlob
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastlob-0.0.13-py3-none-any.whl -
Subject digest:
dcf717ef13ef5a9b97347d76afde16a8018a9b3791cd15ef29031720d38fe05b - Sigstore transparency entry: 197395711
- Sigstore integration time:
-
Permalink:
mrochk/fastlob@58fc208f4ca23b536271b7be251149d7e0f83215 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/mrochk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@58fc208f4ca23b536271b7be251149d7e0f83215 -
Trigger Event:
push
-
Statement type: