Audit Pagination in a Market-History Download
If you pull historical trades or markets and silently drop pages, every backtest downstream inherits the gap. The general pipeline design lives in our how-to-build-a-prediction-market-data-pipeline post. This one is narrower: a download audit you can run on each paginated pull that records page identity, confirms the first and last observation on every page, checks for overlap between pages, and stops on an explicit condition instead of a guessed one.
Verify the venue's pagination before you assume it
Do not assume offset or page-number semantics. Kalshi's list endpoints are cursor-based. The documented contract is to make the first request without a cursor, read the cursor field from the response, and pass that value as the cursor query parameter on the next request.
Kalshi's pagination guide covers cursor-based pagination for list endpoints including Get Markets, Get Events, and Get Series. The individual endpoint reference pages confirm cursor pagination for Get Trades, Get Historical Trades, Get Historical Fills, and Get Orders as well. Page size is controlled by a limit parameter. The documented behavior differs by endpoint, so read the specific reference page for the endpoint you are calling rather than copying a limit from a different one.
For Get Trades specifically, the official reference states the endpoint returns a paginated response, that limit controls page size up to 1000 (defaulting to 100), and documents two behaviors that matter for an audit: a cursor that cannot be decoded is ignored and the first page is returned, so you must pass the cursor back exactly as received; and min_ts and max_ts must be in seconds, with millisecond values rejected as a 400 bad_request. If you build a stopping condition around a time window, those timestamp units are part of the contract.
The stopping condition is an explicit rule, not a vibe
The documented terminator for Kalshi cursor pagination is the cursor itself. Continue requesting until the cursor comes back null, empty, or missing. The Get Trades reference states plainly that an empty cursor indicates no more pages are available. Encode exactly that, and nothing clever:
cursor = None
pages = 0
MAX_PAGES = 5000 # hard ceiling so a bug can't loop forever
while True:
params = {"limit": 100}
if cursor:
params["cursor"] = cursor
resp = get(url, params=params) # your HTTP call
page = resp.json()
yield page
pages += 1
cursor = page.get("cursor") # Kalshi returns next cursor here
if not cursor: # empty/missing => done, per docs
break
if pages >= MAX_PAGES: # audit guard, not the real end
raise RuntimeError("page ceiling hit: investigate before trusting data")The MAX_PAGES guard is not a substitute for the cursor check. It is a tripwire. If you hit it, a response shape changed or you are re-requesting the same page, and you want a loud failure, not a truncated dataset that looks complete.
Record page identity on every request
Page identity is the metadata that lets you prove, after the fact, which request produced which rows. For each page, log at minimum:
- Page index (monotonic counter starting at 0).
- The cursor you sent (None for page 0) and the cursor you received.
- Row count returned on this page.
- The first and last row's sort key. For Kalshi trades, created_time plus trade_id is a stable identity; log both ends of the page.
- Request timestamp and any time-window params (min_ts, max_ts) you applied.
Storing the sent cursor and the received cursor side by side catches the most common silent failure: a cursor that was not decoded, so the venue returned page 0 again. The Get Trades docs describe exactly that fallback for an undecodable cursor, which is why the sent/received pair is worth recording.
Overlap and gap checks between adjacent pages
Cursor pagination over a time-ordered feed should produce pages that abut without overlapping. With Kalshi trades sorted by created_time, two checks cover most corruption:
- Overlap: the set of trade_id values on page N and page N+1 should be disjoint. Any shared id means a page was re-emitted or the cursor did not advance.
- Monotonicity: the last created_time on page N should order consistently with the first created_time on page N+1 for your sort direction. A reversal signals a cursor that jumped or a sort assumption that is wrong.
Keep these as assertions that halt the pull, not warnings you scroll past. A download that fails loudly on page 40 is cheaper than a backtest you have to retract. Preserve anything you cannot explain rather than smoothing it over; an unexplained gap is a finding, not noise to delete.
Use exact arithmetic for prices and identity keys
Prices and any derived identity keys should not pass through binary float. Python's decimal module is built for this: the standard library documentation notes that decimal numbers can be represented exactly, that the exactness carries over into arithmetic, and gives the example that 0.1 + 0.1 + 0.1 - 0.3 is exactly zero in decimal. That is the property you want when comparing or hashing price fields across pages, so parse price strings straight into Decimal and never round-trip them through float.
Where a paper sandbox fits
An audited download is input. What you do with it is a separate question. Banger is a paper-research beta for prediction-market strategies: you can write a Python strategy or clone one, inspect versioned historical backtests, and run bounded paper experiments. Its hosted historical backtesting currently supports Kalshi binary markets. Public live deployments and local --live are blocked, so nothing here places a real order. Treat a paper result as evidence under a specific data, fill, fee, and valuation model, not proof of real venue execution. A clean pagination audit makes that evidence worth more, because the backtest is reading a dataset you have actually verified is complete.
Checklist
- Confirm cursor (not offset) semantics on the exact endpoint you call, from its reference page.
- Send the first request with no cursor; read the cursor field from each response.
- Log page index, sent cursor, received cursor, row count, and first/last sort keys per page.
- Assert no trade_id overlap and consistent created_time ordering between adjacent pages.
- Stop when the returned cursor is empty or missing; keep a page ceiling as a tripwire only.
- Parse prices into Decimal; keep timestamp units correct (Kalshi trade min_ts/max_ts are seconds).
Primary source for the pagination contract: the Kalshi pagination guide at docs.kalshi.com/getting_started/pagination. Reverify it at the time you build, since endpoint limits and behaviors change. If you want to take an audited Kalshi dataset into bounded paper experiments, you can explore paper research via /sign-up. No fills, returns, or live execution are implied.