Skip to content

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
sjhddh:fix/hardcoded-edt-offset-dst
Open

fix: use DST-aware timezone for US stock price lookups (fixes winter 1-hour offset error)#184
sjhddh wants to merge 1 commit intoHKUDS:mainfrom
sjhddh:fix/hardcoded-edt-offset-dst

Conversation

@sjhddh
Copy link
Copy Markdown

@sjhddh sjhddh commented Apr 12, 2026

Bug

price_fetcher.py hardcodes the US Eastern timezone as UTC-4 (EDT, summer):

ET_OFFSET = timedelta(hours=-4)  # EDT is UTC-4
ET_TZ = timezone(ET_OFFSET)

US Eastern time is actually UTC-5 during winter (EST) and UTC-4 during summer (EDT). The switch happens:

  • EDT → EST: first Sunday of November (~5 months of the year)
  • EST → EDT: second Sunday of March (~7 months of the year)

Impact: _get_us_stock_price() converts executed_at to 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 fixed UTC-5 (EST) offset on Python < 3.9 — conservative but at least correct for the majority of the year.

try:
    from zoneinfo import ZoneInfo
    _ET_ZONEINFO = ZoneInfo("America/New_York")
except ImportError:
    _ET_ZONEINFO = None  # Python < 3.9 fallback

ET_TZ = _ET_ZONEINFO if _ET_ZONEINFO is not None else timezone(timedelta(hours=-5))

No new dependencies required — zoneinfo is in the standard library.

Test plan

  • Trade executed in EST period (e.g. 2025-01-15T14:30:00Z): converts to 09:30 ET (correct, not 10:30 ET)
  • Trade executed in EDT period (e.g. 2025-07-15T14:30:00Z): converts to 10:30 ET (unchanged)
  • DST transition edge case: 2025-03-09T06:30:00Z02:30 ET (just after spring-forward)

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.
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.

1 participant