Skip to main content

Limit Order Book in Python

Project description

lob.py

Tests Coverage Code Quality Python License](LICENSE) PyPI

Limit Order Book in Python

Contributing

  • 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
  • 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_snapshot
  • update
  • set_update

Properties

  • bid: best bid price
  • ask: best ask price
  • vi: volume imbalance
  • bidq: best bid size
  • askq: best ask size
  • bid[0]: bid at level 0 - equals to bid
  • bid[i]: bid at level i
  • ask[0]: ask at level 0 - equals to ask
  • ask[i]: ask at level i
  • vi[0]: volume imbalance of the first level - equals to vi
  • vi[i]: volume imbalance of the top i levels
  • spread: spread in absolute value
  • spread_tick: spread in ticks
  • spread_rel: spread in percentage of the bid level
  • midprice: mid-price
  • vw_midprice: volume-weighted mid-price

Other methods

  • check(): Check consistency of the order book
    • Returns True if the book is consistent (best bid < best ask or one side is empty)
    • Returns False if the book is crossed (best bid >= best ask)
    • Useful for validating order book state before processing or after updates
  • 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 array

    • side: 'b' for bids, 'a' for asks, or None for both sides
    • nlevels: 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 DataFrame

    • side: 'b' for bids, 'a' for asks, or None for both sides
    • nlevels: 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 file

    • path: file path for CSV output
    • side, 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 file

    • path: file path for XLSX output
    • side, 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 file

    • path: file path for Parquet output
    • side, 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

lobpy-0.1.0.tar.gz (86.9 kB view details)

Uploaded Source

Built Distribution

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

lobpy-0.1.0-py3-none-any.whl (58.3 kB view details)

Uploaded Python 3

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

Hashes for lobpy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5638a3b3f4ee22e34a4c8e7a858aaebba2fed666dad93113b4da0c226106980b
MD5 1e2da1fa4a5be99f1235d517a2c6cd9b
BLAKE2b-256 d455ad5cd1b1f5dff696d298e8c4b2f950cd83071fcc026c842b200d58168a2d

See more details on using hashes here.

Provenance

The following attestation bundles were made for lobpy-0.1.0.tar.gz:

Publisher: publish.yml on mattermat/lob.py

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

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

Hashes for lobpy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f91e897abae06e6be411970dc9087bfb6eaa063fd8d2ef23a1127945e88b9def
MD5 a9f15a0345cc0575480d542a177d0e18
BLAKE2b-256 892fbcfdfc17a46227834fa974f51e8f9351c36e0c67b5f0fa2ac1d52568ea6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for lobpy-0.1.0-py3-none-any.whl:

Publisher: publish.yml on mattermat/lob.py

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