Closed benjaminsingleton closed 9 months ago
Hey @benjaminsingleton ,
Thanks for the write up on your thoughts. I definitely agree that breaking up the large client class into separate components makes sense and will lead to more maintainable code.
Great work so far!
Feature Request
The
InteractiveBrokersClient
serves as the central component of the Interactive Brokers adapter, overseeing a range of critical functions. These include establishing and maintaining connections, handling API errors, executing trades, and gathering various types of data such as market, contract/instrument, and account details. Given its important role and many functions, the Client needs to be particularly well-designed and documented, so that it can be easily understood by new users and maintainers.As I’ve begun to design strategies in Nautilus Trader, I’ve found that I’ve spent considerable time trying to understand and troubleshoot the behavior of the Client. This is in large part due to the design of IB’s
ibapi
package and confusing elements of the TWS API. That said, there are opportunities to continue to build on the great work of (primarily) @rsmb7z and @limx0 to make theInteractiveBrokersClient
even better.The following are some observations / areas I think can be improved:
I propose refactoring the
InteractiveBrokersClient
to address these issues / opportunities. The most significant change I propose is splitting the Client class into 7 classes (1 Client + 6 subcomponents), following the principle of composition. Each class should handle a subset of functionality:InteractiveBrokersClient
: This class interfaces with the Interactive Brokers TWS or Gateway and integrates various managers and handlers to provide functionality for connection management, account management, market / instrument data, and order processing.InteractiveBrokersConnectionManager
: This class is dedicated to managing the connection with TWS/Gateway.InteractiveBrokersErrorHandler
: It focuses on addressing all encountered errors and warnings.InteractiveBrokersAccountManager
: Responsible for handling requests related to account information and positions.InteractiveBrokersContractManager
: Handles retrieving contracts (instruments) dataInteractiveBrokersMarketDataManager
: Handles market data requests, subscriptions and data processingInteractiveBrokersOrderManager
: Oversees all aspects of order placement and management.Refactoring the Client will hopefully enable the NT IB adapter maintainers to more easily address issues and add new features. And it should also enable new NT users to more easily investigate issues on their own.