Support

Symbology mapping for live data

Overview

In this example we will use the Live client to create symbology mappings using the SymbolMappingMsg. This message is sent at the start of every session and when a subscribed symbol mapping is updated. These messages allow us to map between the instrument ID on every record and more human-readable STypes like raw and continuous contract symbologies.

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 a few symbols
live_client.subscribe(
    dataset="GLBX.MDP3",
    schema="ohlcv-1s",
    symbols=["ES.c.0", "ES.c.1", "ES.c.2", "ES.c.3"],
    stype_in="continuous",
)

# Finally, we will iterate over the incoming records.
# The SymbolMappingMsgs will be sent at the start.
for record in live_client:
    if isinstance(record, db.SymbolMappingMsg):
        instrument_id = record.instrument_id

        # The stype_in_symbol contains the continuous symbology we requested
        continuous_symbol = record.stype_in_symbol

        # The stype_out_symbol contains the raw symbol for smart symbology
        raw_symbol = record.stype_out_symbol

        print(
            f"{continuous_symbol} ({raw_symbol}) has an instrument ID of {instrument_id}",
        )

        # Close the connection
        # We will still process all received data
        live_client.stop()

Result

ES.c.0 (ESM4) has an instrument ID of 5602
ES.c.1 (ESU4) has an instrument ID of 118
ES.c.2 (ESZ4) has an instrument ID of 183748
ES.c.3 (ESH5) has an instrument ID of 5002
See also
See also

Symbology for further details.