> 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 historical trade data

OKX Spot historical trade data since 2021-10-01, available through SQL or REST in one unified schema and checked every day against OKX's own data. Free demo, no sign-up.

Koinju provides OKX Spot trades and OHLCV candles for all markets since **2021-10-01**, under the exchange ID `okx`. This page explains what that gives you compared with OKX's own API, how we collect the data, and how we verify that no trades are missed.

{% 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 %}

## Why use Koinju for OKX data

* **Full history in one place.** OKX's REST trade history API, which our checks use to add missing trades, covers the last 3 months and is paged under a rate limit ([OKX docs](https://www.okx.com/docs-v5/en/#order-book-trading-market-data-get-trades-history)). Koinju provides it as one continuous history since 2021-10-01, aligned to UTC days and queryable for any period through SQL or REST.
* **One schema across exchanges.** Unified symbols and columns across every exchange we cover, OKX included, so the same query works on all of them.
* **Checked against OKX's own data.** Every day, each market's volume is compared with OKX's own daily candle and any missing trades are added. Trade IDs are also checked several times a day.
* **Faithful to OKX.** OKX's own trade IDs and trade times, with prices and quantities exactly as OKX publishes them.

## At a glance

| Property                | Value                                                        |
| ----------------------- | ------------------------------------------------------------ |
| Exchange ID             | `okx`                                                        |
| Historical data since   | 2021-10-01                                                   |
| Data types              | Public trades, OHLCV                                         |
| Collected from          | OKX's real-time WebSocket, All trades channel (`trades-all`) |
| Volume check            | 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

* **Every trade, individually.** We subscribe to OKX's [All trades channel](https://www.okx.com/docs-v5/en/#order-book-trading-market-data-ws-all-trades-channel) (`trades-all`) for every listed OKX Spot market. OKX pushes each trade as its own update, and each row 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.
* **Active redundancy.** Every market is collected twice, by two collectors running in parallel. If one connection drops, it reconnects and resubscribes automatically while the other keeps collecting trades. Both copies of a trade share one key (exchange, market and trade ID), so the second copy is dropped before storage, and anything missed is recovered by the checks below.
* **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.

## From OKX's message to your data

Each trade OKX publishes is stored as one row, with OKX's fields mapped to the same columns used for every exchange:

| OKX field | Koinju column | Notes                                             |
| --------- | ------------- | ------------------------------------------------- |
| `instId`  | `market`      | Koinju's universal symbol, for example `BTC-USDT` |
| `tradeId` | `trade_id`    | OKX's own trade ID                                |
| `px`      | `price`       | Exact decimal, no rounding                        |
| `sz`      | `quantity`    | In the base currency                              |
| `side`    | `side`        | `buy` or `sell`, as OKX publishes it              |
| `ts`      | `timestamp`   | OKX's trade time, in milliseconds                 |

## How we verify that no OKX trades are missed

OKX Spot data goes through two independent checks: a volume check 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 the volume check also catches a market that goes completely quiet, which leaves no trade IDs to check.

### Volume check against OKX's own candles

The reference is OKX's own daily candle for each UTC day. A daily candle is only final once the day has closed, so each day is checked the morning after, against OKX's final figures for that day. The daily rhythm follows the reference data rather than an arbitrary time window.

1. **Compare.** Each market's volume for the UTC day is compared with OKX's daily candle. Markets where our volume is lower than OKX's go to step 2.
2. **Add.** For those markets, the comparison is repeated hour by hour. The trades of the hours that differ are fetched from OKX's REST API, and only trades we do not already hold are added.
3. **Re-check.** The whole comparison runs again on the updated data, and any difference that remains is flagged for review.

A day is marked as checked only after all three steps complete. If any step fails, the day stays open and is retried 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 for jumps in trade IDs (gaps) and repeated trade IDs (duplicates).

* **Gaps.** For each gap, the missing trades are requested from OKX by trade ID, and each one is added only if its timestamp falls inside the gap's time window.
* **Duplicates.** When a trade ID appears more than once, the extra copies are removed so a single row remains, and the hour's volume is checked again to confirm that only the copies were removed.

## OKX-specific details

* **UTC daily candles.** OKX's standard daily candle (`1D`) runs midnight to midnight UTC+8, which is 16:00 to 16:00 UTC. We request OKX's UTC version (`1Dutc`), so OKX's figure covers exactly the same 24 hours as our daily total.
* **The exact figure compared.** The volume check compares OKX's candle field `vol`, which for spot markets is the volume in the base currency, with the sum of the trade quantities we hold for that day.
* **Trade-ID restarts on relisted markets.** When OKX delists a market and later relists it, the market's trade IDs start again from 1, so one trade ID can belong to two different trades. The trade-ID sequence check keeps them apart: a missing trade is only added if its timestamp falls inside the gap's time window.

## Access OKX data

* [Try it on the free demo database](https://demo.koinju.io/?sql=SELECT+timestamp,+market,+side,+price,+quantity+FROM+api.trade+WHERE+exchange+=+'okx'+AND+timestamp+%3E=+%28SELECT+max%28timestamp%29+-+INTERVAL+1+HOUR+FROM+public_data.candle_1m+WHERE+exchange+=+'okx'%29+ORDER+BY+timestamp+DESC+LIMIT+100): the last trades of 18 October 2026, up to 100, with `exchange = 'okx'`, or [the same query as JSON](https://sn7u7x3woz.eu-central-1.aws.clickhouse.cloud:8443/?user=demo\&default_format=JSONEachRow\&query=SELECT%20timestamp%2C%20market%2C%20side%2C%20price%2C%20quantity%20FROM%20api.trade%20WHERE%20exchange%20%3D%20%27okx%27%20AND%20timestamp%20%3E%3D%20%28SELECT%20max%28timestamp%29%20-%20INTERVAL%201%20HOUR%20FROM%20public_data.candle_1m%20WHERE%20exchange%20%3D%20%27okx%27%29%20ORDER%20BY%20timestamp%20DESC%20LIMIT%20100) for an agent.
* [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 does Koinju handle OKX trade-ID restarts?**

When OKX relists a market, its trade IDs start again from 1, so one trade ID can belong to two different trades. The trade-ID sequence check keeps them apart: a missing trade is only added if its timestamp falls inside the gap's time window.

**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 a new listing is subscribed within a minute of appearing in OKX's market list.
