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

Source: https://databento.com/docs/examples/symbology/live-symbol-mapping


# Symbology mapping for live data

## Overview

In this example we will use the [LiveThreaded client](https://databento.com/docs/api-live/client/live.md) to create symbology mappings using the `SymbolMappingMsg`, which are sent at the start of every session and when a subscribed symbol's mapping changes.
These messages allow us to map between the instrument ID on every record and more human-readable STypes like raw and continuous contract symbologies.

[PitSymbolMap](https://databento.com/docs/api-live/helpers/pit-symbol-map.md) streamlines keeping track of these mappings.

> **See also**
>
> [Symbology](https://databento.com/docs/api-live/basics/symbology.md) for further details.

## Example
```cpp
#include <cstddef>
#include <databento/constants.hpp>  // dataset
#include <databento/live.hpp>       // LiveBuilder
#include <databento/record.hpp>     // SymbolMappingMsg
#include <iostream>
#include <string>
#include <vector>

namespace db = databento;

int main() {
  auto client = db::LiveBlocking::Builder()
                    .SetKey("$YOUR_API_KEY")
                    .SetDataset(db::Dataset::GlbxMdp3)
                    .BuildBlocking();
  std::vector<std::string> raw_symbols{"ES.c.0", "ES.c.1", "ES.c.2", "ES.c.3"};
  client.Subscribe(raw_symbols, db::Schema::Ohlcv1S, db::SType::Continuous);
  client.Start();

  std::size_t mappings_received{};
  while (mappings_received < raw_symbols.size()) {
    auto record = client.NextRecord();
    if (auto* sym_mapping = record.GetIf<db::SymbolMappingMsg>()) {
      std::cout << sym_mapping->STypeInSymbol() << " ("
                << sym_mapping->STypeOutSymbol() << ") has an instrument ID of "
                << sym_mapping->hd.instrument_id << '\n';
      ++mappings_received;
    }
  }
  return 0;
}
```
## Result
```plaintext
ES.c.0 (ESM5) has an instrument ID of 4916
ES.c.1 (ESU5) has an instrument ID of 14160
ES.c.2 (ESZ5) has an instrument ID of 294973
ES.c.3 (ESH6) has an instrument ID of 42140878
```


# Symbology mapping for live data

## Overview

In this example we will use the [Live client](https://databento.com/docs/api-live/client/live.md) 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

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

```plaintext
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**
>
> [Symbology](https://databento.com/docs/api-live/basics/symbology.md) for further details.


# Symbology mapping for live data

## Overview
In this example we will use the [Live client](https://databento.com/docs/api-live/client/live.md) to create symbology mappings using the `SymbolMappingMsg`, which are sent at the start of every session and when a subscribed symbol's mapping changes.
These messages allow us to map between the instrument ID on every record and more human-readable STypes like raw and continuous contract symbologies.

[PitSymbolMap](https://databento.com/docs/api-live/helpers/pit-symbol-map.md) streamlines keeping track of these mappings.

> **See also**
>
> [Symbology](https://databento.com/docs/api-live/basics/symbology.md) for further details.

## Example
```rust
use databento::{
    LiveClient,
    dbn::{Dataset, SType, Schema, SymbolMappingMsg, SymbolMappingRec},
    live::Subscription,
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut client = LiveClient::builder()
        .key_from_env()?
        .dataset(Dataset::GlbxMdp3)
        .build()
        .await?;
    let symbols = vec!["ES.c.0", "ES.c.1", "ES.c.2", "ES.c.3"];
    client
        .subscribe(
            Subscription::builder()
                .schema(Schema::Ohlcv1S)
                .symbols(symbols.clone())
                .stype_in(SType::Continuous)
                .build(),
        )
        .await?;
    client.start().await?;
    let mut count = 0;
    while let Some(record) = client.next_record().await? {
        if let Some(symbol_mapping) = record.get::<SymbolMappingMsg>() {
            let instrument_id = symbol_mapping.hd.instrument_id;
            let symbol = symbol_mapping.stype_in_symbol()?;
            println!("{symbol} has an instrument ID of {instrument_id}");
            count += 1;
            if count >= symbols.len() {
                break;
            }
        }
    }
    client.close().await?;
    Ok(())
}
```
## Result
```plaintext
ES.c.0 has an instrument ID of 42140878
ES.c.1 has an instrument ID of 42140864
ES.c.2 has an instrument ID of 42140870
ES.c.3 has an instrument ID of 10252
```
