IBKR (Interactive Brokers) Client Portal Rust API Client

I am personally implementing a rust-written trading bots for the stocks/options for personal interests. Here I published a package to support the IBKR (Interactive Brokers) unofficial API client in Rust. I'm still working on that (but the main APIs are tested with UT) and any contributions/stars are welcomed.

Also, I implemented a dockerfile (could be found in the GitHub source code) to run the IBKR client portal with only one command (docker-compose).

Would it be possible to comment about how reliable the connection is with the web api, and if you have a mechanism to handle disconnects. In the past the client api would drop the connections more frequently than the TWS api. Thanks!

The major issues are:

  • The endpoints have a ton of repetitive, uninteresting code. Every function in the modules under endpoints/ is essentially the following, repeated dozens of times with very slight variations:

        let response = self
            .client
            .get(self.get_url(path))
            .query(request.some_property())
            .send()
            .await?;
    
        response.error_for_status_ref()?;
        response
            .json()
            .await
            .map_err(reqwest_middleware::Error::from)
    

    This screams for refactoring. Follow the pattern described here.

  • The API is stringly-typed. E.g. URLs are not arbitrary strings; use a real URL type.

  • The new constructor has too many parameters. You should probably use a configuration struct with named fields and a sensible Default impl, and/or the builder pattern instead.

  • The name of the main entry type, IBClientPortal, is non-idiomatic. Rust has real namespaces, there's no need for prefixes.

  • Having a separate test module inside src is unconventional. You can have inline unit tests in each module, or larger-scale integration test in tests/ under the crate root. Similarly, having mock data (assets/get_market_data.json) under src feels dirty as well.

1 Like

Good point, I haven't noticed that given my trading frequency is low (maybe just 1-2 times per day).

To handle disconnects, I just doing the exponential retry and looks working normal now. Do we have any existing mechanisms to monitor the connection dropping rates? I'll deep dive this issue then

thanks for your code review and I also learned a lot from that. i'm a part-time Rust fans now and will address those comments when I get my free time :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.