Binary Protocol Parsing for Low Latency Feeds
Context & Rationale
Exchanges distribute market data as raw binary packets over UDP multicast. Text
and schema-driven serialization (JSON, XML, Protocol Buffers) costs orders of
magnitude more than a fixed-layout binary decode, so feed handlers read network
bytes directly into application structs using precompiled layouts.
The dominant production risk in this code is not slowness — it is a silent
misparse. A frame decoded against the wrong layout does not raise; it returns
a plausible message with a real-looking symbol, size, and price, which then
corrupts order book state and feeds bad inputs to alpha models. Every decode
path in this skill therefore validates before it returns.
Core Concepts
- Struct Unpacking: Converting contiguous bytes into primitives using a
precompiled
struct.Struct, with endianness stated explicitly.
- Offset-Based Buffer Walking: Passing
(buffer, offset) into the decoder
rather than slicing per message. Slicing bytes copies; a memoryview plus
an offset does not.
- Fixed-Point Scaling: Exchanges transmit prices as scaled integers (ITCH
scales by 10,000). The integer tick is the authoritative value; a float is a
derived convenience.
- Message-Type Dispatch: The leading type byte selects the layout. The
decoder re-checks it so a dispatch bug fails loudly instead of silently.
- Memory Slots:
__slots__ (or packed structs in C++) reduces per-message
memory overhead and speeds attribute access.
Quick Start
See scripts/binary_parser.py, which decodes the 36-byte NASDAQ ITCH 5.0
Add Order (No MPID Attribution, type 'A') message.
view = memoryview(packet) # no copy
msg = BinaryFeedParserEngine.unpack_itch_add_order(view, offset=header_len)
book.add(msg.order_ref_id, msg.price_ticks, msg.shares) # integer ticks
When to Use
Use this skill when writing or reviewing a feed handler that decodes
fixed-layout binary market data — NASDAQ ITCH, CME MDP 3.0/SBE, Eurex T7
EMDI, or a venue's proprietary binary protocol — and you need the decode step
itself to be correct and fail-loud. It applies when you are:
- unpacking length-framed binary messages into typed application structs;
- walking multiple concatenated messages inside a single UDP payload;
- deciding how to carry exchange prices through the system (ticks vs floats);
- hardening an existing parser that trusts its input.
When NOT to Use
- Text or self-describing protocols — FIX tag=value, JSON/WebSocket feeds.
Those are parsed by field name, not byte offset; see
websocket-subscription-reconciliation-after-reconnect.
- Full ITCH order book reconstruction. This skill covers the decode layer
generically. For the ITCH message set (
A/F/E/X/D/P) and L3 book
state machine, use nasdaq-totalview-itch-feed-parsing.
- Transport concerns — A/B multicast arbitration, gap detection, and
retransmission are upstream of this skill. See
exchange-multicast-feed-handling and
sequence-number-gap-detection-for-feeds.
- Latency budgeting in pure Python. CPython is appropriate for research,
replay, and reference decoding; see Prerequisites for measured numbers before
assuming it belongs on a production hot path.
Prerequisites
The venue's binary specification document, at the exact version the feed
publishes. Field offsets change between protocol versions.
The venue's endianness, read from that spec rather than assumed —
see the Common Pitfalls entry below.
The venue's price scaling divisor and any per-instrument overrides.
Python 3.10+ (@dataclass(slots=True)).
Realistic expectations for CPython throughput. Measured on this repo's
reference implementation (CPython 3.11.15, timeit, best of 5 repeats ×
200k iterations, warm cache, single core):
struct.Struct.unpack_from alone: ~186 ns/message
- full
unpack_itch_add_order incl. validation + dataclass: ~1.08 µs/message
These are indicative single-machine numbers, not a specification. Re-measure
on your own hardware. Sustained full-depth ITCH rates need C++/Rust/FPGA.
Workflow
- Frame before you parse. Read the transport's length prefix (MoldUDP64
for ITCH, the 2-byte message size field for CME MDP 3.0) and confirm the
full frame is present. Never infer message length from the payload you hope
is there.
- Dispatch on the type byte. Read the leading message-type byte and select
the layout from a dispatch table. If the type is unknown, skip exactly
declared_length bytes and continue — do not guess a layout, and do not
abandon the packet, or you will drop every subsequent message in it.
- Re-validate the type inside the decoder. The decoder independently
asserts the type byte matches its own layout. This is what converts a
dispatch bug or an off-by-one offset from silent book corruption into an
exception at the point of failure.
- Decode at an offset, do not slice. Pass the
memoryview and an offset.
Advance by the message's declared length to reach the next message.
- Keep prices as integer ticks. Store the raw scaled integer as the
authoritative value. Convert to float only at the boundary of a component
that requires one.
- Classify decode failures. A frame that fails validation is a data
integrity event: count it, log it with the packet sequence number and byte
offset, and decide explicitly whether to skip the message or drop the
packet. Do not let a decode exception silently kill the feed thread.
- Reconcile. Periodically verify decoded state against the venue's
snapshot/recovery channel; see
market-data-snapshot-plus-delta-reconciliation.
Full procedure: see references/workflows.md.
Standards reference: see references/standards.md.
Printable pre-flight checklist: see assets/checklist.md.
Common Pitfalls
- Assuming "network protocol" means big-endian. ITCH is big-endian (
>),
but CME MDP 3.0 uses Simple Binary Encoding, which is little-endian.
Applying a blanket > to an SBE feed corrupts every multi-byte field while
still producing structurally valid-looking messages. Read the endianness off
each venue's spec.
- Decoding a frame without checking its type byte. Feeding a type
'P'
(Non-Cross Trade) frame to an Add Order decoder does not raise — it returns a
well-formed message with a wrong symbol, size, and price, which is then
applied to the book.
- Silently truncating over-long symbols at encode time. Truncating a
9-character symbol to 8 mints a frame for a different instrument than the
caller named.
- Slicing the receive buffer per message.
buf[i:i+36] copies on every
message, defeating the point of the zero-copy path. Use an offset.
- Treating a float price as the source of truth. For ITCH's uint32/10,000
domain the float conversion itself is exact, but float arithmetic is not
associative — summing notionals across fills drifts. Aggregate in ticks.
- Using native
struct alignment. A format string with no endianness
prefix uses native alignment and inserts padding: the ITCH Add Order layout
measures 36 bytes with > but 44 bytes natively.
- Assuming a 48-bit timestamp fits a 32-bit int. ITCH timestamps are 6-byte
nanoseconds since midnight and must be reassembled explicitly.
Verification
- Run
python -m unittest discover -s skills/binary-protocol-parsing-for-low-latency-feeds/scripts.
- Confirm
BinaryFeedParserEngine.ADD_ORDER_FORMAT.size == 36 and that the
format string begins with >.
- Assert wire offsets independently of the format string: MsgType@0,
StockLocate@1, Tracking@3, Timestamp@5, OrderRef@11, BuySell@19, Shares@20,
Stock@24, Price@32.
- Feed a frame whose type byte is not
'A' and confirm ITCHFrameError is
raised rather than a populated message being returned.
- Pack a known price (150.1234) and confirm
price_ticks == 1501234 exactly.
- Round-trip the maximum 48-bit timestamp (2**48 - 1).
- Walk a buffer of concatenated messages by offset and confirm each decodes
with the expected symbol and tick price.
Related Skills
nasdaq-totalview-itch-feed-parsing — the full ITCH message set and L3 order
book reconstruction built on top of this decode layer.
exchange-multicast-feed-handling — A/B multicast arbitration and gap fill,
upstream of parsing.
sequence-number-gap-detection-for-feeds — detecting dropped packets before
decoded messages reach the book.
market-data-snapshot-plus-delta-reconciliation — recovering correct state
after a decode failure or gap.
memory-mapped-ring-buffer-for-ultra-low-latency — the zero-copy buffer this
parser reads from.
order-book-depth-processing-l2-l3 — consuming decoded messages into book
state.
1---2name: binary-protocol-parsing-for-low-latency-feeds3description: >-4 Use when decoding fixed-layout binary market data by byte offset (NASDAQ ITCH, CME MDP5 3.0 SBE, Eurex T7 EMDI): frame validation, message-type dispatch, fixed-point tick6 prices and zero-copy buffer walking that fails loud.7license: Apache-2.08metadata:9 source: https://skillmd.com/skills/himanshuj16/binary-protocol-parsing-for-low-latency-feeds10 domain: algorithmic-trading11 subdomain: real-time-architecture12 tags: real-time-architecture, binary-protocol, struct-unpack, fixed-point-pricing, zero-copy13 brokers_frameworks: "Nasdaq TotalView-ITCH 5.0; CME MDP 3.0 (SBE); Python struct; Python Dataclasses"14 version: "1.1.0"15 author: algo-trading-skills-contributors16---1718# Binary Protocol Parsing for Low Latency Feeds1920## Context & Rationale21Exchanges distribute market data as raw binary packets over UDP multicast. Text22and schema-driven serialization (JSON, XML, Protocol Buffers) costs orders of23magnitude more than a fixed-layout binary decode, so feed handlers read network24bytes directly into application structs using precompiled layouts.2526The dominant production risk in this code is not slowness — it is a **silent27misparse**. A frame decoded against the wrong layout does not raise; it returns28a plausible message with a real-looking symbol, size, and price, which then29corrupts order book state and feeds bad inputs to alpha models. Every decode30path in this skill therefore validates before it returns.3132## Core Concepts33- **Struct Unpacking**: Converting contiguous bytes into primitives using a34 precompiled `struct.Struct`, with endianness stated explicitly.35- **Offset-Based Buffer Walking**: Passing `(buffer, offset)` into the decoder36 rather than slicing per message. Slicing `bytes` copies; a `memoryview` plus37 an offset does not.38- **Fixed-Point Scaling**: Exchanges transmit prices as scaled integers (ITCH39 scales by 10,000). The integer tick is the authoritative value; a float is a40 derived convenience.41- **Message-Type Dispatch**: The leading type byte selects the layout. The42 decoder re-checks it so a dispatch bug fails loudly instead of silently.43- **Memory Slots**: `__slots__` (or packed structs in C++) reduces per-message44 memory overhead and speeds attribute access.4546## Quick Start47See `scripts/binary_parser.py`, which decodes the 36-byte NASDAQ ITCH 5.048Add Order (No MPID Attribution, type `'A'`) message.4950```python51view = memoryview(packet) # no copy52msg = BinaryFeedParserEngine.unpack_itch_add_order(view, offset=header_len)53book.add(msg.order_ref_id, msg.price_ticks, msg.shares) # integer ticks54```5556## When to Use5758Use this skill when writing or reviewing a feed handler that decodes59**fixed-layout binary** market data — NASDAQ ITCH, CME MDP 3.0/SBE, Eurex T760EMDI, or a venue's proprietary binary protocol — and you need the decode step61itself to be correct and fail-loud. It applies when you are:6263- unpacking length-framed binary messages into typed application structs;64- walking multiple concatenated messages inside a single UDP payload;65- deciding how to carry exchange prices through the system (ticks vs floats);66- hardening an existing parser that trusts its input.6768## When NOT to Use6970- **Text or self-describing protocols** — FIX tag=value, JSON/WebSocket feeds.71 Those are parsed by field name, not byte offset; see72 `websocket-subscription-reconciliation-after-reconnect`.73- **Full ITCH order book reconstruction.** This skill covers the decode layer74 generically. For the ITCH message *set* (`A`/`F`/`E`/`X`/`D`/`P`) and L3 book75 state machine, use `nasdaq-totalview-itch-feed-parsing`.76- **Transport concerns** — A/B multicast arbitration, gap detection, and77 retransmission are upstream of this skill. See78 `exchange-multicast-feed-handling` and79 `sequence-number-gap-detection-for-feeds`.80- **Latency budgeting in pure Python.** CPython is appropriate for research,81 replay, and reference decoding; see Prerequisites for measured numbers before82 assuming it belongs on a production hot path.8384## Prerequisites8586- The venue's **binary specification document**, at the exact version the feed87 publishes. Field offsets change between protocol versions.88- The venue's **endianness**, read from that spec rather than assumed —89 see the Common Pitfalls entry below.90- The venue's **price scaling divisor** and any per-instrument overrides.91- Python 3.10+ (`@dataclass(slots=True)`).92- Realistic expectations for CPython throughput. Measured on this repo's93 reference implementation (CPython 3.11.15, `timeit`, best of 5 repeats ×94 200k iterations, warm cache, single core):95 - `struct.Struct.unpack_from` alone: **~186 ns/message**96 - full `unpack_itch_add_order` incl. validation + dataclass: **~1.08 µs/message**9798 These are indicative single-machine numbers, not a specification. Re-measure99 on your own hardware. Sustained full-depth ITCH rates need C++/Rust/FPGA.100101## Workflow1021031. **Frame before you parse.** Read the transport's length prefix (MoldUDP64104 for ITCH, the 2-byte message size field for CME MDP 3.0) and confirm the105 full frame is present. Never infer message length from the payload you hope106 is there.1072. **Dispatch on the type byte.** Read the leading message-type byte and select108 the layout from a dispatch table. If the type is unknown, skip exactly109 `declared_length` bytes and continue — do **not** guess a layout, and do not110 abandon the packet, or you will drop every subsequent message in it.1113. **Re-validate the type inside the decoder.** The decoder independently112 asserts the type byte matches its own layout. This is what converts a113 dispatch bug or an off-by-one offset from silent book corruption into an114 exception at the point of failure.1154. **Decode at an offset, do not slice.** Pass the `memoryview` and an offset.116 Advance by the message's declared length to reach the next message.1175. **Keep prices as integer ticks.** Store the raw scaled integer as the118 authoritative value. Convert to float only at the boundary of a component119 that requires one.1206. **Classify decode failures.** A frame that fails validation is a data121 integrity event: count it, log it with the packet sequence number and byte122 offset, and decide explicitly whether to skip the message or drop the123 packet. Do not let a decode exception silently kill the feed thread.1247. **Reconcile.** Periodically verify decoded state against the venue's125 snapshot/recovery channel; see126 `market-data-snapshot-plus-delta-reconciliation`.127128> Full procedure: see `references/workflows.md`.129> Standards reference: see `references/standards.md`.130> Printable pre-flight checklist: see `assets/checklist.md`.131132## Common Pitfalls133134- **Assuming "network protocol" means big-endian.** ITCH is big-endian (`>`),135 but **CME MDP 3.0 uses Simple Binary Encoding, which is little-endian**.136 Applying a blanket `>` to an SBE feed corrupts every multi-byte field while137 still producing structurally valid-looking messages. Read the endianness off138 each venue's spec.139- **Decoding a frame without checking its type byte.** Feeding a type `'P'`140 (Non-Cross Trade) frame to an Add Order decoder does not raise — it returns a141 well-formed message with a wrong symbol, size, and price, which is then142 applied to the book.143- **Silently truncating over-long symbols at encode time.** Truncating a144 9-character symbol to 8 mints a frame for a *different instrument* than the145 caller named.146- **Slicing the receive buffer per message.** `buf[i:i+36]` copies on every147 message, defeating the point of the zero-copy path. Use an offset.148- **Treating a float price as the source of truth.** For ITCH's uint32/10,000149 domain the float conversion itself is exact, but float *arithmetic* is not150 associative — summing notionals across fills drifts. Aggregate in ticks.151- **Using native `struct` alignment.** A format string with no endianness152 prefix uses native alignment and inserts padding: the ITCH Add Order layout153 measures 36 bytes with `>` but 44 bytes natively.154- **Assuming a 48-bit timestamp fits a 32-bit int.** ITCH timestamps are 6-byte155 nanoseconds since midnight and must be reassembled explicitly.156157## Verification158159- Run `python -m unittest discover -s skills/binary-protocol-parsing-for-low-latency-feeds/scripts`.160- Confirm `BinaryFeedParserEngine.ADD_ORDER_FORMAT.size == 36` and that the161 format string begins with `>`.162- Assert wire offsets independently of the format string: MsgType@0,163 StockLocate@1, Tracking@3, Timestamp@5, OrderRef@11, BuySell@19, Shares@20,164 Stock@24, Price@32.165- Feed a frame whose type byte is not `'A'` and confirm `ITCHFrameError` is166 raised rather than a populated message being returned.167- Pack a known price (150.1234) and confirm `price_ticks == 1501234` exactly.168- Round-trip the maximum 48-bit timestamp (2**48 - 1).169- Walk a buffer of concatenated messages by offset and confirm each decodes170 with the expected symbol and tick price.171172## Related Skills173174- `nasdaq-totalview-itch-feed-parsing` — the full ITCH message set and L3 order175 book reconstruction built on top of this decode layer.176- `exchange-multicast-feed-handling` — A/B multicast arbitration and gap fill,177 upstream of parsing.178- `sequence-number-gap-detection-for-feeds` — detecting dropped packets before179 decoded messages reach the book.180- `market-data-snapshot-plus-delta-reconciliation` — recovering correct state181 after a decode failure or gap.182- `memory-mapped-ring-buffer-for-ultra-low-latency` — the zero-copy buffer this183 parser reads from.184- `order-book-depth-processing-l2-l3` — consuming decoded messages into book185 state.