Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Issue #303: Bypass regression in TWS 480.4l+ that no longer sends acc…
Browse files Browse the repository at this point in the history
…ountDownloadEnd for multi-accounts.
  • Loading branch information
erdewit committed Oct 25, 2020
1 parent 97cc8b9 commit 983177f
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions ib_insync/ib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1621,7 +1621,7 @@ async def connectAsync(
account: str = ''):

if self.isConnected():
self._logger.warn('Already connected')
self._logger.warning('Already connected')
return self
self.wrapper.clientId = clientId

Expand All @@ -1634,32 +1634,31 @@ async def connectAsync(
if clientId == 0:
self.reqAutoOpenOrders(True)

# request completed orders
if not readonly and self.client.serverVersion() >= 150:
try:
await asyncio.wait_for(
self.reqCompletedOrdersAsync(False), timeout or None)
except asyncio.TimeoutError:
self._logger.error('reqCompletedOrders timed out')

# request updates for the main account
accounts = self.client.getAccounts()
await asyncio.wait_for(
asyncio.gather(
self.reqAccountUpdatesAsync(account or accounts[0]),
self.reqPositionsAsync(),
self.reqExecutionsAsync()),
timeout or None)

# request updates for sub-accounts, if there are not too many
if not account and len(accounts) == 1:
account = accounts[0]

# prepare initializing requests
reqs = {} # name -> request
reqs['positions'] = self.reqPositionsAsync()
reqs['executions'] = self.reqExecutionsAsync()
if not readonly and self.client.serverVersion() >= 150:
reqs['completed orders'] = self.reqCompletedOrdersAsync(False)
if account:
reqs['account updates'] =self.reqAccountUpdatesAsync(account)
if len(accounts) <= self.MaxSyncedSubAccounts:
await asyncio.wait_for(
asyncio.gather(
*(self.reqAccountUpdatesMultiAsync(a)
for a in accounts)),
timeout or None)
else:
self._logger.warning('Not requesting sub-account updates')
for acc in accounts:
reqs[f'account updates for {acc}'] = \
self.reqAccountUpdatesMultiAsync(acc)

# run initializing requests concurrently and log if any times out
tasks = [
asyncio.wait_for(req, timeout or None)
for req in reqs.values()]
resps = await asyncio.gather(*tasks, return_exceptions=True)
for name, resp in zip(reqs, resps):
if isinstance(resp, asyncio.TimeoutError):
self._logger.error(f'{name} request timed out')

self._logger.info('Synchronization complete')
self.connectedEvent.emit()
Expand Down

0 comments on commit 983177f

Please sign in to comment.