Skip to content

Commit c464f21

Browse files
committed
Fix details.liquidHours() crash if no dates
Apparently sometimes the IBKR API doesn't populate all the date fields, so attempting to parse empty timezone names crashes the date range parsing logic. Easy fix: if no dates available or if timezone isn't populated, just return nothing (instead of crashing) because we can't do anything. Fixes #46
1 parent 20fc09f commit c464f21

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

ib_async/contract.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,22 @@ def liquidSessions(self) -> List[TradingSession]:
582582
return self._parseSessions(self.liquidHours)
583583

584584
def _parseSessions(self, s: str) -> List[TradingSession]:
585+
"""Parse the IBKR session date range text format into native Python objects.
586+
587+
Note: The IBKR date range format looks like:
588+
timeZoneId='US/Eastern',
589+
tradingHours='20240721:CLOSED;20240722:0400-20240722:2000;20240723:0400-20240723:'
590+
'2000;20240724:0400-20240724:2000;20240725:0400-20240725:2000;'
591+
'20240726:0400-20240726:2000',
592+
liquidHours='20240721:CLOSED;20240722:0930-20240722:1600;20240723:0930-20240723:'
593+
'1600;20240724:0930-20240724:1600;20240725:0930-20240725:1600;'
594+
'20240726:0930-20240726:1600',
595+
"""
596+
597+
# if the time values don't exist, we can't parse anything, so return nothing.
598+
if not (s or self.timeZoneId):
599+
return []
600+
585601
tz = util.ZoneInfo(self.timeZoneId)
586602
sessions = []
587603
for sess in s.split(";"):

0 commit comments

Comments
 (0)