nautechsystems / nautilus_trader

A high-performance algorithmic trading platform and event-driven backtester
https://nautilustrader.io
GNU Lesser General Public License v3.0
2.19k stars 500 forks source link

Refactor the Interactive Brokers Client #1397

Closed benjaminsingleton closed 9 months ago

benjaminsingleton commented 11 months ago

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 the InteractiveBrokersClient 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:

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.

cjdsellers commented 11 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!