ib_async
ib_async copied to clipboard
util.py parseIBDatetime fails to parse some date strings from IB
https://github.com/ib-api-reloaded/ib_async/blob/2afdae44a47f067bf3d1a23a4840234f2906ef64/ib_async/util.py#L578
Error handling fields: ['17', '6', '20250714 08:59:54 US/Eastern', '20250715 08:59:54 US/Eastern', '13', '0714 09:30:00 US/Eastern', '209.93', '210.91', '207.67', '207.89', '5583527', '208.961', '28611', '0714 10:00:00 US/Eastern', '207.90', '208.78', '207.54', '208.03', '2978782', '208.082', '16926', '0714 10:30:00 US/Eastern', '208.04', '208.76', '207.82', '208.49', '1717073', '208.324', '8826', '0714 11:00:00 US/Eastern', '208.48', '208.90', '208.20', '208.77', '1462992', '208.554', '8347', '0714 11:30:00 US/Eastern', '208.77', '209.15', '208.56', '208.84', '1732084', '208.819', '8034', '0714 12:00:00 US/Eastern', '208.83', '209.18', '208.68', '208.84', '989992', '208.945', '5748', '0714 12:30:00 US/Eastern', '208.85', '209.25', '208.79', '208.90', '857673', '209.008', '5302', '0714 13:00:00 US/Eastern', '208.89', '209.33', '208.76', '209.00', '1064395', '209.044', '5872', '0714 13:30:00 US/Eastern', '209.01', '209.30', '209.01', '209.16', '887083', '209.153', '4847', '0714 14:00:00 US/Eastern', '209.16', '209.45', '208.96', '209.00', '952379', '209.177', '5626', '0714 14:30:00 US/Eastern', '209.00', '209.41', '208.99', '209.31', '876578', '209.216', '5342', '0714 15:00:00 US/Eastern', '209.31', '209.36', '208.87', '209.20', '1574582', '209.146', '9532', '0714 15:30:00 US/Eastern', '209.17', '209.20', '208.52', '208.55', '3254491', '208.752', '18704']
Traceback (most recent call last):
File "C:\Users\...\Lib\site-packages\ib_async\decoder.py", line 175, in interpret
handler(fields)
File "C:\Users\...\Lib\site-packages\ib_async\decoder.py", line 486, in historicalData
self.wrapper.historicalData(int(reqId), bar)
File "C:\Users\...\Lib\site-packages\ib_async\wrapper.py", line 732, in historicalData
bar.date = parseIBDatetime(bar.date) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\...\Lib\site-packages\ib_async\util.py", line 570, in parseIBDatetime
t = dt.datetime.strptime(s0 + s1, "%Y%m%d%H:%M:%S")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\...\Lib\_strptime.py", line 653, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\...\Lib\_strptime.py", line 432, in _strptime
raise ValueError("time data %r does not match format %r" %
ValueError: time data '071409:30:00' does not match format '%Y%m%d%H:%M:%S'
reqHistoricalData: Timeout for Contract(secType='STK', conId=265598, symbol='AAPL', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='AAPL', tradingClass='NMS')
Error 366, reqId 6: No historical data query found for ticker id:6, contract: Contract(secType='STK', conId=265598, symbol='AAPL', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='AAPL', tradingClass='NMS')
The parseIBDatetime code is expecting a string like '20250715 08:59:54 US/Eastern' but we can see here that IB sometimes returns a shortened version like '0714 09:30:00 US/Eastern' and ib_async can't parse it.
This happened when requesting historical 30-minute bars for AAPL using formatDate=3. Something like this:
ib.reqHistoricalData(
contract=...,
durationStr='1 D',
barSizeSetting='30 mins',
whatToShow='TRADES',
useRTH=True,
formatDate=3,
)
I verified that the data is like this in the IB Gateway API logs too.
IB TWS API documentation also mention this short format when formatDate is 3: https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#hist-format-date