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

Source: https://databento.com/docs/examples/options/nbbo-resampling


# Resample US equity options NBBO

## Overview

This example shows how to use the [Historical client](https://databento.com/docs/api-historical/client.md) to retrieve the regional and consolidated best bid and offer (BBO) for the US equities market from the OPRA dataset. From this, we'll filter out the NBBO and then downsample at a 1-minute interval. To do this we will use pandas, specifically the `DataFrame.groupby` method.

Note that sometimes the BBO is referred to as a *quote*, and *ask* and *offer* are synonyms referring to the sell price.

## MBP-1 schema

We'll use the [MBP-1 schema](https://databento.com/docs/schemas/mbp-1.md) in this example, which contains the price and size for both the bid and the ask.

For datasets such as OPRA that cover multiple execution venues (exchanges), the `publisher_id` field indicates which venue the record is associated with.
There will be records for each individual venue to indicate the *regional* BBO, and then an additional record to represent the *consolidated* NBBO.

For the OPRA dataset, which is the SIP for US equity options, the consolidated `publisher_id` is `30` (venue `OPRA`).

Trades, which are included in the MBP-1 schema as records with `action=T`, also use `publisher_id` to indicate which venue they occurred on.

> **See also**
>
> [API reference - Historical - metadata.list_publishers](https://databento.com/docs/api-historical/metadata/metadata-list-publishers.md)

## Example

```python
import databento as db
import pandas as pd

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

# Next, request MBP-1 for the SPIKE options chain (a volatility index)
df = client.timeseries.get_range(
    dataset="OPRA.PILLAR",
    start="2023-07-03",
    schema="mbp-1",
    stype_in="parent",
    symbols="SPIKE.OPT",
    limit=20_000,
).to_df()

# Only include NBBO records (`publisher_id == 30`)
df = df[df["publisher_id"] == db.Publisher.OPRA_PILLAR_OPRA.to_int()]

# Resample the data at a fixed one minute interval, per symbol
df_1m = df.groupby([pd.Grouper(freq="1T"), "symbol"]).first()

# Print the NBBO
print(df_1m[["bid_px_00", "ask_px_00"]])
```
## Result

```plaintext
                                                 bid_px_00  ask_px_00
ts_recv                   symbol
2023-07-03 13:30:00+00:00 SPIKE 230719C00010000       5.25       5.40
                          SPIKE 230719C00010500       4.75       4.90
                          SPIKE 230719C00011000       4.25       4.40
                          SPIKE 230719C00011500       3.75       3.90
                          SPIKE 230719C00012000       3.25       3.40
...                                                    ...        ...
2023-07-03 13:34:00+00:00 SPIKE 231018C00047500       0.16       0.70
                          SPIKE 231018C00050000       0.12       0.66
                          SPIKE 231018C00055000       0.06       0.59
                          SPIKE 231018P00014000       0.15       0.69
                          SPIKE 231115C00050000       0.20       0.75

[1657 rows x 2 columns]
```
