Skip to content

fix: add end_str to aggregate_trade_iter to bound iteration - #1695

Open
tiyaagarwal wants to merge 1 commit into
sammchardy:masterfrom
tiyaagarwal:fix/aggregate-trade-iter-end-str
Open

fix: add end_str to aggregate_trade_iter to bound iteration#1695
tiyaagarwal wants to merge 1 commit into
sammchardy:masterfrom
tiyaagarwal:fix/aggregate-trade-iter-end-str

Conversation

@tiyaagarwal

Copy link
Copy Markdown

Summary

Fixes #497. aggregate_trade_iter had no way to stop before "now" — pulling
trades over a long historical range would keep paginating forever and run
the caller out of memory, exactly as reported.

Adds an optional end_str parameter, following the same convention already
used by get_historical_klines_generator (start_str/end_str, parsed via
convert_ts_str). The iterator now stops once it has yielded the last trade
at or before end_str. Backward compatible: omitting end_str preserves
the existing unbounded behavior exactly.

Client.aggregate_trade_iter and AsyncClient.aggregate_trade_iter share
this logic (the async version reuses the sync docstring), so both are fixed.

Test plan

  • Added unit tests covering: stopping mid-window when using start_str,
    spanning multiple fromId pages before hitting end_str, and a
    regression test confirming behavior without end_str is unchanged.
    All run fully offline (mocked get_aggregate_trades, Client(..., ping=False) to avoid the constructor's network ping).
  • ruff check / ruff format clean on the changed lines.
  • pytest tests/test_client.py tests/test_async_client.py -k aggregate_trade_iter — 4 passed.

Disclosure

I used Claude (Anthropic) to help implement and test this fix; I reviewed
and understand the change. Happy to adjust based on maintainer feedback.

aggregate_trade_iter has no way to stop before "now", so collecting
trades over a long historical range runs out of memory (sammchardy#497). Adds
an optional end_str parameter, following the same convention as
get_historical_klines_generator's start_str/end_str pair: the iterator
now stops once it has yielded the last trade at or before end_str.
Fixed in both Client and AsyncClient, which share this logic.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing 'end_str' from aggregate_trade_iter( )

1 participant