> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tplus.cx/llms.txt
> Use this file to discover all available pages before exploring further.

# Market data client

> Depth snapshots, diffs, klines, public trades, tickers, and reconnect behavior.

Market discovery is on OMS. Public market data is on MDS.

```text theme={null}
OMS: https://oms.tplus.cx
MDS: https://mds.tplus.cx
```

Use OMS `GET /markets` or `GET /market/{asset_id}` to discover listed markets and book decimals, then use MDS for depth, trades, klines, and tickers.

## Raw depth sync

```bash theme={null}
curl https://mds.tplus.cx/marketdepth/200
```

Then open:

```text theme={null}
wss://mds.tplus.cx/marketdepth/diff/200
```

Resync loop:

```python theme={null}
snapshot = get("/marketdepth/200")
book = build_book(snapshot)
last_seq = snapshot["sequence_number"]

for diff in ws("wss://mds.tplus.cx/marketdepth/diff/200"):
    if diff["sequence_number"] != last_seq + 1:
        snapshot = get("/marketdepth/200")
        book = build_book(snapshot)
        last_seq = snapshot["sequence_number"]
        continue
    apply_diff(book, diff)
    last_seq = diff["sequence_number"]
```

## tpluspy parsing

```python theme={null}
import httpx

from tplus.model.asset_identifier import AssetIdentifier
from tplus.model.orderbook import OrderBook, OrderBookDiff

asset = AssetIdentifier(200)

async with httpx.AsyncClient(base_url="https://mds.tplus.cx") as md:
    response = await md.get(f"/marketdepth/{asset}")
    response.raise_for_status()
    snapshot = OrderBook(**response.json())

    # Parse each websocket message body the same way.
    diff = OrderBookDiff(**message_json)
```

The current SDK exports typed MDS models, not a dedicated public `MarketDataClient`.

## Klines

MDS exposes one fixed kline bucket set by the service, not a selectable interval API. The current service config uses `timebar_bucket_size_ms = 1000`, so one bucket is one second. REST history is capped at 18000 rows per page and the in-memory retention is 7 days of one-second bars.

REST:

```bash theme={null}
curl "https://mds.tplus.cx/klines/200?limit=100&end_timestamp_ns=1760000000000000000"
```

WS:

```text theme={null}
wss://mds.tplus.cx/klines/diff/200
```

Kline updates are emitted when a bucket closes. The active open bucket is included in ticker 24h stats but is not in the REST kline history until flushed. Kline `volume` is quote/notional volume: the updater adds `trade.price * trade.quantity`.

## v0 and v1 envelopes

MDS market-data streams send a welcome frame, then bare payloads:

```json theme={null}
{"type":"subscriptions","channels":[{"name":"depth"}],"errors":null}
```

OMS authenticated streams support the v1 envelope when `tplus.ws.v1` is present in `Sec-WebSocket-Protocol`; v0 is the bare payload. See [WebSocket streams](/api-reference/websockets).

## Reconnect

On disconnect, re-fetch the matching REST snapshot before applying new WS messages. MDS streams close on broadcast lag without a resync envelope. OMS v1 streams can send `RESYNC_REQUIRED` before close code `1013`.
