ib: only raise first quote timeout err after tries

Previously we were actually failing silently too fast instead of
actually trying multiple times (now we do for 100) before finally
raising any timeout in the final loop `else:` block.
distribute_dis
Tyler Goodlet 2023-12-18 11:45:19 -05:00
parent 1f9a497637
commit d5d68f75ea
2 changed files with 26 additions and 13 deletions

View File

@ -843,6 +843,7 @@ class Client:
self, self,
contract: Contract, contract: Contract,
timeout: float = 1, timeout: float = 1,
tries: int = 100,
raise_on_timeout: bool = False, raise_on_timeout: bool = False,
) -> Ticker | None: ) -> Ticker | None:
@ -857,30 +858,30 @@ class Client:
ready: ticker.TickerUpdateEvent = ticker.updateEvent ready: ticker.TickerUpdateEvent = ticker.updateEvent
# ensure a last price gets filled in before we deliver quote # ensure a last price gets filled in before we deliver quote
timeouterr: Exception | None = None
warnset: bool = False warnset: bool = False
for _ in range(100): for _ in range(tries):
if isnan(ticker.last):
# wait for a first update(Event) # wait for a first update(Event) indicatingn a
# live quote feed.
if isnan(ticker.last):
try: try:
tkr = await asyncio.wait_for( tkr = await asyncio.wait_for(
ready, ready,
timeout=timeout, timeout=timeout,
) )
except TimeoutError:
import pdbp
pdbp.set_trace()
if raise_on_timeout:
raise
return None
if tkr: if tkr:
break break
except TimeoutError as err:
timeouterr = err
await asyncio.sleep(0.01)
continue
else: else:
if not warnset: if not warnset:
log.warning( log.warning(
f'Quote for {contract} timed out: market is closed?' f'Quote req timed out..maybe venue is closed?\n'
f'{asdict(contract)}'
) )
warnset = True warnset = True
@ -888,6 +889,11 @@ class Client:
log.info(f'Got first quote for {contract}') log.info(f'Got first quote for {contract}')
break break
else: else:
if timeouterr and raise_on_timeout:
import pdbp
pdbp.set_trace()
raise timeouterr
if not warnset: if not warnset:
log.warning( log.warning(
f'Contract {contract} is not returning a quote ' f'Contract {contract} is not returning a quote '
@ -895,6 +901,8 @@ class Client:
) )
warnset = True warnset = True
return None
return ticker return ticker
# async to be consistent for the client proxy, and cuz why not. # async to be consistent for the client proxy, and cuz why not.

View File

@ -943,6 +943,11 @@ async def stream_quotes(
contract=con, contract=con,
raise_on_timeout=True, raise_on_timeout=True,
) )
first_quote: dict = normalize(first_ticker)
log.info(
'Rxed init quote:\n'
f'{pformat(first_quote)}'
)
cs: trio.CancelScope | None = None cs: trio.CancelScope | None = None
startup: bool = True startup: bool = True
while ( while (