Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve IB client's lifecycle methods #1534

Closed
benjaminsingleton opened this issue Mar 10, 2024 · 2 comments
Closed

Improve IB client's lifecycle methods #1534

benjaminsingleton opened this issue Mar 10, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@benjaminsingleton
Copy link
Collaborator

benjaminsingleton commented Mar 10, 2024

Feature Request

While troubleshooting #1533, it became evident that the code handling the InteractiveBrokersClient component lifecycle (_start, _stop, _reset, _degrade) could use some refactoring given how integrated it is with connectivity events.

Here are some observations:

  • In the client __init__, the _reset component lifecycle method is called. Starting an event loop or running an asyncio task from the constructor is generally considered an anti-pattern.
  • When the component is reseting to start up, it starts the watch dog task which checks for connectivity (which obviously isn't yet established) then initiates a reconnect. Once the reconnect establishes a successful first connection, the watchdog then calls the _start method, which indicates the client is ready. This sequence of events works, but it's not especially intuitive. It would make more sense in the _start method to (1) establish a connection using the _connect method, and then (2) start the three tasks: watchdog, TWS incoming message reader, and internal message queue processor.
  • The _reset method should call _stop then _start, which intuitively is what resetting does.
  • The _resume method should call the method to resubscribe to all subscriptions.
  • The watchdog should be simplified to simply monitor for connectivity issues and trigger reconnects. That said, in reality, connectivity issues tend to be triggered by the TWS incoming message reader since that runs in a continuous loop while the watchdog only executes every second.
  • In live / paper trading, you may want to indefinitely attempt to reconnect rather than stop after N attempts.
  • Nits: some variables and methods could be named more appropriately. E.g. _is_ib_ready could be renamed to _is_ib_connected since that's really what the event flag indciates. _watch_dog_task could be renamed to _connection_watchdog_task (watchdog generally doesn't have a space or underscore).
@cjdsellers
Copy link
Member

@benjaminsingleton @rsmb7z

Where did we manage to get to with these points so far - can we close the ticket or are there some outstanding items?

@rsmb7z
Copy link
Collaborator

rsmb7z commented May 29, 2024

@cjdsellers I believe these updates have been merged in #1532 and can now be closed. I will have the opportunity to fully test them and write related tests using the simulated mock that was added last week in a follow-up.

@rsmb7z rsmb7z closed this as completed May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

No branches or pull requests

3 participants