Paginating OKX Candlestick Data to Retrieve Historical Bars
Summary
This document demonstrates how to retrieve historical candlesticks from OKX’s V5 market endpoint when each request is limited to 100 bars. Its example repeatedly requests pages, uses the timestamp of the most recently processed record as the cursor for the next request, and stops after collecting 1,440 records or receiving an empty response. It converts the returned fields into timestamped OHLCV records and reverses the collected list so that bars are in chronological order.
The example uses ETH-USDT perpetual swap hourly candles and checks adjacent timestamps for hourly gaps, then logs and plots the records. It is an implementation illustration rather than a trading strategy or performance study. The code’s fixed retrieval target and interval check are tailored to its example, and it does not discuss resilience beyond a basic exception handler, duplicate or missing records, rate limits, or verification of API response semantics. The embedded source also includes a placeholder cookie value, so it should not be treated as a complete production-ready client.
Key ideas
- The example pages through candle requests capped at 100 records per call.
- It uses a candle timestamp as a cursor and collects up to 1,440 bars.
- Returned OHLCV records are reversed to place them in chronological order.
- The example checks whether neighboring hourly bars are separated by the expected interval.
- The code illustrates data retrieval and validation, not a trading signal or backtest.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.