Best bid, best offer, and midprice
Overview
In this example we will use the Historical client to retrieve the best bid and offer (BBO) and use these data to calculate the midprice. To do this we will request a subset of the entire order book data and then average the bid and ask prices to calculate the midprice.
Note that ask and offer are synonyms referring to the sell price.
TBBO schema
We'll use the TBBO schema to demonstrate this. This schema contains every trade execution event together with the best bid and offer immediately before the trade.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import databento as db
# Create a historical client
client = db.Historical("$YOUR_API_KEY")
# Request TBBO data
data = client.timeseries.get_range(
dataset="GLBX.MDP3",
start="2022-10-28T20:30:00",
end="2022-10-28T21:00:00",
symbols="ESH3",
stype_in="raw_symbol",
schema="tbbo",
)
# Convert to DataFrame
tbbo_data = data.to_df()
# Calculate the midprice for each row
tbbo_data["mid_px"] = tbbo_data[["ask_px_00", "bid_px_00"]].mean(axis=1)
# We now have a DataFrame that contains execution price, BBO, and midprice
print(tbbo_data[["symbol", "side", "price", "ask_px_00", "mid_px", "bid_px_00"]]) Result
1 2 3 4 5 6 7 8 9 10 11
symbol side price ask_px_00 mid_px bid_px_00 ts_recv 2022-10-28 20:30:59.047138053+00:00 ESH3 B 3955.25 3955.25 3955.000 3954.75 2022-10-28 20:37:53.112494436+00:00 ESH3 A 3955.00 3955.75 3955.375 3955.00 2022-10-28 20:39:05.152071481+00:00 ESH3 A 3955.00 3955.75 3955.375 3955.00 2022-10-28 20:51:37.650473254+00:00 ESH3 B 3955.50 3955.50 3955.125 3954.75 2022-10-28 20:52:36.842346377+00:00 ESH3 A 3956.00 3956.25 3956.125 3956.00 2022-10-28 20:52:57.780797186+00:00 ESH3 B 3955.00 3955.00 3954.750 3954.50 2022-10-28 20:57:28.474236098+00:00 ESH3 A 3954.75 3956.25 3955.500 3954.75 2022-10-28 20:59:15.075191111+00:00 ESH3 A 3953.75 3956.00 3954.875 3953.75 2022-10-28 20:59:34.607239899+00:00 ESH3 A 3954.50 3956.00 3955.250 3954.50