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

Cancelling Market Depth requests #224

Closed
dkimot opened this issue Feb 25, 2025 · 2 comments · Fixed by #225
Closed

Cancelling Market Depth requests #224

dkimot opened this issue Feb 25, 2025 · 2 comments · Fixed by #225

Comments

@dkimot
Copy link

dkimot commented Feb 25, 2025

Using the MPE below, when calling subscription.cancel() TWS returns "4\02\09000\0300\0Can't find EId with tickerId:9000\0". This is true if cancel is called directly or if the subscription is passed to drop.

use std::time::Duration;
use std::thread::sleep;

use ibapi::Client;

fn main() {
    env_logger::init();

    let connection_url = "127.0.0.1:4001";

    let client = Client::connect(connection_url, 105).expect("connection to TWS failed!");
    println!("Successfully connected to TWS at {connection_url}");

    let contract = ibapi::contracts::Contract::stock("TSLA");
    let subscription = client.market_depth(&contract, 5, true).expect("error requesting market depth");
    if let Some(row) = subscription.try_next() {
        println!("row: {row:?}");
    }

    println!("sleeping...");
    sleep(Duration::from_secs(5));

    if let Some(error) = subscription.error() {
        println!("error: {:?}", error);
    }

    subscription.cancel();
    sleep(Duration::from_secs(1));
}

I'm not sure where things are going wrong and the IBKR docs on this are sparse to say the least.

@dkimot
Copy link
Author

dkimot commented Feb 26, 2025

Figured it out. The impl for MarketDepths calls encoders::encode_cancel_tick_by_tick(request_id). This sends the message ID 98 which is wrong. Changing this to a new method cancel_market_depth(request_id) with the provided implementation works. This isn't a perfect solution since I'm hard coding the final 1 -- I believe this is to denote that I'm requesting a "smart" market depth.

cancel_market_depth implementation:

pub(super) fn encode_cancel_market_depth(request_id: i32) -> Result<RequestMessage, Error> {
    let mut message = RequestMessage::new();

    const VERSION: i32 = 1;

    message.push_field(&OutgoingMessages::CancelMarketDepth);
    message.push_field(&VERSION);
    message.push_field(&request_id);
    message.push_field(&1);

    Ok(message)
}

I validated the expected message by using the python ib_sync library. I don't have access to the TWS api repo so I'm guessing on the message structure.

@wboayue
Copy link
Owner

wboayue commented Feb 26, 2025

Thanks for the detailed writeup. I'll verify the arguments with the TWS api source and fix this in the next patch.

@wboayue wboayue linked a pull request Feb 26, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants