> For the complete documentation index, see [llms.txt](https://docs.koinju.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.koinju.io/exchanges/okx.md).

# OKX Spot: data collection and validation

OKX spot historical trade data on Koinju, available since 2021-10-01. How trades are collected from OKX's live feed and validated against OKX's own candles and trade IDs.

Koinju provides trades and OHLCV candles for all OKX spot markets since **2021-10-01**, under the exchange ID `okx`. This page explains how we collect those trades and how we check them against OKX's own data.

{% hint style="info" %}
How far back you can query depends on your plan: see [Pricing](/pricing.md). Every exchange and its start date is listed on [Coverage](/data/coverage.md).
{% endhint %}

## At a glance

| Property                | Value                                            |
| ----------------------- | ------------------------------------------------ |
| Exchange ID             | `okx`                                            |
| Available since         | 2021-10-01                                       |
| Data                    | Public trades, OHLCV                             |
| Live source             | OKX WebSocket, All trades channel (`trades-all`) |
| Daily validation        | Every day, against OKX's UTC daily candles       |
| Trade-ID sequence check | Several times a day, against OKX's trade IDs     |

## How we collect OKX trades

* **All trades channel.** We subscribe to OKX's All trades channel (`trades-all`) for every listed OKX spot market. Each record keeps OKX's own trade ID.
* **New listings picked up automatically.** The market list is refreshed every minute, and new markets are subscribed without a restart.
* **Two collectors.** Two collectors subscribe to every market in parallel. Both copies of a trade carry the same key (exchange, market and trade ID), and the second copy is dropped before storage, so either collector alone is enough to capture a trade.
* **OKX's timestamps.** A trade's `timestamp` is the trade time reported by OKX, not the time we received it.
* **Exact values.** Prices and quantities are stored exactly as OKX publishes them, as high-precision decimals with no rounding.
* **Automatic reconnection.** Connections are kept alive with regular pings. A dropped connection is re-established and resubscribed automatically, and anything missed while reconnecting is recovered by the validation below.

## How we validate OKX trades

OKX spot data goes through two independent checks: daily validation against OKX's candles, and a trade-ID sequence check against OKX's trade IDs. The two complement each other: the trade-ID check pinpoints individual missing trades within hours, while daily validation also catches a market that goes completely quiet, which leaves no trade IDs to check.

### Daily validation against OKX's own candles

Every day, for every market, we compare the volume we collected with the volume in OKX's own daily candle. We request OKX's UTC-aligned daily candle (`1Dutc`), so each comparison covers exactly one UTC day. OKX's figure is independent of our live feed, so a shortfall means trades are missing.

1. **Compare** each market's daily volume with OKX's daily candle.
2. **Fill:** for a market with a shortfall, compare hour by hour, fetch the trades of the hours that differ from OKX's REST API, and add only the trades we do not already hold.
3. **Compare again** on the updated data. Any difference that remains is traced to OKX's own data.

A day is marked as validated only after all three steps complete. If any step fails, the day stays open and is checked again on the next run.

### Trade-ID sequence check

OKX gives every trade in a market a sequential trade ID, so a jump in the sequence shows exactly which trades are missing. Several times a day, we scan newly collected trades inside our ClickHouse database for jumps in trade IDs (gaps) and repeated trade IDs (duplicates).

* For each gap, the missing trade IDs are requested from OKX and added, but only if their timestamps fall inside the gap's time window.
* A repeated trade ID is reduced to a single record, and the hour's volume is verified afterwards.

## OKX-specific details

* **Trade-ID restarts on relisted markets.** An in-depth internal review of our full OKX history showed that when OKX delists a market and later relists it, the market's trade IDs start again from 1. We built this into our trade-ID checks: each restart is recognised rather than mistaken for missing or duplicated trades, and a trade is only added inside the time window of the gap it belongs to.
* **UTC daily candles.** Daily validation uses OKX's UTC-aligned daily candle, which matches the UTC day boundaries of our data.
* **OKX's own trade IDs.** `trade_id` is the ID OKX assigned, so any trade can be matched against OKX's own data.

## Access OKX data

* [Public trades](/data/public-trades.md): every trade, through SQL (`api.trade`) or REST, with `exchange = 'okx'`.
* [OHLCV](/data/ohlcv.md): candles for every market.
* [Market list](/data/market-list.md): all `okx` markets and their symbols.
* [How to connect](/how-to-connect.md): SQL, REST and client setup.

## FAQ

**How complete is Koinju's OKX spot trade data?**

It is checked in two independent ways: every day against OKX's own daily candles, and several times a day trade ID by trade ID. Missing trades found by either check are fetched from OKX and added.

**How does Koinju handle OKX trade-ID resets?**

Our trade-ID checks recognise them. When OKX relists a market, its trade IDs start again from 1, so each restart is treated as a new sequence rather than as missing or duplicated trades, and a trade is only added if its timestamp falls inside the time window of the gap it belongs to.

**Which timestamp does Koinju store?**

The trade time reported by OKX.

**Which OKX markets are included?**

Every spot market OKX lists as trading. The market list is refreshed every minute, so new listings are collected from their first minutes.
