Skip to main content

Read and process limit order book data

Project description

MeatPy

PyPI version License Documentation Status codecov

MeatPy Logo

MeatPy is a Python framework for processing and analyzing high-frequency financial market data, specifically designed for working with NASDAQ ITCH protocol data feeds. It provides robust tools for reconstructing limit order books and extracting key market events from historical market data files.

🎯 Key Features

  • 📊 Limit Order Book Reconstruction: Complete order book state tracking with proper handling of all order types and modifications
  • 🏛️ NASDAQ ITCH Support: Full implementation for ITCH 5.0 and 4.1 protocols with native message parsing
  • ⚡ Event-Driven Architecture: Flexible observer pattern for real-time event processing and analysis
  • 🔒 Type Safety: Modern Python with comprehensive type hints and generic interfaces for robust data handling
  • 📁 Multiple Output Formats: Export to CSV, Parquet, or implement custom output formats
  • 🚀 Performance Optimized: Efficiently process multi-gigabyte ITCH files with streaming capabilities
  • 🔧 Extensible Design: Easy to adapt for other market data formats and custom analysis needs

📊 Common Use Cases

MeatPy is designed for market microstructure research and analysis:

  • Order Book Reconstruction: Rebuild complete limit order book state at any point in time
  • Market Event Analysis: Extract and analyze trades, quotes, and order modifications
  • Top-of-Book Sampling: Generate regular snapshots of best bid/ask prices and sizes

📦 Installation

Quick Install

pip install meatpy

With Optional Dependencies

# For Parquet file support
pip install meatpy[parquet]

🚀 Quick Start

Complete documentation is available at https://www.vincentgregoire.com/MeatPy

Basic Message Reading

from pathlib import Path
from meatpy.itch50 import ITCH50MessageReader

# Define the path to our sample data file
data_dir = Path("data")
file_path = data_dir / "S081321-v50.txt.gz"

# Read ITCH messages from a file
with ITCH50MessageReader(file_path) as reader:
    for i, message in enumerate(reader):
        print(f"Message {i}: {message.type} - {message}")
        if i >= 10:  # Just show first 10 messages
            break

List Available Symbols

symbols = set()
message_count = 0

with ITCH50MessageReader(file_path) as reader:
    for message in reader:
        message_count += 1

        # Stock Directory messages (type 'R') contain symbol information
        if message.type == b"R":
            symbol = message.stock.decode().strip()
            symbols.add(symbol)

        if message_count >= 100000:
            break

Extract all Messages for Specific Symbols

from pathlib import Path
from meatpy.itch50 import ITCH50MessageReader, ITCH50Writer

# Define paths
data_dir = Path("data")
input_file = data_dir / "S081321-v50.txt.gz"
output_file = data_dir / "S081321-v50-AAPL-SPY.itch50.gz"

# Symbols we want to extract
target_symbols = ["AAPL", "SPY"]

message_count = 0
with ITCH50MessageReader(input_file) as reader:
    with ITCH50Writer(output_file, symbols=target_symbols) as writer:
        for message in reader:
            message_count += 1
            writer.process_message(message)

Extract Full LOB at 1-Minute Intervals

from pathlib import Path
import datetime
from meatpy.itch50 import ITCH50MessageReader, ITCH50MarketProcessor
from meatpy.event_handlers.lob_recorder import LOBRecorder
from meatpy.writers.parquet_writer import ParquetWriter

# Define paths and parameters
data_dir = Path("data")

file_path = data_dir / "S081321-v50-AAPL-SPY.itch50.gz"
outfile_path = data_dir / "spy_lob.parquet"
book_date = datetime.datetime(2021, 8, 13)

with ITCH50MessageReader(file_path) as reader, ParquetWriter(outfile_path) as writer:
    processor = ITCH50MarketProcessor("SPY", book_date)

    # We only care about the top of book
    lob_recorder = LOBRecorder(writer=writer, collapse_orders=False)
    # Generate a list of timedeltas from 9:30 to 16:00 (inclusive) in 30-minute increments
    market_open = book_date + datetime.timedelta(hours=9, minutes=30)
    market_close = book_date + datetime.timedelta(hours=16, minutes=0)
    record_timestamps = [market_open + datetime.timedelta(minutes=i)
                     for i in range(int((market_close - market_open).total_seconds() // (30*60)) + 1)]
    lob_recorder.record_timestamps = record_timestamps

    # Attach the recorders to the processor
    processor.handlers.append(lob_recorder)

    for message in reader:
        processor.process_message(message)

📊 Common Use Cases

MeatPy is designed for market microstructure research and analysis:

  • Order Book Reconstruction: Rebuild complete limit order book state at any point in time
  • Market Event Analysis: Extract and analyze trades, quotes, and order modifications
  • Top-of-Book Sampling: Generate regular snapshots of best bid/ask prices and sizes
  • Market Quality Metrics: Calculate spreads, depth, and other liquidity measures
  • Academic Research: Analyze market microstructure for research papers and studies

MeatPy is not suitable for real-time applications or for production use where money is at stake.

🎓 Academic Use

MeatPy has been used in several academic publications, including:

🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

📄 License

MeatPy is released under the permissive BSD 3-Clause License. See LICENSE file for details.

👥 Credits

MeatPy was created by Vincent Grégoire and Charles Martineau. Seoin Kim and Javad YaAli provided valuable research assistance on the project.

Acknowledgments: MeatPy development benefited from the financial support of IVADO

📞 Support


Made with ❤️ for the market microstructure research community

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

meatpy-0.2.7.tar.gz (200.6 kB view details)

Uploaded Source

Built Distribution

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

meatpy-0.2.7-py3-none-any.whl (77.9 kB view details)

Uploaded Python 3

File details

Details for the file meatpy-0.2.7.tar.gz.

File metadata

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

File hashes

Hashes for meatpy-0.2.7.tar.gz
Algorithm Hash digest
SHA256 f0802566973fcb2791c321cf7d10af93816ab355c1e035bd3478c017b88f1c04
MD5 00b42c7f595c9f4e402168efd7065bd4
BLAKE2b-256 a7983a9d30b30aff68bca69b0a2b1c523a51429c18e00b14f20d7baa9344aed4

See more details on using hashes here.

Provenance

The following attestation bundles were made for meatpy-0.2.7.tar.gz:

Publisher: publish.yml on vgreg/MeatPy

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

File details

Details for the file meatpy-0.2.7-py3-none-any.whl.

File metadata

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

File hashes

Hashes for meatpy-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 71f6733dc6620478a36a9f8dad9a2c80b3f132dc639aba10128e31b24f5f89ee
MD5 c136bc6fadf8e23a683fa27cdd942409
BLAKE2b-256 1a2a8f5120324f2df191986e9501a93ec300ac34ae4bb34e7aff7eb43277c0df

See more details on using hashes here.

Provenance

The following attestation bundles were made for meatpy-0.2.7-py3-none-any.whl:

Publisher: publish.yml on vgreg/MeatPy

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