Available languages: [Python](https://databento.com/docs/examples/order-book/order-actions.python.md) · [C++](https://databento.com/docs/examples/order-book/order-actions.cpp.md) · [Rust](https://databento.com/docs/examples/order-book/order-actions.rust.md) · [HTTP](https://databento.com/docs/examples/order-book/order-actions.http.md) · [Raw](https://databento.com/docs/examples/order-book/order-actions.raw.md)

Source: https://databento.com/docs/examples/order-book/order-actions


# Types of order book events

## Overview

This example shows how to use the [Historical client](https://databento.com/docs/api-historical/client.md) 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](https://databento.com/docs/schemas/mbo.md) for this example.
This is the most granular schema which contains every individual order event keyed by its order ID.

## Example

```cpp
#include <databento/datetime.hpp>
#include <databento/historical.hpp>
#include <databento/pretty.hpp>
#include <iomanip>
#include <iostream>

namespace db = databento;

int main() {
  auto client = db::Historical::Builder().SetKey("$YOUR_API_KEY").Build();

  // We'll inspect the history of a particular order
  constexpr auto kOrderId = 6410543150678;
  auto handle_record = [](const db::Record& record) {
    const auto& mbo = record.Get<db::MboMsg>();
    if (mbo.order_id == kOrderId) {
      constexpr auto kSep = "  ";
      std::cout << db::ToIso8601(mbo.ts_recv) << kSep << std::setprecision(2)
                << db::pretty::Px{mbo.price} << kSep << std::setw(2)
                << std::setfill(' ') << mbo.size << kSep;
      switch (mbo.action) {
        case db::Action::Add:
          std::cout << "[A]dded";
          break;
        case db::Action::Cancel:
          std::cout << "[C]anceled";
          break;
        case db::Action::Modify:
          std::cout << "[M]odified";
          break;
        case db::Action::Clear:
          std::cout << "clea[R]ed";
          break;
        case db::Action::Trade:
          std::cout << "[T]raded";
          break;
        case db::Action::Fill:
          std::cout << "[F]illed";
          break;
        case db::Action::None:
          std::cout << "[N]one";
          break;
      }
      std::cout << '\n';
    }
    return db::KeepGoing::Continue;
  };

  // First, we request a minute of MBO data
  client.TimeseriesGetRange("GLBX.MDP3",
                            db::DateTimeRange<std::string>{"2022-08-26T10:59"},
                            {"ES.v.0"}, db::Schema::Mbo, db::SType::Continuous,
                            db::SType::InstrumentId, {}, {}, handle_record);
  return 0;
}
```
## Result

```plaintext
2022-08-26T10:59:37.217088184Z  4184.75  14  [A]dded
2022-08-26T10:59:50.544746948Z  4184.75   7  [F]illed
2022-08-26T10:59:50.544805832Z  4184.75   7  [M]odified
2022-08-26T10:59:50.544805832Z  4184.75   1  [F]illed
2022-08-26T10:59:50.544805832Z  4184.75   6  [M]odified
2022-08-26T10:59:50.544880884Z  4184.75   1  [F]illed
2022-08-26T10:59:50.544880884Z  4184.75   5  [M]odified
2022-08-26T10:59:50.544927788Z  4184.75   1  [F]illed
2022-08-26T10:59:50.544927788Z  4184.75   4  [M]odified
2022-08-26T10:59:50.544927788Z  4184.75   1  [F]illed
2022-08-26T10:59:50.544927788Z  4184.75   3  [M]odified
2022-08-26T10:59:50.545075499Z  4184.75   3  [F]illed
2022-08-26T10:59:50.545093643Z  4184.75   3  [C]anceled
```


# Types of order book events

## Overview

This example shows how to use the [Historical client](https://databento.com/docs/api-historical/client.md) 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](https://databento.com/docs/schemas/mbo.md) for this example. This is the most granular schema which contains every individual order event keyed by its order ID.

## Example

```python
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

```plaintext
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
```


# Types of order book events

## Overview
This example shows how to use the [Historical client](https://databento.com/docs/api-historical/client.md) 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](https://databento.com/docs/schemas/whats-a-schema.md) for this example.
This is the most granular schema which contains every individual order event keyed by its order ID.

## Example
```rust
use databento::{
    HistoricalClient,
    dbn::{Action, MboMsg, MboRec, SType, Schema, pretty::Px},
    historical::timeseries::GetRangeParams,
};
use time::macros::datetime;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut client = HistoricalClient::builder().key_from_env()?.build()?;
    // First, we request a minute of MBO data
    let mut decoder = client
        .timeseries()
        .get_range(
            &GetRangeParams::builder()
                .dataset("GLBX.MDP3")
                .date_time_range(
                    datetime!(2022-08-26 10:59:00 UTC)..datetime!(2022-08-26 11:00:00 UTC),
                )
                .symbols("ES.v.0")
                .stype_in(SType::Continuous)
                .schema(Schema::Mbo)
                .build(),
        )
        .await?;
    // Now, we will inspect the history of a particular order
    const ORDER_ID: u64 = 6410543150678;
    println!("events for order_id: {ORDER_ID}");
    while let Some(record) = decoder.decode_record::<MboMsg>().await? {
        if record.order_id != ORDER_ID {
            continue;
        }
        let ts_recv = record.ts_recv().unwrap();
        let price = Px(record.price);
        let size = record.size;
        match record.action()? {
            Action::Add => {
                println!("{ts_recv}  {price:.2}  {size:2}  [A]dded")
            }
            Action::Cancel => {
                println!("{ts_recv}  {price:.2}  {size:2}  [C]anceled")
            }
            Action::Modify => {
                println!("{ts_recv}  {price:.2}  {size:2}  [M]odified")
            }
            Action::Clear => {
                println!("{ts_recv}  {price:.2}  {size:2}  clea[R]ed")
            }
            Action::Trade => {
                println!("{ts_recv}  {price:.2}  {size:2}  [T]raded")
            }
            Action::Fill => {
                println!("{ts_recv}  {price:.2}  {size:2}  [F]illed")
            }
            Action::None => {
                println!("{ts_recv}  {price:.2}  {size:2}  [N]one")
            }
        };
    }

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