Support

Types of order book events

Overview

This example shows how to use the Historical client to retrieve individual order events and process all events for a single order. This will allow us to track the life cycle of an order, such as when it was added to the order book and when it was removed.

MBO schemas

We'll use the MBO schema for this example. This is the most granular schema which contains every individual order event keyed by its order ID.

Example

import databento as db

# First, create a historical client
client = db.Historical("$YOUR_API_KEY")

# Next, we will request a minute of MBO data
data = client.timeseries.get_range(
    dataset="GLBX.MDP3",
    start="2022-08-26T10:59:00",
    end="2022-08-26T11:00:00",
    symbols="ES.v.0",
    stype_in="continuous",
    schema="mbo",
)

df = data.to_df()

# Now, we will inspect the history of a particular order
ORDER_ID = 6410543150678
order_df = df.loc[df["order_id"] == ORDER_ID]

# Finally, we will process each event for this order
print(f"events for order_id: {ORDER_ID}")
for index, row in order_df.iterrows():
    event = (row["price"], row["size"])
    action = row["action"]
    if action == "A":
        # Add action
        print(index, *event, "[A]dded", sep="\t")
    elif action == "C":
        # Cancel action
        print(index, *event, "[C]anceled", sep="\t")
    elif action == "M":
        # Modify action
        print(index, *event, "[M]odified", sep="\t")
    elif action == "R":
        # Book clear action
        print(index, *event, "clea[R]ed", sep="\t")
    elif action == "T":
        # Trade action
        print(index, *event, "[T]raded", sep="\t")
    elif action == "F":
        # Fill action
        print(index, *event, "[F]illed", sep="\t")
    elif action == "N":
        # None action
        print(index, *event, "[N]one", sep="\t")

Result

events for order_id: 6410543150678
2022-08-26 10:59:37.217088184+00:00	4184.75	14	[A]dded
2022-08-26 10:59:50.544746948+00:00	4184.75	7	[F]illed
2022-08-26 10:59:50.544805832+00:00	4184.75	7	[M]odified
2022-08-26 10:59:50.544805832+00:00	4184.75	1	[F]illed
2022-08-26 10:59:50.544805832+00:00	4184.75	6	[M]odified
2022-08-26 10:59:50.544880884+00:00	4184.75	1	[F]illed
2022-08-26 10:59:50.544880884+00:00	4184.75	5	[M]odified
2022-08-26 10:59:50.544927788+00:00	4184.75	1	[F]illed
2022-08-26 10:59:50.544927788+00:00	4184.75	4	[M]odified
2022-08-26 10:59:50.544927788+00:00	4184.75	1	[F]illed
2022-08-26 10:59:50.544927788+00:00	4184.75	3	[M]odified
2022-08-26 10:59:50.545075499+00:00	4184.75	3	[F]illed
2022-08-26 10:59:50.545093643+00:00	4184.75	3	[C]anceled