Skip to content
All library documents

Paginating Exchange K-Line Queries to Retrieve Long Histories

Article FMZ digest · Author: 发明者量化-小小梦

Summary

This tutorial explains how to retrieve a specified number of historical bars when an exchange API limits the amount returned by a single request. Its example targets Binance futures: map supported bar durations to exchange intervals, request successive time ranges while stepping backward from the present, and prepend returned bars until the requested history is collected or data runs out. A companion update routine fetches recent bars and appends new ones or replaces the latest bar when it is still forming.

The article tests the resulting series by plotting it and checking for duplicate timestamps and equal spacing between adjacent bars. It also compares an intentionally oversized daily-bar request with the exchange chart, which returns fewer records because older history is unavailable. The implementation supports only the described Binance futures markets and interval mapping, and the article notes that other exchanges would need their own adapters. Its sample checks are useful diagnostics, though they do not prove that every API response, boundary, or update condition is handled correctly.

Key ideas

  • Pagination can overcome per-request limits when an exchange supports historical time ranges.
  • Query range size can be calculated from the bar duration and requested number of records.
  • An update routine should append newly closed bars and replace a bar that is still changing.
  • Plotting, duplicate-timestamp checks, and interval continuity checks help inspect retrieved histories.
  • Exchange-specific limits and available history constrain the amount and type of data returned.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.