Speed up multi-symbol PricesYahoo instantiation - #568
Closed
maread99 wants to merge 1 commit into
Closed
Conversation
`composite_schedule` reduced open/close across calendars by building a DataFrame with one column per session and reducing over those columns. That is O(sessions) columns and dominates instantiation when calendars span a long period (e.g. when a symbol has an early first-trade date, which drags the daily limit, and hence the calendars, far back). Reduce across calendars (axis=1) instead, which is identical in result and orders of magnitude faster. Also evaluate `CompositeCalendar.first_session` / `last_session` directly from the underlying calendars so that querying the composite bounds (as happens during instantiation) no longer forces construction of the full composite schedule. Finally, fall back to a calendar's earliest supported start in `_set_calendars` when the daily limit predates the calendar's `bound_min`, so that combining an old symbol with a symbol on a younger-bounded calendar (e.g. XTKS) no longer raises on instantiation. https://claude.ai/code/session_018CDCHDRV5ikwiB6SSHYEcP
Owner
Author
|
Claude completely missed the real problem (addressed in #569). What was offered here seems something of a nonsense. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Instantiation of
mp.PricesYahoo(tickers)becomes very slow with a high number of tickers, with the performance impairment possibly growing disproportionately with ticker count, and possibly some symbols having a greater affect that others.Investigation
Profiling
PricesYahoo.__init__with the yahooquery network calls mocked out isolated the cost to calendar handling rather than networking:PricesYahoo._set_daily_bi_limitsets the daily base limit to the global earliest first-trade date across all symbols. A single old symbol (e.g. KO, first trade 1962) drags this limit — and therefore every calendar built inPricesBase._set_calendars— back ~60 years.composite_scheduleinutils/calendar_utils.py. It reduced open/close times across calendars by constructingpd.DataFrame([sch["open"] for sch in schedules]).min(), which builds a frame with one column per session and reduces over those (tens of thousands of) columns. Measured in isolation: 3.22s vs 0.006s for the equivalent per-row reduction — a ~500× difference, identical result.CompositeCalendar.first_session/last_sessionreadself.sessions[0]/[-1], forcing construction of the entire composite schedule just to obtain the bounds (which__init__queries).XTKS,bound_min1997) made instantiation raise, because the global daily limit was passed asstartto a calendar that cannot be evaluated that far back.Changes
composite_schedule— reduce open/close across calendars withpd.concat(..., axis=1).min(axis=1)/.max(axis=1)instead of the transposed per-session-column reduction. Verified to produce identical schedules (full-DataFrame comparison across several calendar combinations, includingXTKS/XKRX).CompositeCalendar.first_session/last_session— evaluate directly asmax/minof the underlying calendars' bounds, avoiding a full composite-schedule build when only the bounds are needed.PricesBase._set_calendars— when the requestedstart(driven by the daily limit) predates a calendar'sbound_min, fall back to the calendar's earliest supported start instead of raising. The pre-existingCalendarTooShortWarningalready flags any shortfall against the limit.Measured impact
Instantiation time (yahooquery mocked, fresh process, sample mixing old US/EU symbols with a Tokyo symbol):
ValueErroronXTKS)All changes are behaviour-preserving for the schedule/bounds; the third change only affects cases that previously raised.
Test plan
pytest tests/test_calendar_utils.py(full suite needs network for answer-CSV fixtures; run in CI)pytest tests/test_yahoo.py tests/test_base.pycomposite_scheduleandCompositeCalendarbounds match the original implementation across multiple calendar combinationsruff check/ruff format --checkpass on changed fileshttps://claude.ai/code/session_018CDCHDRV5ikwiB6SSHYEcP
Generated by Claude Code