Limit Order Book in Python
Project description
lob.py
](LICENSE)
Limit Order Book in Python
-
LOB is the basic limit order book.
-
LOB has the following methods:
- basics:
- set snapshot: push multiple levels (should be equal to set updates)
- set updates: push multiple updates
- update: update a single level
- utils:
- diff: difference between two lob (it returns the updates needed to change the lob 1 to the lob 2)
- track_queue_position: to define how and why
- len in tick: you provide side and price, it return the number of tick the provided price is far from the top of the book
- methods to convert numpy/pandas
- stats:
- spread
- limit order book
- basics:
-
LOBts: time series of LOB
- it has the same basic methods of LOB
- at any group of updates (pushed via set_updates), it produce another LOB in the inner data structure
- LOB(t) are indexed by timestamp
- methods to convert numpy/pandas
- stats: it can have further stats (time-based stats)
- basic LOB stats in form of time series
- arrival frequency
- cancel frequency
We just need one dep: sortedcontainers. Consider to implement it.
LOB API
Methods
set_snapshotupdateset_update
Properties
bid: best bid priceask: best ask pricevi: volume imbalancebidq: best bid sizeaskq: best ask sizebid[0]: bid at level 0 - equals tobidbid[i]: bid at level iask[0]: ask at level 0 - equals toaskask[i]: ask at level ivi[0]: volume imbalance of the first level - equals tovivi[i]: volume imbalance of the top i levelsspread: spread in absolute valuespread_tick: spread in ticksspread_rel: spread in percentage of the bid levelmidprice: mid-pricevw_midprice: volume-weighted mid-price
Other methods
check(): Check consistency of the order book- Returns
Trueif the book is consistent (best bid < best ask or one side is empty) - Returns
Falseif the book is crossed (best bid >= best ask) - Useful for validating order book state before processing or after updates
- Returns
get_slippage(volume, side=['midprice', 'ask', 'bid']): calculate the slippage from the top level (from the midprice is not declared)
Export Methods
Numpy and Pandas Export
-
to_np(side=None, nlevels=None): Export order book to numpy arrayside:'b'for bids,'a'for asks, orNonefor both sidesnlevels: number of top levels to export (default: all levels)- Returns 2D array with shape
(n, 2)[price, size] when side specified - Returns 2D array with shape
(n, 3)[side, price, size] when side=None - When both sides, bids come first (best to worst), then asks (best to worst)
-
to_pd(side=None, nlevels=None): Export order book to pandas DataFrameside:'b'for bids,'a'for asks, orNonefor both sidesnlevels: number of top levels to export (default: all levels)- Returns DataFrame with columns
['price', 'size']when side specified - Returns DataFrame with columns
['price', 'size', 'side']when side=None - Side column contains
'b'for bids and'a'for asks
File Export
-
to_csv(path, side=None, nlevels=None): Export order book to CSV filepath: file path for CSV outputside,nlevels: same as to_np/to_pd- Saves current snapshot with appropriate columns
-
to_xlsx(path, side=None, nlevels=None): Export order book to XLSX filepath: file path for XLSX outputside,nlevels: same as to_np/to_pd- Saves current snapshot with appropriate columns
-
to_parquet(path, side=None, nlevels=None): Export order book to Parquet filepath: file path for Parquet outputside,nlevels: same as to_np/to_pd- Saves current snapshot with appropriate columns
- Efficient binary format for large order books
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
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 lobpy-0.1.0.tar.gz.
File metadata
- Download URL: lobpy-0.1.0.tar.gz
- Upload date:
- Size: 86.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5638a3b3f4ee22e34a4c8e7a858aaebba2fed666dad93113b4da0c226106980b
|
|
| MD5 |
1e2da1fa4a5be99f1235d517a2c6cd9b
|
|
| BLAKE2b-256 |
d455ad5cd1b1f5dff696d298e8c4b2f950cd83071fcc026c842b200d58168a2d
|
Provenance
The following attestation bundles were made for lobpy-0.1.0.tar.gz:
Publisher:
publish.yml on mattermat/lob.py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lobpy-0.1.0.tar.gz -
Subject digest:
5638a3b3f4ee22e34a4c8e7a858aaebba2fed666dad93113b4da0c226106980b - Sigstore transparency entry: 951112906
- Sigstore integration time:
-
Permalink:
mattermat/lob.py@4afcd84b53684b92d0a6314e012517155d301437 -
Branch / Tag:
refs/tags/v1.0 - Owner: https://github.com/mattermat
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4afcd84b53684b92d0a6314e012517155d301437 -
Trigger Event:
release
-
Statement type:
File details
Details for the file lobpy-0.1.0-py3-none-any.whl.
File metadata
- Download URL: lobpy-0.1.0-py3-none-any.whl
- Upload date:
- Size: 58.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f91e897abae06e6be411970dc9087bfb6eaa063fd8d2ef23a1127945e88b9def
|
|
| MD5 |
a9f15a0345cc0575480d542a177d0e18
|
|
| BLAKE2b-256 |
892fbcfdfc17a46227834fa974f51e8f9351c36e0c67b5f0fa2ac1d52568ea6a
|
Provenance
The following attestation bundles were made for lobpy-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on mattermat/lob.py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lobpy-0.1.0-py3-none-any.whl -
Subject digest:
f91e897abae06e6be411970dc9087bfb6eaa063fd8d2ef23a1127945e88b9def - Sigstore transparency entry: 951112947
- Sigstore integration time:
-
Permalink:
mattermat/lob.py@4afcd84b53684b92d0a6314e012517155d301437 -
Branch / Tag:
refs/tags/v1.0 - Owner: https://github.com/mattermat
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4afcd84b53684b92d0a6314e012517155d301437 -
Trigger Event:
release
-
Statement type: