Add `is_expired()` and harden `.ib.venues` helpers
Add an expiry-date predicate and guard venue session
lookups against expired contracts and empty session
lists in `.ib.venues`; use in `api.py` to skip gap
detection for expired tracts.
Deats,
- add `is_expired()` predicate using
`pendulum.parse()` on `realExpirationDate`.
- `sesh_times()`: raise `ValueError` if contract is
expired or has no session intervals (instead of
`StopIteration` from `next(iter(...))`).
- `is_venue_closure()`: handle `None` return from
`sesh_times()` with guard + `breakpoint()`.
Also in `api.py`,
- import and call `is_expired()` from `.venues`.
- gate gap-detection on `not _is_expired`.
- default `timeZoneId` to `'EST'` when IB returns
empty/`None`.
(this commit msg was generated in some part by [`claude-code`][claude-code-gh])
[claude-code-gh]: https://github.com/anthropics/claude-code
(cherry picked from commit de82df727d)
wkt/land_ib_venues
parent
a031a9fa8b
commit
a77967e832
|
|
@ -99,6 +99,7 @@ from .symbols import (
|
|||
)
|
||||
from ...log import get_logger
|
||||
from .venues import (
|
||||
is_expired,
|
||||
is_venue_open,
|
||||
sesh_times,
|
||||
is_venue_closure,
|
||||
|
|
@ -671,7 +672,7 @@ class Client:
|
|||
await self.ib.reqContractDetailsAsync(contract)
|
||||
)[0]
|
||||
# convert to makt-native tz
|
||||
tz: str = details.timeZoneId
|
||||
tz: str = details.timeZoneId or 'EST'
|
||||
end_dt = end_dt.in_tz(tz)
|
||||
first_dt: DateTime = from_timestamp(first).in_tz(tz)
|
||||
last_dt: DateTime = from_timestamp(last).in_tz(tz)
|
||||
|
|
@ -683,10 +684,18 @@ class Client:
|
|||
_open_now: bool = is_venue_open(
|
||||
con_deats=details,
|
||||
)
|
||||
_is_expired: bool = is_expired(
|
||||
con_deats=details,
|
||||
)
|
||||
|
||||
# XXX, do gap detections.
|
||||
has_closure_gap: bool = False
|
||||
if (
|
||||
# XXX, expired tracts can't be introspected
|
||||
# for open/closure intervals due to ib's chitty
|
||||
# details seemingly..
|
||||
not _is_expired
|
||||
and
|
||||
last_dt.add(seconds=sample_period_s)
|
||||
<
|
||||
end_dt
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ from zoneinfo import ZoneInfo
|
|||
|
||||
import exchange_calendars as xcals
|
||||
from pendulum import (
|
||||
parse,
|
||||
now,
|
||||
Duration,
|
||||
Interval,
|
||||
|
|
@ -126,6 +127,17 @@ def normalize_schedule(
|
|||
)
|
||||
|
||||
|
||||
def is_expired(
|
||||
con_deats: ContractDetails,
|
||||
) -> bool:
|
||||
'''
|
||||
Predicate
|
||||
|
||||
'''
|
||||
expiry_dt: datetime = parse(con_deats.realExpirationDate)
|
||||
return expiry_dt.date() >= now().date()
|
||||
|
||||
|
||||
def has_weekend(
|
||||
period: Interval,
|
||||
) -> bool:
|
||||
|
|
@ -240,7 +252,22 @@ def sesh_times(
|
|||
get the (day-agnostic) times for the start/end.
|
||||
|
||||
'''
|
||||
earliest_sesh: Interval = next(iter_sessions(con_deats))
|
||||
# ?TODO, lookup the next front contract instead?
|
||||
if is_expired(con_deats):
|
||||
raise ValueError(
|
||||
f'Contract is already expired!\n'
|
||||
f'Choose an active alt contract instead.\n'
|
||||
f'con_deats: {con_deats!r}\n'
|
||||
)
|
||||
|
||||
maybe_sessions: list[Interval] = list(iter_sessions(con_deats))
|
||||
if not maybe_sessions:
|
||||
raise ValueError(
|
||||
f'Contract has no trading-session info?\n'
|
||||
f'con_deats: {con_deats!r}\n'
|
||||
)
|
||||
|
||||
earliest_sesh: Interval = maybe_sessions[0]
|
||||
return (
|
||||
earliest_sesh.start.time(),
|
||||
earliest_sesh.end.time(),
|
||||
|
|
@ -281,7 +308,13 @@ def is_venue_closure(
|
|||
'''
|
||||
open: Time
|
||||
close: Time
|
||||
open, close = sesh_times(con_deats)
|
||||
maybe_oc: tuple|None = sesh_times(con_deats)
|
||||
if maybe_oc is None:
|
||||
# XXX, should never get here.
|
||||
breakpoint()
|
||||
return False
|
||||
|
||||
open, close = maybe_oc
|
||||
|
||||
# ensure times are in mkt-native timezone
|
||||
tz: str = con_deats.timeZoneId
|
||||
|
|
|
|||
Loading…
Reference in New Issue