fix: use DST-aware timezone for US stock price lookups (fixes winter 1-hour offset error)#184
Open
sjhddh wants to merge 1 commit intoHKUDS:mainfrom
Open
fix: use DST-aware timezone for US stock price lookups (fixes winter 1-hour offset error)#184sjhddh wants to merge 1 commit intoHKUDS:mainfrom
sjhddh wants to merge 1 commit intoHKUDS:mainfrom
Conversation
ET_TZ was hardcoded to UTC-4 (EDT / summer offset). This is wrong during
Eastern Standard Time (November through mid-March, UTC-5), causing all
historical stock price lookups to compare timestamps with a 1-hour error.
The practical effect: `_get_us_stock_price()` looks up Alpha Vantage candles
by converting `executed_at` to Eastern time, then matching against the API's
ET-labelled timestamps. A 1-hour error shifts that comparison into the wrong
candle bucket — the "closest previous" fallback returns a price from one hour
earlier than the actual execution time.
Fix: import `zoneinfo.ZoneInfo("America/New_York")` (stdlib since Python 3.9)
for proper DST-aware conversion. Fall back to the fixed UTC-5 EST offset on
older Pythons — conservative but at least correct for the majority of the year.
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.
Bug
price_fetcher.pyhardcodes the US Eastern timezone asUTC-4(EDT, summer):US Eastern time is actually UTC-5 during winter (EST) and UTC-4 during summer (EDT). The switch happens:
Impact:
_get_us_stock_price()convertsexecuted_atto Eastern time, then compares against Alpha Vantage's ET-labelled candle timestamps. During EST months (November – mid-March), every timestamp comparison is off by 1 hour — the function either misses an exact match or picks up the "closest previous" price from the wrong candle, silently returning an incorrect fill price for all US stock trades.For an AI trading system that back-tests or records real trades, this introduces a systematic 1-hour price error for nearly half the calendar year.
Fix
Use
zoneinfo.ZoneInfo("America/New_York")from the Python 3.9+ stdlib for proper DST-aware conversion. Fall back to a fixedUTC-5(EST) offset on Python < 3.9 — conservative but at least correct for the majority of the year.No new dependencies required —
zoneinfois in the standard library.Test plan
2025-01-15T14:30:00Z): converts to09:30 ET(correct, not10:30 ET)2025-07-15T14:30:00Z): converts to10:30 ET(unchanged)2025-03-09T06:30:00Z→02:30 ET(just after spring-forward)