Support

Subscribe to MBO snapshot

Overview

This example shows how to use the Live client to request a snapshot for MBO schema, which can be used to reconstruct the limit order book without replaying the entire trading session.

More details about live snapshot can be found in this article.

Requesting live snapshot

To request a subscription with snapshot, we provide snapshot=True and leave start default to None. Once the session has started, it streams all the snapshot records (with the F_SNAPSHOT flag set), followed by real-time records. We stop iterating over the records when encountering a record with the last flag set, to make sure the book is in a valid state.

Example

import databento as db

# First, create a live client and connect
live_client = db.Live(key="$YOUR_API_KEY")

# Next, we will subscribe to the MBO snapshot
live_client.subscribe(
    dataset="GLBX.MDP3",
    schema="mbo",
    stype_in="continuous",
    symbols="ES.c.0",
    snapshot=True,
)

# Finally, we will process all snapshot records, and stop at the first record
# with F_LAST flag, which indicates that the snapshot is complete and the
# order book is in a valid state
for record in live_client:
    if isinstance(record, db.MBOMsg):
        if record.flags & db.RecordFlags.F_SNAPSHOT:
            print(f"Snapshot: {record}")
        else:
            print(f"Live: {record}")

        if record.flags & db.RecordFlags.F_LAST:
            print("Snapshot is complete")
            break
    elif isinstance(record, db.ErrorMsg):
        print(f"Error {record.err}")
        break

Result

Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721730475728226035 }, order_id: 0, price: UNDEF_PRICE, size: 0, flags: SNAPSHOT | BAD_TS_RECV (40), channel_id: 0, action: 'R', side: 'N', ts_recv: 1721730475728226035, ts_in_delta: 0, sequence: 0 }
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721579296070204729 }, order_id: 6413514530616, price: 5547.500000000, size: 8, flags: SNAPSHOT | BAD_TS_RECV (40), channel_id: 0, action: 'A', side: 'B', ts_recv: 1721730475728226035, ts_in_delta: 0, sequence: 1022 }
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721579296070204729 }, order_id: 6413514530330, price: 5547.250000000, size: 7, flags: SNAPSHOT | BAD_TS_RECV (40), channel_id: 0, action: 'A', side: 'B', ts_recv: 1721730475728226035, ts_in_delta: 0, sequence: 1022 }
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721579296070204729 }, order_id: 6413514530315, price: 5547.000000000, size: 9, flags: SNAPSHOT | BAD_TS_RECV (40), channel_id: 0, action: 'A', side: 'B', ts_recv: 1721730475728226035, ts_in_delta: 0, sequence: 1022 }
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721579296070204729 }, order_id: 6413514528530, price: 5546.750000000, size: 8, flags: SNAPSHOT | BAD_TS_RECV (40), channel_id: 0, action: 'A', side: 'B', ts_recv: 1721730475728226035, ts_in_delta: 0, sequence: 1021 }
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721579296070204729 }, order_id: 6413514517321, price: 5546.500000000, size: 7, flags: SNAPSHOT | BAD_TS_RECV (40), channel_id: 0, action: 'A', side: 'B', ts_recv: 1721730475728226035, ts_in_delta: 0, sequence: 1020 }
...
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721730473718309131 }, order_id: 6413522272291, price: 5610.250000000, size: 1, flags: SNAPSHOT | BAD_TS_RECV (42), channel_id: 0, action: 'A', side: 'A', ts_recv: 1721730475728226035, ts_in_delta: 0, sequence: 13006709 }
Snapshot: MboMsg { hd: RecordHeader { length: 14, rtype: Mbo, publisher_id: GlbxMdp3Glbx, instrument_id: 118, ts_event: 1721730474807313645 }, order_id: 6413522272292, price: 5610.000000000, size: 2, flags: LAST | SNAPSHOT | BAD_TS_RECV (170), channel_id: 0, action: 'A', side: 'B', ts_recv: 1721730475728226035, ts_in_delta: 0, sequence: 13006710 }
Snapshot is complete