diff --git a/.supply-chain/audits.toml b/.supply-chain/audits.toml index e56b5eccb4e7..266b6d25c94b 100644 --- a/.supply-chain/audits.toml +++ b/.supply-chain/audits.toml @@ -2707,6 +2707,12 @@ criteria = "safe-to-deploy" delta = "0.7.0 -> 0.7.1" notes = "Adds a serde alias for Arbitrum Nitro's miscased L1BlockNumber receipt field plus regression fixtures. No new dependencies, unsafe, build script, filesystem, network, process, credential, or data-exfiltration behavior." +[[audits.ibapi]] +who = "Faysal Aberkane " +criteria = "safe-to-deploy" +version = "5.0.0" +notes = "Recorded at the PR author's direction, who states they vetted this release. Temporary acceptance so CI can run inside the 3 day cooldown; remove the cooldown allow entry once 5.0.0 is 3 days old." + [[audits.icu_collections]] who = "Nautech Systems " criteria = "safe-to-deploy" diff --git a/.supply-chain/config.toml b/.supply-chain/config.toml index 06f60116ae96..635b86afb599 100644 --- a/.supply-chain/config.toml +++ b/.supply-chain/config.toml @@ -1411,7 +1411,7 @@ version = "0.1.65" criteria = "safe-to-deploy" [[exemptions.ibapi]] -version = "3.3.0" +version = "5.0.0" criteria = "safe-to-deploy" [[exemptions.icu_collections]] diff --git a/.supply-chain/crate-dates.json b/.supply-chain/crate-dates.json index b72ce7de3ed7..0f8228281ff6 100644 --- a/.supply-chain/crate-dates.json +++ b/.supply-chain/crate-dates.json @@ -1520,9 +1520,9 @@ "published": "2026-01-28T08:40:52.528531Z", "verified_at": "2026-09-13T06:09:27Z" }, - "ibapi@3.3.0": { - "published": "2026-07-17T03:34:20.383654Z", - "verified_at": "2026-09-13T06:09:27Z" + "ibapi@5.0.0": { + "published": "2026-10-04T23:16:47.621967Z", + "verified_at": "2026-10-05T06:55:41Z" }, "icu_collections@2.3.0": { "published": "2026-08-13T23:28:40.279341Z", diff --git a/Cargo.lock b/Cargo.lock index d2b60ae4b34b..3308301028b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2113,7 +2113,7 @@ version = "3.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "faf9468729b8cbcea668e36183cb69d317348c2e08e994829fb56ebfdfbaac34" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -3752,7 +3752,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -4600,9 +4600,9 @@ dependencies = [ [[package]] name = "ibapi" -version = "3.3.0" +version = "5.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc5651f11bacdf138a1a910dc075c21e425912ff767e7f3e237b9fe14d9f3f76" +checksum = "ea07cb0b82d0cfa1388cdcbcd15803a29a7b41d7cf572b46a94eae6404acca33" dependencies = [ "async-trait", "byteorder", @@ -4821,7 +4821,7 @@ checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" dependencies = [ "hermit-abi", "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -7771,7 +7771,7 @@ version = "0.29.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4688ddedf473e32662b9b067670129a8afb8c18e351482c70d62ba4a88171e8b" dependencies = [ - "hashbrown 0.15.5", + "hashbrown 0.17.1", "indexmap 2.14.2", "inventory", "jiff", @@ -7909,7 +7909,7 @@ dependencies = [ "quinn-udp", "rustc-hash 2.1.3", "rustls", - "socket2 0.5.10", + "socket2 0.6.5", "thiserror 2.0.21", "tokio", "tracing", @@ -7948,9 +7948,9 @@ dependencies = [ "cfg_aliases", "libc", "once_cell", - "socket2 0.5.10", + "socket2 0.6.5", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -8548,7 +8548,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -8607,7 +8607,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -9717,10 +9717,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.3.4", + "getrandom 0.4.3", "once_cell", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -10781,7 +10781,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 9e20648828e3..de2bdbc15fc7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -342,7 +342,7 @@ dydx-proto = { version = "0.4.0", default-features = false } fallible-streaming-iterator = "0.1.9" flate2 = "1.1.10" hypersync-client = { version = "1.4.1" } -ibapi = "=3.3.0" +ibapi = "=5.0.0" ryu = "1.0.23" serde_repr = "0.1.21" time = { version = "0.3.55", default-features = false, features = [ @@ -417,6 +417,9 @@ store = { path = '.supply-chain' } days = 3 trusted-base = "origin/develop" +[workspace.metadata.cooldown.allow] +ibapi = "5.0.0" + # ----------------------------------------------------------------------------- # Profiles # ----------------------------------------------------------------------------- diff --git a/crates/adapters/interactive_brokers/Cargo.toml b/crates/adapters/interactive_brokers/Cargo.toml index 3864f8705c16..9b2defa8b1eb 100644 --- a/crates/adapters/interactive_brokers/Cargo.toml +++ b/crates/adapters/interactive_brokers/Cargo.toml @@ -104,6 +104,7 @@ nautilus-trading = { workspace = true } rstest = { workspace = true } rust_decimal_macros = { workspace = true } tempfile = { workspace = true } +tokio = { workspace = true, features = ["test-util"] } # Run live smoke with `cargo run --example ib-data-tester --package nautilus-interactive-brokers --features examples`. # Run embedded config unit tests with `cargo test --example ib-data-tester --package nautilus-interactive-brokers --features examples`. @@ -113,7 +114,8 @@ path = "examples/node_data_tester.rs" required-features = ["examples"] doc = false -# Run live smoke with `cargo run --example ib-exec-tester --package nautilus-interactive-brokers --features examples`. +# Build offline with `cargo run --example ib-exec-tester --package nautilus-interactive-brokers --features examples`. +# Set `NAUTILUS_IB_RUN=1` to connect and `NAUTILUS_IB_LIVE_ORDERS=1` to submit orders. # Run embedded config unit tests with `cargo test --example ib-exec-tester --package nautilus-interactive-brokers --features examples`. [[example]] name = "ib-exec-tester" diff --git a/crates/adapters/interactive_brokers/examples/contracts/active_future.rs b/crates/adapters/interactive_brokers/examples/contracts/active_future.rs new file mode 100644 index 000000000000..db9d719a86e3 --- /dev/null +++ b/crates/adapters/interactive_brokers/examples/contracts/active_future.rs @@ -0,0 +1,101 @@ +// ------------------------------------------------------------------------------------------------- +// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. +// https://nautechsystems.io +// +// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------------------------------- + +//! Resolves the live quarterly futures contract the testers trade, so they keep working after +//! a contract expires. Mirrors `active_quarterly_contract` in +//! `examples/live/interactive_brokers/_common.py`. + +use jiff::{ + Timestamp, ToSpan, + civil::{Date, Weekday}, + tz::TimeZone, +}; +use nautilus_interactive_brokers::common::futures::{format_futures_symbol, futures_month_code}; +use nautilus_model::identifiers::InstrumentId; + +const QUARTERLY_CONTRACT_MONTHS: [i8; 4] = [3, 6, 9, 12]; +// Roll to the next contract this many days before expiry, so a run never lands in expiry week. +const MIN_DAYS_TO_EXPIRY: i64 = 45; + +pub(crate) fn es_future_instrument_id() -> InstrumentId { + let today = Timestamp::now().to_zoned(TimeZone::UTC).date(); + active_quarterly_contract("ES", "XCME", today) +} + +/// Returns the first quarterly contract whose third-Friday expiry is at least +/// `MIN_DAYS_TO_EXPIRY` days after `today`. +pub(crate) fn active_quarterly_contract(root: &str, venue: &str, today: Date) -> InstrumentId { + let target_expiry = today + .checked_add(MIN_DAYS_TO_EXPIRY.days()) + .expect("target expiry within the supported date range"); + let mut year = today.year(); + + loop { + for month in QUARTERLY_CONTRACT_MONTHS { + if third_friday(year, month) < target_expiry { + continue; + } + + let month_code = futures_month_code(month as u8).expect("quarterly month code"); + let symbol = format_futures_symbol(root, month_code, i32::from(year), 1) + .expect("single-digit futures symbol"); + return InstrumentId::from(format!("{symbol}.{venue}").as_str()); + } + year += 1; + } +} + +fn third_friday(year: i16, month: i8) -> Date { + Date::new(year, month, 1) + .expect("first day of a quarterly month") + .nth_weekday_of_month(3, Weekday::Friday) + .expect("every month has a third Friday") +} + +#[cfg(test)] +mod tests { + use jiff::civil::date; + use rstest::rstest; + + use super::*; + + #[rstest] + #[case(2026, 3, date(2026, 3, 20))] + #[case(2026, 12, date(2026, 12, 18))] + #[case(2027, 3, date(2027, 3, 19))] + fn test_third_friday(#[case] year: i16, #[case] month: i8, #[case] expected: Date) { + assert_eq!(third_friday(year, month), expected); + } + + #[rstest] + #[case(date(2026, 9, 21), "ESZ6.XCME")] + #[case(date(2026, 11, 3), "ESZ6.XCME")] + #[case(date(2026, 11, 4), "ESH7.XCME")] + #[case(date(2026, 12, 31), "ESH7.XCME")] + fn test_active_quarterly_contract_rolls_before_expiry( + #[case] today: Date, + #[case] expected: &str, + ) { + assert_eq!( + active_quarterly_contract("ES", "XCME", today), + InstrumentId::from(expected) + ); + } + + #[rstest] + fn test_es_future_instrument_id_uses_xcme() { + assert_eq!(es_future_instrument_id().venue.as_str(), "XCME"); + } +} diff --git a/crates/adapters/interactive_brokers/examples/node_data_tester.rs b/crates/adapters/interactive_brokers/examples/node_data_tester.rs index d5f96274059c..7033e5fd6935 100644 --- a/crates/adapters/interactive_brokers/examples/node_data_tester.rs +++ b/crates/adapters/interactive_brokers/examples/node_data_tester.rs @@ -21,10 +21,10 @@ //! Run embedded config unit tests with: //! `cargo test --example ib-data-tester --package nautilus-interactive-brokers --features examples` //! -//! Edit the constants below to change the TWS/Gateway connection, target -//! instrument, market data type, and data spec profile. The adapter connects to -//! a locally running TWS or IB Gateway, so no credential environment variables -//! are required. +//! Edit the constants below to change the TWS/Gateway connection, market data type, +//! and data spec profile. The tester streams the live quarterly ES contract so it +//! works outside stock market hours. The adapter connects to a locally running TWS +//! or IB Gateway, so no credential environment variables are required. use std::{collections::HashSet, time::Duration}; @@ -44,6 +44,9 @@ use nautilus_model::{ }; use nautilus_testkit::testers::{DataTester, DataTesterConfig}; +#[path = "contracts/active_future.rs"] +mod active_future; + // Each variant is exercised by the tests and selected by editing DATA_SPEC_PROFILE, // but only the default is constructed in a non-test build #[allow(dead_code)] @@ -59,15 +62,19 @@ const NODE_NAME: &str = "IB-DATA-TESTER-001"; const HOST: &str = DEFAULT_HOST; const PORT: u16 = DEFAULT_TWS_PORT; const CLIENT_ID: i32 = DEFAULT_CLIENT_ID; -const INSTRUMENT_ID: &str = "AAPL=STK.SMART"; -const MARKET_DATA_TYPE: &str = "realtime"; +// Delayed data streams without a real-time subscription; override with +// `NAUTILUS_IB_MARKET_DATA_TYPE` when the account is entitled to real-time data. +const MARKET_DATA_TYPE: &str = "delayed"; const AUTO_STOP_SECS: u64 = 0; const DATA_SPEC_PROFILE: IbDataSpecProfile = IbDataSpecProfile::Supported; #[tokio::main] async fn main() -> Result<(), Box> { - let instrument_id = InstrumentId::from(INSTRUMENT_ID); - let market_data_type = parse_market_data_type(MARKET_DATA_TYPE); + let instrument_id = active_future::es_future_instrument_id(); + let market_data_type = parse_market_data_type( + &std::env::var("NAUTILUS_IB_MARKET_DATA_TYPE") + .unwrap_or_else(|_| MARKET_DATA_TYPE.to_string()), + ); let bar_type = BarType::from(format!("{instrument_id}-1-MINUTE-LAST-EXTERNAL").as_str()); let data_config = InteractiveBrokersDataClientConfig { diff --git a/crates/adapters/interactive_brokers/examples/node_exec_tester.rs b/crates/adapters/interactive_brokers/examples/node_exec_tester.rs index 3167231f4f5e..898a939462e8 100644 --- a/crates/adapters/interactive_brokers/examples/node_exec_tester.rs +++ b/crates/adapters/interactive_brokers/examples/node_exec_tester.rs @@ -15,14 +15,17 @@ //! Example demonstrating live execution testing with the Interactive Brokers adapter. //! -//! Run live smoke with: +//! Build the node and tester configuration with: //! `cargo run --example ib-exec-tester --package nautilus-interactive-brokers --features examples` //! +//! Set `NAUTILUS_IB_RUN=1` to connect. Set `NAUTILUS_IB_LIVE_ORDERS=1` as a second opt-in to +//! submit orders. Select an order profile with `NAUTILUS_IB_EXEC_PROFILE`. +//! //! Run embedded config unit tests with: //! `cargo test --example ib-exec-tester --package nautilus-interactive-brokers --features examples` //! -//! Edit the constants below to change the TWS/Gateway connection, target -//! instrument, order size, and exec spec profile. +//! Edit the constants below to change the TWS/Gateway connection and order size. The tester +//! trades the live quarterly ES contract so it works outside stock market hours. //! //! Required environment variable: //! - `NAUTILUS_IB_ACCOUNT_ID` is your IB account, for example `U1234567` @@ -50,15 +53,19 @@ use nautilus_model::{ use nautilus_testkit::testers::{ExecTester, ExecTesterConfig}; use nautilus_trading::strategy::StrategyConfig; -// Each variant is exercised by the tests and selected by editing EXEC_SPEC_PROFILE, -// but only the default is constructed in a non-test build -#[allow(dead_code)] +#[path = "contracts/active_future.rs"] +mod active_future; + #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum IbExecutionSpecProfile { Lifecycle, CancelModify, Rejection, Options, + Stop, + StopLimit, + Trailing, + Bracket, UnsupportedFlags, } @@ -72,19 +79,33 @@ const STRATEGY_ID: &str = "IB-EXEC-TESTER-001"; const HOST: &str = DEFAULT_HOST; const PORT: u16 = DEFAULT_TWS_PORT; const CLIENT_ID: i32 = DEFAULT_CLIENT_ID; -const INSTRUMENT_ID: &str = "AAPL=STK.SMART"; -const MARKET_DATA_TYPE: &str = "realtime"; +// Delayed data streams without a real-time subscription; override with +// `NAUTILUS_IB_MARKET_DATA_TYPE` when the account is entitled to real-time data. +const MARKET_DATA_TYPE: &str = "delayed"; const ORDER_QTY: &str = "1"; const AUTO_STOP_SECS: u64 = 0; -const EXEC_SPEC_PROFILE: IbExecutionSpecProfile = IbExecutionSpecProfile::Lifecycle; +const EXEC_SPEC_PROFILE: &str = "lifecycle"; #[tokio::main] async fn main() -> Result<(), Box> { - let account_id_raw = env::var("NAUTILUS_IB_ACCOUNT_ID")?; + let run = env_enabled("NAUTILUS_IB_RUN"); + let live_orders = env_enabled("NAUTILUS_IB_LIVE_ORDERS"); + validate_live_opt_ins(run, live_orders)?; + + let account_id_raw = match env::var("NAUTILUS_IB_ACCOUNT_ID") { + Ok(value) => value, + Err(e) if run => return Err(e.into()), + Err(_) => "U1234567".to_string(), + }; let trader_id = TraderId::from(TRADER_ID); - let instrument_id = InstrumentId::from(INSTRUMENT_ID); - let market_data_type = parse_market_data_type(MARKET_DATA_TYPE); + let instrument_id = active_future::es_future_instrument_id(); + let market_data_type = parse_market_data_type( + &env::var("NAUTILUS_IB_MARKET_DATA_TYPE").unwrap_or_else(|_| MARKET_DATA_TYPE.to_string()), + ); let order_qty = Quantity::from(ORDER_QTY); + let profile = parse_exec_spec_profile( + &env::var("NAUTILUS_IB_EXEC_PROFILE").unwrap_or_else(|_| EXEC_SPEC_PROFILE.to_string()), + ); let routing = RoutingConfig::builder().default(true).build(); @@ -131,19 +152,39 @@ async fn main() -> Result<(), Box> { .build()?; let tester_config = exec_tester_config_for_profile( - EXEC_SPEC_PROFILE, + profile, instrument_id, ClientId::new(IB), order_qty, + live_orders, ); node.add_strategy(ExecTester::new(tester_config))?; + if !run { + println!("Built Interactive Brokers exec tester node. Set NAUTILUS_IB_RUN=1 to connect."); + return Ok(()); + } + schedule_auto_stop(&node, AUTO_STOP_SECS); node.run().await?; Ok(()) } +fn env_enabled(name: &str) -> bool { + env::var(name).is_ok_and(|value| value == "1") +} + +fn validate_live_opt_ins(run: bool, live_orders: bool) -> Result<(), std::io::Error> { + if live_orders && !run { + return Err(std::io::Error::other( + "NAUTILUS_IB_LIVE_ORDERS=1 requires NAUTILUS_IB_RUN=1", + )); + } + + Ok(()) +} + fn parse_market_data_type(value: &str) -> MarketDataType { match value { "realtime" => MarketDataType::Realtime, @@ -154,6 +195,21 @@ fn parse_market_data_type(value: &str) -> MarketDataType { } } +fn parse_exec_spec_profile(value: &str) -> IbExecutionSpecProfile { + match value { + "lifecycle" => IbExecutionSpecProfile::Lifecycle, + "cancel-modify" | "cancel_modify" => IbExecutionSpecProfile::CancelModify, + "rejection" => IbExecutionSpecProfile::Rejection, + "options" => IbExecutionSpecProfile::Options, + "stop" => IbExecutionSpecProfile::Stop, + "stop-limit" | "stop_limit" => IbExecutionSpecProfile::StopLimit, + "trailing" => IbExecutionSpecProfile::Trailing, + "bracket" => IbExecutionSpecProfile::Bracket, + "unsupported-flags" | "unsupported_flags" => IbExecutionSpecProfile::UnsupportedFlags, + value => panic!("invalid NAUTILUS_IB_EXEC_PROFILE={value}"), + } +} + fn instrument_provider_config( instrument_id: InstrumentId, ) -> InteractiveBrokersInstrumentProviderConfig { @@ -184,25 +240,26 @@ fn exec_tester_config_for_profile( instrument_id: InstrumentId, client_id: ClientId, order_qty: Quantity, + live_orders: bool, ) -> ExecTesterConfig { let builder = ExecTesterConfig::builder() .base(StrategyConfig { strategy_id: Some(StrategyId::from(STRATEGY_ID)), - external_order_instrument_ids: Some(vec![instrument_id]), + use_uuid_client_order_ids: true, ..Default::default() }) .instrument_id(instrument_id) .client_id(client_id) .order_qty(order_qty) - .dry_run(DRY_RUN) + .dry_run(DRY_RUN || !live_orders) .log_data(false); match profile { IbExecutionSpecProfile::Lifecycle => builder .open_position_on_start_qty(order_qty.as_decimal()) + .open_position_on_first_quote(true) .enable_limit_buys(false) .enable_limit_sells(false) - .close_positions_on_stop(true) .build() .unwrap(), IbExecutionSpecProfile::CancelModify => builder @@ -221,9 +278,44 @@ fn exec_tester_config_for_profile( .unwrap(), IbExecutionSpecProfile::Options => builder .open_position_on_start_qty(order_qty.as_decimal()) + .open_position_on_first_quote(true) + .enable_limit_buys(false) + .enable_limit_sells(false) + .build() + .unwrap(), + IbExecutionSpecProfile::Stop => builder + .enable_limit_buys(false) + .enable_limit_sells(false) + .enable_stop_buys(true) + .enable_stop_sells(true) + .stop_order_type(OrderType::StopMarket) + .modify_stop_orders_to_maintain_offset(true) + .build() + .unwrap(), + IbExecutionSpecProfile::StopLimit => builder + .enable_limit_buys(false) + .enable_limit_sells(false) + .enable_stop_buys(true) + .enable_stop_sells(true) + .stop_order_type(OrderType::StopLimit) + .stop_limit_offset_ticks(25) + .build() + .unwrap(), + IbExecutionSpecProfile::Trailing => builder .enable_limit_buys(false) .enable_limit_sells(false) - .close_positions_on_stop(true) + .enable_stop_buys(true) + .enable_stop_sells(true) + .stop_order_type(OrderType::TrailingStopMarket) + .trailing_offset(rust_decimal::Decimal::new(25, 0)) + .build() + .unwrap(), + IbExecutionSpecProfile::Bracket => builder + .enable_limit_buys(true) + .enable_limit_sells(true) + .enable_brackets(true) + .bracket_entry_order_type(OrderType::Limit) + .bracket_offset_ticks(500) .build() .unwrap(), IbExecutionSpecProfile::UnsupportedFlags => builder @@ -257,14 +349,34 @@ mod tests { instrument_id(), ClientId::new(IB), Quantity::from("1"), + true, ) } + #[rstest::rstest] + fn test_default_connection_opt_in_keeps_tester_in_dry_run() { + let config = exec_tester_config_for_profile( + IbExecutionSpecProfile::Lifecycle, + instrument_id(), + ClientId::new(IB), + Quantity::from("1"), + false, + ); + + assert!(config.dry_run); + assert!(validate_live_opt_ins(true, false).is_ok()); + assert_eq!( + validate_live_opt_ins(false, true).unwrap_err().to_string(), + "NAUTILUS_IB_LIVE_ORDERS=1 requires NAUTILUS_IB_RUN=1", + ); + } + #[rstest::rstest] fn test_lifecycle_exec_spec_profile_opens_and_closes_position() { let config = config(IbExecutionSpecProfile::Lifecycle); assert_eq!(config.open_position_on_start_qty, Some(Decimal::ONE)); + assert!(config.open_position_on_first_quote); assert!(!config.enable_limit_buys); assert!(!config.enable_limit_sells); assert!(config.close_positions_on_stop); @@ -295,11 +407,55 @@ mod tests { let config = config(IbExecutionSpecProfile::Options); assert_eq!(config.open_position_on_start_qty, Some(Decimal::ONE)); + assert!(config.open_position_on_first_quote); assert!(!config.enable_limit_buys); assert!(!config.enable_limit_sells); assert!(config.close_positions_on_stop); } + #[rstest::rstest] + fn test_stop_exec_spec_profile_enables_modifiable_stop_orders() { + let config = config(IbExecutionSpecProfile::Stop); + + assert!(!config.enable_limit_buys); + assert!(!config.enable_limit_sells); + assert!(config.enable_stop_buys); + assert!(config.enable_stop_sells); + assert_eq!(config.stop_order_type, OrderType::StopMarket); + assert!(config.modify_stop_orders_to_maintain_offset); + } + + #[rstest::rstest] + fn test_stop_limit_exec_spec_profile_enables_stop_limit_orders() { + let config = config(IbExecutionSpecProfile::StopLimit); + + assert!(config.enable_stop_buys); + assert!(config.enable_stop_sells); + assert_eq!(config.stop_order_type, OrderType::StopLimit); + assert_eq!(config.stop_limit_offset_ticks, Some(25)); + } + + #[rstest::rstest] + fn test_trailing_exec_spec_profile_enables_trailing_stop_orders() { + let config = config(IbExecutionSpecProfile::Trailing); + + assert!(config.enable_stop_buys); + assert!(config.enable_stop_sells); + assert_eq!(config.stop_order_type, OrderType::TrailingStopMarket); + assert_eq!(config.trailing_offset, Some(Decimal::new(25, 0))); + } + + #[rstest::rstest] + fn test_bracket_exec_spec_profile_enables_bracket_orders() { + let config = config(IbExecutionSpecProfile::Bracket); + + assert!(config.enable_limit_buys); + assert!(config.enable_limit_sells); + assert!(config.enable_brackets); + assert_eq!(config.bracket_entry_order_type, OrderType::Limit); + assert_eq!(config.bracket_offset_ticks, 500); + } + #[rstest::rstest] fn test_unsupported_flags_exec_spec_profile_exercises_rejection_and_batch_cancel_flags() { let config = config(IbExecutionSpecProfile::UnsupportedFlags); diff --git a/crates/adapters/interactive_brokers/src/common/connection.rs b/crates/adapters/interactive_brokers/src/common/connection.rs deleted file mode 100644 index 339adc110d96..000000000000 --- a/crates/adapters/interactive_brokers/src/common/connection.rs +++ /dev/null @@ -1,284 +0,0 @@ -// ------------------------------------------------------------------------------------------------- -// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -// https://nautechsystems.io -// -// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// ------------------------------------------------------------------------------------------------- - -//! Connection management utilities for Interactive Brokers adapter. - -use std::{ - fmt::Debug, - sync::{ - Arc, - atomic::{AtomicBool, AtomicU32, Ordering}, - }, - time::Duration, -}; - -use anyhow::Context; -use ibapi::client::Client; -use nautilus_common::live::get_runtime; - -/// Connection manager for Interactive Brokers clients. -/// -/// Handles automatic reconnection with exponential backoff, connection monitoring, -/// and subscription resubscription on reconnect. -#[derive(Debug, Clone)] -pub struct ConnectionManager { - /// Host address for IB Gateway/TWS. - host: String, - /// Port for IB Gateway/TWS. - port: u16, - /// Client ID. - client_id: i32, - /// Current connection state. - is_connected: Arc, - /// Connection attempt counter. - attempt_count: Arc, - /// Maximum connection attempts (0 = infinite). - max_attempts: u32, - /// Whether to retry indefinitely. - retry_indefinitely: bool, - /// Current backoff duration. - current_backoff: Arc>, - /// Last disconnection time. - last_disconnection: Arc>>, -} - -impl ConnectionManager { - /// Create a new connection manager. - /// - /// # Arguments - /// - /// * `host` - Host address - /// * `port` - Port number - /// * `client_id` - Client ID - /// * `max_attempts` - Maximum connection attempts (0 = infinite) - pub fn new(host: String, port: u16, client_id: i32, max_attempts: u32) -> Self { - Self { - host, - port, - client_id, - is_connected: Arc::new(AtomicBool::new(false)), - attempt_count: Arc::new(AtomicU32::new(0)), - max_attempts, - retry_indefinitely: max_attempts == 0, - current_backoff: Arc::new(parking_lot::Mutex::new(Duration::from_secs(1))), - last_disconnection: Arc::new(parking_lot::Mutex::new(None)), - } - } - - /// Connect to IB Gateway/TWS with automatic retry. - /// - /// # Returns - /// - /// Returns the connected client on success. - /// - /// # Errors - /// - /// Returns an error if connection fails after max attempts. - pub async fn connect_with_retry(&self) -> anyhow::Result> { - const MAX_BACKOFF: Duration = Duration::from_secs(60); - let mut attempt = 0; - let mut backoff = Duration::from_secs(1); - - loop { - attempt += 1; - self.attempt_count.store(attempt, Ordering::Relaxed); - - if !self.retry_indefinitely && attempt > self.max_attempts { - anyhow::bail!("Failed to connect after {} attempts", self.max_attempts); - } - - tracing::debug!( - "Connection attempt {} to {}:{} (client_id: {})", - attempt, - self.host, - self.port, - self.client_id - ); - - let address = format!("{}:{}", self.host, self.port); - match Client::connect(&address, self.client_id).await { - Ok(client) => { - tracing::info!( - "Successfully connected to IB Gateway/TWS at {} (client_id: {})", - address, - self.client_id - ); - - self.is_connected.store(true, Ordering::Relaxed); - self.attempt_count.store(0, Ordering::Relaxed); - *self.current_backoff.lock() = Duration::from_secs(1); - - return Ok(Arc::new(client)); - } - Err(e) => { - tracing::warn!( - "Connection attempt {} failed: {} (backoff: {:?})", - attempt, - e, - backoff - ); - - if !self.retry_indefinitely && attempt >= self.max_attempts { - return Err(e).context(format!( - "Failed to connect after {} attempts", - self.max_attempts - )); - } - - // Exponential backoff - tokio::time::sleep(backoff).await; - backoff = std::cmp::min(backoff * 2, MAX_BACKOFF); - *self.current_backoff.lock() = backoff; - } - } - } - } - - /// Check if currently connected. - pub fn is_connected(&self) -> bool { - self.is_connected.load(Ordering::Relaxed) - } - - /// Mark connection as disconnected. - pub fn mark_disconnected(&self) { - self.is_connected.store(false, Ordering::Relaxed); - *self.last_disconnection.lock() = Some(tokio::time::Instant::now()); - } - - /// Get current attempt count. - pub fn attempt_count(&self) -> u32 { - self.attempt_count.load(Ordering::Relaxed) - } - - /// Get current backoff duration. - pub fn current_backoff(&self) -> Duration { - *self.current_backoff.lock() - } - - /// Get time since last disconnection. - pub fn time_since_disconnection(&self) -> Option { - self.last_disconnection - .lock() - .map(|time: tokio::time::Instant| time.elapsed()) - } -} - -/// Connection watchdog for monitoring connection health. -/// -/// Periodically checks connection status and triggers reconnection if needed. -#[derive(Clone)] -pub struct ConnectionWatchdog { - /// Connection manager. - manager: Arc, - /// Check interval. - check_interval: Duration, - /// Client reference (for health checks). - client: Arc>>>, - /// Callback to call when reconnection is needed. - reconnect_callback: Arc< - dyn Fn() -> tokio::task::JoinHandle>> + Send + Sync + 'static, - >, - /// Whether the watchdog is running. - is_running: Arc, -} - -impl Debug for ConnectionWatchdog { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct(stringify!(ConnectionWatchdog)) - .field("check_interval", &self.check_interval) - .field("is_running", &self.is_running.load(Ordering::Relaxed)) - .finish_non_exhaustive() - } -} - -impl ConnectionWatchdog { - /// Create a new connection watchdog. - /// - /// # Arguments - /// - /// * `manager` - Connection manager - /// * `check_interval` - Interval between health checks - /// * `reconnect_callback` - Callback to trigger reconnection - pub fn new( - manager: Arc, - check_interval: Duration, - reconnect_callback: Arc< - dyn Fn() -> tokio::task::JoinHandle>> + Send + Sync, - >, - ) -> Self { - Self { - manager, - check_interval, - client: Arc::new(parking_lot::Mutex::new(None)), - reconnect_callback, - is_running: Arc::new(AtomicBool::new(false)), - } - } - - /// Set the client reference for health checks. - pub fn set_client(&self, client: Arc) { - *self.client.lock() = Some(client); - } - - /// Start the watchdog. - pub fn start(&self) -> tokio::task::JoinHandle<()> { - let manager = Arc::clone(&self.manager); - let client = Arc::clone(&self.client); - let reconnect_callback = Arc::clone(&self.reconnect_callback); - let check_interval = self.check_interval; - let is_running = Arc::clone(&self.is_running); - - is_running.store(true, Ordering::Relaxed); - - get_runtime().spawn(async move { - tracing::debug!("Connection watchdog started"); - - while is_running.load(Ordering::Relaxed) { - tokio::time::sleep(check_interval).await; - - if !manager.is_connected() { - tracing::warn!( - "Connection watchdog detected disconnection, triggering reconnection" - ); - - // Trigger reconnection - let handle = reconnect_callback(); - - // Wait for reconnection attempt - match handle.await { - Ok(Ok(new_client)) => { - tracing::info!("Reconnection successful via watchdog"); - *client.lock() = Some(new_client); - manager.is_connected.store(true, Ordering::Relaxed); - } - Ok(Err(e)) => { - tracing::error!("Reconnection failed via watchdog: {}", e); - } - Err(e) => { - tracing::error!("Reconnection task panicked: {}", e); - } - } - } - } - - tracing::debug!("Connection watchdog stopped"); - }) - } - - /// Stop the watchdog. - pub fn stop(&self) { - self.is_running.store(false, Ordering::Relaxed); - } -} diff --git a/crates/adapters/interactive_brokers/src/common/consts.rs b/crates/adapters/interactive_brokers/src/common/consts.rs index 90b15c765efd..1bc62b4d18c7 100644 --- a/crates/adapters/interactive_brokers/src/common/consts.rs +++ b/crates/adapters/interactive_brokers/src/common/consts.rs @@ -20,9 +20,6 @@ use std::sync::LazyLock; use nautilus_model::identifiers::{ClientId, Venue}; use ustr::Ustr; -/// Long-form venue identifier string. -pub const INTERACTIVE_BROKERS: &str = "INTERACTIVE_BROKERS"; - /// Short-form venue identifier string used as the canonical venue and client ID. pub const IB: &str = "IB"; diff --git a/crates/adapters/interactive_brokers/src/common/contracts.rs b/crates/adapters/interactive_brokers/src/common/contracts.rs index cd30376540b5..3791b23789fb 100644 --- a/crates/adapters/interactive_brokers/src/common/contracts.rs +++ b/crates/adapters/interactive_brokers/src/common/contracts.rs @@ -17,6 +17,7 @@ use std::str::FromStr; +use anyhow::Context; use ibapi::contracts::{ Contract, Currency as IBCurrency, Exchange as IBExchange, OptionRight, SecurityIdType, SecurityType, Symbol, @@ -26,6 +27,101 @@ use serde_json::Value; use crate::common::enums::{IbOptionRight, IbSecurityType}; +pub(crate) const KEY_BUILD_OPTIONS_CHAIN: &str = "build_options_chain"; +pub(crate) const KEY_BUILD_FUTURES_CHAIN: &str = "build_futures_chain"; +pub(crate) const KEY_MIN_EXPIRY_DAYS: &str = "min_expiry_days"; +pub(crate) const KEY_MAX_EXPIRY_DAYS: &str = "max_expiry_days"; +pub(crate) const KEY_OPTIONS_CHAIN_EXCHANGE: &str = "options_chain_exchange"; +pub(crate) const KEY_OPTIONS_CHAIN_EXCHANGE_ALT: &str = "optionsChainExchange"; + +/// A configured IB contract with optional per-contract chain-building settings. +#[derive(Clone, Debug)] +pub struct ConfiguredContract { + /// The IB contract to load. + pub contract: Contract, + /// Whether to build the options chain for this contract, overriding the global flag. + pub build_options_chain: Option, + /// Whether to build the futures chain for this contract, overriding the global flag. + pub build_futures_chain: Option, + /// Minimum expiry days for this contract's chains, overriding the global value. + pub min_expiry_days: Option, + /// Maximum expiry days for this contract's chains, overriding the global value. + pub max_expiry_days: Option, + /// Exchange override for options chain lookups. + pub options_chain_exchange: Option, +} + +impl From for ConfiguredContract { + fn from(contract: Contract) -> Self { + Self { + contract, + build_options_chain: None, + build_futures_chain: None, + min_expiry_days: None, + max_expiry_days: None, + options_chain_exchange: None, + } + } +} + +impl ConfiguredContract { + /// Convert to the JSON representation used in configuration files. + #[must_use] + pub fn to_json_value(&self) -> Value { + let mut json = contract_to_json_value(&self.contract); + + if let Value::Object(map) = &mut json { + for (key, value) in self.chain_entries() { + map.insert(key.to_string(), value); + } + } + + json + } + + /// Chain-building spec for `load_contract_spec`, when any per-contract setting is present. + #[must_use] + pub fn chain_spec_json(&self) -> Option { + let entries = self.chain_entries(); + + if entries.is_empty() { + return None; + } + + let mut map = serde_json::Map::new(); + for (key, value) in entries { + map.insert(key.to_string(), value); + } + Some(Value::Object(map)) + } + + fn chain_entries(&self) -> Vec<(&'static str, Value)> { + let mut entries = Vec::new(); + + if let Some(value) = self.build_options_chain { + entries.push((KEY_BUILD_OPTIONS_CHAIN, Value::from(value))); + } + + if let Some(value) = self.build_futures_chain { + entries.push((KEY_BUILD_FUTURES_CHAIN, Value::from(value))); + } + + if let Some(value) = self.min_expiry_days { + entries.push((KEY_MIN_EXPIRY_DAYS, Value::from(value))); + } + + if let Some(value) = self.max_expiry_days { + entries.push((KEY_MAX_EXPIRY_DAYS, Value::from(value))); + } + + if let Some(value) = &self.options_chain_exchange { + entries.push((KEY_OPTIONS_CHAIN_EXCHANGE, Value::from(value.as_str()))); + } + + entries + } +} + /// Convert an IB contract into JSON metadata suitable for instrument `info["contract"]`. #[must_use] pub fn contract_to_json_value(contract: &Contract) -> Value { @@ -155,6 +251,153 @@ pub fn parse_contract_from_json(json: &Value) -> anyhow::Result { }) } +/// Parse a configured IB contract with strict field and enum validation. +/// +/// # Errors +/// +/// Returns an error when a known field has the wrong JSON type, a numeric field is out of +/// range, or an enum value is unknown. +pub fn parse_configured_contract_from_json(json: &Value) -> anyhow::Result { + let obj = json + .as_object() + .ok_or_else(|| anyhow::anyhow!("Expected JSON object for configured contract"))?; + + for key in [ + "symbol", + "lastTradeDateOrContractMonth", + "multiplier", + "exchange", + "currency", + "localSymbol", + "primaryExchange", + "tradingClass", + "secId", + "issuerId", + "description", + "comboLegsDescrip", + ] { + if let Some(value) = obj.get(key) + && !value.is_string() + { + anyhow::bail!("Configured contract field '{key}' must be a string"); + } + } + + if let Some(value) = obj.get("strike") + && !value.is_number() + { + anyhow::bail!("Configured contract field 'strike' must be a number"); + } + + if let Some(value) = obj.get("includeExpired") + && !value.is_boolean() + { + anyhow::bail!("Configured contract field 'includeExpired' must be a boolean"); + } + + let mut contract_id = 0; + + if let Some(value) = obj.get("conId") { + let raw_contract_id = value + .as_i64() + .context("Configured contract field 'conId' must be an integer")?; + contract_id = i32::try_from(raw_contract_id) + .context("Configured contract field 'conId' exceeds i32")?; + } + + match obj.get("secType").and_then(Value::as_str) { + Some(security_type) => { + IbSecurityType::from_str(security_type)?; + } + None if contract_id > 0 => {} // IB resolves the contract by conId + None => anyhow::bail!( + "Configured contract requires 'secType' as a known IB security type string, or a positive 'conId'" + ), + } + + if let Some(value) = obj.get("right") + && !value.is_null() + { + let right = value + .as_str() + .context("Configured contract field 'right' must be a string")?; + IbOptionRight::from_str(right)?; + } + + if let Some(value) = obj.get("secIdType") + && !value.is_null() + { + let security_id_type = value + .as_str() + .context("Configured contract field 'secIdType' must be a string")?; + + if !matches!( + security_id_type.to_ascii_uppercase().as_str(), + "CUSIP" | "ISIN" | "SEDOL" | "RIC" | "FIGI" + ) { + anyhow::bail!("Unknown IB security ID type: {security_id_type}"); + } + } + + let build_options_chain = configured_bool(obj, KEY_BUILD_OPTIONS_CHAIN)?; + let build_futures_chain = configured_bool(obj, KEY_BUILD_FUTURES_CHAIN)?; + let min_expiry_days = configured_u32(obj, KEY_MIN_EXPIRY_DAYS)?; + let max_expiry_days = configured_u32(obj, KEY_MAX_EXPIRY_DAYS)?; + let options_chain_exchange = match configured_string(obj, KEY_OPTIONS_CHAIN_EXCHANGE)? { + Some(exchange) => Some(exchange), + None => configured_string(obj, KEY_OPTIONS_CHAIN_EXCHANGE_ALT)?, + }; + + Ok(ConfiguredContract { + contract: parse_contract_from_json(json)?, + build_options_chain, + build_futures_chain, + min_expiry_days, + max_expiry_days, + options_chain_exchange, + }) +} + +fn configured_bool( + obj: &serde_json::Map, + key: &str, +) -> anyhow::Result> { + obj.get(key) + .map(|value| { + value + .as_bool() + .with_context(|| format!("Configured contract field '{key}' must be a boolean")) + }) + .transpose() +} + +fn configured_u32(obj: &serde_json::Map, key: &str) -> anyhow::Result> { + obj.get(key) + .map(|value| { + value + .as_u64() + .and_then(|days| u32::try_from(days).ok()) + .with_context(|| { + format!("Configured contract field '{key}' must be a non-negative integer") + }) + }) + .transpose() +} + +fn configured_string( + obj: &serde_json::Map, + key: &str, +) -> anyhow::Result> { + obj.get(key) + .map(|value| { + value + .as_str() + .map(ToString::to_string) + .with_context(|| format!("Configured contract field '{key}' must be a string")) + }) + .transpose() +} + /// Parse multiple IB contracts from JSON array. /// /// # Errors @@ -180,5 +423,3 @@ pub fn parse_contracts_from_json_array(json_str: &str) -> anyhow::Result for IbSecurityType { + type Error = anyhow::Error; + + fn try_from(value: String) -> Result { + Self::from_str(&value) + } +} + +impl From for String { + fn from(value: IbSecurityType) -> Self { + value.as_str().to_string() + } +} + impl Display for IbSecurityType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.as_str()) diff --git a/crates/adapters/interactive_brokers/src/common/enums/market_data.rs b/crates/adapters/interactive_brokers/src/common/enums/market_data.rs index ad4b37999343..6128320b083f 100644 --- a/crates/adapters/interactive_brokers/src/common/enums/market_data.rs +++ b/crates/adapters/interactive_brokers/src/common/enums/market_data.rs @@ -13,6 +13,8 @@ // limitations under the License. // ------------------------------------------------------------------------------------------------- +//! Interactive Brokers market data enumerations. + use std::{fmt::Display, str::FromStr}; /// Interactive Brokers historical tick request types. @@ -288,16 +290,6 @@ pub enum IbRealtimeBarSize { Sec5, } -impl IbRealtimeBarSize { - /// Converts to the rust-ibapi realtime bar-size enum. - #[must_use] - pub const fn ibapi_bar_size(self) -> ibapi::market_data::realtime::BarSize { - match self { - Self::Sec5 => ibapi::market_data::realtime::BarSize::Sec5, - } - } -} - impl Display for IbRealtimeBarSize { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { diff --git a/crates/adapters/interactive_brokers/src/common/enums/misc.rs b/crates/adapters/interactive_brokers/src/common/enums/misc.rs index ac9b12e8892a..9d9341802cfa 100644 --- a/crates/adapters/interactive_brokers/src/common/enums/misc.rs +++ b/crates/adapters/interactive_brokers/src/common/enums/misc.rs @@ -13,6 +13,8 @@ // limitations under the License. // ------------------------------------------------------------------------------------------------- +//! Interactive Brokers account, condition, and routing enumerations. + use std::fmt::Display; /// Interactive Brokers order origin values. @@ -52,7 +54,10 @@ impl From for IbOrderOrigin { match value { 0 => Self::Customer, 1 => Self::Firm, - _ => Self::default(), + _ => { + tracing::warn!("Unknown IB order origin code {value}; using the default"); + Self::default() + } } } } @@ -110,7 +115,10 @@ impl From for IbShortSaleSlot { 0 => Self::None, 1 => Self::Broker, 2 => Self::ThirdParty, - _ => Self::default(), + _ => { + tracing::warn!("Unknown IB short-sale slot code {value}; using the default"); + Self::default() + } } } } @@ -165,7 +173,10 @@ impl From for IbVolatilityType { match value { 1 => Self::Daily, 2 => Self::Annual, - _ => Self::default(), + _ => { + tracing::warn!("Unknown IB volatility type code {value}; using the default"); + Self::default() + } } } } @@ -220,7 +231,10 @@ impl From for IbReferencePriceType { match value { 1 => Self::AverageOfNbbo, 2 => Self::Nbbo, - _ => Self::default(), + _ => { + tracing::warn!("Unknown IB reference price type code {value}; using the default"); + Self::default() + } } } } @@ -278,7 +292,10 @@ impl From for IbAuctionStrategy { 1 => Self::Match, 2 => Self::Improvement, 3 => Self::Transparent, - _ => Self::default(), + _ => { + tracing::warn!("Unknown IB auction strategy code {value}; using the default"); + Self::default() + } } } } @@ -289,13 +306,6 @@ impl Display for IbAuctionStrategy { } } -impl IbAuctionStrategy { - #[must_use] - pub fn ibapi_auction_strategy(self) -> ibapi::orders::AuctionStrategy { - ibapi::orders::AuctionStrategy::from(self.as_i32()) - } -} - /// Interactive Brokers option exercise action values. #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] #[cfg_attr( @@ -333,7 +343,10 @@ impl From for IbExerciseAction { match value { 1 => Self::Exercise, 2 => Self::Lapse, - _ => Self::default(), + _ => { + tracing::warn!("Unknown IB exercise action code {value}; using the default"); + Self::default() + } } } } @@ -391,7 +404,10 @@ impl From for IbArticleType { match value { 0 => Self::Text, 1 => Self::Binary, - _ => Self::default(), + _ => { + tracing::warn!("Unknown IB article type code {value}; using the default"); + Self::default() + } } } } @@ -449,7 +465,10 @@ impl From for IbAuctionType { 1 => Self::Opening, 2 => Self::Closing, 4 => Self::Volatility, - _ => Self::default(), + _ => { + tracing::warn!("Unknown IB auction type code {value}; using the default"); + Self::default() + } } } } diff --git a/crates/adapters/interactive_brokers/src/common/enums/order.rs b/crates/adapters/interactive_brokers/src/common/enums/order.rs index 186441a81f87..54da1af8359f 100644 --- a/crates/adapters/interactive_brokers/src/common/enums/order.rs +++ b/crates/adapters/interactive_brokers/src/common/enums/order.rs @@ -13,6 +13,8 @@ // limitations under the License. // ------------------------------------------------------------------------------------------------- +//! Interactive Brokers order enumerations. + use std::{fmt::Display, str::FromStr}; use nautilus_model::enums::{ @@ -164,10 +166,9 @@ impl IbOrderStatus { #[must_use] pub const fn nautilus_status(self) -> NautilusOrderStatus { match self { - Self::ApiPending | Self::PendingSubmit | Self::PreSubmitted => { - NautilusOrderStatus::Submitted - } - Self::Submitted => NautilusOrderStatus::Accepted, + Self::ApiPending | Self::PendingSubmit => NautilusOrderStatus::Submitted, + // IB accepted the order but holds it, for example until `goodAfterTime` + Self::PreSubmitted | Self::Submitted => NautilusOrderStatus::Accepted, Self::PendingCancel => NautilusOrderStatus::PendingCancel, Self::ApiCancelled | Self::Cancelled => NautilusOrderStatus::Canceled, Self::Filled => NautilusOrderStatus::Filled, @@ -498,12 +499,12 @@ impl IbTimeInForce { pub const fn ibapi_time_in_force(self) -> ibapi::orders::TimeInForce { match self { Self::Day => ibapi::orders::TimeInForce::Day, - Self::GoodTilCanceled => ibapi::orders::TimeInForce::GoodTilCanceled, + Self::GoodTilCanceled => ibapi::orders::TimeInForce::GoodTillCanceled, Self::ImmediateOrCancel => ibapi::orders::TimeInForce::ImmediateOrCancel, - Self::GoodTilDate => ibapi::orders::TimeInForce::GoodTilDate, + Self::GoodTilDate => ibapi::orders::TimeInForce::GoodTillDate, Self::OnOpen => ibapi::orders::TimeInForce::OnOpen, Self::FillOrKill => ibapi::orders::TimeInForce::FillOrKill, - Self::DayTilCanceled => ibapi::orders::TimeInForce::DayTilCanceled, + Self::DayTilCanceled => ibapi::orders::TimeInForce::DayTillCanceled, Self::Auction => ibapi::orders::TimeInForce::Auction, } } @@ -513,13 +514,22 @@ impl From for IbTimeInForce { fn from(value: ibapi::orders::TimeInForce) -> Self { match value { ibapi::orders::TimeInForce::Day => Self::Day, - ibapi::orders::TimeInForce::GoodTilCanceled => Self::GoodTilCanceled, + ibapi::orders::TimeInForce::GoodTillCanceled => Self::GoodTilCanceled, ibapi::orders::TimeInForce::ImmediateOrCancel => Self::ImmediateOrCancel, - ibapi::orders::TimeInForce::GoodTilDate => Self::GoodTilDate, + ibapi::orders::TimeInForce::GoodTillDate => Self::GoodTilDate, ibapi::orders::TimeInForce::OnOpen => Self::OnOpen, ibapi::orders::TimeInForce::FillOrKill => Self::FillOrKill, - ibapi::orders::TimeInForce::DayTilCanceled => Self::DayTilCanceled, + ibapi::orders::TimeInForce::DayTillCanceled => Self::DayTilCanceled, ibapi::orders::TimeInForce::Auction => Self::Auction, + // Decoded as DAY before ibapi 4.2 modeled them + ibapi::orders::TimeInForce::GoodTillCrossing => { + tracing::warn!("Unsupported IB time in force GTX; reporting DAY"); + Self::Day + } + ibapi::orders::TimeInForce::Unknown(raw) => { + tracing::warn!("Unknown IB time in force {raw}; reporting DAY"); + Self::Day + } } } } @@ -600,26 +610,6 @@ impl IbBuilderTimeInForce { Self::OpeningAuction => "OPG", } } - - #[must_use] - pub fn ibapi_builder_time_in_force( - self, - good_till_date: Option, - ) -> ibapi::orders::builder::TimeInForce { - match self { - Self::Day => ibapi::orders::builder::TimeInForce::Day, - Self::GoodTillCancel => ibapi::orders::builder::TimeInForce::GoodTillCancel, - Self::ImmediateOrCancel => ibapi::orders::builder::TimeInForce::ImmediateOrCancel, - Self::GoodTillDate => ibapi::orders::builder::TimeInForce::GoodTillDate { - date: good_till_date.unwrap_or_default(), - }, - Self::FillOrKill => ibapi::orders::builder::TimeInForce::FillOrKill, - Self::GoodTillCrossing => ibapi::orders::builder::TimeInForce::GoodTillCrossing, - Self::DayTillCanceled => ibapi::orders::builder::TimeInForce::DayTillCanceled, - Self::Auction => ibapi::orders::builder::TimeInForce::Auction, - Self::OpeningAuction => ibapi::orders::builder::TimeInForce::OpeningAuction, - } - } } impl FromStr for IbBuilderTimeInForce { @@ -894,16 +884,19 @@ impl IbTriggerMethod { } } -impl From for IbTriggerMethod { - fn from(value: i32) -> Self { +impl TryFrom for IbTriggerMethod { + type Error = anyhow::Error; + + fn try_from(value: i32) -> Result { match value { - 1 => Self::DoubleBidAsk, - 2 => Self::Last, - 3 => Self::DoubleLast, - 4 => Self::BidAsk, - 7 => Self::LastOrBidAsk, - 8 => Self::Midpoint, - _ => Self::Default, + 0 => Ok(Self::Default), + 1 => Ok(Self::DoubleBidAsk), + 2 => Ok(Self::Last), + 3 => Ok(Self::DoubleLast), + 4 => Ok(Self::BidAsk), + 7 => Ok(Self::LastOrBidAsk), + 8 => Ok(Self::Midpoint), + _ => anyhow::bail!("Unknown IB trigger method: {value}"), } } } @@ -916,7 +909,19 @@ impl From for i32 { impl From for IbTriggerMethod { fn from(value: ibapi::orders::conditions::TriggerMethod) -> Self { - i32::from(value).into() + match value { + ibapi::orders::conditions::TriggerMethod::Default => Self::Default, + ibapi::orders::conditions::TriggerMethod::DoubleBidAsk => Self::DoubleBidAsk, + ibapi::orders::conditions::TriggerMethod::Last => Self::Last, + ibapi::orders::conditions::TriggerMethod::DoubleLast => Self::DoubleLast, + ibapi::orders::conditions::TriggerMethod::BidAsk => Self::BidAsk, + ibapi::orders::conditions::TriggerMethod::LastOrBidAsk => Self::LastOrBidAsk, + ibapi::orders::conditions::TriggerMethod::Midpoint => Self::Midpoint, + ibapi::orders::conditions::TriggerMethod::Unknown(code) => { + tracing::warn!("Unknown IB trigger method {code}; using the default"); + Self::Default + } + } } } @@ -977,13 +982,16 @@ impl IbOcaType { } } -impl From for IbOcaType { - fn from(value: i32) -> Self { +impl TryFrom for IbOcaType { + type Error = anyhow::Error; + + fn try_from(value: i32) -> Result { match value { - 1 => Self::CancelWithBlock, - 2 => Self::ReduceWithBlock, - 3 => Self::ReduceWithoutBlock, - _ => Self::None, + 0 => Ok(Self::None), + 1 => Ok(Self::CancelWithBlock), + 2 => Ok(Self::ReduceWithBlock), + 3 => Ok(Self::ReduceWithoutBlock), + _ => anyhow::bail!("Unknown IB OCA type: {value}"), } } } @@ -996,7 +1004,16 @@ impl From for i32 { impl From for IbOcaType { fn from(value: ibapi::orders::OcaType) -> Self { - i32::from(value).into() + match value { + ibapi::orders::OcaType::None => Self::None, + ibapi::orders::OcaType::CancelWithBlock => Self::CancelWithBlock, + ibapi::orders::OcaType::ReduceWithBlock => Self::ReduceWithBlock, + ibapi::orders::OcaType::ReduceWithoutBlock => Self::ReduceWithoutBlock, + ibapi::orders::OcaType::Unknown(code) => { + tracing::warn!("Unknown IB OCA type {code}; reporting no OCA group handling"); + Self::None + } + } } } @@ -1027,6 +1044,8 @@ pub enum IbLiquidity { AddedLiquidity, RemovedLiquidity, LiquidityRoutedOut, + /// An unrecognized value received from IB. + Unknown, } impl IbLiquidity { @@ -1038,6 +1057,7 @@ impl IbLiquidity { Self::AddedLiquidity => 1, Self::RemovedLiquidity => 2, Self::LiquidityRoutedOut => 3, + Self::Unknown => -1, } } @@ -1051,10 +1071,14 @@ impl IbLiquidity { impl From for IbLiquidity { fn from(value: i32) -> Self { match value { + 0 => Self::None, 1 => Self::AddedLiquidity, 2 => Self::RemovedLiquidity, 3 => Self::LiquidityRoutedOut, - _ => Self::None, + _ => { + tracing::warn!("IB execution used unknown liquidity code {value}"); + Self::Unknown + } } } } @@ -1066,6 +1090,10 @@ impl From for IbLiquidity { ibapi::orders::Liquidity::AddedLiquidity => Self::AddedLiquidity, ibapi::orders::Liquidity::RemovedLiquidity => Self::RemovedLiquidity, ibapi::orders::Liquidity::LiquidityRoutedOut => Self::LiquidityRoutedOut, + ibapi::orders::Liquidity::Unknown(code) => { + tracing::warn!("IB execution used unknown liquidity code {code}"); + Self::Unknown + } } } } diff --git a/crates/adapters/interactive_brokers/src/common/futures.rs b/crates/adapters/interactive_brokers/src/common/futures.rs new file mode 100644 index 000000000000..ea29538cb7b8 --- /dev/null +++ b/crates/adapters/interactive_brokers/src/common/futures.rs @@ -0,0 +1,228 @@ +// ------------------------------------------------------------------------------------------------- +// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. +// https://nautechsystems.io +// +// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------------------------------- + +//! Futures symbol parsing and formatting. + +/// The parsed components of a futures symbol. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct FuturesSymbol<'a> { + /// Symbol root before the month code. + pub root: &'a str, + /// Standard futures month code. + pub month_code: char, + /// One-, two-, or four-digit year suffix. + pub year_digits: &'a str, +} + +/// Parses a single-leg futures symbol such as `ESZ6`, `ESZ26`, or `ESZ2026`. +/// Callers must split multi-leg symbols before parsing. +#[must_use] +pub fn parse_futures_symbol(symbol: &str) -> Option> { + for (month_pos, month_code) in symbol.char_indices().rev() { + if futures_month(month_code).is_none() { + continue; + } + + let year_digits = &symbol[month_pos + month_code.len_utf8()..]; + if !matches!(year_digits.len(), 1 | 2 | 4) + || !year_digits.bytes().all(|b| b.is_ascii_digit()) + || month_pos == 0 + { + continue; + } + + return Some(FuturesSymbol { + root: &symbol[..month_pos], + month_code, + year_digits, + }); + } + + None +} + +/// Formats a futures symbol with one, two, or four year digits. +#[must_use] +pub fn format_futures_symbol( + root: &str, + month_code: char, + year: i32, + year_digits: u8, +) -> Option { + if root.is_empty() || futures_month(month_code).is_none() || !(0..=9999).contains(&year) { + return None; + } + + let year = match year_digits { + 1 => format!("{}", year % 10), + 2 => format!("{:02}", year % 100), + 4 => format!("{year:04}"), + _ => return None, + }; + + Some(format!("{root}{month_code}{year}")) +} + +/// Resolves one-, two-, or four-digit futures years against a reference year. +/// +/// A single digit resolves to the nearest matching year at or after the reference year, +/// matching venue listing practice. Two digits carry the century-local year and resolve +/// inside a window from 30 years before to 69 years after the reference year, so a +/// recently expired id (`ESZ25` referenced in 2026) resolves to its actual year instead +/// of a century ahead. Four digits are exact. +#[must_use] +pub fn resolve_futures_year(year_digits: &str, reference_year: i32) -> Option { + if reference_year < 0 || !year_digits.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + + let partial = year_digits.parse::().ok()?; + match year_digits.len() { + 1 => { + let mut year = reference_year - reference_year.rem_euclid(10) + partial; + if year < reference_year { + year += 10; + } + Some(year) + } + 2 => { + let mut year = reference_year - reference_year.rem_euclid(100) + partial; + if year < reference_year - 30 { + year += 100; + } else if year > reference_year + 69 { + year -= 100; + } + Some(year) + } + 4 => Some(partial), + _ => None, + } +} + +/// Returns the standard futures month code for a calendar month. +#[must_use] +pub const fn futures_month_code(month: u8) -> Option { + match month { + 1 => Some('F'), + 2 => Some('G'), + 3 => Some('H'), + 4 => Some('J'), + 5 => Some('K'), + 6 => Some('M'), + 7 => Some('N'), + 8 => Some('Q'), + 9 => Some('U'), + 10 => Some('V'), + 11 => Some('X'), + 12 => Some('Z'), + _ => None, + } +} + +/// Returns the calendar month for a standard futures month code. +#[must_use] +pub const fn futures_month(month_code: char) -> Option { + match month_code { + 'F' => Some(1), + 'G' => Some(2), + 'H' => Some(3), + 'J' => Some(4), + 'K' => Some(5), + 'M' => Some(6), + 'N' => Some(7), + 'Q' => Some(8), + 'U' => Some(9), + 'V' => Some(10), + 'X' => Some(11), + 'Z' => Some(12), + _ => None, + } +} + +#[cfg(test)] +mod tests { + use rstest::rstest; + + use super::*; + + #[rstest] + #[case("ESZ6", "ES", 'Z', "6")] + #[case("ESZ26", "ES", 'Z', "26")] + #[case("ESZ2026", "ES", 'Z', "2026")] + #[case("EX2G3", "EX2", 'G', "3")] + fn test_parse_futures_symbol( + #[case] value: &str, + #[case] root: &str, + #[case] month_code: char, + #[case] year_digits: &str, + ) { + assert_eq!( + parse_futures_symbol(value), + Some(FuturesSymbol { + root, + month_code, + year_digits, + }) + ); + } + + #[rstest] + #[case(1, "ESZ6")] + #[case(2, "ESZ26")] + #[case(4, "ESZ2026")] + fn test_format_futures_symbol(#[case] digits: u8, #[case] expected: &str) { + assert_eq!( + format_futures_symbol("ES", 'Z', 2026, digits).as_deref(), + Some(expected) + ); + } + + #[rstest] + #[case("6", 2026, 2026)] + #[case("5", 2026, 2035)] + #[case("26", 2026, 2026)] + #[case("25", 2026, 2025)] + #[case("96", 2026, 1996)] + #[case("95", 2026, 2095)] + #[case("00", 2099, 2100)] + #[case("01", 2099, 2101)] + #[case("2026", 2099, 2026)] + fn test_resolve_futures_year( + #[case] digits: &str, + #[case] reference_year: i32, + #[case] expected: i32, + ) { + assert_eq!(resolve_futures_year(digits, reference_year), Some(expected)); + } + + #[rstest] + #[case(1, 'F')] + #[case(6, 'M')] + #[case(12, 'Z')] + fn test_futures_month_round_trip(#[case] month: u8, #[case] code: char) { + assert_eq!(futures_month_code(month), Some(code)); + assert_eq!(futures_month(code), Some(month)); + } + + #[rstest] + #[case("")] + #[case("ES")] + #[case("ESZ")] + #[case("ESZ123")] + #[case("Z26")] + fn test_parse_futures_symbol_rejects_invalid_values(#[case] value: &str) { + assert_eq!(parse_futures_symbol(value), None); + } +} diff --git a/crates/adapters/interactive_brokers/src/common/mod.rs b/crates/adapters/interactive_brokers/src/common/mod.rs index e88ec8b55eb7..19df6b07303f 100644 --- a/crates/adapters/interactive_brokers/src/common/mod.rs +++ b/crates/adapters/interactive_brokers/src/common/mod.rs @@ -15,16 +15,16 @@ //! Shared utilities, constants, and data structures for the Interactive Brokers adapter. -pub mod connection; pub mod consts; pub mod contracts; pub mod enums; -pub mod parse; +pub mod futures; pub mod shared_client; -pub mod types; +pub mod spreads; +pub mod symbology; pub use contracts::{ contract_to_json_value, contract_to_params, parse_contract_from_json, parse_contracts_from_json_array, }; -pub use parse::{VENUE_MEMBERS, ib_contract_to_instrument_id_simple, instrument_id_to_ib_contract}; +pub use symbology::{Symbology, VENUE_MEMBERS}; diff --git a/crates/adapters/interactive_brokers/src/common/shared_client.rs b/crates/adapters/interactive_brokers/src/common/shared_client.rs index 7adc259ce725..1b6b6fa67b3a 100644 --- a/crates/adapters/interactive_brokers/src/common/shared_client.rs +++ b/crates/adapters/interactive_brokers/src/common/shared_client.rs @@ -22,31 +22,242 @@ use std::{ collections::HashMap, fmt::Debug, + future::Future, ops::Deref, sync::{Arc, LazyLock}, time::Duration, }; use anyhow::Context; -use ibapi::client::Client; +use ibapi::{Error, client::Client}; use parking_lot::Mutex; /// Key for the connection registry: (host, port, client_id). #[derive(Clone, Debug, Eq, Hash, PartialEq)] struct ConnectionKey(String, u16, i32); -/// Registry entry: shared client and its ref count. -type RegistryMap = HashMap, u32)>; +enum RegistryEntry { + Connected { + client: Arc, + ref_count: u32, + }, + Connecting { + flight: Arc>, + ref_count: u32, + }, +} + +struct InFlight { + result: Mutex, String>>>, + notify: tokio::sync::Notify, +} + +impl Default for InFlight { + fn default() -> Self { + Self { + result: Mutex::new(None), + notify: tokio::sync::Notify::new(), + } + } +} + +impl InFlight { + fn complete(&self, result: Result, String>) { + *self.result.lock() = Some(result); + self.notify.notify_waiters(); + } + + async fn wait(&self) -> anyhow::Result> { + loop { + let notified = self.notify.notified(); + + if let Some(result) = self.result.lock().clone() { + return result.map_err(anyhow::Error::msg); + } + notified.await; + } + } +} + +struct SharedRegistry { + entries: Mutex>>, +} + +impl Default for SharedRegistry { + fn default() -> Self { + Self { + entries: Mutex::new(HashMap::new()), + } + } +} + +impl SharedRegistry { + async fn acquire( + &self, + key: ConnectionKey, + is_connected: IsConnected, + connect: Connect, + ) -> anyhow::Result> + where + Connect: FnOnce() -> ConnectFuture, + ConnectFuture: Future>, + IsConnected: Fn(&T) -> bool, + { + let (flight, should_connect) = { + let mut entries = self.entries.lock(); + + match entries.get_mut(&key) { + Some(RegistryEntry::Connected { client, ref_count }) if is_connected(client) => { + *ref_count = ref_count + .checked_add(1) + .context("Shared IB client reference count overflow")?; + return Ok(Arc::clone(client)); + } + Some(RegistryEntry::Connecting { flight, .. }) => (Arc::clone(flight), false), + Some(RegistryEntry::Connected { .. }) | None => { + let flight = Arc::new(InFlight::default()); + entries.insert( + key.clone(), + RegistryEntry::Connecting { + flight: Arc::clone(&flight), + ref_count: 1, + }, + ); + (flight, true) + } + } + }; + + if !should_connect { + let client = flight.wait().await?; + let mut entries = self.entries.lock(); + match entries.get_mut(&key) { + Some(RegistryEntry::Connected { + client: current, + ref_count, + }) if Arc::ptr_eq(current, &client) => { + *ref_count = ref_count + .checked_add(1) + .context("Shared IB client reference count overflow")?; + } + None => { + entries.insert( + key, + RegistryEntry::Connected { + client: Arc::clone(&client), + ref_count: 1, + }, + ); + } + _ => anyhow::bail!("Shared IB client was replaced while acquisition was waiting"), + } + return Ok(client); + } + + let mut guard = ConnectGuard { + registry: self, + key: key.clone(), + flight: Arc::clone(&flight), + armed: true, + }; + let result = connect().await.map(Arc::new).map_err(|e| format!("{e:#}")); + { + let mut entries = self.entries.lock(); + let ref_count = match entries.get(&key) { + Some(RegistryEntry::Connecting { + flight: current, + ref_count, + }) if Arc::ptr_eq(current, &flight) => *ref_count, + _ => anyhow::bail!("Shared IB client in-flight registry entry was replaced"), + }; + + match &result { + Ok(client) => { + entries.insert( + key.clone(), + RegistryEntry::Connected { + client: Arc::clone(client), + ref_count, + }, + ); + } + Err(_) => { + entries.remove(&key); + } + } + } + flight.complete(result.clone()); + guard.armed = false; + result.map_err(anyhow::Error::msg) + } + + fn release(&self, key: &ConnectionKey, client: &Arc) { + let mut entries = self.entries.lock(); + let Some(RegistryEntry::Connected { + client: registered, + ref_count, + }) = entries.get_mut(key) + else { + return; + }; + + if !Arc::ptr_eq(registered, client) { + return; + } + + *ref_count = ref_count.saturating_sub(1); + if *ref_count == 0 { + entries.remove(key); + } + } + + fn ref_count(&self, key: &ConnectionKey) -> Option { + match self.entries.lock().get(key) { + Some( + RegistryEntry::Connected { ref_count, .. } + | RegistryEntry::Connecting { ref_count, .. }, + ) => Some(*ref_count), + None => None, + } + } +} + +/// Fails the in-flight connect when the winning `acquire` future is dropped mid-connect, +/// so waiters error out and the key can be reacquired instead of hanging forever. +struct ConnectGuard<'a, T> { + registry: &'a SharedRegistry, + key: ConnectionKey, + flight: Arc>, + armed: bool, +} + +impl Drop for ConnectGuard<'_, T> { + fn drop(&mut self) { + if !self.armed { + return; + } + + let mut entries = self.registry.entries.lock(); + if let Some(RegistryEntry::Connecting { flight, .. }) = entries.get(&self.key) + && Arc::ptr_eq(flight, &self.flight) + { + entries.remove(&self.key); + } + drop(entries); + self.flight + .complete(Err(String::from("Shared IB client connect was cancelled"))); + } +} /// Global registry: one shared client per (host, port, client_id) with ref count. -static REGISTRY: LazyLock>> = - LazyLock::new(|| Arc::new(Mutex::new(HashMap::new()))); +static REGISTRY: LazyLock>> = + LazyLock::new(|| Arc::new(SharedRegistry::default())); /// Handle to a shared IB client; when dropped, ref count is decremented and the /// connection is removed from the registry when the count reaches zero. pub struct SharedClientHandle { client: Arc, - registry: Arc>, + registry: Arc>, key: ConnectionKey, } @@ -59,7 +270,7 @@ impl Debug for SharedClientHandle { } impl SharedClientHandle { - fn new(client: Arc, registry: Arc>, key: ConnectionKey) -> Self { + fn new(client: Arc, registry: Arc>, key: ConnectionKey) -> Self { Self { client, registry, @@ -83,19 +294,7 @@ impl Deref for SharedClientHandle { impl Drop for SharedClientHandle { fn drop(&mut self) { - let mut guard = self.registry.lock(); - if let Some((_, ref_count)) = guard.get_mut(&self.key) { - *ref_count = ref_count.saturating_sub(1); - if *ref_count == 0 { - guard.remove(&self.key); - tracing::debug!( - "Shared IB client removed from registry (host={}, port={}, client_id={})", - self.key.0, - self.key.1, - self.key.2 - ); - } - } + self.registry.release(&self.key, &self.client); } } @@ -116,83 +315,336 @@ pub async fn get_or_connect( let registry = Arc::clone(®ISTRY); log::debug!( - "Acquiring shared IB client (host={}, port={}, client_id={}, timeout_secs={})", - host, - port, - client_id, - connection_timeout_secs + "Acquiring shared IB client (host={host}, port={port}, client_id={client_id}, timeout_secs={connection_timeout_secs})" ); - let (reuse_client, ref_count_val) = { - let mut guard = registry.lock(); - - if let Some((client, ref_count)) = guard.get_mut(&key) { - if client.is_connected() { - *ref_count += 1; - let ref_count_val = *ref_count; - let client = Arc::clone(client); - (Some(client), ref_count_val) - } else { - tracing::debug!( - "Removing disconnected shared IB client before reconnect (host={}, port={}, client_id={})", - host, - port, - client_id - ); - guard.remove(&key); - (None, 0) - } - } else { - (None, 0) - } + let address = format!("{host}:{port}"); + let connect_timeout = Duration::from_secs(connection_timeout_secs); + let client = registry + .acquire(key.clone(), Client::is_connected, || async move { + log::debug!( + "Establishing shared IB connection to {address} with timeout {connect_timeout:?}" + ); + tokio::time::timeout(connect_timeout, Client::connect(&address, client_id)) + .await + .map_err(|_| { + anyhow::anyhow!( + "Timed out connecting to IB Gateway/TWS after {connection_timeout_secs}s; verify socket API settings and use a TWS/IB Gateway version supported by the IB integration guide" + ) + })? + .map_err(connection_error) + .context("Failed to connect to IB Gateway/TWS") + }) + .await?; + + if registry.ref_count(&key).is_some_and(|count| count > 1) { + log::debug!("Reusing shared IB client (host={host}, port={port}, client_id={client_id})"); + } + + Ok(SharedClientHandle::new(client, registry, key)) +} + +fn connection_error(e: Error) -> anyhow::Error { + match e { + Error::ServerVersion(required, actual, _) => anyhow::anyhow!( + "TWS/IB Gateway server protocol {actual} is unsupported; server protocol {required} or newer is required. Upgrade TWS/IB Gateway" + ), + e => anyhow::Error::new(e), + } +} + +#[cfg(test)] +mod tests { + use std::{ + sync::atomic::{AtomicUsize, Ordering}, + task::{Context as TaskContext, Waker}, }; - if let Some(client) = reuse_client { - log::debug!( - "Reusing shared IB client (host={}, port={}, client_id={}, ref_count={})", - host, - port, - client_id, - ref_count_val + use rstest::rstest; + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + + use super::*; + + #[derive(Debug)] + struct TestClient; + + #[rstest] + #[tokio::test] + async fn concurrent_acquire_uses_one_connection_and_reserves_both_references() { + let registry = Arc::new(SharedRegistry::default()); + let key = ConnectionKey(String::from("127.0.0.1"), 7_497, 42); + let connect_count = Arc::new(AtomicUsize::new(0)); + let connect_started = Arc::new(tokio::sync::Notify::new()); + let release_connect = Arc::new(tokio::sync::Notify::new()); + + let first = { + let registry = Arc::clone(®istry); + let key = key.clone(); + let connect_count = Arc::clone(&connect_count); + let connect_started = Arc::clone(&connect_started); + let release_connect = Arc::clone(&release_connect); + + tokio::spawn(async move { + registry + .acquire( + key, + |_| true, + || async move { + connect_count.fetch_add(1, Ordering::SeqCst); + connect_started.notify_one(); + release_connect.notified().await; + Ok::<_, anyhow::Error>(TestClient) + }, + ) + .await + }) + }; + + connect_started.notified().await; + + let second = registry.acquire( + key.clone(), + |_| true, + || async { + connect_count.fetch_add(1, Ordering::SeqCst); + anyhow::bail!("second connector must not run") + }, + ); + tokio::pin!(second); + assert!( + second + .as_mut() + .poll(&mut TaskContext::from_waker(Waker::noop())) + .is_pending() ); - return Ok(SharedClientHandle::new(client, registry, key)); + assert_eq!(registry.ref_count(&key), Some(1)); + + release_connect.notify_one(); + + let first_client = first.await.unwrap().unwrap(); + let second_client = second.await.unwrap(); + + assert!(Arc::ptr_eq(&first_client, &second_client)); + assert_eq!(connect_count.load(Ordering::SeqCst), 1); + assert_eq!(registry.ref_count(&key), Some(2)); + + registry.release(&key, &first_client); + assert_eq!(registry.ref_count(&key), Some(1)); + registry.release(&key, &second_client); + assert_eq!(registry.ref_count(&key), None); } - let address = format!("{host}:{port}"); - let connect_timeout = Duration::from_secs(connection_timeout_secs); - log::debug!( - "No shared IB client found, establishing new connection to {} with timeout {:?}", - address, - connect_timeout - ); - let client = tokio::time::timeout(connect_timeout, Client::connect(&address, client_id)) - .await - .map_err(|_| { - anyhow::anyhow!( - "Timed out connecting to IB Gateway/TWS after {}s", - connection_timeout_secs - ) - })? - .context("Failed to connect to IB Gateway/TWS")?; - let client = Arc::new(client); + #[rstest] + #[tokio::test] + async fn cancelled_connect_releases_key_and_fails_waiters() { + let registry = Arc::new(SharedRegistry::default()); + let key = ConnectionKey(String::from("127.0.0.1"), 7_497, 43); + let connect_started = Arc::new(tokio::sync::Notify::new()); - { - let mut guard = registry.lock(); - log::debug!( - "Registering shared IB client in registry (host={}, port={}, client_id={})", - host, - port, - client_id + let winner = { + let registry = Arc::clone(®istry); + let key = key.clone(); + let connect_started = Arc::clone(&connect_started); + + tokio::spawn(async move { + registry + .acquire( + key, + |_| true, + || async move { + connect_started.notify_one(); + std::future::pending::<()>().await; + Ok::<_, anyhow::Error>(TestClient) + }, + ) + .await + }) + }; + + connect_started.notified().await; + + let waiter = registry.acquire( + key.clone(), + |_| true, + || async { anyhow::bail!("waiter must not connect while flight is active") }, + ); + tokio::pin!(waiter); + assert!( + waiter + .as_mut() + .poll(&mut TaskContext::from_waker(Waker::noop())) + .is_pending() ); - guard.insert(key.clone(), (Arc::clone(&client), 1)); + + winner.abort(); + assert!(winner.await.unwrap_err().is_cancelled()); + + let waiter_error = tokio::time::timeout(Duration::from_secs(1), waiter) + .await + .expect("waiter must fail fast after cancellation") + .unwrap_err(); + assert_eq!( + waiter_error.to_string(), + "Shared IB client connect was cancelled" + ); + assert_eq!(registry.ref_count(&key), None); + + let client = registry + .acquire( + key.clone(), + |_| true, + || async move { Ok::<_, anyhow::Error>(TestClient) }, + ) + .await + .unwrap(); + assert_eq!(registry.ref_count(&key), Some(1)); + registry.release(&key, &client); + assert_eq!(registry.ref_count(&key), None); } + #[rstest] + #[case(1, false)] + #[case(3, false)] + #[case(1, true)] + #[tokio::test] + async fn cancelled_waiters_do_not_retain_connection( + #[case] waiter_count: usize, + #[case] cancel_after_connect: bool, + ) { + let registry = SharedRegistry::default(); + let key = ConnectionKey(String::from("127.0.0.1"), 7_497, 44); + let release_connect = tokio::sync::Notify::new(); + let winner = registry.acquire( + key.clone(), + |_| true, + || async { + release_connect.notified().await; + Ok(TestClient) + }, + ); + tokio::pin!(winner); + assert!( + winner + .as_mut() + .poll(&mut TaskContext::from_waker(Waker::noop())) + .is_pending() + ); + let mut waiters = (0..waiter_count) + .map(|_| { + Box::pin(registry.acquire( + key.clone(), + |_| true, + || async { anyhow::bail!("waiter must not connect") }, + )) + }) + .collect::>(); - tracing::debug!( - "Registered new shared IB client (host={}, port={}, client_id={})", - host, - port, - client_id - ); + for waiter in &mut waiters { + assert!( + waiter + .as_mut() + .poll(&mut TaskContext::from_waker(Waker::noop())) + .is_pending() + ); + } - Ok(SharedClientHandle::new(client, registry, key)) + if !cancel_after_connect { + waiters.clear(); + } + release_connect.notify_one(); + let client = winner.await.unwrap(); + waiters.clear(); + assert_eq!(registry.ref_count(&key), Some(1)); + registry.release(&key, &client); + assert_eq!(registry.ref_count(&key), None); + } + + #[rstest] + #[tokio::test] + async fn cancelled_old_waiter_preserves_replacement_connection() { + let registry = SharedRegistry::default(); + let key = ConnectionKey(String::from("127.0.0.1"), 7_497, 45); + let mut winner = Box::pin(registry.acquire( + key.clone(), + |_| true, + || async { std::future::pending::>().await }, + )); + assert!( + winner + .as_mut() + .poll(&mut TaskContext::from_waker(Waker::noop())) + .is_pending() + ); + let mut waiter = Box::pin(registry.acquire( + key.clone(), + |_| true, + || async { anyhow::bail!("waiter must not connect") }, + )); + assert!( + waiter + .as_mut() + .poll(&mut TaskContext::from_waker(Waker::noop())) + .is_pending() + ); + drop(winner); + let replacement = registry + .acquire(key.clone(), |_| true, || async { Ok(TestClient) }) + .await + .unwrap(); + drop(waiter); + assert_eq!(registry.ref_count(&key), Some(1)); + registry.release(&key, &replacement); + assert_eq!(registry.ref_count(&key), None); + } + #[rstest] + #[case(187)] + #[case(212)] + #[tokio::test] + async fn unsupported_server_is_rejected_before_start_api(#[case] server_version: i32) { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = listener.local_addr().unwrap().port(); + + let server = tokio::spawn(async move { + for _ in 0..2 { + let (mut socket, _) = listener.accept().await.unwrap(); + let mut magic = [0_u8; 4]; + socket.read_exact(&mut magic).await.unwrap(); + let size = socket.read_u32().await.unwrap(); + let mut version = vec![0_u8; size as usize]; + socket.read_exact(&mut version).await.unwrap(); + let version = String::from_utf8(version).unwrap(); + let response = format!("{server_version}\020260911 12:00:00 UTC\0"); + socket.write_u32(response.len() as u32).await.unwrap(); + socket.write_all(response.as_bytes()).await.unwrap(); + let mut extra = [0_u8; 1]; + let read = tokio::time::timeout(Duration::from_secs(2), socket.read(&mut extra)) + .await + .unwrap() + .unwrap(); + assert_eq!(magic, *b"API\0"); + assert_eq!(version.split("..").next(), Some("v213")); + assert_eq!( + read, 0, + "unsupported servers must receive no StartApi message" + ); + } + }); + let mut errors = Vec::new(); + for _ in 0..2 { + errors.push( + get_or_connect("127.0.0.1", port, 917, 1) + .await + .unwrap_err() + .to_string(), + ); + } + server.await.unwrap(); + let expected = format!( + "Failed to connect to IB Gateway/TWS: TWS/IB Gateway server protocol {server_version} is unsupported; server protocol 213 or newer is required. Upgrade TWS/IB Gateway" + ); + assert_eq!(errors, vec![expected.clone(), expected]); + assert_eq!( + REGISTRY.ref_count(&ConnectionKey("127.0.0.1".to_string(), port, 917)), + None + ); + } } diff --git a/crates/adapters/interactive_brokers/src/common/spreads.rs b/crates/adapters/interactive_brokers/src/common/spreads.rs new file mode 100644 index 000000000000..af89c71b3a53 --- /dev/null +++ b/crates/adapters/interactive_brokers/src/common/spreads.rs @@ -0,0 +1,101 @@ +// ------------------------------------------------------------------------------------------------- +// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. +// https://nautechsystems.io +// +// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------------------------------- + +//! Generic spread identifier adaptation for Interactive Brokers combo contracts. + +use nautilus_model::identifiers::{ + InstrumentId, new_generic_spread_id, parse_generic_spread_id_legs, +}; + +/// Returns whether an instrument ID uses the generic spread grammar. +#[must_use] +pub fn is_spread_instrument_id(instrument_id: &InstrumentId) -> bool { + parse_generic_spread_id_legs(instrument_id).is_ok() +} + +/// Creates a generic spread instrument ID from IB-compatible leg ratios. +/// +/// # Errors +/// +/// Returns an error if the legs do not satisfy the generic spread grammar. +pub fn create_spread_instrument_id(legs: &[(InstrumentId, i32)]) -> anyhow::Result { + let legs = legs + .iter() + .map(|(instrument_id, ratio)| (*instrument_id, i64::from(*ratio))) + .collect::>(); + new_generic_spread_id(&legs) +} + +/// Parses a generic spread instrument ID into IB-compatible leg ratios. +/// +/// # Errors +/// +/// Returns an error if the ID does not use the generic spread grammar or a ratio is outside the +/// range supported by IB combo legs. +pub fn parse_spread_instrument_id_to_legs( + instrument_id: &InstrumentId, +) -> anyhow::Result> { + parse_generic_spread_id_legs(instrument_id)? + .into_iter() + .map(|(instrument_id, ratio)| { + i32::try_from(ratio) + .map(|ratio| (instrument_id, ratio)) + .map_err(|_| anyhow::anyhow!("Spread leg ratio {ratio} exceeds the IB i32 range")) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use nautilus_model::identifiers::{InstrumentId, new_generic_spread_id}; + use rstest::rstest; + + use super::{ + create_spread_instrument_id, is_spread_instrument_id, parse_spread_instrument_id_to_legs, + }; + + #[rstest] + fn test_spread_adapter_matches_generic_spread_grammar() { + let legs = [ + (InstrumentId::from("MSFT.NASDAQ"), -2), + (InstrumentId::from("AAPL.NASDAQ"), 1), + ]; + let generic_legs = legs + .iter() + .map(|(instrument_id, ratio)| (*instrument_id, i64::from(*ratio))) + .collect::>(); + + let spread = create_spread_instrument_id(&legs).unwrap(); + + assert_eq!(spread, new_generic_spread_id(&generic_legs).unwrap()); + assert_eq!(spread, InstrumentId::from("(1)AAPL___((2))MSFT.NASDAQ")); + assert_eq!( + parse_spread_instrument_id_to_legs(&spread).unwrap(), + [ + (InstrumentId::from("AAPL.NASDAQ"), 1), + (InstrumentId::from("MSFT.NASDAQ"), -2), + ] + ); + assert!(is_spread_instrument_id(&spread)); + } + + #[rstest] + fn test_spread_adapter_rejects_legacy_single_underscore_separator() { + let instrument_id = InstrumentId::from("(1)AAPL_((2))MSFT.NASDAQ"); + + assert!(parse_spread_instrument_id_to_legs(&instrument_id).is_err()); + assert!(!is_spread_instrument_id(&instrument_id)); + } +} diff --git a/crates/adapters/interactive_brokers/src/common/parse.rs b/crates/adapters/interactive_brokers/src/common/symbology.rs similarity index 67% rename from crates/adapters/interactive_brokers/src/common/parse.rs rename to crates/adapters/interactive_brokers/src/common/symbology.rs index a16c11381ab3..39257ca209b8 100644 --- a/crates/adapters/interactive_brokers/src/common/parse.rs +++ b/crates/adapters/interactive_brokers/src/common/symbology.rs @@ -13,15 +13,125 @@ // limitations under the License. // ------------------------------------------------------------------------------------------------- -//! Parsing utilities for converting Interactive Brokers data to Nautilus types. +//! Symbology for converting between Interactive Brokers contracts and Nautilus identifiers. use std::{collections::HashMap, str::FromStr, sync::LazyLock}; use ibapi::contracts::{Contract, Currency, Exchange, OptionRight, SecurityType, Symbol}; -use nautilus_core::UnixNanos; -use nautilus_model::identifiers::{InstrumentId, Symbol as NautilusSymbol, TradeId, Venue}; +use jiff::{Timestamp, tz::Offset}; +use nautilus_model::identifiers::{InstrumentId, Symbol as NautilusSymbol, Venue}; + +use crate::{ + common::{ + enums::{IbOptionRight, IbSecurityType}, + futures::{ + format_futures_symbol, futures_month, futures_month_code, + parse_futures_symbol as parse_futures_code, resolve_futures_year, + }, + }, + config::SymbologyMethod, +}; + +/// Converts between IB contracts and Nautilus instrument IDs using one configured method. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Symbology { + method: SymbologyMethod, + futures_year_digits: u8, +} + +impl Symbology { + #[must_use] + pub const fn new(method: SymbologyMethod) -> Self { + Self { + method, + // Pinned to the venue local symbol; two-digit contract years are not enabled. + futures_year_digits: 1, + } + } + + #[must_use] + pub const fn method(self) -> SymbologyMethod { + self.method + } -use crate::common::enums::{IbOptionRight, IbSecurityType}; + /// Converts an IB contract to a Nautilus instrument ID. + /// + /// # Errors + /// + /// Returns an error if the instrument ID cannot be constructed. + pub fn instrument_id( + self, + contract: &Contract, + venue: Option, + ) -> anyhow::Result { + anyhow::ensure!( + matches!(self.futures_year_digits, 1 | 2), + "`futures_year_digits` must be either 1 or 2" + ); + + let instrument_id = match self.method { + SymbologyMethod::Simplified => { + contract_to_instrument_id_simplified(contract, venue, self.futures_year_digits) + } + SymbologyMethod::Raw => { + contract_to_instrument_id_raw(contract, venue, self.futures_year_digits) + } + }; + Ok(instrument_id) + } + + /// Converts a Nautilus instrument ID to an IB contract. + /// + /// # Errors + /// + /// Returns an error if the identifier format is unsupported. + pub fn contract( + self, + instrument_id: InstrumentId, + exchange: Option<&str>, + ) -> anyhow::Result { + self.contract_with_currency(instrument_id, exchange, None) + } + + pub(crate) fn contract_with_currency( + self, + instrument_id: InstrumentId, + exchange: Option<&str>, + currency: Option<&str>, + ) -> anyhow::Result { + anyhow::ensure!( + matches!(self.futures_year_digits, 1 | 2), + "`futures_year_digits` must be either 1 or 2" + ); + + if instrument_id.venue.as_str() == "OPRA" { + let symbol = instrument_id + .symbol + .as_str() + .strip_suffix("=OPT") + .unwrap_or(instrument_id.symbol.as_str()); + + if !is_canonical_occ_option_symbol(symbol) { + anyhow::bail!("Invalid OPRA option symbol: {symbol}"); + } + } + + let reference_year = i32::from(Offset::UTC.to_datetime(Timestamp::now()).year()); + Ok(instrument_id_to_contract( + instrument_id, + exchange, + self.futures_year_digits, + reference_year, + currency, + )) + } +} + +impl From for Symbology { + fn from(method: SymbologyMethod) -> Self { + Self::new(method) + } +} fn ib_option_right_to_option_right(right: IbOptionRight) -> OptionRight { match right { @@ -30,14 +140,6 @@ fn ib_option_right_to_option_right(right: IbOptionRight) -> OptionRight { } } -/// Generate a unique trade ID for Interactive Brokers trades. -/// -/// This format matches the Python adapter: "{secs}-{price}-{size}" -pub fn generate_ib_trade_id(ts_event: UnixNanos, price: f64, size: f64) -> TradeId { - let ts_secs = ts_event.as_i64() / 1_000_000_000; - TradeId::new(format!("{ts_secs}-{price}-{size}")) -} - /// Convert an IB Contract to an InstrumentId using simplified symbology. /// /// This implements IB_SIMPLIFIED symbology: clean, readable symbols. @@ -47,14 +149,11 @@ pub fn generate_ib_trade_id(ts_event: UnixNanos, price: f64, size: f64) -> Trade /// - FUT: "ESM23" -> "ESM23.GLOBEX" /// - OPT: "AAPL230120C00150000" -> "AAPL230120C00150000.SMART" /// - IND: "SPX" -> "^SPX.SMART" -/// -/// # Errors -/// -/// Returns an error if the instrument ID cannot be constructed. -pub fn ib_contract_to_instrument_id_simplified( +fn contract_to_instrument_id_simplified( contract: &Contract, venue: Option, -) -> anyhow::Result { + futures_year_digits: u8, +) -> InstrumentId { let venue = venue.unwrap_or_else(|| { // For Index and Future, use contract exchange when set (e.g. ESTX50 -> EUREX, FESX -> EUREX). match contract.security_type { @@ -82,8 +181,8 @@ pub fn ib_contract_to_instrument_id_simplified( Venue::from("SMART") } } - SecurityType::CFD => Venue::from("SMART"), - SecurityType::Commodity => Venue::from("SMART"), + SecurityType::CFD => Venue::from("IBCFD"), + SecurityType::Commodity => Venue::from("IBCMDTY"), SecurityType::Bond => Venue::from("SMART"), _ => Venue::from("SMART"), } @@ -137,6 +236,18 @@ pub fn ib_contract_to_instrument_id_simplified( NautilusSymbol::from(symbol_str.as_str()) } SecurityType::Future => { + if futures_year_digits == 2 { + match futures_symbol_from_contract(contract, futures_year_digits) { + Ok(symbol) => { + return InstrumentId::new(NautilusSymbol::from(symbol), venue); + } + Err(e) => tracing::warn!( + "Falling back to legacy futures symbol for local symbol '{}': {e}", + contract.local_symbol + ), + } + } + // FUT: Use localSymbol if available; else symbol + trading_class + expiry (e.g. ESTX50 FESX 20240315). if contract.local_symbol.is_empty() { if !contract.trading_class.is_empty() @@ -161,6 +272,18 @@ pub fn ib_contract_to_instrument_id_simplified( } } SecurityType::FuturesOption => { + if futures_year_digits == 2 { + match futures_symbol_from_contract(contract, futures_year_digits) { + Ok(symbol) => { + return InstrumentId::new(NautilusSymbol::from(symbol), venue); + } + Err(e) => tracing::warn!( + "Falling back to legacy futures option symbol for local symbol '{}': {e}", + contract.local_symbol + ), + } + } + // FOP: Preserve IB local symbol spacing, matching Python simplified symbology. if contract.local_symbol.is_empty() { // Fallback construction @@ -214,7 +337,7 @@ pub fn ib_contract_to_instrument_id_simplified( } }; - Ok(InstrumentId::new(symbol, venue)) + InstrumentId::new(symbol, venue) } /// Convert an IB Contract to an InstrumentId using raw symbology. @@ -224,14 +347,11 @@ pub fn ib_contract_to_instrument_id_simplified( /// - "AAPL=STK.SMART" /// - "EUR.USD=CASH.IDEALPRO" /// - "ESM23=FUT.GLOBEX" -/// -/// # Errors -/// -/// Returns an error if the instrument ID cannot be constructed. -pub fn ib_contract_to_instrument_id_raw( +fn contract_to_instrument_id_raw( contract: &Contract, venue: Option, -) -> anyhow::Result { + futures_year_digits: u8, +) -> InstrumentId { let venue = venue.unwrap_or_else(|| match contract.security_type { SecurityType::ForexPair => Venue::from("IDEALPRO"), SecurityType::Crypto => derive_crypto_venue(contract), @@ -240,16 +360,31 @@ pub fn ib_contract_to_instrument_id_raw( SecurityType::FuturesOption => Venue::from("SMART"), SecurityType::Future => Venue::from("GLOBEX"), SecurityType::Index => Venue::from("SMART"), - SecurityType::CFD => Venue::from("SMART"), - SecurityType::Commodity => Venue::from("SMART"), + SecurityType::CFD => Venue::from("IBCFD"), + SecurityType::Commodity => Venue::from("IBCMDTY"), SecurityType::Bond => Venue::from("SMART"), _ => Venue::from("SMART"), }); - let local_symbol = if contract.local_symbol.is_empty() { - contract.symbol.as_str() + let local_symbol = if futures_year_digits == 2 + && matches!( + contract.security_type, + SecurityType::Future | SecurityType::FuturesOption + ) { + match futures_symbol_from_contract(contract, futures_year_digits) { + Ok(symbol) => symbol, + Err(e) => { + tracing::warn!( + "Falling back to legacy futures symbol for local symbol '{}': {e}", + contract.local_symbol + ); + legacy_raw_local_symbol(contract) + } + } + } else if contract.local_symbol.is_empty() { + contract.symbol.as_str().to_string() } else { - contract.local_symbol.as_str() + contract.local_symbol.as_str().to_string() }; let sec_type_str = IbSecurityType::try_from(&contract.security_type).map_or_else( @@ -259,19 +394,15 @@ pub fn ib_contract_to_instrument_id_raw( let symbol_str = format!("{local_symbol}={sec_type_str}"); let symbol = NautilusSymbol::from(symbol_str.as_str()); - Ok(InstrumentId::new(symbol, venue)) + InstrumentId::new(symbol, venue) } -/// Convert an IB Contract to an InstrumentId (simple version using contract fields). -/// -/// This is a convenience wrapper that uses simplified symbology by default. -/// For more accurate mapping, use the instrument provider which has contract details. -/// -/// # Errors -/// -/// Returns an error if the instrument ID cannot be constructed. -pub fn ib_contract_to_instrument_id_simple(contract: &Contract) -> anyhow::Result { - ib_contract_to_instrument_id_simplified(contract, None) +fn legacy_raw_local_symbol(contract: &Contract) -> String { + if contract.local_symbol.is_empty() { + contract.symbol.as_str().to_string() + } else { + contract.local_symbol.as_str().to_string() + } } /// Venue to IB exchange mappings. @@ -496,7 +627,6 @@ const FUTURES_MONTH_CODES: &[(char, &str)] = &[ ('X', "11"), ('Z', "12"), ]; - /// Determine venue from contract using provider configuration. /// /// This implements the same logic as Python's `determine_venue_from_contract`: @@ -552,10 +682,8 @@ pub fn determine_venue_from_contract( } } - if convert_exchange_to_mic_venue { - if let Some(venue) = exchange_to_mic_venue(&exchange) { - return venue; - } + if convert_exchange_to_mic_venue && let Some(venue) = exchange_to_mic_venue(&exchange) { + return venue; } exchange @@ -584,14 +712,13 @@ pub fn exchange_to_mic_venue(exchange: &str) -> Option { /// - Commodities (CMDTY) /// - Indices (IND) /// - Option Spreads (BAG) - requires contract details map -/// -/// # Errors -/// -/// Returns an error if the conversion fails (e.g., unsupported instrument type, invalid format). -pub fn instrument_id_to_ib_contract( +fn instrument_id_to_contract( instrument_id: InstrumentId, exchange: Option<&str>, -) -> anyhow::Result { + futures_year_digits: u8, + reference_year: i32, + currency: Option<&str>, +) -> Contract { let venue_str = instrument_id.venue.to_string(); let derived_exchange = if venue_str == "OPRA" { "SMART" @@ -615,8 +742,14 @@ pub fn instrument_id_to_ib_contract( let exchange_str = exchange.unwrap_or(derived_exchange); let symbol_str = instrument_id.symbol.as_str(); - if let Some(contract) = instrument_id_to_ib_contract_raw(&instrument_id, exchange) { - return Ok(contract); + if let Some(contract) = instrument_id_to_ib_contract_raw( + &instrument_id, + exchange, + futures_year_digits, + reference_year, + currency, + ) { + return contract; } // Handle spreads (BAG contracts) - requires contract details, so we skip for now @@ -629,52 +762,44 @@ pub fn instrument_id_to_ib_contract( if venue_matches(venue_str.as_str(), VENUES_CASH) && let Some(captures) = parse_cash_symbol(symbol_str) { - return Ok(Contract { - contract_id: 0, + return Contract { symbol: Symbol::from(&captures.base), security_type: SecurityType::ForexPair, exchange: Exchange::from(exchange_str), currency: Currency::from(&captures.quote), local_symbol: format!("{}.{}", captures.base, captures.quote), ..Default::default() - }); + }; } // Handle Crypto if venue_matches(venue_str.as_str(), VENUES_CRYPTO) && let Some(captures) = parse_crypto_symbol(symbol_str) { - return Ok(Contract { - contract_id: 0, + return Contract { symbol: Symbol::from(&captures.base), security_type: SecurityType::Crypto, exchange: Exchange::from(exchange_str), currency: Currency::from(&captures.quote), local_symbol: format!("{}.{}", captures.base, captures.quote), ..Default::default() - }); + }; } // Handle Options (OPT) if is_option_venue(venue_str.as_str()) { if venue_str == "OPRA" { - if !is_canonical_occ_option_symbol(symbol_str) { - anyhow::bail!("Invalid OPRA option symbol: {symbol_str}"); - } - - return Ok(Contract { - contract_id: 0, + return Contract { security_type: SecurityType::Option, exchange: Exchange::from(exchange_str), currency: Currency::from("USD"), local_symbol: symbol_str.to_string(), ..Default::default() - }); + }; } if let Some(opt) = parse_option_symbol(symbol_str) { - return Ok(Contract { - contract_id: 0, + return Contract { symbol: Symbol::from(&opt.symbol), security_type: SecurityType::Option, exchange: Exchange::from(exchange_str), @@ -684,12 +809,11 @@ pub fn instrument_id_to_ib_contract( strike: opt.strike_value, right: Some(opt.right), ..Default::default() - }); + }; } if let Some(opt) = parse_named_option_symbol(symbol_str) { - return Ok(Contract { - contract_id: 0, + return Contract { symbol: Symbol::from(&opt.trading_class), security_type: SecurityType::Option, exchange: Exchange::from(exchange_str), @@ -699,15 +823,14 @@ pub fn instrument_id_to_ib_contract( strike: opt.strike_value, right: Some(opt.right), ..Default::default() - }); + }; } } // Handle Futures and Futures Options if venue_matches(venue_str.as_str(), VENUES_FUT) { if let Some(fut) = parse_named_futures_symbol(symbol_str) { - return Ok(Contract { - contract_id: 0, + return Contract { symbol: Symbol::from(&fut.underlying), security_type: SecurityType::Future, exchange: Exchange::from(exchange_str), @@ -715,44 +838,65 @@ pub fn instrument_id_to_ib_contract( trading_class: fut.trading_class, last_trade_date_or_contract_month: fut.expiry, ..Default::default() - }); + }; } // Check for continuous futures (underlying only, no expiry) // IB uses FUT with no expiry date to represent continuous futures - if let Some(underlying) = parse_futures_underlying(symbol_str) { - return Ok(Contract { - contract_id: 0, - symbol: Symbol::from(&underlying), + if is_futures_underlying(symbol_str) { + return Contract { + symbol: Symbol::from(symbol_str), security_type: SecurityType::ContinuousFuture, exchange: Exchange::from(exchange_str), currency: Currency::from("USD"), // Will be resolved from contract details ..Default::default() - }); + }; + } + + if futures_year_digits == 2 + && let Some(contract) = futures_contract_from_symbol( + symbol_str, + SecurityType::FuturesOption, + exchange_str, + reference_year, + currency, + ) + { + return contract; } // Check for Futures Options (FOP) - if let Some(local_symbol) = parse_futures_option_symbol(symbol_str) { - return Ok(Contract { - contract_id: 0, + if validate_futures_option_symbol(symbol_str).is_some() { + return Contract { security_type: SecurityType::FuturesOption, exchange: Exchange::from(exchange_str), currency: Currency::from("USD"), - local_symbol, + local_symbol: symbol_str.to_string(), ..Default::default() - }); + }; + } + + if futures_year_digits == 2 + && let Some(contract) = futures_contract_from_symbol( + symbol_str, + SecurityType::Future, + exchange_str, + reference_year, + currency, + ) + { + return contract; } // Check for regular Futures (FUT) - if let Some(fut) = parse_futures_symbol(symbol_str) { - return Ok(Contract { - contract_id: 0, + if parse_futures_month_and_year(symbol_str).is_some() { + return Contract { security_type: SecurityType::Future, exchange: Exchange::from(exchange_str), currency: Currency::from("USD"), - local_symbol: fut.local_symbol, + local_symbol: symbol_str.to_string(), ..Default::default() - }); + }; } } @@ -761,71 +905,69 @@ pub fn instrument_id_to_ib_contract( if let Some(captures) = parse_cash_symbol(symbol_str).or_else(|| parse_cfd_cash_symbol(symbol_str)) { - return Ok(Contract { - contract_id: 0, + return Contract { symbol: Symbol::from(&captures.base), security_type: SecurityType::CFD, exchange: Exchange::from("SMART"), currency: Currency::from(&captures.quote), local_symbol: format!("{}.{}", captures.base, captures.quote), ..Default::default() - }); + }; } else { // CFD with space-separated symbol let symbol_clean = symbol_str.replace('-', " "); - return Ok(Contract { - contract_id: 0, + return Contract { symbol: Symbol::from(&symbol_clean), security_type: SecurityType::CFD, exchange: Exchange::from("SMART"), - currency: Currency::from("USD"), + currency: Currency::from(""), ..Default::default() - }); + }; } } // Handle Commodities if VENUES_CMDTY.contains(&venue_str.as_str()) { let symbol_clean = symbol_str.replace('-', " "); - return Ok(Contract { - contract_id: 0, + return Contract { symbol: Symbol::from(&symbol_clean), security_type: SecurityType::Commodity, exchange: Exchange::from("SMART"), - currency: Currency::from("USD"), + currency: Currency::from(""), ..Default::default() - }); + }; } // Handle Indices (symbols starting with ^) if let Some(local_symbol) = symbol_str.strip_prefix('^') { - return Ok(Contract { - contract_id: 0, + return Contract { symbol: Symbol::from(local_symbol), security_type: SecurityType::Index, exchange: Exchange::from(exchange_str), currency: Currency::from("USD"), local_symbol: local_symbol.into(), ..Default::default() - }); + }; } // Default to Stock (STK) let symbol_clean = symbol_str.replace('-', " "); - Ok(Contract { - contract_id: 0, + Contract { symbol: Symbol::from(&symbol_clean), security_type: SecurityType::Stock, exchange: Exchange::from("SMART"), currency: Currency::from(""), // Will be resolved from contract details primary_exchange: Exchange::from(exchange_str), ..Default::default() - }) + } } fn instrument_id_to_ib_contract_raw( instrument_id: &InstrumentId, exchange: Option<&str>, + futures_year_digits: u8, + reference_year: i32, + currency: Option<&str>, ) -> Option { let (local_symbol, sec_type_code) = instrument_id.symbol.as_str().rsplit_once('=')?; @@ -841,9 +983,24 @@ fn instrument_id_to_ib_contract_raw( }; let exchange_str = exchange.unwrap_or(default_exchange); + if futures_year_digits == 2 + && matches!( + security_type, + SecurityType::Future | SecurityType::FuturesOption + ) + && let Some(contract) = futures_contract_from_symbol( + local_symbol, + security_type.clone(), + exchange_str, + reference_year, + currency, + ) + { + return Some(contract); + } + let contract = match security_type { SecurityType::Stock => Contract { - contract_id: 0, security_type, exchange: Exchange::from("SMART"), primary_exchange: Exchange::from(exchange_str), @@ -851,21 +1008,19 @@ fn instrument_id_to_ib_contract_raw( ..Default::default() }, SecurityType::CFD | SecurityType::Commodity => Contract { - contract_id: 0, security_type, exchange: Exchange::from("SMART"), + currency: Currency::from(""), local_symbol: local_symbol.to_string(), ..Default::default() }, SecurityType::Index => Contract { - contract_id: 0, security_type, exchange: Exchange::from(exchange_str), local_symbol: local_symbol.to_string(), ..Default::default() }, _ => Contract { - contract_id: 0, security_type, exchange: Exchange::from(exchange_str), local_symbol: local_symbol.to_string(), @@ -1055,220 +1210,133 @@ fn format_option_strike(strike: f64) -> String { } } -/// Futures symbol captures -struct FuturesSymbol { - local_symbol: String, -} - -/// Parse futures underlying (continuous) - just the symbol without expiry -fn parse_futures_underlying(symbol: &str) -> Option { - // If it's just 1-3 characters, it's likely an underlying - if symbol.len() <= 3 && symbol.chars().all(|c| c.is_alphabetic()) { - Some(symbol.to_string()) +fn futures_symbol_from_contract( + contract: &Contract, + futures_year_digits: u8, +) -> anyhow::Result { + let expiry = contract.last_trade_date_or_contract_month.as_str(); + let year = expiry + .get(..4) + .filter(|value| value.bytes().all(|b| b.is_ascii_digit())) + .and_then(|value| value.parse::().ok()) + .ok_or_else(|| anyhow::anyhow!("Invalid futures contract month '{expiry}'"))?; + + let (futures_symbol, suffix) = if contract.security_type == SecurityType::FuturesOption { + contract + .local_symbol + .split_once(' ') + .map(|(symbol, suffix)| (symbol, Some(suffix))) + .ok_or_else(|| { + anyhow::anyhow!( + "Invalid futures option local symbol '{}'", + contract.local_symbol + ) + })? + } else if contract.local_symbol.is_empty() { + let month = expiry + .get(4..6) + .and_then(|value| value.parse::().ok()) + .and_then(futures_month_code) + .ok_or_else(|| anyhow::anyhow!("Invalid futures contract month '{expiry}'"))?; + let formatted = + format_futures_symbol(contract.symbol.as_str(), month, year, futures_year_digits) + .ok_or_else(|| { + anyhow::anyhow!("Invalid futures year digit count {futures_year_digits}") + })?; + return Ok(formatted); } else { - None - } -} + (contract.local_symbol.as_str(), None) + }; -fn is_futures_month_code(ch: char) -> bool { - matches!( - ch, - 'F' | 'G' | 'H' | 'J' | 'K' | 'M' | 'N' | 'Q' | 'U' | 'V' | 'X' | 'Z' - ) + let parsed = parse_futures_code(futures_symbol) + .ok_or_else(|| anyhow::anyhow!("Invalid futures local symbol '{futures_symbol}'"))?; + // The expiry field carries the last-trade date on details responses, which can fall in + // the year before the contract month (January contracts such as CLF6 trading last in + // December). Resolving the symbol's own year digits against that year is exact because + // the contract-month year is never earlier than the last-trade year. + let year = resolve_futures_year(parsed.year_digits, year) + .ok_or_else(|| anyhow::anyhow!("Invalid futures year digits '{}'", parsed.year_digits))?; + let formatted = + format_futures_symbol(parsed.root, parsed.month_code, year, futures_year_digits) + .ok_or_else(|| { + anyhow::anyhow!("Invalid futures year digit count {futures_year_digits}") + })?; + + Ok(suffix.map_or(formatted.clone(), |suffix| format!("{formatted} {suffix}"))) } -fn parse_futures_month_and_year(symbol: &str) -> Option<(usize, char, String)> { - for (month_pos, month_char) in symbol.char_indices().rev() { - if !is_futures_month_code(month_char) { - continue; - } - - let remaining = &symbol[month_pos + month_char.len_utf8()..]; - if remaining.is_empty() || !remaining.chars().all(|ch| ch.is_ascii_digit()) { - continue; - } - - let year = match remaining.len() { - 1 | 2 => remaining.to_string(), - 4 => remaining[remaining.len() - 2..].to_string(), - _ => continue, - }; - - if month_pos == 0 { - continue; - } +fn futures_contract_from_symbol( + symbol: &str, + security_type: SecurityType, + exchange: &str, + reference_year: i32, + currency: Option<&str>, +) -> Option { + let (futures_symbol, option) = if security_type == SecurityType::FuturesOption { + let (futures_symbol, rest) = symbol.split_once(' ')?; + let mut chars = rest.chars(); + let right_char = chars.next()?; + let right = IbOptionRight::from_str(&right_char.to_string()).ok()?; + let strike = chars.as_str().parse::().ok()?; + (futures_symbol, Some((right, strike))) + } else { + (symbol, None) + }; + let parsed = parse_futures_code(futures_symbol)?; + let year = resolve_futures_year(parsed.year_digits, reference_year)?; + let month = futures_month(parsed.month_code)?; + let currency = currency.unwrap_or_else(|| { + log::warn!("Falling back to USD for unresolved IB futures contract {symbol} on {exchange}"); + "USD" + }); - return Some((month_pos, month_char, year)); - } + Some(Contract { + symbol: Symbol::from(parsed.root), + trading_class: parsed.root.to_string(), + security_type, + exchange: Exchange::from(exchange), + currency: Currency::from(currency), + last_trade_date_or_contract_month: format!("{year:04}{month:02}"), + right: option.map(|(right, _)| ib_option_right_to_option_right(right)), + strike: option.map_or(0.0, |(_, strike)| strike), + ..Default::default() + }) +} - None +fn is_futures_underlying(symbol: &str) -> bool { + symbol.len() <= 3 && symbol.chars().all(char::is_alphabetic) } -/// Parse futures symbol like "YMM6", "ESM23", or "ESM2023" -fn parse_futures_symbol(symbol: &str) -> Option { - parse_futures_month_and_year(symbol).map(|_| FuturesSymbol { - local_symbol: symbol.to_string(), - }) +fn parse_futures_month_and_year(symbol: &str) -> Option<(usize, char, String)> { + let parsed = parse_futures_code(symbol)?; + Some(( + parsed.root.len(), + parsed.month_code, + parsed.year_digits.to_string(), + )) } -/// Parse futures option symbol like "YMM6 C4500", "ESM23 C4500", or "ESM2023 C4500" -fn parse_futures_option_symbol(symbol: &str) -> Option { +fn validate_futures_option_symbol(symbol: &str) -> Option<()> { let (futures_symbol, rest) = symbol.split_once(' ')?; - let (month_pos, _, _) = parse_futures_month_and_year(futures_symbol)?; - let _fut_symbol = &futures_symbol[..month_pos]; + parse_futures_month_and_year(futures_symbol)?; - // Parse right and strike let right_char = rest.chars().next()?; IbOptionRight::from_str(&right_char.to_string()).ok()?; let strike_str = &rest[1..]; strike_str.parse::().ok()?; - Some(symbol.to_string()) -} - -/// Check if an instrument ID represents a spread. -/// -/// This checks if the symbol contains the spread format pattern: `(ratio)symbol_` or `((ratio))symbol_` -#[must_use] -pub fn is_spread_instrument_id(instrument_id: &InstrumentId) -> bool { - let symbol_str = instrument_id.symbol.as_str(); - // Check if symbol contains spread pattern: (ratio) or ((ratio)) - symbol_str.contains('(') && symbol_str.contains('_') -} - -/// Create a spread instrument ID from leg tuples. -/// -/// This implements the same logic as Python's `InstrumentId.new_spread`: -/// - Creates a symbol string like `(1)SYMBOL1_((2))SYMBOL2` -/// - Positive ratios: `(ratio)SYMBOL` -/// - Negative ratios: `((abs(ratio)))SYMBOL` -/// - Sorts legs alphabetically by symbol -/// - All legs must have the same venue -/// -/// # Errors -/// -/// Returns an error if: -/// - Less than 2 legs provided -/// - Any ratio is zero -/// - Venues don't match across legs -pub fn create_spread_instrument_id( - leg_tuples: &[(InstrumentId, i32)], -) -> anyhow::Result { - if leg_tuples.len() < 2 { - anyhow::bail!("instrument_ratios list needs to have at least 2 legs"); - } - - let first_venue = leg_tuples[0].0.venue; - - for (instrument_id, ratio) in leg_tuples { - if *ratio == 0 { - anyhow::bail!("ratio cannot be zero"); - } - - if instrument_id.venue != first_venue { - anyhow::bail!( - "All venues must match. Expected {}, was {}", - first_venue, - instrument_id.venue - ); - } - } - - let mut sorted_ratios = leg_tuples.to_vec(); - sorted_ratios.sort_by(|a, b| a.0.symbol.as_str().cmp(b.0.symbol.as_str())); - - let symbol_parts = sorted_ratios - .iter() - .map(|(instrument_id, ratio)| { - if *ratio > 0 { - format!("({}){}", ratio, instrument_id.symbol.as_str()) - } else { - format!("(({})){}", ratio.abs(), instrument_id.symbol.as_str()) - } - }) - .collect::>(); - - let composite_symbol = symbol_parts.join("_"); - let symbol = NautilusSymbol::from(composite_symbol.as_str()); - - Ok(InstrumentId::new(symbol, first_venue)) -} - -/// Parse a spread instrument ID back into leg tuples. -/// -/// This implements the same logic as Python's `InstrumentId.to_list()`: -/// - Parses symbol string like `(1)SYMBOL1_((2))SYMBOL2` -/// - Positive ratios: `(ratio)SYMBOL` -/// - Negative ratios: `((abs(ratio)))SYMBOL` -/// - Returns sorted list of (instrument_id, ratio) tuples -/// -/// # Errors -/// -/// Returns an error if the symbol format is invalid. -pub fn parse_spread_instrument_id_to_legs( - instrument_id: &InstrumentId, -) -> anyhow::Result> { - let symbol_str = instrument_id.symbol.as_str(); - let venue = instrument_id.venue; - - let components: Vec<&str> = symbol_str.split('_').collect(); - let mut result = Vec::new(); - - for component in components { - if component.is_empty() { - continue; - } - - // Check for negative ratio: ((ratio))symbol - if let Some(rest) = component.strip_prefix("((") - && let Some(pos) = rest.find("))") - { - let ratio_str = &rest[..pos]; - let symbol_value = &rest[pos + 2..]; - - if let Ok(ratio) = ratio_str.parse::() { - let leg_instrument_id = - InstrumentId::new(NautilusSymbol::from(symbol_value), venue); - result.push((leg_instrument_id, -ratio)); - continue; - } - } - - // Check for positive ratio: (ratio)symbol - if let Some(rest) = component.strip_prefix('(') - && let Some(pos) = rest.find(')') - { - let ratio_str = &rest[..pos]; - let symbol_value = &rest[pos + 1..]; - - if let Ok(ratio) = ratio_str.parse::() { - let leg_instrument_id = - InstrumentId::new(NautilusSymbol::from(symbol_value), venue); - result.push((leg_instrument_id, ratio)); - continue; - } - } - - anyhow::bail!("Invalid spread symbol format for component: {component}"); - } - - // Sort result alphabetically by symbol - result.sort_by(|a, b| a.0.symbol.as_str().cmp(b.0.symbol.as_str())); - - Ok(result) + Some(()) } #[cfg(test)] mod tests { use ibapi::contracts::{Contract, Currency, Exchange, OptionRight, SecurityType, Symbol}; - use nautilus_model::identifiers::InstrumentId; + use nautilus_model::identifiers::{InstrumentId, Symbol as NautilusSymbol, Venue}; use rstest::rstest; - use super::{ - exchange_to_mic_venue, ib_contract_to_instrument_id_simplified, - instrument_id_to_ib_contract, possible_exchanges_for_venue, - }; + use super::{Symbology, exchange_to_mic_venue, possible_exchanges_for_venue}; + use crate::config::SymbologyMethod; #[rstest] fn test_ib_contract_to_instrument_id_simplified_normalizes_occ_option_root() { @@ -1284,7 +1352,9 @@ mod tests { ..Default::default() }; - let instrument_id = ib_contract_to_instrument_id_simplified(&contract, None).unwrap(); + let instrument_id = Symbology::new(SymbologyMethod::Simplified) + .instrument_id(&contract, None) + .unwrap(); assert_eq!( instrument_id, @@ -1307,7 +1377,9 @@ mod tests { ..Default::default() }; - let instrument_id = ib_contract_to_instrument_id_simplified(&contract, None).unwrap(); + let instrument_id = Symbology::new(SymbologyMethod::Simplified) + .instrument_id(&contract, None) + .unwrap(); assert_eq!( instrument_id, @@ -1326,16 +1398,147 @@ mod tests { ..Default::default() }; - let instrument_id = ib_contract_to_instrument_id_simplified(&contract, None).unwrap(); + let instrument_id = Symbology::new(SymbologyMethod::Simplified) + .instrument_id(&contract, None) + .unwrap(); assert_eq!(instrument_id, InstrumentId::from("EX2G3 P4080.NYBOT")); } + #[rstest] + #[case(SecurityType::Future, "ESZ6", "ESZ6.XCME")] + #[case(SecurityType::FuturesOption, "ESZ6 P5000", "ESZ6 P5000.XCME")] + fn test_ib_contract_to_instrument_id_keeps_the_local_symbol_year( + #[case] security_type: SecurityType, + #[case] local_symbol: &str, + #[case] expected: &str, + ) { + let contract = Contract { + symbol: Symbol::from("ES"), + security_type, + exchange: Exchange::from("CME"), + currency: Currency::from("USD"), + local_symbol: local_symbol.to_string(), + last_trade_date_or_contract_month: "202612".to_string(), + ..Default::default() + }; + + let instrument_id = Symbology::new(SymbologyMethod::Simplified) + .instrument_id(&contract, Some(Venue::from("XCME"))) + .unwrap(); + + assert_eq!(instrument_id, InstrumentId::from(expected)); + } + + #[rstest] + #[case(SecurityType::Future, "CLF6", "20251219", "CLF6.XNYM")] + #[case(SecurityType::Future, "ESZ6", "20261218", "ESZ6.XNYM")] + #[case( + SecurityType::FuturesOption, + "CLF6 P7000", + "20251216", + "CLF6 P7000.XNYM" + )] + fn test_local_symbol_year_is_not_rewritten_from_last_trade_date( + #[case] security_type: SecurityType, + #[case] local_symbol: &str, + #[case] last_trade_date: &str, + #[case] expected: &str, + ) { + let contract = Contract { + symbol: Symbol::from("CL"), + security_type, + exchange: Exchange::from("NYMEX"), + currency: Currency::from("USD"), + local_symbol: local_symbol.to_string(), + last_trade_date_or_contract_month: last_trade_date.to_string(), + ..Default::default() + }; + + let instrument_id = Symbology::new(SymbologyMethod::Simplified) + .instrument_id(&contract, Some(Venue::from("XNYM"))) + .unwrap(); + + assert_eq!(instrument_id, InstrumentId::from(expected)); + } + + #[rstest] + fn test_non_grammar_local_symbol_is_emitted_verbatim() { + let contract = Contract { + symbol: Symbol::from("ZC"), + security_type: SecurityType::Future, + exchange: Exchange::from("CBOT"), + currency: Currency::from("USD"), + local_symbol: "ZC DEC 26".to_string(), + last_trade_date_or_contract_month: "20261214".to_string(), + ..Default::default() + }; + + let simplified = Symbology::new(SymbologyMethod::Simplified) + .instrument_id(&contract, Some(Venue::from("XCBT"))) + .unwrap(); + let raw = Symbology::new(SymbologyMethod::Raw) + .instrument_id(&contract, Some(Venue::from("XCBT"))) + .unwrap(); + + assert_eq!(simplified, InstrumentId::from("ZC DEC 26.XCBT")); + assert_eq!(raw, InstrumentId::from("ZC DEC 26=FUT.XCBT")); + } + + #[rstest] + fn test_fop_without_local_symbol_falls_back_to_contract_fields() { + let contract = Contract { + symbol: Symbol::from("ES"), + security_type: SecurityType::FuturesOption, + exchange: Exchange::from("CME"), + currency: Currency::from("USD"), + local_symbol: String::new(), + last_trade_date_or_contract_month: "20261218".to_string(), + right: Some(OptionRight::Put), + strike: 5000.0, + ..Default::default() + }; + + let instrument_id = Symbology::new(SymbologyMethod::Simplified) + .instrument_id(&contract, Some(Venue::from("XCME"))) + .unwrap(); + + assert_eq!(instrument_id, InstrumentId::from("ES20261218 P5000.XCME")); + } + + #[rstest] + #[case("ESZ6", SecurityType::Future)] + #[case("ESZ26", SecurityType::Future)] + #[case("ESZ2026", SecurityType::Future)] + #[case("ESZ26 P5000", SecurityType::FuturesOption)] + fn test_futures_id_keeps_its_symbol_as_the_contract_local_symbol_outbound( + #[case] symbol: &str, + #[case] security_type: SecurityType, + ) { + let instrument_id = InstrumentId::new(NautilusSymbol::from(symbol), Venue::from("XCME")); + + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); + + assert_eq!(contract.security_type, security_type); + assert_eq!(contract.local_symbol.as_str(), symbol); + assert_eq!(contract.exchange.as_str(), "CME"); + assert_eq!(contract.currency.as_str(), "USD"); + assert!(contract.symbol.as_str().is_empty()); + assert!(contract.trading_class.is_empty()); + assert!(contract.last_trade_date_or_contract_month.is_empty()); + assert_eq!(contract.right, None); + assert_eq!(contract.strike, 0.0); + } + #[rstest] fn test_instrument_id_to_ib_contract_parses_named_option_symbol() { let instrument_id = InstrumentId::from("C OESX 20260213 4775.EUREX"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Option); assert_eq!(contract.exchange.as_str(), "EUREX"); @@ -1353,7 +1556,9 @@ mod tests { fn test_instrument_id_to_ib_contract_preserves_occ_local_symbol() { let instrument_id = InstrumentId::from("AAPL 230217P00155000.SMART"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Option); assert_eq!(contract.exchange.as_str(), "SMART"); @@ -1379,7 +1584,9 @@ mod tests { fn test_opra_occ_option_default_contract_routes_to_smart() { let instrument_id = InstrumentId::from("SPY 240319P00511000.OPRA"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Option); assert_eq!(contract.exchange.as_str(), "SMART"); @@ -1398,9 +1605,9 @@ mod tests { assert_eq!(exchanges, vec!["SMART".to_string()]); - let contract = - instrument_id_to_ib_contract(instrument_id, exchanges.first().map(String::as_str)) - .unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, exchanges.first().map(String::as_str)) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Option); assert_eq!(contract.exchange.as_str(), "SMART"); @@ -1416,7 +1623,9 @@ mod tests { fn test_opra_raw_option_default_contract_routes_to_smart() { let instrument_id = InstrumentId::from("SPY 240319P00511000=OPT.OPRA"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Raw) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Option); assert_eq!(contract.exchange.as_str(), "SMART"); @@ -1429,7 +1638,14 @@ mod tests { fn test_opra_option_respects_exchange_override(#[case] value: &str) { let instrument_id = InstrumentId::from(value); - let contract = instrument_id_to_ib_contract(instrument_id, Some("CBOE")).unwrap(); + let method = if value.contains("=OPT") { + SymbologyMethod::Raw + } else { + SymbologyMethod::Simplified + }; + let contract = Symbology::new(method) + .contract(instrument_id, Some("CBOE")) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Option); assert_eq!(contract.exchange.as_str(), "CBOE"); @@ -1442,7 +1658,7 @@ mod tests { fn test_invalid_opra_occ_option_returns_error(#[case] value: &str) { let instrument_id = InstrumentId::from(value); - let result = instrument_id_to_ib_contract(instrument_id, None); + let result = Symbology::new(SymbologyMethod::Simplified).contract(instrument_id, None); assert!(result.is_err()); } @@ -1456,7 +1672,9 @@ mod tests { fn test_instrument_id_to_ib_contract_parses_named_futures_symbol() { let instrument_id = InstrumentId::from("ESTX50 FESX 20240315.EUREX"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Future); assert_eq!(contract.exchange.as_str(), "EUREX"); @@ -1472,7 +1690,9 @@ mod tests { fn test_instrument_id_to_ib_contract_maps_xcbt_to_cbot_exchange() { let instrument_id = InstrumentId::from("YMM6.XCBT"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Future); assert_eq!(contract.exchange.as_str(), "CBOT"); @@ -1485,29 +1705,41 @@ mod tests { fn test_instrument_id_to_ib_contract_maps_mic_future_to_member_exchange() { let instrument_id = InstrumentId::from("OESXH6.XEUR"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Future); assert_eq!(contract.exchange.as_str(), "DTB"); assert_eq!(contract.local_symbol.as_str(), "OESXH6"); + assert!(contract.symbol.as_str().is_empty()); + assert!(contract.last_trade_date_or_contract_month.is_empty()); } #[rstest] fn test_instrument_id_to_ib_contract_parses_futures_option_with_month_code_in_symbol() { let instrument_id = InstrumentId::from("YMM6 C45000.XCBT"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::FuturesOption); assert_eq!(contract.exchange.as_str(), "CBOT"); assert_eq!(contract.local_symbol.as_str(), "YMM6 C45000"); + assert!(contract.symbol.as_str().is_empty()); + assert!(contract.last_trade_date_or_contract_month.is_empty()); + assert_eq!(contract.right, None); + assert_eq!(contract.strike, 0.0); } #[rstest] fn test_instrument_id_to_ib_contract_parses_ibcfd_cash_symbol() { let instrument_id = InstrumentId::from("EUR/USD.IBCFD"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::CFD); assert_eq!(contract.exchange.as_str(), "SMART"); @@ -1516,11 +1748,70 @@ mod tests { assert_eq!(contract.local_symbol.as_str(), "EUR.USD"); } + #[rstest] + #[case( + SymbologyMethod::Simplified, + SecurityType::CFD, + "IBDE30", + "EUR", + "IBDE30.IBCFD" + )] + #[case( + SymbologyMethod::Raw, + SecurityType::CFD, + "IBDE30", + "EUR", + "IBDE30=CFD.IBCFD" + )] + #[case( + SymbologyMethod::Simplified, + SecurityType::Commodity, + "XAUUSD", + "GBP", + "XAUUSD.IBCMDTY" + )] + #[case( + SymbologyMethod::Raw, + SecurityType::Commodity, + "XAUUSD", + "GBP", + "XAUUSD=CMDTY.IBCMDTY" + )] + fn test_non_cash_pseudo_venue_round_trip_defers_currency_to_contract_details( + #[case] method: SymbologyMethod, + #[case] security_type: SecurityType, + #[case] symbol: &str, + #[case] currency: &str, + #[case] expected_instrument_id: &str, + ) { + let contract = Contract { + symbol: Symbol::from(symbol), + security_type: security_type.clone(), + exchange: Exchange::from("SMART"), + currency: Currency::from(currency), + local_symbol: symbol.to_string(), + ..Default::default() + }; + let symbology = Symbology::new(method); + + let instrument_id = symbology.instrument_id(&contract, None).unwrap(); + let unresolved = symbology.contract(instrument_id, None).unwrap(); + let round_trip_id = symbology.instrument_id(&unresolved, None).unwrap(); + + assert_eq!(instrument_id, InstrumentId::from(expected_instrument_id)); + assert_eq!(unresolved.security_type, security_type); + assert_eq!(unresolved.exchange.as_str(), "SMART"); + assert!(unresolved.currency.as_str().is_empty()); + assert_eq!(round_trip_id, instrument_id); + } + #[rstest] fn test_instrument_id_to_ib_contract_parses_long_crypto_symbol() { let instrument_id = InstrumentId::from("DOGE/USD.PAXOS"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Crypto); assert_eq!(contract.exchange.as_str(), "PAXOS"); @@ -1534,7 +1825,9 @@ mod tests { // ZEROHASH is one of IB's crypto venues (alongside PAXOS). let instrument_id = InstrumentId::from("BTC/USD.ZEROHASH"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Crypto); assert_eq!(contract.exchange.as_str(), "ZEROHASH"); @@ -1555,7 +1848,9 @@ mod tests { ..Default::default() }; - let instrument_id = ib_contract_to_instrument_id_simplified(&contract, None).unwrap(); + let instrument_id = Symbology::new(SymbologyMethod::Simplified) + .instrument_id(&contract, None) + .unwrap(); assert_eq!(instrument_id, InstrumentId::from("BTC/USD.ZEROHASH")); } @@ -1572,7 +1867,9 @@ mod tests { ..Default::default() }; - let instrument_id = ib_contract_to_instrument_id_simplified(&contract, None).unwrap(); + let instrument_id = Symbology::new(SymbologyMethod::Simplified) + .instrument_id(&contract, None) + .unwrap(); assert_eq!(instrument_id, InstrumentId::from("DOGE/USD.PAXOS")); } @@ -1582,7 +1879,9 @@ mod tests { // PAXOS remains a live IB crypto venue alongside ZEROHASH. let instrument_id = InstrumentId::from("BTC/USD.PAXOS"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Crypto); assert_eq!(contract.exchange.as_str(), "PAXOS"); @@ -1604,7 +1903,9 @@ mod tests { ..Default::default() }; - let instrument_id = ib_contract_to_instrument_id_simplified(&contract, None).unwrap(); + let instrument_id = Symbology::new(SymbologyMethod::Simplified) + .instrument_id(&contract, None) + .unwrap(); assert_eq!(instrument_id, InstrumentId::from("BTC/USD.PAXOS")); } @@ -1613,7 +1914,9 @@ mod tests { fn test_instrument_id_to_ib_contract_uses_contfut_for_underlying() { let instrument_id = InstrumentId::from("ES.XCME"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::ContinuousFuture); assert_eq!(contract.exchange.as_str(), "CME"); @@ -1624,7 +1927,9 @@ mod tests { fn test_instrument_id_to_ib_contract_parses_raw_stock_symbol() { let instrument_id = InstrumentId::from("AAPL=STK.NASDAQ"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Stock); assert_eq!(contract.exchange.as_str(), "SMART"); @@ -1637,7 +1942,9 @@ mod tests { fn test_instrument_id_to_ib_contract_parses_raw_forex_symbol() { let instrument_id = InstrumentId::from("EUR.USD=CASH.IDEALPRO"); - let contract = instrument_id_to_ib_contract(instrument_id, None).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, None) + .unwrap(); assert_eq!(contract.security_type, SecurityType::ForexPair); assert_eq!(contract.exchange.as_str(), "IDEALPRO"); @@ -1649,18 +1956,24 @@ mod tests { fn test_instrument_id_to_ib_contract_raw_respects_exchange_override() { let instrument_id = InstrumentId::from("YMM6=FUT.XCBT"); - let contract = instrument_id_to_ib_contract(instrument_id, Some("CBOT")).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, Some("CBOT")) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Future); assert_eq!(contract.exchange.as_str(), "CBOT"); assert_eq!(contract.local_symbol.as_str(), "YMM6"); + assert!(contract.symbol.as_str().is_empty()); + assert!(contract.last_trade_date_or_contract_month.is_empty()); } #[rstest] fn test_instrument_id_to_ib_contract_default_stock_omits_local_symbol_and_currency() { let instrument_id = InstrumentId::from("IUSA.IBIS"); - let contract = instrument_id_to_ib_contract(instrument_id, Some("IBIS")).unwrap(); + let contract = Symbology::new(SymbologyMethod::Simplified) + .contract(instrument_id, Some("IBIS")) + .unwrap(); assert_eq!(contract.security_type, SecurityType::Stock); assert_eq!(contract.symbol.as_str(), "IUSA"); diff --git a/crates/adapters/interactive_brokers/src/common/types.rs b/crates/adapters/interactive_brokers/src/common/types.rs deleted file mode 100644 index 03ea0c7825d7..000000000000 --- a/crates/adapters/interactive_brokers/src/common/types.rs +++ /dev/null @@ -1,18 +0,0 @@ -// ------------------------------------------------------------------------------------------------- -// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -// https://nautechsystems.io -// -// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// ------------------------------------------------------------------------------------------------- - -//! Common types for Interactive Brokers adapter. - -// Placeholder for common types that will be added as needed. diff --git a/crates/adapters/interactive_brokers/src/config.rs b/crates/adapters/interactive_brokers/src/config.rs index f4067e1f91fd..1d153934d2f4 100644 --- a/crates/adapters/interactive_brokers/src/config.rs +++ b/crates/adapters/interactive_brokers/src/config.rs @@ -22,9 +22,13 @@ use std::{ use nautilus_core::string::secret::SecretString; use nautilus_model::identifiers::InstrumentId; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Error}; -use crate::common::consts::{DEFAULT_CLIENT_ID, DEFAULT_HOST, DEFAULT_PORT}; +use crate::common::{ + consts::{DEFAULT_CLIENT_ID, DEFAULT_HOST, DEFAULT_PORT}, + contracts::{ConfiguredContract, parse_configured_contract_from_json}, + enums::IbSecurityType, +}; /// Market data type for switching between real-time and frozen/delayed. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] @@ -110,17 +114,45 @@ pub struct InteractiveBrokersDataClientConfig { /// execution/account.rs for call sites. #[builder(default = 60)] pub request_timeout: u64, - /// Whether to handle revised bars. + /// Whether to publish every revision of an open bar. + /// + /// When `false`, a bar that IB keeps up to date is published once it completes: on IB's + /// update after the bar closes, 2 seconds after the close, or on the next bar's first update, + /// whichever comes first. #[builder(default)] pub handle_revised_bars: bool, /// Whether to use batch quotes (reqMktData) by default instead of tick-by-tick. #[builder(default = true)] pub batch_quotes: bool, + /// Whether to include special-condition trades in tick-by-tick subscriptions. + #[builder(default = true)] + pub all_last_trades: bool, + /// Optional interval without incoming market data before emitting a subscription idle event. + pub subscription_idle_timeout_secs: Option, /// Instrument provider configuration. #[builder(default)] pub instrument_provider: InteractiveBrokersInstrumentProviderConfig, } +impl InteractiveBrokersDataClientConfig { + pub(crate) fn validate(&self) -> anyhow::Result<()> { + anyhow::ensure!( + self.subscription_idle_timeout_secs != Some(0), + "subscription_idle_timeout_secs must be positive when set", + ); + + if let Some(timeout) = self.subscription_idle_timeout_secs { + anyhow::ensure!( + std::time::Instant::now() + .checked_add(std::time::Duration::from_secs(timeout)) + .is_some(), + "subscription_idle_timeout_secs exceeds the supported clock range", + ); + } + Ok(()) + } +} + impl Default for InteractiveBrokersDataClientConfig { fn default() -> Self { Self::builder().build() @@ -154,7 +186,8 @@ pub struct InteractiveBrokersExecutionClientConfig { /// Client ID. #[builder(default = DEFAULT_CLIENT_ID)] pub client_id: i32, - /// Account ID. + /// Raw IB account code, such as `DU123456`. The Nautilus account ID becomes + /// `{client name}-{code}`. pub account_id: Option, /// Connection timeout in seconds. #[builder(default = 300)] @@ -232,7 +265,11 @@ pub struct InteractiveBrokersInstrumentProviderConfig { pub load_ids: HashSet, /// IB contracts to load on startup. #[builder(default)] - pub load_contracts: Vec, + #[serde( + deserialize_with = "deserialize_configured_contracts", + serialize_with = "serialize_configured_contracts" + )] + pub load_contracts: Vec, /// Minimum expiry days for options and futures chains. pub min_expiry_days: Option, /// Maximum expiry days for options and futures chains. @@ -251,7 +288,7 @@ pub struct InteractiveBrokersInstrumentProviderConfig { pub symbol_to_mic_venue: HashMap, /// Security types to filter out. #[builder(default)] - pub filter_sec_types: HashSet, + pub filter_sec_types: HashSet, /// Fully-qualified Python callable path for custom instrument filtering. /// /// Configuring this without the Python feature enabled is an error. @@ -267,6 +304,33 @@ impl Default for InteractiveBrokersInstrumentProviderConfig { } } +fn deserialize_configured_contracts<'de, D>( + deserializer: D, +) -> Result, D::Error> +where + D: Deserializer<'de>, +{ + Vec::::deserialize(deserializer)? + .iter() + .map(parse_configured_contract_from_json) + .collect::>>() + .map_err(D::Error::custom) +} + +fn serialize_configured_contracts( + contracts: &[ConfiguredContract], + serializer: S, +) -> Result +where + S: Serializer, +{ + contracts + .iter() + .map(ConfiguredContract::to_json_value) + .collect::>() + .serialize(serializer) +} + /// Trading mode for Dockerized IB Gateway. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[cfg_attr( @@ -298,7 +362,7 @@ pub enum TradingMode { /// /// This configuration is for managing containerized IB Gateway instances. /// It supports environment variable loading and sensitive data masking. -#[derive(Debug, Clone, Serialize, Deserialize, bon::Builder)] +#[derive(Clone, Serialize, Deserialize, bon::Builder)] #[serde(default)] #[cfg_attr( feature = "python", @@ -318,6 +382,7 @@ pub struct DockerizedIBGatewayConfig { /// Username for IB account (falls back to `TWS_USERNAME` env var via [`Default`]). pub username: Option, /// Password for IB account (falls back to `TWS_PASSWORD` env var via [`Default`]). + #[serde(skip_serializing)] pub password: Option, /// Trading mode (paper or live). #[builder(default)] @@ -336,20 +401,6 @@ pub struct DockerizedIBGatewayConfig { } impl DockerizedIBGatewayConfig { - /// Mask sensitive information for display. - pub fn mask_sensitive_info(value: &str) -> String { - if value.len() <= 2 { - "*".repeat(value.len()) - } else { - format!( - "{}{}{}", - &value[0..1], - "*".repeat(value.len() - 2), - &value[value.len() - 1..] - ) - } - } - /// Validate configuration. /// /// # Errors @@ -374,6 +425,20 @@ impl DockerizedIBGatewayConfig { } } +impl Debug for DockerizedIBGatewayConfig { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct(stringify!(DockerizedIBGatewayConfig)) + .field("username", &self.username) + .field("password", &self.password.as_ref().map(|_| "")) + .field("trading_mode", &self.trading_mode) + .field("read_only_api", &self.read_only_api) + .field("timeout", &self.timeout) + .field("container_image", &self.container_image) + .field("vnc_port", &self.vnc_port) + .finish() + } +} + impl Default for DockerizedIBGatewayConfig { fn default() -> Self { Self::builder() @@ -387,20 +452,153 @@ impl Default for DockerizedIBGatewayConfig { mod tests { use rstest::rstest; - use super::*; + use super::{DockerizedIBGatewayConfig, InteractiveBrokersInstrumentProviderConfig}; + use crate::common::enums::IbSecurityType; #[rstest] - fn dockerized_gateway_config_debug_redacts_credentials() { + fn test_gateway_password_is_redacted_from_debug_and_serialization() { let config = DockerizedIBGatewayConfig::builder() - .username("test-user".into()) - .password("test-password".into()) + .username(String::from("user").into()) + .password(String::from("secret-password").into()) .build(); - let formatted = format!("{config:?}"); + let debug = format!("{config:?}"); + let json = serde_json::to_string(&config).unwrap(); + + assert!(!debug.contains("secret-password")); + assert!(debug.contains("")); + assert!(!json.contains("secret-password")); + assert!(!json.contains("password")); + } + + #[rstest] + fn test_instrument_provider_config_decodes_typed_contracts_and_filters() { + let config: InteractiveBrokersInstrumentProviderConfig = + serde_json::from_value(serde_json::json!({ + "load_contracts": [{ + "secType": "STK", + "symbol": "AAPL", + "exchange": "SMART", + "conId": 265598 + }], + "filter_sec_types": ["opt"] + })) + .unwrap(); + + assert_eq!(config.load_contracts.len(), 1); + assert_eq!(config.load_contracts[0].contract.contract_id, 265598); + assert_eq!( + config.load_contracts[0].contract.security_type, + ibapi::contracts::SecurityType::Stock + ); + assert!(config.filter_sec_types.contains(&IbSecurityType::Option)); + } + + #[rstest] + fn test_instrument_provider_config_keeps_per_contract_chain_settings() { + let config: InteractiveBrokersInstrumentProviderConfig = + serde_json::from_value(serde_json::json!({ + "build_futures_chain": true, + "load_contracts": [ + { + "secType": "STK", + "symbol": "SPY", + "exchange": "SMART", + "primaryExchange": "ARCA", + "build_options_chain": true, + "min_expiry_days": 7, + "max_expiry_days": 14 + }, + { + "secType": "FUT", + "exchange": "NYMEX", + "localSymbol": "CLZ6", + "build_futures_chain": false + } + ] + })) + .unwrap(); + + let spy = &config.load_contracts[0]; + assert_eq!(spy.build_options_chain, Some(true)); + assert_eq!(spy.build_futures_chain, None); + assert_eq!(spy.min_expiry_days, Some(7)); + assert_eq!(spy.max_expiry_days, Some(14)); + assert_eq!(spy.options_chain_exchange, None); + assert_eq!( + spy.chain_spec_json().unwrap(), + serde_json::json!({ + "build_options_chain": true, + "min_expiry_days": 7, + "max_expiry_days": 14 + }) + ); + + let cl = &config.load_contracts[1]; + assert_eq!(cl.build_futures_chain, Some(false)); + assert_eq!( + cl.chain_spec_json().unwrap(), + serde_json::json!({"build_futures_chain": false}) + ); + + let round_trip = serde_json::to_value(&config).unwrap(); + assert_eq!( + round_trip["load_contracts"][0]["build_options_chain"], + serde_json::json!(true) + ); + assert_eq!( + round_trip["load_contracts"][1]["build_futures_chain"], + serde_json::json!(false) + ); + } + + #[rstest] + fn test_instrument_provider_config_accepts_con_id_only_contract() { + let config: InteractiveBrokersInstrumentProviderConfig = + serde_json::from_value(serde_json::json!({ + "load_contracts": [{"conId": 265598}] + })) + .unwrap(); + + assert_eq!(config.load_contracts[0].contract.contract_id, 265598); + assert_eq!( + config.load_contracts[0].contract.security_type, + ibapi::contracts::SecurityType::Stock + ); + } + + #[rstest] + #[case( + serde_json::json!({"load_contracts": [{"secType": "UNKNOWN"}]}), + "Unknown IB security type: UNKNOWN" + )] + #[case( + serde_json::json!({"load_contracts": [{"secType": "STK", "conId": "265598"}]}), + "Configured contract field 'conId' must be an integer" + )] + #[case( + serde_json::json!({"load_contracts": [{"symbol": "SPY"}]}), + "Configured contract requires 'secType' as a known IB security type string, or a positive 'conId'" + )] + #[case( + serde_json::json!({"load_contracts": [{"secType": "STK", "build_options_chain": "yes"}]}), + "Configured contract field 'build_options_chain' must be a boolean" + )] + #[case( + serde_json::json!({"load_contracts": [{"secType": "STK", "min_expiry_days": -1}]}), + "Configured contract field 'min_expiry_days' must be a non-negative integer" + )] + #[case( + serde_json::json!({"filter_sec_types": ["UNKNOWN"]}), + "Unknown IB security type: UNKNOWN" + )] + fn test_instrument_provider_config_rejects_invalid_typed_values( + #[case] value: serde_json::Value, + #[case] expected: &str, + ) { + let error = serde_json::from_value::(value) + .unwrap_err(); - assert!(formatted.contains("username: Some()")); - assert!(formatted.contains("password: Some()")); - assert!(!formatted.contains("test-user")); - assert!(!formatted.contains("test-password")); + assert!(error.to_string().contains(expected), "{error}"); } } diff --git a/crates/adapters/interactive_brokers/src/data/cache.rs b/crates/adapters/interactive_brokers/src/data/cache.rs index f2aa44b220c2..a66bd3123201 100644 --- a/crates/adapters/interactive_brokers/src/data/cache.rs +++ b/crates/adapters/interactive_brokers/src/data/cache.rs @@ -25,6 +25,16 @@ use nautilus_model::{ types::{Price, Quantity}, }; +/// IB publishes price -1 when a side has no quote; a zero or negative price +/// with genuine size is a legitimate value for spreads. The paired zero-size +/// sentinel is handled by the size updates, which clear the side. +// IB sends -1 without a size for a side that has no quote; -1 with a size is a real +// price, such as a spread's bid +pub(crate) fn is_sentinel_price(price: f64, size: Option) -> bool { + !price.is_finite() + || ((price + 1.0).abs() < f64::EPSILON && !size.is_some_and(|size| size > 0.0)) +} + fn checked_quantity(size: f64, precision: u8) -> Option { let quantity = match Quantity::new_checked(size, precision) { Ok(quantity) => quantity, @@ -47,6 +57,16 @@ fn checked_quantity(size: f64, precision: u8) -> Option { Some(quantity) } +fn checked_price(price: f64, precision: u8) -> Option { + match Price::new_checked(price, precision) { + Ok(price) => Some(price), + Err(e) => { + tracing::warn!("Ignoring invalid IB quote price {price}: {e}"); + None + } + } +} + /// Quote cache that accumulates IB tick updates to build complete quotes. /// /// Interactive Brokers sends individual tick price and size updates (bid price, @@ -89,11 +109,19 @@ impl QuoteCache { &mut self, instrument_id: InstrumentId, price: f64, + size: Option, price_precision: u8, size_precision: u8, ts_event: UnixNanos, ts_init: UnixNanos, ) -> Option { + if is_sentinel_price(price, size) { + if let Some(cached) = self.quotes.get_mut(&instrument_id) { + cached.bid_price = None; + cached.bid_size = None; + } + return None; + } let cached = self .quotes .entry(instrument_id) @@ -122,11 +150,19 @@ impl QuoteCache { &mut self, instrument_id: InstrumentId, price: f64, + size: Option, price_precision: u8, size_precision: u8, ts_event: UnixNanos, ts_init: UnixNanos, ) -> Option { + if is_sentinel_price(price, size) { + if let Some(cached) = self.quotes.get_mut(&instrument_id) { + cached.ask_price = None; + cached.ask_size = None; + } + return None; + } let cached = self .quotes .entry(instrument_id) @@ -183,6 +219,12 @@ impl QuoteCache { ts_init: UnixNanos, ignore_size_only: bool, ) -> Option { + if !size.is_finite() || size <= 0.0 { + if let Some(cached) = self.quotes.get_mut(&instrument_id) { + cached.bid_size = None; + } + return None; + } checked_quantity(size, size_precision)?; let cached = self @@ -253,6 +295,12 @@ impl QuoteCache { ts_init: UnixNanos, ignore_size_only: bool, ) -> Option { + if !size.is_finite() || size <= 0.0 { + if let Some(cached) = self.quotes.get_mut(&instrument_id) { + cached.ask_size = None; + } + return None; + } checked_quantity(size, size_precision)?; let cached = self @@ -304,8 +352,8 @@ impl QuoteCache { // Check if we have all required fields let bid_price = cached.bid_price?; let ask_price = cached.ask_price?; - let bid_size = cached.bid_size.unwrap_or(0.0); - let ask_size = cached.ask_size.unwrap_or(0.0); + let bid_size = cached.bid_size?; + let ask_size = cached.ask_size?; let bid_qty = checked_quantity(bid_size, size_precision)?; let ask_qty = checked_quantity(ask_size, size_precision)?; @@ -313,8 +361,8 @@ impl QuoteCache { // Build the quote let quote = QuoteTick::new( instrument_id, - Price::new(bid_price, price_precision), - Price::new(ask_price, price_precision), + checked_price(bid_price, price_precision)?, + checked_price(ask_price, price_precision)?, bid_qty, ask_qty, ts_event, @@ -475,12 +523,77 @@ mod tests { use nautilus_model::identifiers::{InstrumentId, Symbol, Venue}; use rstest::rstest; - use super::{OptionGreeksCache, QuoteCache}; + use super::{OptionGreeksCache, QuoteCache, is_sentinel_price}; fn instrument_id() -> InstrumentId { InstrumentId::new(Symbol::from("AAPL"), Venue::from("NASDAQ")) } + #[rstest] + #[case(-1.0, None, true)] + #[case(-1.0, Some(0.0), true)] + #[case(-1.0, Some(3.0), false)] + #[case(f64::NAN, Some(3.0), true)] + #[case(f64::INFINITY, None, true)] + #[case(-1.25, None, false)] + #[case(0.0, None, false)] + #[case(99.5, None, false)] + fn test_is_sentinel_price( + #[case] price: f64, + #[case] size: Option, + #[case] expected: bool, + ) { + assert_eq!(is_sentinel_price(price, size), expected); + } + + #[rstest] + fn test_quote_cache_emits_negative_spread_prices() { + let mut cache = QuoteCache::new(); + let instrument_id = instrument_id(); + + cache.update_bid_price( + instrument_id, + -3.25, + None, + 2, + 0, + UnixNanos::new(1), + UnixNanos::new(1), + ); + cache.update_ask_price( + instrument_id, + -2.75, + None, + 2, + 0, + UnixNanos::new(2), + UnixNanos::new(2), + ); + cache.update_bid_size( + instrument_id, + 5.0, + 2, + 0, + UnixNanos::new(3), + UnixNanos::new(3), + ); + let quote = cache + .update_ask_size( + instrument_id, + 7.0, + 2, + 0, + UnixNanos::new(4), + UnixNanos::new(4), + ) + .unwrap(); + + assert_eq!(quote.bid_price.as_f64(), -3.25); + assert_eq!(quote.ask_price.as_f64(), -2.75); + assert_eq!(quote.bid_size.as_f64(), 5.0); + assert_eq!(quote.ask_size.as_f64(), 7.0); + } + #[rstest] fn test_quote_cache_requires_both_prices() { let mut cache = QuoteCache::new(); @@ -489,6 +602,7 @@ mod tests { let quote = cache.update_bid_price( instrument_id, 100.0, + None, 2, 0, UnixNanos::new(1), @@ -500,13 +614,14 @@ mod tests { } #[rstest] - fn test_quote_cache_builds_complete_quote_with_default_sizes() { + fn test_quote_cache_requires_both_sizes() { let mut cache = QuoteCache::new(); let instrument_id = instrument_id(); cache.update_bid_price( instrument_id, 100.0, + None, 2, 0, UnixNanos::new(1), @@ -515,18 +630,36 @@ mod tests { let quote = cache.update_ask_price( instrument_id, 101.0, + None, 2, 0, UnixNanos::new(2), UnixNanos::new(2), ); - assert!(quote.is_some()); - let quote = quote.unwrap(); + assert!(quote.is_none()); + cache.update_bid_size( + instrument_id, + 10.0, + 2, + 0, + UnixNanos::new(3), + UnixNanos::new(3), + ); + let quote = cache + .update_ask_size( + instrument_id, + 11.0, + 2, + 0, + UnixNanos::new(4), + UnixNanos::new(4), + ) + .unwrap(); assert_eq!(quote.bid_price.as_f64(), 100.0); assert_eq!(quote.ask_price.as_f64(), 101.0); - assert_eq!(quote.bid_size.as_f64(), 0.0); - assert_eq!(quote.ask_size.as_f64(), 0.0); + assert_eq!(quote.bid_size.as_f64(), 10.0); + assert_eq!(quote.ask_size.as_f64(), 11.0); assert!(cache.get_last_quote(&instrument_id).is_some()); } @@ -538,6 +671,7 @@ mod tests { cache.update_bid_price( instrument_id, 100.0, + None, 2, 0, UnixNanos::new(1), @@ -546,25 +680,42 @@ mod tests { cache.update_ask_price( instrument_id, 101.0, + None, 2, 0, UnixNanos::new(2), UnixNanos::new(2), ); - - let quote = cache.update_bid_size_with_filter( + cache.update_bid_size( instrument_id, 10.0, 2, 0, UnixNanos::new(3), UnixNanos::new(3), + ); + cache.update_ask_size( + instrument_id, + 11.0, + 2, + 0, + UnixNanos::new(4), + UnixNanos::new(4), + ); + + let quote = cache.update_bid_size_with_filter( + instrument_id, + 10.0, + 2, + 0, + UnixNanos::new(5), + UnixNanos::new(5), true, ); assert!(quote.is_none()); let last_quote = cache.get_last_quote(&instrument_id).unwrap(); - assert_eq!(last_quote.bid_size.as_f64(), 0.0); + assert_eq!(last_quote.bid_size.as_f64(), 10.0); } #[rstest] @@ -575,6 +726,7 @@ mod tests { cache.update_bid_price( instrument_id, 100.0, + None, 2, 0, UnixNanos::new(1), @@ -583,24 +735,41 @@ mod tests { cache.update_ask_price( instrument_id, 101.0, + None, 2, 0, UnixNanos::new(2), UnixNanos::new(2), ); + cache.update_bid_size( + instrument_id, + 10.0, + 2, + 0, + UnixNanos::new(3), + UnixNanos::new(3), + ); + cache.update_ask_size( + instrument_id, + 11.0, + 2, + 0, + UnixNanos::new(4), + UnixNanos::new(4), + ); let quote = cache.update_bid_size( instrument_id, 0.5, 2, 0, - UnixNanos::new(3), - UnixNanos::new(3), + UnixNanos::new(5), + UnixNanos::new(5), ); assert!(quote.is_none()); let last_quote = cache.get_last_quote(&instrument_id).unwrap(); - assert_eq!(last_quote.bid_size.as_f64(), 0.0); + assert_eq!(last_quote.bid_size.as_f64(), 10.0); } #[rstest] @@ -611,6 +780,7 @@ mod tests { cache.update_bid_price( instrument_id, 100.0, + None, 2, 0, UnixNanos::new(1), @@ -619,28 +789,37 @@ mod tests { cache.update_ask_price( instrument_id, 101.0, + None, 2, 0, UnixNanos::new(2), UnixNanos::new(2), ); - cache.update_bid_size_with_filter( + cache.update_bid_size( instrument_id, 10.0, 2, 0, UnixNanos::new(3), UnixNanos::new(3), - true, + ); + cache.update_ask_size( + instrument_id, + 11.0, + 2, + 0, + UnixNanos::new(4), + UnixNanos::new(4), ); let quote = cache.update_bid_price( instrument_id, 100.5, + None, 2, 0, - UnixNanos::new(4), - UnixNanos::new(4), + UnixNanos::new(5), + UnixNanos::new(5), ); assert!(quote.is_some()); diff --git a/crates/adapters/interactive_brokers/src/data/convert.rs b/crates/adapters/interactive_brokers/src/data/convert.rs index 8c3fc1f92454..ca8f1b135615 100644 --- a/crates/adapters/interactive_brokers/src/data/convert.rs +++ b/crates/adapters/interactive_brokers/src/data/convert.rs @@ -279,60 +279,33 @@ pub fn jiff_to_ib_datetime(dt: &Timestamp) -> OffsetDateTime { .unwrap_or_else(|_| OffsetDateTime::now_utc()) } -/// Calculate duration for IB historical data request. -/// -/// # Errors -/// -/// Returns an error if duration calculation fails. -pub fn calculate_duration( - start: Option, - end: Option, -) -> anyhow::Result { - match (start, end) { - (Some(start_dt), Some(end_dt)) => { - let duration = end_dt.duration_since(start_dt); - let days = duration.as_secs() / (24 * 60 * 60); - - if days > 0 && days <= i32::MAX as i64 { - Ok((days as i32).days()) - } else { - // Fallback to seconds if less than a day or too large - let seconds = duration.as_secs(); - if seconds > 0 && seconds <= i32::MAX as i64 { - Ok((seconds as i32).seconds()) - } else { - // Default to 1 day if calculation fails - Ok(1.days()) - } - } - } - (None, Some(_)) => { - // Default to 1 day if only end is provided - Ok(1.days()) - } - (Some(_), None) => { - // Default to 1 day if only start is provided - Ok(1.days()) - } - (None, None) => { - // Default to 1 day if neither is provided - Ok(1.days()) - } - } -} - /// Calculate duration segments for IB historical data request. /// /// This is used to break down a large time range into multiple requests /// to comply with IB's duration limits for specific bar sizes. pub fn calculate_duration_segments( - start: Timestamp, - end: Timestamp, + start: Option, + end: Option, + duration: Option, ) -> Vec<(Timestamp, IBDuration)> { + let end = end.unwrap_or_else(Timestamp::now); + + if let Some(duration) = duration { + return vec![(end, duration)]; + } + + let Some(start) = start else { + return vec![(end, 1.days())]; + }; + let mut results = Vec::new(); let duration = end.duration_since(start); let mut total_seconds = duration.as_secs(); + if duration.subsec_nanos() > 0 { + total_seconds += 1; + } + if total_seconds <= 0 { return results; } @@ -363,6 +336,92 @@ pub fn calculate_duration_segments( results } +/// Return whether a backward historical tick request needs another page. +#[must_use] +pub(crate) fn should_continue_historical_tick_pagination( + current_start_date: Option, + current_end_date: Option, + current_len: usize, + limit: Option, +) -> bool { + limit.is_none_or(|limit| current_len < limit) + && current_start_date + .zip(current_end_date) + .is_none_or(|(start, end)| end > start) +} + +/// Add a historical tick page and advance the next request before its earliest tick. +#[allow(clippy::too_many_arguments)] // The parameters describe one pagination transition. +pub(crate) fn extend_historical_tick_batch( + all_ticks: &mut Vec, + batch_ticks: Vec, + current_start_date: Option, + current_end_date: &mut Option, + start_nanos: Option, + end_nanos: Option, + limit: Option, + ts_event: impl Fn(&T) -> UnixNanos, +) -> bool { + if batch_ticks.is_empty() { + return false; + } + + // The IB wire encodes the request end at second resolution and treats it as + // inclusive, so each page re-delivers ticks from the boundary second that a + // previous page already collected. Drop everything at or past the requested + // end before extending; an entirely re-delivered page means the end second + // cannot retreat (a single second holds a full page) and pagination must stop. + let mut batch_ticks = batch_ticks; + + if let Some(end) = *current_end_date { + let end_bound_nanos = end.as_nanosecond(); + batch_ticks.retain(|tick| i128::from(ts_event(tick).as_u64()) <= end_bound_nanos); + if batch_ticks.is_empty() { + tracing::warn!( + "Historical tick pagination pinned at {end}; a single second exceeds the page \ + size, earlier ticks within that second cannot be requested" + ); + retain_historical_ticks_in_range(all_ticks, start_nanos, end_nanos, &ts_event); + return false; + } + } + + let Some(min_ts_nanos) = batch_ticks.iter().map(&ts_event).min() else { + return false; + }; + let new_end_nanos = min_ts_nanos.as_u64().saturating_sub(1); + let Ok(new_end) = Timestamp::from_nanosecond(i128::from(new_end_nanos)) else { + return false; + }; + *current_end_date = Some(new_end); + + all_ticks.extend(batch_ticks); + + if current_start_date + .zip(*current_end_date) + .is_some_and(|(start, end)| end <= start) + { + retain_historical_ticks_in_range(all_ticks, start_nanos, end_nanos, &ts_event); + return false; + } + + limit.is_none_or(|limit| all_ticks.len() < limit) +} + +/// Retain historical ticks within the inclusive requested range. +pub(crate) fn retain_historical_ticks_in_range( + ticks: &mut Vec, + start_nanos: Option, + end_nanos: Option, + ts_event: impl Fn(&T) -> UnixNanos, +) { + ticks.retain(|tick| { + let ts_event = ts_event(tick); + start_nanos.is_none_or(|start| ts_event >= start) + && end_nanos.is_none_or(|end| ts_event <= end) + }); +} + /// Adapt duration segments for an IB historical bars request. /// /// For continuous futures the end date is dropped and only the first segment is @@ -740,50 +799,173 @@ mod tests { } #[rstest] - fn test_calculate_duration_with_start_and_end() { + fn test_calculate_duration_segments_with_start_and_end() { let start = "2024-01-01T10:00:00Z".parse::().unwrap(); let end = "2024-01-02T10:00:00Z".parse::().unwrap(); - let result = calculate_duration(Some(start), Some(end)); - assert!(result.is_ok()); - // Should be 1 day - let duration = result.unwrap(); - assert!(duration.to_string().contains("1 D") || duration.to_string().contains("1D")); + let result = calculate_duration_segments(Some(start), Some(end), None); + + assert_eq!(result, vec![(end, IBDuration::days(1))]); } #[rstest] - fn test_calculate_duration_no_start() { + fn test_calculate_duration_segments_without_start_defaults_to_one_day() { let end = "2024-01-02T10:00:00Z".parse::().unwrap(); - let result = calculate_duration(None, Some(end)); - assert!(result.is_ok()); - // Should default to 1 day - let duration = result.unwrap(); - assert!(duration.to_string().contains("1 D") || duration.to_string().contains("1D")); + let result = calculate_duration_segments(None, Some(end), None); + + assert_eq!(result, vec![(end, IBDuration::days(1))]); + } + + #[rstest] + fn test_calculate_duration_segments_with_explicit_duration() { + let end = "2024-01-02T10:00:00Z".parse::().unwrap(); + let result = calculate_duration_segments(None, Some(end), Some(IBDuration::weeks(2))); + + assert_eq!(result, vec![(end, IBDuration::weeks(2))]); } #[rstest] - fn test_calculate_duration_no_end() { + fn test_calculate_duration_segments_splits_years_days_and_seconds() { + let end = "2025-07-03T10:00:01Z".parse::().unwrap(); let start = "2024-01-01T10:00:00Z".parse::().unwrap(); - let result = calculate_duration(Some(start), None); - assert!(result.is_ok()); - // Should default to 1 day - let duration = result.unwrap(); - assert!(duration.to_string().contains("1 D") || duration.to_string().contains("1D")); + let segments = calculate_duration_segments(Some(start), Some(end), None); + + assert_eq!( + segments, + vec![ + (end, IBDuration::years(1)), + ( + "2024-07-03T10:00:01Z".parse::().unwrap(), + IBDuration::days(184), + ), + ( + "2024-01-01T10:00:01Z".parse::().unwrap(), + IBDuration::seconds(1), + ), + ] + ); } #[rstest] - fn test_calculate_duration_segments() { - // Test case: 1.5 years ago to now - let now = Timestamp::now(); - let start = now - jiff::SignedDuration::from_hours(24 * (365 + 182)); // ~1.5 years - let segments = calculate_duration_segments(start, now); + fn test_calculate_duration_segments_rounds_subsecond_up() { + let start = Timestamp::new(1, 0).unwrap(); + let end = Timestamp::new(1, 1).unwrap(); + let segments = calculate_duration_segments(Some(start), Some(end), None); + + assert_eq!(segments, vec![(end, IBDuration::seconds(1))]); + } + + #[rstest] + #[case(None, Some(Timestamp::from_second(2).unwrap()), 0, Some(10), true)] + #[case(Some(Timestamp::from_second(1).unwrap()), Some(Timestamp::from_second(2).unwrap()), 0, Some(10), true)] + #[case(Some(Timestamp::from_second(2).unwrap()), Some(Timestamp::from_second(1).unwrap()), 0, Some(10), false)] + #[case(Some(Timestamp::from_second(1).unwrap()), Some(Timestamp::from_second(2).unwrap()), 10, Some(10), false)] + #[case(Some(Timestamp::from_second(1).unwrap()), Some(Timestamp::from_second(2).unwrap()), 10, None, true)] + fn test_should_continue_historical_tick_pagination( + #[case] start: Option, + #[case] end: Option, + #[case] current_len: usize, + #[case] limit: Option, + #[case] expected: bool, + ) { + assert_eq!( + should_continue_historical_tick_pagination(start, end, current_len, limit), + expected + ); + } - assert!(!segments.is_empty()); - // Should have at least one 1Y segment and one D/S segment - assert!(segments.len() >= 2); + #[rstest] + fn test_extend_historical_tick_batch_retreats_one_nanosecond() { + let mut ticks = vec![UnixNanos::from(30)]; + let mut end = Some(Timestamp::from_nanosecond(100).unwrap()); + + let should_continue = extend_historical_tick_batch( + &mut ticks, + vec![UnixNanos::from(20), UnixNanos::from(10)], + Some(Timestamp::from_nanosecond(1).unwrap()), + &mut end, + Some(UnixNanos::from(1)), + Some(UnixNanos::from(100)), + Some(10), + |ts| *ts, + ); + + assert!(should_continue); + assert_eq!( + ticks, + vec![ + UnixNanos::from(30), + UnixNanos::from(20), + UnixNanos::from(10) + ] + ); + assert_eq!(end, Some(Timestamp::from_nanosecond(9).unwrap())); + } + + #[rstest] + fn test_extend_historical_tick_batch_stops_and_trims_at_start() { + let mut ticks = Vec::new(); + let mut end = Some(Timestamp::from_nanosecond(100).unwrap()); + + let should_continue = extend_historical_tick_batch( + &mut ticks, + vec![UnixNanos::from(20), UnixNanos::from(10)], + Some(Timestamp::from_nanosecond(10).unwrap()), + &mut end, + Some(UnixNanos::from(10)), + Some(UnixNanos::from(15)), + Some(10), + |ts| *ts, + ); + + assert!(!should_continue); + assert_eq!(ticks, vec![UnixNanos::from(10)]); + assert_eq!(end, Some(Timestamp::from_nanosecond(9).unwrap())); + } + + #[rstest] + fn test_extend_historical_tick_batch_drops_boundary_duplicates() { + let mut ticks = vec![UnixNanos::from(30)]; + let mut end = Some(Timestamp::from_nanosecond(29).unwrap()); + + let should_continue = extend_historical_tick_batch( + &mut ticks, + vec![ + UnixNanos::from(35), + UnixNanos::from(30), + UnixNanos::from(20), + ], + None, + &mut end, + None, + None, + Some(10), + |ts| *ts, + ); + + assert!(should_continue); + assert_eq!(ticks, vec![UnixNanos::from(30), UnixNanos::from(20)]); + assert_eq!(end, Some(Timestamp::from_nanosecond(19).unwrap())); + } + + #[rstest] + fn test_extend_historical_tick_batch_stops_when_end_cannot_retreat() { + let mut ticks = vec![UnixNanos::from(30)]; + let mut end = Some(Timestamp::from_nanosecond(29).unwrap()); + + let should_continue = extend_historical_tick_batch( + &mut ticks, + vec![UnixNanos::from(35), UnixNanos::from(30)], + None, + &mut end, + None, + None, + Some(10), + |ts| *ts, + ); - // Check first segment is ~1Y - let dur1 = &segments[0].1; - assert!(dur1.to_string().contains("1 Y") || dur1.to_string().contains("1Y")); + assert!(!should_continue); + assert_eq!(ticks, vec![UnixNanos::from(30)]); + assert_eq!(end, Some(Timestamp::from_nanosecond(29).unwrap())); } #[rstest] diff --git a/crates/adapters/interactive_brokers/src/data/core.rs b/crates/adapters/interactive_brokers/src/data/core.rs index 4be077cdd0fb..961ae8aa1c19 100644 --- a/crates/adapters/interactive_brokers/src/data/core.rs +++ b/crates/adapters/interactive_brokers/src/data/core.rs @@ -15,14 +15,11 @@ //! Core data client implementation for Interactive Brokers. -#[path = "core_streams.rs"] -mod streams; - use std::{ collections::HashMap, fmt::Debug, sync::{ - Arc, + Arc, Mutex, atomic::{AtomicBool, Ordering}, }, time::Duration, @@ -32,11 +29,9 @@ use ahash::AHashMap; use anyhow::Context; use ibapi::{ contracts::{Contract, Currency as IBCurrency, Exchange as IBExchange, SecurityType, Symbol}, - market_data::{IgnoreSize, historical::ToDuration}, + market_data::IgnoreSize, prelude::{StreamExt, SubscriptionItemStreamExt}, }; -#[cfg(test)] -use nautilus_common::live::get_runtime; use nautilus_common::{ clients::DataClient, live::{runner::get_data_event_sender, sender::EventSender}, @@ -58,31 +53,39 @@ use nautilus_core::{ }; use nautilus_live::task::{TaskGroup, TaskJoinOutcome, TaskSlot, finish_task}; use nautilus_model::{ + data::BarType, enums::BookType, identifiers::{ClientId, InstrumentId, Venue}, instruments::{Instrument, any::InstrumentAny}, }; use tokio_util::sync::CancellationToken; -use self::streams::{ - DataFarmConnectionState, handle_historical_bars_subscription, handle_index_price_subscription, - handle_market_depth_subscription, handle_option_greeks_subscription, handle_quote_subscription, - handle_realtime_bars_subscription, handle_tick_by_tick_quote_subscription, - handle_trade_subscription, monitor_data_farm_notices, -}; use super::{ cache::{OptionGreeksCache, QuoteCache}, convert::{ apply_bar_price_magnifier, apply_price_magnifier, bar_request_segments, - bar_type_to_ib_bar_size, calculate_duration, calculate_duration_segments, + bar_type_to_ib_bar_size, calculate_duration_segments, extend_historical_tick_batch, ib_bar_to_nautilus_bar, jiff_to_ib_datetime, price_type_to_ib_realtime_what_to_show_for_security, - price_type_to_ib_what_to_show_for_security, + price_type_to_ib_what_to_show_for_security, retain_historical_ticks_in_range, + should_continue_historical_tick_pagination, + }, + parse::log_tick_parse_error, + streams::{ + DataFarmConnectionState, StreamConfig, handle_historical_bars_subscription, + handle_index_price_subscription, handle_market_depth_subscription, + handle_option_greeks_subscription, handle_quote_subscription, + handle_realtime_bars_subscription, handle_tick_by_tick_quote_subscription, + handle_trade_subscription, monitor_data_farm_notices, }, }; use crate::{ - common::{consts::IB_VENUE, shared_client::SharedClientHandle}, - config::InteractiveBrokersDataClientConfig, + common::{ + consts::IB_VENUE, contracts::parse_contract_from_json, shared_client::SharedClientHandle, + symbology::is_crypto_contract, + }, + config::{InteractiveBrokersDataClientConfig, MarketDataType}, + data_types::register_ib_custom_data, providers::instruments::InteractiveBrokersInstrumentProvider, }; @@ -99,13 +102,13 @@ pub struct InteractiveBrokersDataClient { client_id: ClientId, config: InteractiveBrokersDataClientConfig, instrument_provider: Arc, - is_connected: AtomicBool, + is_connected: Arc, cancellation_token: CancellationToken, session_tasks: TaskGroup, command_tasks: TaskGroup, data_sender: EventSender, - subscriptions: Arc>>, - option_greeks_subscriptions: Arc>>, + subscriptions: Arc>>, + option_greeks_subscriptions: Arc>>, quote_cache: Arc>, option_greeks_cache: Arc>, clock: &'static AtomicTime, @@ -117,18 +120,15 @@ pub struct InteractiveBrokersDataClient { /// Information about an active subscription. #[derive(Debug)] -#[allow(dead_code)] struct SubscriptionInfo { - /// Instrument ID for the subscription. - instrument_id: InstrumentId, - /// Subscription type. - subscription_type: SubscriptionType, /// Cancellation token for this specific subscription. cancellation_token: CancellationToken, + /// Unique id distinguishing this registration from later ones for the same key. + id: u64, } /// Type of subscription. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] enum SubscriptionType { /// Quote subscription. Quotes, @@ -136,12 +136,54 @@ enum SubscriptionType { IndexPrices, /// Trade subscription. Trades, - /// Bar subscription. - Bars, + /// Bar subscription, one stream per bar type. + Bars(BarType), /// Order book delta subscription. BookDeltas, } +type SubscriptionKey = (InstrumentId, SubscriptionType); + +static SUBSCRIPTION_ID_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + +fn next_subscription_id() -> u64 { + SUBSCRIPTION_ID_SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed) +} + +/// Removes a subscription's registry entry when its stream task ends permanently, +/// so a later re-subscribe is not blocked by a dead registration. The id check +/// keeps a finishing task from removing a newer registration for the same key. +struct SubscriptionGuard { + subscriptions: Arc>>, + key: SubscriptionKey, + id: u64, +} + +impl SubscriptionGuard { + fn release(self) { + // Dropping performs the removal; a panicking stream task reaches the same + // path through Drop, so a dead registration can never block a re-subscribe. + } +} + +impl Drop for SubscriptionGuard { + fn drop(&mut self) { + if let Ok(mut subscriptions) = self.subscriptions.lock() + && subscriptions + .get(&self.key) + .is_some_and(|info| info.id == self.id) + { + subscriptions.remove(&self.key); + } + } +} + +fn lock(mutex: &Mutex) -> anyhow::Result> { + mutex + .lock() + .map_err(|_| anyhow::anyhow!("Failed to lock IB data client state")) +} + fn parse_start_ns(params: Option<&nautilus_core::Params>) -> Option { params .and_then(|params| params.get_u64("start_ns")) @@ -177,89 +219,11 @@ fn request_trading_hours(use_regular_trading_hours: bool) -> ibapi::market_data: } } -fn retreat_historical_tick_end_datetime(min_ts_nanos: u64) -> Option { - let new_end_nanos = min_ts_nanos.saturating_sub(1_000_000); - jiff::Timestamp::from_nanosecond(i128::from(new_end_nanos)).ok() -} - -fn should_continue_historical_tick_pagination( - current_start_date: Option, - current_end_date: Option, - current_len: usize, - limit: Option, -) -> bool { - limit.is_none_or(|limit| current_len < limit) - && current_start_date - .zip(current_end_date) - .is_none_or(|(start, end)| end > start) -} - -fn retreat_end_to_earliest_tick( - batch: &[T], - ts_event: impl Fn(&T) -> UnixNanos, -) -> Option { - batch - .iter() - .min_by_key(|tick| ts_event(tick)) - .and_then(|tick| retreat_historical_tick_end_datetime(ts_event(tick).as_u64())) -} - -fn retain_historical_ticks_in_range( - ticks: &mut Vec, - start_nanos: Option, - end_nanos: Option, - ts_event: impl Fn(&T) -> UnixNanos, -) { - ticks.retain(|tick| { - let ts_event = ts_event(tick); - start_nanos.is_none_or(|start| ts_event >= start) - && end_nanos.is_none_or(|end| ts_event <= end) - }); -} - -fn extend_historical_tick_batch( - all_ticks: &mut Vec, - batch_ticks: Vec, - current_start_date: Option, - current_end_date: &mut Option, - start_nanos: Option, - end_nanos: Option, - limit: Option, - ts_event: impl Fn(&T) -> UnixNanos, -) -> bool { - if batch_ticks.is_empty() { - return false; - } - - if let Some(new_end) = retreat_end_to_earliest_tick(&batch_ticks, &ts_event) { - *current_end_date = Some(new_end); - } else { - return false; - } - - all_ticks.extend(batch_ticks); - - if current_start_date - .as_ref() - .zip(current_end_date.as_ref()) - .is_some_and(|(start, end)| end <= start) - { - retain_historical_ticks_in_range(all_ticks, start_nanos, end_nanos, &ts_event); - return false; - } - - limit.is_none_or(|limit| all_ticks.len() < limit) -} +const HISTORICAL_TICK_DEFAULT_LIMIT: usize = 10_000; impl InteractiveBrokersDataClient { /// Create a new `InteractiveBrokersDataClient`. /// - /// # Arguments - /// - /// * `client_id` - Client identifier - /// * `config` - Configuration for the client - /// * `instrument_provider` - Instrument provider - /// /// # Errors /// /// Returns an error if client creation fails. @@ -269,6 +233,8 @@ impl InteractiveBrokersDataClient { instrument_provider: Arc, ) -> anyhow::Result { let clock = get_atomic_clock_realtime(); + config.validate()?; + register_ib_custom_data(); let data_sender = get_data_event_sender(); let session_tasks = TaskGroup::new(); @@ -278,13 +244,13 @@ impl InteractiveBrokersDataClient { client_id, config, instrument_provider, - is_connected: AtomicBool::new(false), + is_connected: Arc::new(AtomicBool::new(false)), cancellation_token: session_tasks.cancellation_token(), session_tasks, command_tasks, data_sender, - subscriptions: Arc::new(tokio::sync::Mutex::new(AHashMap::new())), - option_greeks_subscriptions: Arc::new(tokio::sync::Mutex::new(AHashMap::new())), + subscriptions: Arc::new(Mutex::new(AHashMap::new())), + option_greeks_subscriptions: Arc::new(Mutex::new(AHashMap::new())), quote_cache: Arc::new(tokio::sync::Mutex::new(QuoteCache::new())), option_greeks_cache: Arc::new(tokio::sync::Mutex::new(OptionGreeksCache::new())), clock, @@ -297,20 +263,14 @@ impl InteractiveBrokersDataClient { fn cancel_active_subscriptions(&self) -> anyhow::Result<()> { { - let mut subscriptions = self - .subscriptions - .try_lock() - .context("Failed to lock IB subscriptions for cancellation")?; + let mut subscriptions = lock(&self.subscriptions)?; for subscription in subscriptions.values() { subscription.cancellation_token.cancel(); } subscriptions.clear(); } { - let mut subscriptions = self - .option_greeks_subscriptions - .try_lock() - .context("Failed to lock IB option greeks subscriptions for cancellation")?; + let mut subscriptions = lock(&self.option_greeks_subscriptions)?; for cancellation_token in subscriptions.values() { cancellation_token.cancel(); } @@ -401,10 +361,6 @@ impl InteractiveBrokersDataClient { /// /// This method calls the provider's batch_load with the data client's IB client. /// - /// # Arguments - /// - /// * `instrument_ids` - Vector of instrument IDs to load - /// /// # Errors /// /// Returns an error if: @@ -435,14 +391,6 @@ impl InteractiveBrokersDataClient { /// /// This method calls the provider's fetch_option_chain_by_range with the data client's IB client. /// - /// # Arguments - /// - /// * `underlying_symbol` - The underlying symbol (e.g., "AAPL") - /// * `exchange` - The exchange (defaults to "SMART") - /// * `currency` - The currency (defaults to "USD") - /// * `expiry_min` - Minimum expiry date string (YYYYMMDD format, optional) - /// * `expiry_max` - Maximum expiry date string (YYYYMMDD format, optional) - /// /// # Errors /// /// Returns an error if: @@ -457,12 +405,7 @@ impl InteractiveBrokersDataClient { expiry_max: Option<&str>, ) -> anyhow::Result { log::debug!( - "Fetching IB option chain by range (symbol={}, exchange={:?}, currency={:?}, expiry_min={:?}, expiry_max={:?})", - underlying_symbol, - exchange, - currency, - expiry_min, - expiry_max + "Fetching IB option chain by range (symbol={underlying_symbol}, exchange={exchange:?}, currency={currency:?}, expiry_min={expiry_min:?}, expiry_max={expiry_max:?})" ); let client = self .ib_client @@ -497,11 +440,7 @@ impl InteractiveBrokersDataClient { .instrument_provider .fetch_option_chain_by_range(client, &underlying, expiry_min, expiry_max, None) .await?; - log::debug!( - "Fetched {} IB option instruments for {}", - count, - underlying_symbol - ); + log::debug!("Fetched {count} IB option instruments for {underlying_symbol}"); Ok(count) } @@ -509,12 +448,6 @@ impl InteractiveBrokersDataClient { /// /// This method calls the provider's fetch_futures_chain with the data client's IB client. /// - /// # Arguments - /// - /// * `symbol` - The underlying symbol (e.g., "ES") - /// * `exchange` - The exchange (defaults to empty string for all exchanges) - /// * `currency` - The currency (defaults to "USD") - /// /// # Errors /// /// Returns an error if: @@ -529,12 +462,7 @@ impl InteractiveBrokersDataClient { max_expiry_days: Option, ) -> anyhow::Result { log::debug!( - "Fetching IB futures chain (symbol={}, exchange={:?}, currency={:?}, min_days={:?}, max_days={:?})", - symbol, - exchange, - currency, - min_expiry_days, - max_expiry_days + "Fetching IB futures chain (symbol={symbol}, exchange={exchange:?}, currency={currency:?}, min_days={min_expiry_days:?}, max_days={max_expiry_days:?})" ); let client = self .ib_client @@ -554,7 +482,7 @@ impl InteractiveBrokersDataClient { max_expiry_days, ) .await?; - log::debug!("Fetched {} IB futures instruments for {}", count, symbol); + log::debug!("Fetched {count} IB futures instruments for {symbol}"); Ok(count) } @@ -562,10 +490,6 @@ impl InteractiveBrokersDataClient { /// /// This method calls the provider's fetch_bag_contract with the data client's IB client. /// - /// # Arguments - /// - /// * `bag_contract` - The BAG contract with populated combo_legs - /// /// # Errors /// /// Returns an error if: @@ -590,7 +514,7 @@ impl InteractiveBrokersDataClient { .instrument_provider .fetch_bag_contract(client, bag_contract) .await?; - log::debug!("Fetched {} BAG instruments", count); + log::debug!("Fetched {count} BAG instruments"); Ok(count) } } @@ -654,21 +578,9 @@ impl DataClient for InteractiveBrokersDataClient { self.command_tasks.begin_shutdown(); self.is_connected.store(false, Ordering::Relaxed); self.clear_bar_tracking_state(); - - { - let mut cache = self - .quote_cache - .try_lock() - .context("Failed to lock IB quote cache for reset")?; - cache.clear(); - } - { - let mut cache = self - .option_greeks_cache - .try_lock() - .context("Failed to lock IB option greeks cache for reset")?; - cache.clear(); - } + self.quote_cache = Arc::new(tokio::sync::Mutex::new(QuoteCache::new())); + self.option_greeks_cache = Arc::new(tokio::sync::Mutex::new(OptionGreeksCache::new())); + self.cancellation_token = CancellationToken::new(); Ok(()) } @@ -701,9 +613,10 @@ impl DataClient for InteractiveBrokersDataClient { ); // Set market data type if not default - if self.config.market_data_type != crate::config::MarketDataType::Realtime { - let ib_data_type: ibapi::market_data::MarketDataType = - self.config.market_data_type.into(); + let ib_data_type = (self.config.market_data_type != MarketDataType::Realtime) + .then(|| self.config.market_data_type.into()); + + if let Some(ib_data_type) = ib_data_type { client .switch_market_data_type(ib_data_type) .await @@ -731,12 +644,20 @@ impl DataClient for InteractiveBrokersDataClient { self.ib_client = Some(handle); let data_farm_state = Arc::clone(&self.data_farm_state); + let is_connected = Arc::clone(&self.is_connected); let cancellation_token = self.cancellation_token.child_token(); let clock = self.clock; if let Err(e) = self.session_tasks.spawn(async move { - if let Err(e) = - monitor_data_farm_notices(client, data_farm_state, clock, cancellation_token).await + if let Err(e) = monitor_data_farm_notices( + client, + data_farm_state, + ib_data_type, + clock, + cancellation_token, + is_connected, + ) + .await { tracing::warn!("IB data farm notice monitor stopped: {e:?}"); } @@ -796,6 +717,13 @@ impl DataClient for InteractiveBrokersDataClient { // Subscription handlers fn subscribe_quotes(&mut self, cmd: SubscribeQuotes) -> anyhow::Result<()> { tracing::debug!("Subscribing to quotes for {}", cmd.instrument_id); + if self.has_subscription(cmd.instrument_id, SubscriptionType::Quotes)? { + tracing::debug!( + "Quote subscription already active for {}", + cmd.instrument_id + ); + return Ok(()); + } let client = self .ib_client @@ -844,11 +772,27 @@ impl DataClient for InteractiveBrokersDataClient { let subscription_token = self.cancellation_token.child_token(); + // Record the subscription before spawning so a fast-failing task cannot + // finish before its registry entry exists. + let subscription_id = next_subscription_id(); + lock(&self.subscriptions)?.insert( + (cmd.instrument_id, SubscriptionType::Quotes), + SubscriptionInfo { + cancellation_token: subscription_token.clone(), + id: subscription_id, + }, + ); + let guard = SubscriptionGuard { + subscriptions: Arc::clone(&self.subscriptions), + key: (cmd.instrument_id, SubscriptionType::Quotes), + id: subscription_id, + }; + // Spawn subscription task let client_clone = client.as_arc().clone(); - let subscription_token_clone = subscription_token.clone(); let ignore_size_updates = self.config.ignore_quote_tick_size_updates; let data_farm_state = Arc::clone(&self.data_farm_state); + let stream_config = StreamConfig::new(self.client_id, &self.config); let task = async move { if use_market_data { @@ -869,9 +813,10 @@ impl DataClient for InteractiveBrokersDataClient { data_sender, quote_cache, clock, - subscription_token_clone, + subscription_token, ignore_size_updates, Arc::clone(&data_farm_state), + stream_config, ) .await { @@ -893,9 +838,10 @@ impl DataClient for InteractiveBrokersDataClient { size_precision, data_sender.clone(), clock, - subscription_token_clone.clone(), + subscription_token.clone(), price_magnifier, Arc::clone(&data_farm_state), + stream_config, ) .await { @@ -918,9 +864,10 @@ impl DataClient for InteractiveBrokersDataClient { data_sender, quote_cache, clock, - subscription_token_clone, + subscription_token, ignore_size_updates, Arc::clone(&data_farm_state), + stream_config, ) .await { @@ -938,26 +885,13 @@ impl DataClient for InteractiveBrokersDataClient { } } } + guard.release(); }; self.session_tasks .spawn(task) .context("failed to register IB data subscription task")?; - // Record subscription - let mut subscriptions = self - .subscriptions - .try_lock() - .context("Failed to lock IB subscriptions")?; - subscriptions.insert( - cmd.instrument_id, - SubscriptionInfo { - instrument_id: cmd.instrument_id, - subscription_type: SubscriptionType::Quotes, - cancellation_token: subscription_token, - }, - ); - tracing::debug!( "Quote subscription started for {} (method: {})", cmd.instrument_id, @@ -973,6 +907,14 @@ impl DataClient for InteractiveBrokersDataClient { fn subscribe_index_prices(&mut self, cmd: SubscribeIndexPrices) -> anyhow::Result<()> { tracing::debug!("Subscribing to index prices for {}", cmd.instrument_id); + if self.has_subscription(cmd.instrument_id, SubscriptionType::IndexPrices)? { + tracing::debug!( + "Index price subscription already active for {}", + cmd.instrument_id + ); + return Ok(()); + } + let client = self .ib_client .as_ref() @@ -1010,9 +952,23 @@ impl DataClient for InteractiveBrokersDataClient { let subscription_token = self.cancellation_token.child_token(); + let subscription_id = next_subscription_id(); + lock(&self.subscriptions)?.insert( + (cmd.instrument_id, SubscriptionType::IndexPrices), + SubscriptionInfo { + cancellation_token: subscription_token.clone(), + id: subscription_id, + }, + ); + let guard = SubscriptionGuard { + subscriptions: Arc::clone(&self.subscriptions), + key: (cmd.instrument_id, SubscriptionType::IndexPrices), + id: subscription_id, + }; + let client_clone = client.as_arc().clone(); - let subscription_token_clone = subscription_token.clone(); let data_farm_state = Arc::clone(&self.data_farm_state); + let stream_config = StreamConfig::new(self.client_id, &self.config); let task = async move { if let Err(e) = handle_index_price_subscription( @@ -1023,8 +979,9 @@ impl DataClient for InteractiveBrokersDataClient { price_magnifier, data_sender, clock, - subscription_token_clone, + subscription_token, data_farm_state, + stream_config, ) .await { @@ -1034,25 +991,13 @@ impl DataClient for InteractiveBrokersDataClient { e ); } + guard.release(); }; self.session_tasks .spawn(task) .context("failed to register IB data subscription task")?; - let mut subscriptions = self - .subscriptions - .try_lock() - .context("Failed to lock IB subscriptions")?; - subscriptions.insert( - cmd.instrument_id, - SubscriptionInfo { - instrument_id: cmd.instrument_id, - subscription_type: SubscriptionType::IndexPrices, - cancellation_token: subscription_token, - }, - ); - tracing::debug!("Index price subscription started for {}", cmd.instrument_id); Ok(()) } @@ -1060,6 +1005,14 @@ impl DataClient for InteractiveBrokersDataClient { fn subscribe_option_greeks(&mut self, cmd: SubscribeOptionGreeks) -> anyhow::Result<()> { tracing::debug!("Subscribing to option greeks for {}", cmd.instrument_id); + if lock(&self.option_greeks_subscriptions)?.contains_key(&cmd.instrument_id) { + tracing::debug!( + "Option greeks subscription already active for {}", + cmd.instrument_id + ); + return Ok(()); + } + let client = self .ib_client .as_ref() @@ -1104,6 +1057,7 @@ impl DataClient for InteractiveBrokersDataClient { let subscription_token_clone = subscription_token.clone(); let client_clone = client.as_arc().clone(); let data_farm_state = Arc::clone(&self.data_farm_state); + let stream_config = StreamConfig::new(self.client_id, &self.config); let task = async move { if let Err(e) = handle_option_greeks_subscription( @@ -1115,6 +1069,7 @@ impl DataClient for InteractiveBrokersDataClient { clock, subscription_token_clone, data_farm_state, + stream_config, ) .await { @@ -1130,13 +1085,8 @@ impl DataClient for InteractiveBrokersDataClient { .spawn(task) .context("failed to register IB data subscription task")?; - let mut subscriptions = self - .option_greeks_subscriptions - .try_lock() - .context("Failed to lock IB option greeks subscriptions")?; - if let Some(existing) = subscriptions.insert(cmd.instrument_id, subscription_token) { - existing.cancel(); - } + let mut subscriptions = lock(&self.option_greeks_subscriptions)?; + subscriptions.insert(cmd.instrument_id, subscription_token); tracing::debug!( "Option greeks subscription started for {}", @@ -1148,11 +1098,9 @@ impl DataClient for InteractiveBrokersDataClient { fn unsubscribe_quotes(&mut self, cmd: &UnsubscribeQuotes) -> anyhow::Result<()> { tracing::debug!("Unsubscribing from quotes for {}", cmd.instrument_id); - let mut subscriptions = self - .subscriptions - .try_lock() - .context("Failed to lock IB subscriptions")?; - if let Some(sub_info) = subscriptions.remove(&cmd.instrument_id) { + let mut subscriptions = lock(&self.subscriptions)?; + if let Some(sub_info) = subscriptions.remove(&(cmd.instrument_id, SubscriptionType::Quotes)) + { sub_info.cancellation_token.cancel(); tracing::debug!("Unsubscribed from quotes for {}", cmd.instrument_id); } else { @@ -1174,11 +1122,10 @@ impl DataClient for InteractiveBrokersDataClient { fn unsubscribe_index_prices(&mut self, cmd: &UnsubscribeIndexPrices) -> anyhow::Result<()> { tracing::debug!("Unsubscribing from index prices for {}", cmd.instrument_id); - let mut subscriptions = self - .subscriptions - .try_lock() - .context("Failed to lock IB subscriptions")?; - if let Some(sub_info) = subscriptions.remove(&cmd.instrument_id) { + let mut subscriptions = lock(&self.subscriptions)?; + if let Some(sub_info) = + subscriptions.remove(&(cmd.instrument_id, SubscriptionType::IndexPrices)) + { sub_info.cancellation_token.cancel(); tracing::debug!("Unsubscribed from index prices for {}", cmd.instrument_id); } else { @@ -1194,10 +1141,7 @@ impl DataClient for InteractiveBrokersDataClient { fn unsubscribe_option_greeks(&mut self, cmd: &UnsubscribeOptionGreeks) -> anyhow::Result<()> { tracing::debug!("Unsubscribing from option greeks for {}", cmd.instrument_id); - let mut subscriptions = self - .option_greeks_subscriptions - .try_lock() - .context("Failed to lock IB option greeks subscriptions")?; + let mut subscriptions = lock(&self.option_greeks_subscriptions)?; if let Some(subscription_token) = subscriptions.remove(&cmd.instrument_id) { subscription_token.cancel(); tracing::debug!("Unsubscribed from option greeks for {}", cmd.instrument_id); @@ -1213,6 +1157,13 @@ impl DataClient for InteractiveBrokersDataClient { fn subscribe_trades(&mut self, cmd: SubscribeTrades) -> anyhow::Result<()> { tracing::debug!("Subscribing to trades for {}", cmd.instrument_id); + if self.has_subscription(cmd.instrument_id, SubscriptionType::Trades)? { + tracing::debug!( + "Trade subscription already active for {}", + cmd.instrument_id + ); + return Ok(()); + } let client = self .ib_client @@ -1253,10 +1204,24 @@ impl DataClient for InteractiveBrokersDataClient { // Create subscription-specific cancellation token let subscription_token = self.cancellation_token.child_token(); + let subscription_id = next_subscription_id(); + lock(&self.subscriptions)?.insert( + (cmd.instrument_id, SubscriptionType::Trades), + SubscriptionInfo { + cancellation_token: subscription_token.clone(), + id: subscription_id, + }, + ); + let guard = SubscriptionGuard { + subscriptions: Arc::clone(&self.subscriptions), + key: (cmd.instrument_id, SubscriptionType::Trades), + id: subscription_id, + }; + // Spawn subscription task let client_clone = client.as_arc().clone(); - let subscription_token_clone = subscription_token.clone(); let data_farm_state = Arc::clone(&self.data_farm_state); + let stream_config = StreamConfig::new(self.client_id, &self.config); let task = async move { if let Err(e) = handle_trade_subscription( @@ -1267,33 +1232,21 @@ impl DataClient for InteractiveBrokersDataClient { size_precision, data_sender, clock, - subscription_token_clone, + subscription_token, data_farm_state, + stream_config, ) .await { tracing::error!("Trade subscription error for {}: {:?}", instrument_id, e); } + guard.release(); }; self.session_tasks .spawn(task) .context("failed to register IB data subscription task")?; - // Record subscription - let mut subscriptions = self - .subscriptions - .try_lock() - .context("Failed to lock IB subscriptions")?; - subscriptions.insert( - cmd.instrument_id, - SubscriptionInfo { - instrument_id: cmd.instrument_id, - subscription_type: SubscriptionType::Trades, - cancellation_token: subscription_token, - }, - ); - tracing::debug!("Trade subscription started for {}", cmd.instrument_id); Ok(()) } @@ -1301,11 +1254,9 @@ impl DataClient for InteractiveBrokersDataClient { fn unsubscribe_trades(&mut self, cmd: &UnsubscribeTrades) -> anyhow::Result<()> { tracing::debug!("Unsubscribing from trades for {}", cmd.instrument_id); - let mut subscriptions = self - .subscriptions - .try_lock() - .context("Failed to lock IB subscriptions")?; - if let Some(sub_info) = subscriptions.remove(&cmd.instrument_id) { + let mut subscriptions = lock(&self.subscriptions)?; + if let Some(sub_info) = subscriptions.remove(&(cmd.instrument_id, SubscriptionType::Trades)) + { sub_info.cancellation_token.cancel(); tracing::debug!("Unsubscribed from trades for {}", cmd.instrument_id); } else { @@ -1328,6 +1279,10 @@ impl DataClient for InteractiveBrokersDataClient { // Get instrument from provider let instrument_id = cmd.bar_type.instrument_id(); + if self.has_subscription(instrument_id, SubscriptionType::Bars(cmd.bar_type))? { + tracing::debug!("Bar subscription already active for {}", cmd.bar_type); + return Ok(()); + } let instrument = self .instrument_provider .find(&instrument_id) @@ -1356,15 +1311,29 @@ impl DataClient for InteractiveBrokersDataClient { // realtime (reqRealTimeBars) and historical (reqHistoricalData) paths, per // the Java engine's whatToShowFor rule. Capture the flag before `contract` // is moved into the subscription task below. - let is_crypto = crate::common::parse::is_crypto_contract(&contract); + let is_crypto = is_crypto_contract(&contract); // Create subscription-specific cancellation token let subscription_token = self.cancellation_token.child_token(); + let subscription_id = next_subscription_id(); + lock(&self.subscriptions)?.insert( + (instrument_id, SubscriptionType::Bars(bar_type)), + SubscriptionInfo { + cancellation_token: subscription_token.clone(), + id: subscription_id, + }, + ); + let guard = SubscriptionGuard { + subscriptions: Arc::clone(&self.subscriptions), + key: (instrument_id, SubscriptionType::Bars(bar_type)), + id: subscription_id, + }; + // Spawn subscription task let client_clone = client.as_arc().clone(); - let subscription_token_clone = subscription_token.clone(); let data_farm_state = Arc::clone(&self.data_farm_state); + let stream_config = StreamConfig::new(self.client_id, &self.config); let task = async move { let result = if bar_type.spec().timedelta().as_secs() == 5 { @@ -1386,8 +1355,9 @@ impl DataClient for InteractiveBrokersDataClient { bar_timeout_tasks, handle_revised_bars, use_rth, - subscription_token_clone, + subscription_token, Arc::clone(&data_farm_state), + stream_config, ) .await } else { @@ -1406,8 +1376,9 @@ impl DataClient for InteractiveBrokersDataClient { data_sender, handle_revised_bars, clock, - subscription_token_clone, + subscription_token, Arc::clone(&data_farm_state), + stream_config, ) .await }; @@ -1415,26 +1386,13 @@ impl DataClient for InteractiveBrokersDataClient { if let Err(e) = result { tracing::error!("Bars subscription error for {}: {:?}", bar_type, e); } + guard.release(); }; self.session_tasks .spawn(task) .context("failed to register IB data subscription task")?; - // Record subscription - let mut subscriptions = self - .subscriptions - .try_lock() - .context("Failed to lock IB subscriptions")?; - subscriptions.insert( - instrument_id, - SubscriptionInfo { - instrument_id, - subscription_type: SubscriptionType::Bars, - cancellation_token: subscription_token, - }, - ); - tracing::debug!("Real-time bars subscription started for {}", bar_type); Ok(()) } @@ -1443,11 +1401,10 @@ impl DataClient for InteractiveBrokersDataClient { tracing::debug!("Unsubscribing from bars for {}", cmd.bar_type); let instrument_id = cmd.bar_type.instrument_id(); - let mut subscriptions = self - .subscriptions - .try_lock() - .context("Failed to lock IB subscriptions")?; - if let Some(sub_info) = subscriptions.remove(&instrument_id) { + let mut subscriptions = lock(&self.subscriptions)?; + if let Some(sub_info) = + subscriptions.remove(&(instrument_id, SubscriptionType::Bars(cmd.bar_type))) + { sub_info.cancellation_token.cancel(); tracing::debug!("Unsubscribed from bars for {}", cmd.bar_type); } else { @@ -1460,6 +1417,14 @@ impl DataClient for InteractiveBrokersDataClient { fn subscribe_book_deltas(&mut self, cmd: SubscribeBookDeltas) -> anyhow::Result<()> { tracing::debug!("Subscribing to book deltas for {}", cmd.instrument_id); + if self.has_subscription(cmd.instrument_id, SubscriptionType::BookDeltas)? { + tracing::debug!( + "Book delta subscription already active for {}", + cmd.instrument_id + ); + return Ok(()); + } + // Validate book type (IB doesn't support L3_MBO) if cmd.book_type == BookType::L3_MBO { tracing::error!( @@ -1508,10 +1473,24 @@ impl DataClient for InteractiveBrokersDataClient { .and_then(|params| params.get_str("is_smart_depth")) .is_none_or(parse_bool_param_value); + let subscription_id = next_subscription_id(); + lock(&self.subscriptions)?.insert( + (cmd.instrument_id, SubscriptionType::BookDeltas), + SubscriptionInfo { + cancellation_token: subscription_token.clone(), + id: subscription_id, + }, + ); + let guard = SubscriptionGuard { + subscriptions: Arc::clone(&self.subscriptions), + key: (cmd.instrument_id, SubscriptionType::BookDeltas), + id: subscription_id, + }; + // Spawn subscription task let client_clone = client.as_arc().clone(); - let subscription_token_clone = subscription_token.clone(); let data_farm_state = Arc::clone(&self.data_farm_state); + let stream_config = StreamConfig::new(self.client_id, &self.config); let task = async move { if let Err(e) = handle_market_depth_subscription( @@ -1524,8 +1503,9 @@ impl DataClient for InteractiveBrokersDataClient { is_smart_depth, data_sender, clock, - subscription_token_clone, + subscription_token, data_farm_state, + stream_config, ) .await { @@ -1535,26 +1515,13 @@ impl DataClient for InteractiveBrokersDataClient { e ); } + guard.release(); }; self.session_tasks .spawn(task) .context("failed to register IB data subscription task")?; - // Record subscription - let mut subscriptions = self - .subscriptions - .try_lock() - .context("Failed to lock IB subscriptions")?; - subscriptions.insert( - cmd.instrument_id, - SubscriptionInfo { - instrument_id: cmd.instrument_id, - subscription_type: SubscriptionType::BookDeltas, - cancellation_token: subscription_token, - }, - ); - tracing::debug!( "Market depth subscription started for {}", cmd.instrument_id @@ -1565,11 +1532,10 @@ impl DataClient for InteractiveBrokersDataClient { fn unsubscribe_book_deltas(&mut self, cmd: &UnsubscribeBookDeltas) -> anyhow::Result<()> { tracing::debug!("Unsubscribing from book deltas for {}", cmd.instrument_id); - let mut subscriptions = self - .subscriptions - .try_lock() - .context("Failed to lock IB subscriptions")?; - if let Some(sub_info) = subscriptions.remove(&cmd.instrument_id) { + let mut subscriptions = lock(&self.subscriptions)?; + if let Some(sub_info) = + subscriptions.remove(&(cmd.instrument_id, SubscriptionType::BookDeltas)) + { sub_info.cancellation_token.cancel(); tracing::debug!("Unsubscribed from book deltas for {}", cmd.instrument_id); } else { @@ -1727,7 +1693,7 @@ impl DataClient for InteractiveBrokersDataClient { "Parsed {} contract specs from ib_contracts JSON", contract_specs.len() ); - log::debug!("Parsed ib_contracts payload: {}", ib_contracts_json_str); + log::debug!("Parsed ib_contracts payload: {ib_contracts_json_str}"); contract_specs_to_load = contract_specs; } Ok(value) => { @@ -1777,7 +1743,7 @@ impl DataClient for InteractiveBrokersDataClient { if !contract_specs_to_load_clone.is_empty() { for contract_spec in contract_specs_to_load_clone { let contract = - match crate::common::contracts::parse_contract_from_json(&contract_spec) + match parse_contract_from_json(&contract_spec) { Ok(contract) => contract, Err(e) => { @@ -1910,7 +1876,11 @@ impl DataClient for InteractiveBrokersDataClient { .resolve_contract_for_instrument(cmd.instrument_id) .context("Failed to convert instrument_id to IB contract")?; - let number_of_ticks = cmd.limit.map_or(1000, |l| l.get() as i32).min(1000); + let limit = cmd + .limit + .map(|limit| limit.get()) + .or_else(|| cmd.start.is_none().then_some(HISTORICAL_TICK_DEFAULT_LIMIT)); + let number_of_ticks = limit.unwrap_or(1000).min(1000) as i32; let instrument_id = cmd.instrument_id; let data_sender = self.data_sender.clone(); @@ -1923,7 +1893,6 @@ impl DataClient for InteractiveBrokersDataClient { // Spawn async task to handle the request with pagination let client_clone = client.as_arc().clone(); - let limit = cmd.limit.map(|l| l.get()); let start_nanos_clone = start_nanos; let end_nanos_clone = end_nanos; let cmd_start = cmd.start; @@ -1957,10 +1926,8 @@ impl DataClient for InteractiveBrokersDataClient { .historical_ticks(&contract, number_of_ticks) .trading_hours(trading_hours); - if let Some(start) = current_start_date.as_ref().map(jiff_to_ib_datetime) { - builder = builder.starting(start); - } - + // IB accepts start or end on reqHistoricalTicks, not both; backward + // pagination anchors on the end and filters the start client-side. if let Some(end) = current_end_ib { builder = builder.ending(end); } @@ -1986,8 +1953,8 @@ impl DataClient for InteractiveBrokersDataClient { instrument_id, Some(apply_price_magnifier(tick.price_bid, price_magnifier)), Some(apply_price_magnifier(tick.price_ask, price_magnifier)), - Some(tick.size_bid as f64), - Some(tick.size_ask as f64), + tick.size_bid, + tick.size_ask, price_precision, size_precision, ts_event, @@ -2099,7 +2066,11 @@ impl DataClient for InteractiveBrokersDataClient { .resolve_contract_for_instrument(cmd.instrument_id) .context("Failed to convert instrument_id to IB contract")?; - let number_of_ticks = cmd.limit.map_or(1000, |l| l.get() as i32).min(1000); + let limit = cmd + .limit + .map(|limit| limit.get()) + .or_else(|| cmd.start.is_none().then_some(HISTORICAL_TICK_DEFAULT_LIMIT)); + let number_of_ticks = limit.unwrap_or(1000).min(1000) as i32; let instrument_id = cmd.instrument_id; let data_sender = self.data_sender.clone(); @@ -2112,7 +2083,6 @@ impl DataClient for InteractiveBrokersDataClient { // Spawn async task to handle the request with pagination let client_clone = client.as_arc().clone(); - let limit = cmd.limit.map(|l| l.get()); let start_nanos_clone = start_nanos; let end_nanos_clone = end_nanos; let cmd_start = cmd.start; @@ -2146,10 +2116,8 @@ impl DataClient for InteractiveBrokersDataClient { .historical_ticks(&contract, number_of_ticks) .trading_hours(trading_hours); - if let Some(start) = current_start_date.as_ref().map(jiff_to_ib_datetime) { - builder = builder.starting(start); - } - + // IB accepts start or end on reqHistoricalTicks, not both; backward + // pagination anchors on the end and filters the start client-side. if let Some(end) = current_end_ib { builder = builder.ending(end); } @@ -2174,10 +2142,18 @@ impl DataClient for InteractiveBrokersDataClient { // Generate trade ID from exchange and special conditions if available let trade_id = None; + let Some(size) = tick.size else { + tracing::warn!( + "Skipping historical trade tick with no size for {}", + instrument_id + ); + continue; + }; + match super::parse::parse_trade_tick( instrument_id, apply_price_magnifier(tick.price, price_magnifier), - tick.size as f64, + size, price_precision, size_precision, ts_event, @@ -2186,7 +2162,7 @@ impl DataClient for InteractiveBrokersDataClient { ) { Ok(trade_tick) => batch_trades.push(trade_tick), Err(e) => { - tracing::warn!("Failed to parse trade tick: {:?}", e); + log_tick_parse_error("trade", instrument_id, &e); } } } @@ -2293,20 +2269,14 @@ impl DataClient for InteractiveBrokersDataClient { .context("Failed to convert bar type to IB bar size")?; // Crypto trade-price bars require AGGTRADES (TWS rejects TRADES for crypto, // error 10299); mirror the Java engine's whatToShowFor rule. - let is_crypto = crate::common::parse::is_crypto_contract(&contract); + let is_crypto = is_crypto_contract(&contract); let ib_what_to_show = price_type_to_ib_what_to_show_for_security(cmd.bar_type.spec().price_type, is_crypto); // Calculate segments to break down the request if needed. // Omit the end date for continuous futures (IB error 10339). let is_continuous_future = contract.security_type == SecurityType::ContinuousFuture; - let segments = if let (Some(start), Some(end)) = (cmd.start, cmd.end) { - calculate_duration_segments(start, end) - } else { - let end_date = cmd.end.unwrap_or_else(jiff::Timestamp::now); - let duration = calculate_duration(cmd.start, cmd.end).unwrap_or_else(|_| 1i32.days()); - vec![(end_date, duration)] - }; + let segments = calculate_duration_segments(cmd.start, cmd.end, None); let segments = bar_request_segments(segments, is_continuous_future); let bar_type = cmd.bar_type; @@ -2360,13 +2330,10 @@ impl DataClient for InteractiveBrokersDataClient { } } Err(e) => { - tracing::error!( - "Historical data request failed for {} segment: {:?}", - bar_type, - e + tracing::warn!( + "Historical data request failed for {bar_type} segment ending at {seg_end:?}: {e}, emitting the bars already retrieved" ); - // We continue with other segments if one fails? - // For now keep going to return what we have + break; } } } @@ -2414,6 +2381,15 @@ impl DataClient for InteractiveBrokersDataClient { } impl InteractiveBrokersDataClient { + fn has_subscription( + &self, + instrument_id: InstrumentId, + subscription_type: SubscriptionType, + ) -> anyhow::Result { + let subscriptions = lock(&self.subscriptions)?; + Ok(subscriptions.contains_key(&(instrument_id, subscription_type))) + } + fn clear_bar_tracking_state(&self) { if let Ok(mut tasks) = self.bar_timeout_tasks.try_lock() { for task in tasks.values_mut() { @@ -2439,6 +2415,7 @@ impl Drop for InteractiveBrokersDataClient { #[cfg(test)] mod tests { + use nautilus_common::live::get_runtime; use rstest::rstest; use super::*; @@ -2454,42 +2431,6 @@ mod tests { assert_eq!(request_trading_hours(use_regular_trading_hours), expected); } - #[rstest] - fn test_retreat_historical_tick_end_datetime_subtracts_one_millisecond() { - let result = retreat_historical_tick_end_datetime(1_234_567_890).unwrap(); - - assert_eq!( - u64::try_from(result.as_nanosecond()).unwrap(), - 1_233_567_890 - ); - } - - #[rstest] - fn test_retreat_historical_tick_end_datetime_saturates_at_zero() { - let result = retreat_historical_tick_end_datetime(0).unwrap(); - - assert_eq!(result.as_nanosecond(), 0); - } - - #[rstest] - #[case(None, Some(jiff::Timestamp::from_second(2).unwrap()), 0, Some(10), true)] - #[case(Some(jiff::Timestamp::from_second(1).unwrap()), Some(jiff::Timestamp::from_second(2).unwrap()), 0, Some(10), true)] - #[case(Some(jiff::Timestamp::from_second(2).unwrap()), Some(jiff::Timestamp::from_second(1).unwrap()), 0, Some(10), false)] - #[case(Some(jiff::Timestamp::from_second(1).unwrap()), Some(jiff::Timestamp::from_second(2).unwrap()), 10, Some(10), false)] - #[case(Some(jiff::Timestamp::from_second(1).unwrap()), Some(jiff::Timestamp::from_second(2).unwrap()), 10, None, true)] - fn test_should_continue_historical_tick_pagination( - #[case] start: Option, - #[case] end: Option, - #[case] current_len: usize, - #[case] limit: Option, - #[case] expected: bool, - ) { - assert_eq!( - should_continue_historical_tick_pagination(start, end, current_len, limit), - expected - ); - } - #[rstest] #[case("true", true)] #[case("True", true)] @@ -2508,6 +2449,138 @@ mod tests { assert_eq!(datetime_to_unix_nanos(dt), UnixNanos::from(1_000_000_002)); } + #[rstest] + fn test_subscription_keys_keep_quote_and_trade_streams_independent() { + let instrument_id = InstrumentId::from("AAPL.NASDAQ"); + let quote_token = CancellationToken::new(); + let trade_token = CancellationToken::new(); + let mut subscriptions = AHashMap::new(); + subscriptions.insert( + (instrument_id, SubscriptionType::Quotes), + SubscriptionInfo { + cancellation_token: quote_token.clone(), + id: next_subscription_id(), + }, + ); + subscriptions.insert( + (instrument_id, SubscriptionType::Trades), + SubscriptionInfo { + cancellation_token: trade_token.clone(), + id: next_subscription_id(), + }, + ); + + let quote = subscriptions + .remove(&(instrument_id, SubscriptionType::Quotes)) + .unwrap(); + quote.cancellation_token.cancel(); + + assert!(quote_token.is_cancelled()); + assert!(!trade_token.is_cancelled()); + assert_eq!(subscriptions.len(), 1); + assert!(subscriptions.contains_key(&(instrument_id, SubscriptionType::Trades))); + } + + #[rstest] + fn test_subscription_keys_keep_bar_types_of_one_instrument_independent() { + let minute = BarType::from("AAPL.NASDAQ-1-MINUTE-LAST-EXTERNAL"); + let five_minute = BarType::from("AAPL.NASDAQ-5-MINUTE-LAST-EXTERNAL"); + let instrument_id = minute.instrument_id(); + let minute_token = CancellationToken::new(); + let five_minute_token = CancellationToken::new(); + let mut subscriptions = AHashMap::new(); + subscriptions.insert( + (instrument_id, SubscriptionType::Bars(minute)), + SubscriptionInfo { + cancellation_token: minute_token.clone(), + id: next_subscription_id(), + }, + ); + subscriptions.insert( + (instrument_id, SubscriptionType::Bars(five_minute)), + SubscriptionInfo { + cancellation_token: five_minute_token.clone(), + id: next_subscription_id(), + }, + ); + + let five = subscriptions + .remove(&(instrument_id, SubscriptionType::Bars(five_minute))) + .unwrap(); + five.cancellation_token.cancel(); + + assert!(five_minute_token.is_cancelled()); + assert!(!minute_token.is_cancelled()); + assert_eq!(subscriptions.len(), 1); + assert!(subscriptions.contains_key(&(instrument_id, SubscriptionType::Bars(minute)))); + } + + #[rstest] + fn test_subscription_guard_release_removes_only_matching_registration() { + let subscriptions = Arc::new(Mutex::new(AHashMap::new())); + let instrument_id = InstrumentId::from("AAPL.NASDAQ"); + let key = (instrument_id, SubscriptionType::Quotes); + + let first_id = next_subscription_id(); + subscriptions.lock().unwrap().insert( + key, + SubscriptionInfo { + cancellation_token: CancellationToken::new(), + id: first_id, + }, + ); + let stale_guard = SubscriptionGuard { + subscriptions: Arc::clone(&subscriptions), + key, + id: first_id, + }; + + // A newer registration for the same key must survive the stale guard. + let second_id = next_subscription_id(); + subscriptions.lock().unwrap().insert( + key, + SubscriptionInfo { + cancellation_token: CancellationToken::new(), + id: second_id, + }, + ); + stale_guard.release(); + assert!(subscriptions.lock().unwrap().contains_key(&key)); + + let current_guard = SubscriptionGuard { + subscriptions: Arc::clone(&subscriptions), + key, + id: second_id, + }; + current_guard.release(); + assert!(!subscriptions.lock().unwrap().contains_key(&key)); + } + + #[rstest] + fn test_subscription_guard_drop_removes_registration_without_release() { + let subscriptions = Arc::new(Mutex::new(AHashMap::new())); + let instrument_id = InstrumentId::from("AAPL.NASDAQ"); + let key = (instrument_id, SubscriptionType::Quotes); + + let id = next_subscription_id(); + subscriptions.lock().unwrap().insert( + key, + SubscriptionInfo { + cancellation_token: CancellationToken::new(), + id, + }, + ); + { + let _guard = SubscriptionGuard { + subscriptions: Arc::clone(&subscriptions), + key, + id, + }; + // Dropped without release(), as when a stream task panics. + } + assert!(!subscriptions.lock().unwrap().contains_key(&key)); + } + #[rstest] fn test_venue_is_none_for_default_routing() { // IB is a multi-venue adapter: `venue()` must be `None` so the data engine registers @@ -2537,6 +2610,11 @@ mod tests { let mut client = InteractiveBrokersDataClient::new(*IB_CLIENT_ID, config, provider).unwrap(); + client + .session_tasks + .spawn(async { std::future::pending::<()>().await }) + .unwrap(); + client.stop().unwrap(); assert!(client.cancellation_token.is_cancelled()); diff --git a/crates/adapters/interactive_brokers/src/data/mod.rs b/crates/adapters/interactive_brokers/src/data/mod.rs index 1155e22df211..d879460f5718 100644 --- a/crates/adapters/interactive_brokers/src/data/mod.rs +++ b/crates/adapters/interactive_brokers/src/data/mod.rs @@ -20,4 +20,6 @@ pub mod convert; pub mod core; pub mod parse; +mod streams; + pub use core::InteractiveBrokersDataClient; diff --git a/crates/adapters/interactive_brokers/src/data/parse.rs b/crates/adapters/interactive_brokers/src/data/parse.rs index 8a7ca94b43ba..3a52222ad58b 100644 --- a/crates/adapters/interactive_brokers/src/data/parse.rs +++ b/crates/adapters/interactive_brokers/src/data/parse.rs @@ -26,21 +26,31 @@ use nautilus_model::{ identifiers::{InstrumentId, TradeId}, types::{Price, Quantity}, }; +use thiserror::Error; + +#[derive(Debug, PartialEq, Error)] +#[error("quantity size={size} cannot be represented with precision={precision}")] +struct QuantityPrecisionError { + size: f64, + precision: u8, +} fn checked_quantity(size: f64, precision: u8) -> anyhow::Result { let quantity = Quantity::new_checked(size, precision) .map_err(|e| anyhow::anyhow!("invalid quantity size={size}: {e}"))?; let tolerance = 10_f64.powi(-i32::from(precision)) * 1e-9; if (quantity.as_f64() - size).abs() > tolerance { - anyhow::bail!( - "quantity size={} cannot be represented with precision={}", - size, - precision - ); + anyhow::bail!(QuantityPrecisionError { size, precision }); } Ok(quantity) } +/// Generates the stable fallback trade ID used when IB omits an execution identifier. +pub(crate) fn generate_ib_trade_id(ts_event: UnixNanos, price: f64, size: f64) -> TradeId { + let ts_secs = ts_event.as_i64() / 1_000_000_000; + TradeId::new(format!("{ts_secs}-{price}-{size}")) +} + /// Parse IB tick price and size data into a QuoteTick. /// /// This builds a quote from individual tick updates. You typically need to accumulate @@ -97,13 +107,13 @@ pub fn parse_trade_tick( ts_init: UnixNanos, trade_id: Option, ) -> anyhow::Result { - let trade_price = Price::new(price, price_precision); + let trade_price = Price::new_checked(price, price_precision) + .map_err(|e| anyhow::anyhow!("invalid trade price={price}: {e}"))?; let trade_size = checked_quantity(size, size_precision)?; let aggressor_side = AggressorSide::NoAggressor; // IB doesn't provide this directly - let trade_id = trade_id - .unwrap_or_else(|| crate::common::parse::generate_ib_trade_id(ts_event, price, size)); + let trade_id = trade_id.unwrap_or_else(|| generate_ib_trade_id(ts_event, price, size)); - Ok(TradeTick::new( + TradeTick::new_checked( instrument_id, trade_price, trade_size, @@ -111,7 +121,16 @@ pub fn parse_trade_tick( trade_id, ts_event, ts_init, - )) + ) + .map_err(|e| anyhow::anyhow!("invalid trade tick: {e}")) +} + +pub(super) fn log_tick_parse_error(kind: &str, instrument_id: InstrumentId, e: &anyhow::Error) { + if e.is::() { + tracing::debug!("Dropping IB {kind} tick for {instrument_id}: {e}"); + } else { + tracing::warn!("Failed to parse IB {kind} tick for {instrument_id}: {e}"); + } } /// Parse IB index price data into an [`IndexPriceUpdate`]. @@ -221,12 +240,12 @@ pub fn parse_realtime_bar( /// Parse IB market depth operation to BookAction. #[must_use] -pub fn parse_market_depth_operation(operation: i32) -> BookAction { +pub fn parse_market_depth_operation(operation: i32) -> Option { match operation { - 0 => BookAction::Add, - 1 => BookAction::Update, - 2 => BookAction::Delete, - _ => BookAction::Add, // Default to Add for unknown operations + 0 => Some(BookAction::Add), + 1 => Some(BookAction::Update), + 2 => Some(BookAction::Delete), + _ => None, } } @@ -369,7 +388,53 @@ mod tests { None, ); - assert!(result.is_err()); + let e = result.unwrap_err(); + assert_eq!( + e.downcast_ref::(), + Some(&QuantityPrecisionError { + size: 0.5, + precision: 0 + }) + ); + } + + #[rstest] + #[case(0.63462, 0)] + #[case(1.025, 2)] + fn test_quantity_precision_error_preserves_size(#[case] size: f64, #[case] precision: u8) { + let e = checked_quantity(size, precision).unwrap_err(); + assert_eq!( + e.downcast_ref::(), + Some(&QuantityPrecisionError { size, precision }) + ); + } + + #[rstest] + #[case(-1.0)] + #[case(f64::NAN)] + #[case(f64::INFINITY)] + fn test_invalid_quantities_are_not_expected_precision_drops(#[case] size: f64) { + let e = checked_quantity(size, 0).unwrap_err(); + assert!(!e.is::()); + } + + #[rstest] + fn test_parse_trade_tick_rejects_zero_size() { + let result = parse_trade_tick( + create_test_instrument_id(), + 150.25, + 0.0, + 2, + 0, + UnixNanos::new(1000), + UnixNanos::new(1000), + None, + ); + + assert_eq!( + result.unwrap_err().to_string(), + "invalid trade tick: invalid `Quantity` for 'size' not positive, was 0", + ); } #[rstest] @@ -454,26 +519,25 @@ mod tests { #[rstest] fn test_parse_market_depth_operation_insert() { let action = parse_market_depth_operation(0); - assert_eq!(action, BookAction::Add); + assert_eq!(action, Some(BookAction::Add)); } #[rstest] fn test_parse_market_depth_operation_update() { let action = parse_market_depth_operation(1); - assert_eq!(action, BookAction::Update); + assert_eq!(action, Some(BookAction::Update)); } #[rstest] fn test_parse_market_depth_operation_delete() { let action = parse_market_depth_operation(2); - assert_eq!(action, BookAction::Delete); + assert_eq!(action, Some(BookAction::Delete)); } #[rstest] fn test_parse_market_depth_operation_unknown() { let action = parse_market_depth_operation(99); - // Should default to Add for unknown operations - assert_eq!(action, BookAction::Add); + assert_eq!(action, None); } #[rstest] diff --git a/crates/adapters/interactive_brokers/src/data/core_streams.rs b/crates/adapters/interactive_brokers/src/data/streams.rs similarity index 56% rename from crates/adapters/interactive_brokers/src/data/core_streams.rs rename to crates/adapters/interactive_brokers/src/data/streams.rs index ca54f5b1feb2..2057bd8386eb 100644 --- a/crates/adapters/interactive_brokers/src/data/core_streams.rs +++ b/crates/adapters/interactive_brokers/src/data/streams.rs @@ -7,15 +7,19 @@ // You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html // ------------------------------------------------------------------------------------------------- +//! Interactive Brokers market data stream processing. + use std::{ collections::{HashMap, VecDeque}, fmt::Debug, + pin::Pin, sync::Arc, time::Duration, }; use ahash::AHashMap; use anyhow::Context; +use futures_util::Stream; use ibapi::{ ConnectivityStatus, Error, Notice, contracts::tick_types::TickType, @@ -34,24 +38,28 @@ use ibapi::{ subscriptions::{Subscription, SubscriptionItem}, }; use nautilus_common::{live::sender::EventSender, messages::DataEvent}; -use nautilus_core::{UnixNanos, time::AtomicTime}; +use nautilus_core::{DurationNanos, UnixNanos, time::AtomicTime}; use nautilus_live::task::TaskSlot; use nautilus_model::{ data::{Bar, BarType, BookOrder, Data, OrderBookDelta, QuoteTick, option_chain::OptionGreeks}, - enums::OrderSide, - identifiers::InstrumentId, + enums::{BookAction, OrderSide}, + identifiers::{ClientId, InstrumentId}, types::{Price, Quantity}, }; use parking_lot::Mutex; use tokio_util::sync::CancellationToken; -use crate::data::{ - cache::{OptionGreeksCache, QuoteCache}, - convert::{ib_bar_to_nautilus_bar, ib_timestamp_to_unix_nanos}, - parse::{ - parse_index_price, parse_market_depth_operation, parse_option_open_interest, - parse_quote_tick, parse_trade_tick, +use crate::{ + config::InteractiveBrokersDataClientConfig, + data::{ + cache::{OptionGreeksCache, QuoteCache, is_sentinel_price}, + convert::{bar_type_to_ib_bar_size, ib_bar_to_nautilus_bar, ib_timestamp_to_unix_nanos}, + parse::{ + log_tick_parse_error, parse_index_price, parse_market_depth_operation, + parse_option_open_interest, parse_quote_tick, parse_trade_tick, + }, }, + data_types::InteractiveBrokersSubscriptionIdle, }; enum StreamAction { @@ -60,6 +68,231 @@ enum StreamAction { Resubscribe, } +struct StreamContext { + stream_config: StreamConfig, + client: Arc, + data_sender: EventSender, + clock: &'static AtomicTime, + cancellation_token: CancellationToken, + data_farm_state: Arc, + recovery_scope: DataFarmRecoveryScope, + instrument_id: InstrumentId, + price_precision: u8, + size_precision: u8, +} + +type StreamFuture<'a, T> = Pin + Send + 'a>>; + +#[derive(Debug, Clone, Copy)] +pub(super) struct StreamConfig { + client_id: ClientId, + all_last_trades: bool, + idle_timeout_secs: Option, +} + +impl StreamConfig { + pub(super) fn new(client_id: ClientId, config: &InteractiveBrokersDataClientConfig) -> Self { + Self { + client_id, + all_last_trades: config.all_last_trades, + idle_timeout_secs: config.subscription_idle_timeout_secs, + } + } +} + +impl StreamContext { + fn monitor( + &self, + subscription: impl Into, + farm_generation: u64, + ) -> SubscriptionMonitor { + let timeout = self + .stream_config + .idle_timeout_secs + .map(Duration::from_secs); + SubscriptionMonitor { + client_id: self.stream_config.client_id, + instrument_id: self.instrument_id, + subscription: subscription.into(), + timeout, + deadline: timeout.and_then(|timeout| tokio::time::Instant::now().checked_add(timeout)), + last_data_received_ns: None, + data_sender: self.data_sender.clone(), + clock: self.clock, + cancellation_token: self.cancellation_token.clone(), + data_farm_state: Arc::clone(&self.data_farm_state), + recovery_scope: self.recovery_scope, + farm_generation, + } + } +} + +struct SubscriptionMonitor { + client_id: ClientId, + instrument_id: InstrumentId, + subscription: String, + timeout: Option, + deadline: Option, + last_data_received_ns: Option, + data_sender: EventSender, + clock: &'static AtomicTime, + cancellation_token: CancellationToken, + data_farm_state: Arc, + recovery_scope: DataFarmRecoveryScope, + farm_generation: u64, +} + +impl SubscriptionMonitor { + async fn wait_for_idle(&self) { + match self.deadline { + Some(deadline) => tokio::time::sleep_until(deadline).await, + None => std::future::pending::<()>().await, + } + } + + fn received_data(&mut self) { + if let Some(timeout) = self.timeout { + self.last_data_received_ns = Some(self.clock.get_time_ns().as_u64()); + self.deadline = tokio::time::Instant::now().checked_add(timeout); + } + } + + fn notify_idle(&mut self) -> bool { + if self.deadline.take().is_none() || self.cancellation_token.is_cancelled() { + return true; + } + let state = self.data_farm_state.state.lock(); + if state.recovery(self.recovery_scope).recovery_generation != self.farm_generation { + return true; + } + drop(state); + let Some(timeout) = self.timeout else { + return true; + }; + let now = self.clock.get_time_ns(); + let event = InteractiveBrokersSubscriptionIdle { + client_id: self.client_id, + instrument_id: self.instrument_id, + subscription: self.subscription.clone(), + idle_timeout_secs: timeout.as_secs(), + last_data_received_ns: self.last_data_received_ns, + ts_event: now, + ts_init: now, + }; + tracing::warn!( + "IB {} subscription for {} has received no market data for {}s", + self.subscription, + self.instrument_id, + timeout.as_secs(), + ); + self.data_sender + .send(DataEvent::Data(Data::from(event))) + .is_ok() + } +} + +async fn run_stream( + context: &StreamContext, + mut subscribe_fn: Subscribe, + mut process_fn: Process, +) -> anyhow::Result<()> +where + S: CancellableStream, + Subscribe: for<'a> FnMut(&'a StreamContext) -> StreamFuture<'a, anyhow::Result>, + Process: for<'a> FnMut( + &'a mut S, + &'a StreamContext, + u64, + ) -> StreamFuture<'a, anyhow::Result>, +{ + let mut farm_generation = if context.recovery_scope == DataFarmRecoveryScope::MarketData { + context.data_farm_state.recovery_generation() + } else { + context + .data_farm_state + .recovery_generation_for(context.recovery_scope) + }; + + loop { + if !wait_for_connection_or_cancel(context).await { + return Ok(()); + } + + let mut subscription = match subscribe_fn(context).await { + Ok(subscription) => subscription, + Err(e) => { + tracing::warn!( + "Failed to establish IB stream for {}: {e}", + context.instrument_id, + ); + tokio::select! { + () = context.cancellation_token.cancelled() => return Ok(()), + () = tokio::time::sleep(HISTORICAL_BAR_RETRY_DELAY) => continue, + } + } + }; + + match process_fn(&mut subscription, context, farm_generation).await? { + StreamAction::Stop | StreamAction::Continue => return Ok(()), + StreamAction::Resubscribe => { + subscription.cancel().await; + farm_generation = if context.recovery_scope == DataFarmRecoveryScope::MarketData { + context.data_farm_state.recovery_generation() + } else { + context + .data_farm_state + .recovery_generation_for(context.recovery_scope) + }; + + if context.client.is_connected() { + tokio::select! { + () = context.cancellation_token.cancelled() => return Ok(()), + () = tokio::time::sleep(HISTORICAL_BAR_RETRY_DELAY) => {} + } + } + } + } + } +} + +// ibapi 4.2 bounds `Subscription` by a crate-private decoder trait, so generic code +// cannot name `Subscription`; each streamed item type opts in here instead. +trait CancellableStream: Send + 'static { + async fn cancel(&mut self); +} + +macro_rules! impl_cancellable_stream { + ($($item:ty),+ $(,)?) => { + $( + impl CancellableStream for Subscription<$item> { + async fn cancel(&mut self) { + Subscription::cancel(self).await; + } + } + )+ + }; +} + +impl_cancellable_stream!( + HistoricalBarUpdate, + TickTypes, + ibapi::market_data::realtime::BidAsk, + ibapi::market_data::realtime::Trade, + RealtimeBar, + MarketDepths, +); + +async fn wait_for_connection_or_cancel(context: &StreamContext) -> bool { + while !context.client.is_connected() { + tokio::select! { + () = context.cancellation_token.cancelled() => return false, + () = tokio::time::sleep(HISTORICAL_BAR_RETRY_DELAY) => {} + } + } + + !context.cancellation_token.is_cancelled() +} + trait IntoSubscriptionTick { fn into_subscription_item(self) -> SubscriptionItem; } @@ -191,6 +424,22 @@ pub(super) struct DataFarmConnectionState { } impl DataFarmConnectionState { + fn recovery_pending(&self, notice: &Notice) -> bool { + let Some(farm) = DataFarmIdentity::from_notice(notice) else { + return false; + }; + + if notice.connectivity_status() != Some(ConnectivityStatus::Ok) { + return false; + } + + let state = self.state.lock(); + state.degraded_farms.contains_key(&farm) + || state + .degraded_scopes + .contains_key(&DataFarmRecoveryScope::from(farm.kind)) + } + pub(super) fn handle_notice(&self, notice: &Notice, clock: &'static AtomicTime) { let Some(farm) = DataFarmIdentity::from_notice(notice) else { return; @@ -333,7 +582,8 @@ impl DataFarmConnectionState { } fn is_subscription_disconnected_error(error: &Error) -> bool { - matches!(error, Error::Notice(notice) if notice.code == SUBSCRIPTION_DISCONNECTED_CODE) + matches!(error, Error::ConnectionReset) + || matches!(error, Error::Notice(notice) if notice.code == SUBSCRIPTION_DISCONNECTED_CODE) } fn earliest_data_loss_ns(first: Option, second: Option) -> Option { @@ -419,8 +669,10 @@ fn should_emit_historical_bar(bar: &Bar, start_ns: UnixNanos) -> bool { pub(super) async fn monitor_data_farm_notices( client: Arc, data_farm_state: Arc, + market_data_type: Option, clock: &'static AtomicTime, cancellation_token: CancellationToken, + is_connected: Arc, ) -> anyhow::Result<()> { let mut notices = client .notice_stream() @@ -431,14 +683,104 @@ pub(super) async fn monitor_data_farm_notices( () = cancellation_token.cancelled() => return Ok(()), notice = notices.next() => { let Some(notice) = notice else { + if !cancellation_token.is_cancelled() { + tracing::warn!( + "IB data farm notice stream ended; data client is disconnected", + ); + is_connected.store(false, std::sync::atomic::Ordering::Relaxed); + } return Ok(()); }; + + if let Some(market_data_type) = market_data_type + && data_farm_state.recovery_pending(¬ice) + { + if let Err(e) = client + .switch_market_data_type(market_data_type) + .await + { + is_connected.store(false, std::sync::atomic::Ordering::Relaxed); + return Err(e).context( + "Failed to restore IB market data type after reconnect; data client is disconnected", + ); + } + tracing::info!("Restored IB market data type to {market_data_type:?}"); + } data_farm_state.handle_notice(¬ice, clock); } } } } +#[derive(Debug)] +struct HistoricalBarStreamState { + first_start_ns: UnixNanos, + replay_start_ns: UnixNanos, + last_disconnection_ns: Option, + had_connection: bool, + completion: HistoricalBarCompletion, +} + +/// How long after its close a bar still waiting for IB's final update is published anyway. +/// +/// IB sends the final update of a keepUpToDate bar about 0.5 seconds after the bar closes, but +/// sends none when the last 5 seconds of the period had no trades. +const HISTORICAL_BAR_COMPLETION_GRACE: DurationNanos = DurationNanos::from_secs(2); + +/// Publishes each keepUpToDate bar once, when it completes. +/// +/// IB revises the open bar every 5 seconds and marks no update as final. A bar completes on +/// the first of: an update received after its close, the grace period after its close, or the +/// next bar's first update. It persists across resubscriptions, so a replayed history cannot +/// publish a bar twice. +#[derive(Debug, Default)] +struct HistoricalBarCompletion { + pending: Option, + last_published_ns: Option, +} + +impl HistoricalBarCompletion { + // Returns the bars this update completes, oldest first + fn observe(&mut self, bar: Bar, now: UnixNanos) -> Vec { + if self.last_published_ns.is_some_and(|ts| bar.ts_event <= ts) { + return Vec::new(); + } + let mut completed = Vec::new(); + + if let Some(pending) = self.pending.take() { + if pending.ts_event < bar.ts_event { + completed.push(pending); + } + } + + if bar.ts_event <= now { + completed.push(bar); + } else { + self.pending = Some(bar); + } + + if let Some(last) = completed.last() { + self.last_published_ns = Some(last.ts_event); + } + completed + } + + fn deadline(&self) -> Option { + self.pending + .as_ref() + .map(|bar| bar.ts_event.saturating_add(HISTORICAL_BAR_COMPLETION_GRACE)) + } + + fn expire(&mut self, now: UnixNanos) -> Option { + if self.deadline().is_none_or(|deadline| now < deadline) { + return None; + } + let bar = self.pending.take()?; + self.last_published_ns = Some(bar.ts_event); + Some(bar) + } +} + #[allow(clippy::too_many_arguments)] pub(super) async fn handle_historical_bars_subscription( client: Arc, @@ -454,222 +796,379 @@ pub(super) async fn handle_historical_bars_subscription( clock: &'static AtomicTime, cancellation_token: CancellationToken, data_farm_state: Arc, + stream_config: StreamConfig, ) -> anyhow::Result<()> { tracing::debug!("Starting historical bars subscription for {}", bar_type); - let first_start_ns = resolve_historical_bar_start_ns(start_ns, clock.get_time_ns()); - let mut last_disconnection_ns = None; - let mut farm_generation = - data_farm_state.recovery_generation_for(DataFarmRecoveryScope::HistoricalBars); - // Only stamp last_disconnection_ns after receiving real data, mirroring the Python - // adapter's _had_ib_connection guard. historical_data_streaming() returns Ok after - // sending the request; server-side errors arrive later via subscription.next(). A - // pre-data startup failure must not cap the warmup window to now. - let mut had_connection = false; - - loop { - if cancellation_token.is_cancelled() { - break; - } + let stream_state = Arc::new(tokio::sync::Mutex::new(HistoricalBarStreamState { + first_start_ns, + replay_start_ns: first_start_ns, + last_disconnection_ns: None, + had_connection: false, + completion: HistoricalBarCompletion::default(), + })); + let context = StreamContext { + stream_config, + client, + data_sender, + clock, + cancellation_token, + data_farm_state, + recovery_scope: DataFarmRecoveryScope::HistoricalBars, + instrument_id: bar_type.instrument_id(), + price_precision, + size_precision, + }; + let trading_hours = if use_rth { + TradingHours::Regular + } else { + TradingHours::Extended + }; - while !client.is_connected() { - if cancellation_token.is_cancelled() { - tracing::debug!("Historical bars subscription cancelled for {}", bar_type); - return Ok(()); - } - tokio::time::sleep(HISTORICAL_BAR_RETRY_DELAY).await; - } + run_stream( + &context, + |context| { + let contract = contract.clone(); + let stream_state = Arc::clone(&stream_state); + Box::pin(async move { + let replay_start_ns = { + let mut state = stream_state.lock().await; + state.replay_start_ns = resolve_historical_bar_replay_start_ns( + state.first_start_ns, + state.last_disconnection_ns, + ); + state.replay_start_ns + }; + let duration = calculate_historical_bar_subscription_duration( + bar_type, + replay_start_ns, + context.clock.get_time_ns(), + ); + context + .client + .historical_data(&contract, bar_type_to_historical_bar_size(bar_type)?) + .duration(duration) + .what_to_show(what_to_show) + .trading_hours(trading_hours) + .stream() + .await + .context("Failed to create historical bars subscription") + }) + }, + |subscription, context, farm_generation| { + let stream_state = Arc::clone(&stream_state); + Box::pin(process_historical_bar_stream( + subscription, + context, + farm_generation, + bar_type, + handle_revised_bars, + stream_state, + context.monitor(format!("bars:{bar_type}"), farm_generation), + )) + }, + ) + .await +} - let replay_start_ns = - resolve_historical_bar_replay_start_ns(first_start_ns, last_disconnection_ns); - let request_duration = calculate_historical_bar_subscription_duration( - bar_type, - replay_start_ns, - clock.get_time_ns(), - ); - let trading_hours = if use_rth { - TradingHours::Regular +async fn process_historical_bar_stream( + subscription: &mut S, + context: &StreamContext, + farm_generation: u64, + bar_type: BarType, + handle_revised_bars: bool, + stream_state: Arc>, + mut monitor: SubscriptionMonitor, +) -> anyhow::Result +where + S: CancellableStream + + Stream, Error>> + + Unpin, +{ + loop { + let completion_wait = if handle_revised_bars { + None } else { - TradingHours::Extended + let deadline = stream_state.lock().await.completion.deadline(); + deadline.map(|deadline| { + deadline + .duration_since(&context.clock.get_time_ns()) + .map_or(Duration::ZERO, Duration::from) + }) }; - let mut subscription = match client - .historical_data(&contract, bar_type_to_historical_bar_size(bar_type)?) - .duration(request_duration) - .what_to_show(what_to_show) - .trading_hours(trading_hours) - .stream() - .await - { - Ok(subscription) => subscription, - Err(e) => { - tracing::warn!( - "Failed to create historical bars subscription for {}: {:?}", - bar_type, - e - ); - if had_connection { - last_disconnection_ns = Some(clock.get_time_ns()); + tokio::select! { + biased; + () = context.cancellation_token.cancelled() => return Ok(StreamAction::Stop), + () = context.data_farm_state.wait_for_recovery_after_for( + DataFarmRecoveryScope::HistoricalBars, + farm_generation, + ) => { + record_historical_bar_recovery( + context, + farm_generation, + &stream_state, + ) + .await; + return Ok(StreamAction::Resubscribe); + } + () = monitor.wait_for_idle() => { + if !monitor.notify_idle() { + return Ok(StreamAction::Stop); } - - tokio::time::sleep(HISTORICAL_BAR_RETRY_DELAY).await; - continue; } - }; + () = sleep_or_pending(completion_wait) => { + let expired = stream_state + .lock() + .await + .completion + .expire(context.clock.get_time_ns()); - loop { - tokio::select! { - () = cancellation_token.cancelled() => { - tracing::debug!("Historical bars subscription cancelled for {}", bar_type); - subscription.cancel().await; - return Ok(()); + if let Some(bar) = expired + && context.data_sender.send(DataEvent::Data(Data::Bar(bar))).is_err() + { + return Ok(StreamAction::Stop); } - update = subscription.next() => { - match update { - Some(Ok(SubscriptionItem::Data(HistoricalBarUpdate::Historical(data)))) => { - had_connection = true; - - for ib_bar in &data.bars { - let bar = ib_bar_to_nautilus_bar( - ib_bar, - bar_type, - price_precision, - size_precision, - )?; - - if should_emit_historical_bar(&bar, replay_start_ns) - && data_sender.send(DataEvent::Data(Data::Bar(bar))).is_err() - { - return Ok(()); - } - } - } - Some(Ok(SubscriptionItem::Data(HistoricalBarUpdate::Update(ib_bar)))) => { - had_connection = true; - - if !handle_revised_bars { - continue; - } + } + update = subscription.next() => { + match update { + Some(Ok(SubscriptionItem::Data(HistoricalBarUpdate::Historical(data)))) => { + if !data.bars.is_empty() { monitor.received_data(); } + let replay_start_ns = mark_historical_bar_connected(&stream_state).await; + for ib_bar in &data.bars { let bar = ib_bar_to_nautilus_bar( - &ib_bar, + ib_bar, bar_type, - price_precision, - size_precision, + context.price_precision, + context.size_precision, )?; - if should_emit_historical_bar(&bar, replay_start_ns) - && data_sender.send(DataEvent::Data(Data::Bar(bar))).is_err() - { - return Ok(()); - } - } - Some(Ok(SubscriptionItem::Data(HistoricalBarUpdate::End { .. }))) => {} - Some(Ok(SubscriptionItem::Notice(notice))) => { - data_farm_state.handle_notice(¬ice, clock); - tracing::debug!( - "IB historical bars notice for {}: {} - {}", - bar_type, - notice.code, - notice.message - ); - } - Some(Err(e)) => { - if is_subscription_disconnected_error(&e) { - data_farm_state.mark_degraded_for( - DataFarmRecoveryScope::HistoricalBars, - farm_generation, - clock.get_time_ns(), - ); - tracing::warn!( - "Historical bars subscription disconnected for {}; waiting for data farm recovery: {:?}", - bar_type, - e - ); - - if !wait_for_data_farm_recovery_or_cancel_for( - &data_farm_state, - DataFarmRecoveryScope::HistoricalBars, - farm_generation, - &cancellation_token, - ) - .await - { - subscription.cancel().await; - return Ok(()); - } - let recovered_generation = data_farm_state - .recovery_generation_for(DataFarmRecoveryScope::HistoricalBars); - last_disconnection_ns = earliest_data_loss_ns( - last_disconnection_ns, - data_farm_state.recovery_since_ns_after_for( - DataFarmRecoveryScope::HistoricalBars, - farm_generation, - ), - ); - farm_generation = recovered_generation; - break; + if !should_emit_historical_bar(&bar, replay_start_ns) { + continue; } - tracing::warn!( - "Historical bars subscription ended for {}: {:?}", - bar_type, - e - ); - - if had_connection { - last_disconnection_ns = Some(clock.get_time_ns()); + if handle_revised_bars { + if context.data_sender.send(DataEvent::Data(Data::Bar(bar))).is_err() { + return Ok(StreamAction::Stop); + } + } else if !send_completed_historical_bars(context, &stream_state, bar).await { + return Ok(StreamAction::Stop); } - - break; } - None => { - if cancellation_token.is_cancelled() { - return Ok(()); - } + } + Some(Ok(SubscriptionItem::Data(HistoricalBarUpdate::Update(ib_bar)))) => { + monitor.received_data(); + let replay_start_ns = mark_historical_bar_connected(&stream_state).await; + let bar = ib_bar_to_nautilus_bar( + &ib_bar, + bar_type, + context.price_precision, + context.size_precision, + )?; - tracing::warn!( - "Historical bars subscription ended unexpectedly for {}", - bar_type - ); + if !should_emit_historical_bar(&bar, replay_start_ns) { + continue; + } - if had_connection { - last_disconnection_ns = Some(clock.get_time_ns()); + if handle_revised_bars { + if context.data_sender.send(DataEvent::Data(Data::Bar(bar))).is_err() { + return Ok(StreamAction::Stop); } - - break; + } else if !send_completed_historical_bars(context, &stream_state, bar).await { + return Ok(StreamAction::Stop); } } - } - () = data_farm_state.wait_for_recovery_after_for( - DataFarmRecoveryScope::HistoricalBars, - farm_generation, - ) => { - let recovered_generation = data_farm_state - .recovery_generation_for(DataFarmRecoveryScope::HistoricalBars); - last_disconnection_ns = earliest_data_loss_ns( - last_disconnection_ns, - data_farm_state.recovery_since_ns_after_for( + Some(Ok(SubscriptionItem::Data(HistoricalBarUpdate::End { .. }))) => {} + Some(Ok(SubscriptionItem::Notice(notice))) => { + context.data_farm_state.handle_notice(¬ice, context.clock); + } + Some(Err(e)) if is_subscription_disconnected_error(&e) => { + context.data_farm_state.mark_degraded_for( DataFarmRecoveryScope::HistoricalBars, farm_generation, - ), - ); - farm_generation = recovered_generation; - subscription.cancel().await; - break; + context.clock.get_time_ns(), + ); + + if !wait_for_data_farm_recovery_or_cancel_for( + &context.data_farm_state, + DataFarmRecoveryScope::HistoricalBars, + farm_generation, + &context.cancellation_token, + ) + .await + { + return Ok(StreamAction::Stop); + } + record_historical_bar_recovery( + context, + farm_generation, + &stream_state, + ) + .await; + return Ok(StreamAction::Resubscribe); + } + Some(Err(e)) => { + tracing::warn!( + "Historical bars subscription ended for {}: {:?}", + bar_type, + e, + ); + mark_historical_bar_disconnected(context, &stream_state).await; + return Ok(StreamAction::Resubscribe); + } + None => { + tracing::warn!( + "Historical bars subscription ended unexpectedly for {}", + bar_type, + ); + mark_historical_bar_disconnected(context, &stream_state).await; + return Ok(StreamAction::Resubscribe); + } } } } - - tokio::time::sleep(HISTORICAL_BAR_RETRY_DELAY).await; } +} - tracing::debug!("Historical bars subscription ended for {}", bar_type); - Ok(()) +// Returns false when the data channel is closed +async fn send_completed_historical_bars( + context: &StreamContext, + stream_state: &tokio::sync::Mutex, + bar: Bar, +) -> bool { + let completed = stream_state + .lock() + .await + .completion + .observe(bar, context.clock.get_time_ns()); + + completed.into_iter().all(|bar| { + context + .data_sender + .send(DataEvent::Data(Data::Bar(bar))) + .is_ok() + }) +} + +async fn sleep_or_pending(duration: Option) { + match duration { + Some(duration) => tokio::time::sleep(duration).await, + None => std::future::pending().await, + } +} + +async fn mark_historical_bar_connected( + stream_state: &tokio::sync::Mutex, +) -> UnixNanos { + let mut state = stream_state.lock().await; + state.had_connection = true; + state.replay_start_ns +} + +async fn mark_historical_bar_disconnected( + context: &StreamContext, + stream_state: &tokio::sync::Mutex, +) { + let mut state = stream_state.lock().await; + if state.had_connection { + state.last_disconnection_ns = Some(context.clock.get_time_ns()); + } +} + +async fn record_historical_bar_recovery( + context: &StreamContext, + farm_generation: u64, + stream_state: &tokio::sync::Mutex, +) { + let recovered_since = context + .data_farm_state + .recovery_since_ns_after_for(DataFarmRecoveryScope::HistoricalBars, farm_generation); + let mut state = stream_state.lock().await; + state.last_disconnection_ns = + earliest_data_loss_ns(state.last_disconnection_ns, recovered_since); } fn bar_type_to_historical_bar_size(bar_type: BarType) -> anyhow::Result { - crate::data::convert::bar_type_to_ib_bar_size(&bar_type) + bar_type_to_ib_bar_size(&bar_type) +} + +async fn process_market_data_stream( + subscription: &mut S, + context: &StreamContext, + farm_generation: u64, + mut process_fn: Process, + mut monitor: SubscriptionMonitor, +) -> anyhow::Result +where + S: CancellableStream + Stream, Error>> + Unpin, + Process: for<'a> FnMut( + Result, Error>, + &'a StreamContext, + ) -> StreamFuture<'a, anyhow::Result>, +{ + loop { + tokio::select! { + biased; + () = context.cancellation_token.cancelled() => return Ok(StreamAction::Stop), + () = context.data_farm_state.wait_for_recovery_after(farm_generation) => { + return Ok(StreamAction::Resubscribe); + } + () = monitor.wait_for_idle() => { + if !monitor.notify_idle() { + return Ok(StreamAction::Stop); + } + } + tick_result = subscription.next() => { + let Some(tick_result) = tick_result else { + tracing::warn!( + "IB market data stream ended unexpectedly for {}", + context.instrument_id, + ); + return Ok(StreamAction::Resubscribe); + }; + + if let Ok(SubscriptionItem::Data(tick)) = &tick_result + && !matches!(tick, TickTypes::SnapshotEnd | TickTypes::RequestParameters(_) | TickTypes::MarketDataType(_)) + { + monitor.received_data(); + } + + if let Err(e) = &tick_result + && is_subscription_disconnected_error(e) + { + context + .data_farm_state + .mark_degraded(farm_generation, context.clock.get_time_ns()); + tracing::warn!( + "IB market data stream disconnected for {}; waiting for data farm recovery: {:?}", + context.instrument_id, + e, + ); + + if !wait_for_data_farm_recovery_or_cancel( + &context.data_farm_state, + farm_generation, + &context.cancellation_token, + ) + .await + { + return Ok(StreamAction::Stop); + } + return Ok(StreamAction::Resubscribe); + } + + if let Ok(SubscriptionItem::Notice(notice)) = &tick_result { + context.data_farm_state.handle_notice(notice, context.clock); + } + + match process_fn(tick_result, context).await? { + StreamAction::Continue => {} + action => return Ok(action), + } + } + } + } } #[allow(clippy::too_many_arguments)] @@ -685,99 +1184,69 @@ pub(super) async fn handle_quote_subscription( cancellation_token: CancellationToken, ignore_size_updates: bool, data_farm_state: Arc, + stream_config: StreamConfig, ) -> anyhow::Result<()> { tracing::debug!("Starting quote subscription for {}", instrument_id); + let context = StreamContext { + stream_config, + client, + data_sender, + clock, + cancellation_token, + data_farm_state, + recovery_scope: DataFarmRecoveryScope::MarketData, + instrument_id, + price_precision, + size_precision, + }; - let mut farm_generation = data_farm_state.recovery_generation(); - - loop { - if cancellation_token.is_cancelled() { - break; - } - - let mut subscription = client - .market_data(&contract) - .streaming() - .subscribe() - .await - .context("Failed to create market data subscription")?; - - loop { - tokio::select! { - () = cancellation_token.cancelled() => { - tracing::debug!("Quote subscription cancelled for {}", instrument_id); - subscription.cancel().await; - return Ok(()); - } - () = data_farm_state.wait_for_recovery_after(farm_generation) => { - farm_generation = data_farm_state.recovery_generation(); - subscription.cancel().await; - break; - } - tick_result = subscription.next() => { - let Some(tick_result) = tick_result else { - tracing::debug!("Quote subscription stream ended for {}", instrument_id); - break; - }; - - if let Err(e) = &tick_result - && is_subscription_disconnected_error(e) - { - data_farm_state.mark_degraded(farm_generation, clock.get_time_ns()); - tracing::warn!( - "Quote subscription disconnected for {}; waiting for data farm recovery: {:?}", - instrument_id, - e - ); - - if !wait_for_data_farm_recovery_or_cancel( - &data_farm_state, - farm_generation, - &cancellation_token, - ) - .await - { - subscription.cancel().await; - return Ok(()); - } - farm_generation = data_farm_state.recovery_generation(); - break; - } - - if let Ok(SubscriptionItem::Notice(notice)) = &tick_result { - data_farm_state.handle_notice(notice, clock); - } - - let action = process_quote_tick_result( - tick_result, - instrument_id, - price_precision, - size_precision, - &data_sender, - "e_cache, - clock, - ignore_size_updates, - ) - .await?; - - match action { - StreamAction::Continue => {} - StreamAction::Stop => return Ok(()), - StreamAction::Resubscribe => { - farm_generation = data_farm_state.recovery_generation(); - subscription.cancel().await; - break; - } - } - } - } - } - } - - tracing::debug!("Quote subscription ended for {}", instrument_id); - Ok(()) + run_stream( + &context, + |context| { + let contract = contract.clone(); + Box::pin(async move { + context + .client + .market_data(&contract) + .streaming() + .subscribe() + .await + .context("Failed to create market data subscription") + }) + }, + |subscription, context, farm_generation| { + let quote_cache = Arc::clone("e_cache); + Box::pin(async move { + process_market_data_stream( + subscription, + context, + farm_generation, + |tick_result, context| { + let quote_cache = Arc::clone("e_cache); + Box::pin(async move { + process_quote_tick_result( + tick_result, + context.instrument_id, + context.price_precision, + context.size_precision, + &context.data_sender, + "e_cache, + context.clock, + ignore_size_updates, + ) + .await + }) + }, + context.monitor("quotes", farm_generation), + ) + .await + }) + }, + ) + .await } +#[allow(clippy::too_many_arguments)] // Stream state is supplied explicitly to the task. pub(super) async fn handle_option_greeks_subscription( client: Arc, contract: ibapi::contracts::Contract, @@ -787,95 +1256,64 @@ pub(super) async fn handle_option_greeks_subscription( clock: &'static AtomicTime, cancellation_token: CancellationToken, data_farm_state: Arc, + stream_config: StreamConfig, ) -> anyhow::Result<()> { tracing::debug!("Starting option greeks subscription for {}", instrument_id); + let context = StreamContext { + stream_config, + client, + data_sender, + clock, + cancellation_token, + data_farm_state, + recovery_scope: DataFarmRecoveryScope::MarketData, + instrument_id, + price_precision: 0, + size_precision: 0, + }; - let mut farm_generation = data_farm_state.recovery_generation(); - - loop { - if cancellation_token.is_cancelled() { - break; - } - - let mut subscription = client - .market_data(&contract) - .generic_ticks(&[IB_GENERIC_TICK_OPTION_OPEN_INTEREST]) - .streaming() - .subscribe() - .await - .context("Failed to create option greeks market data subscription")?; - - loop { - tokio::select! { - () = cancellation_token.cancelled() => { - tracing::debug!("Option greeks subscription cancelled for {}", instrument_id); - subscription.cancel().await; - return Ok(()); - } - () = data_farm_state.wait_for_recovery_after(farm_generation) => { - farm_generation = data_farm_state.recovery_generation(); - subscription.cancel().await; - break; - } - tick_result = subscription.next() => { - let Some(tick_result) = tick_result else { - tracing::debug!("Option greeks subscription stream ended for {}", instrument_id); - break; - }; - - if let Err(e) = &tick_result - && is_subscription_disconnected_error(e) - { - data_farm_state.mark_degraded(farm_generation, clock.get_time_ns()); - tracing::warn!( - "Option greeks subscription disconnected for {}; waiting for data farm recovery: {:?}", - instrument_id, - e - ); - - if !wait_for_data_farm_recovery_or_cancel( - &data_farm_state, - farm_generation, - &cancellation_token, - ) - .await - { - subscription.cancel().await; - return Ok(()); - } - farm_generation = data_farm_state.recovery_generation(); - break; - } - - if let Ok(SubscriptionItem::Notice(notice)) = &tick_result { - data_farm_state.handle_notice(notice, clock); - } - - let action = process_option_greeks_tick_result( - tick_result, - instrument_id, - &data_sender, - &option_greeks_cache, - clock, - ) - .await?; - - match action { - StreamAction::Continue => {} - StreamAction::Stop => return Ok(()), - StreamAction::Resubscribe => { - farm_generation = data_farm_state.recovery_generation(); - subscription.cancel().await; - break; - } - } - } - } - } - } - - tracing::debug!("Option greeks subscription ended for {}", instrument_id); - Ok(()) + run_stream( + &context, + |context| { + let contract = contract.clone(); + Box::pin(async move { + context + .client + .market_data(&contract) + .generic_ticks(&[IB_GENERIC_TICK_OPTION_OPEN_INTEREST]) + .streaming() + .subscribe() + .await + .context("Failed to create option greeks market data subscription") + }) + }, + |subscription, context, farm_generation| { + let option_greeks_cache = Arc::clone(&option_greeks_cache); + Box::pin(async move { + process_market_data_stream( + subscription, + context, + farm_generation, + |tick_result, context| { + let option_greeks_cache = Arc::clone(&option_greeks_cache); + Box::pin(async move { + process_option_greeks_tick_result( + tick_result, + context.instrument_id, + &context.data_sender, + &option_greeks_cache, + context.clock, + ) + .await + }) + }, + context.monitor("option_greeks", farm_generation), + ) + .await + }) + }, + ) + .await } #[allow(clippy::too_many_arguments)] @@ -889,94 +1327,59 @@ pub(super) async fn handle_index_price_subscription( clock: &'static AtomicTime, cancellation_token: CancellationToken, data_farm_state: Arc, + stream_config: StreamConfig, ) -> anyhow::Result<()> { tracing::debug!("Starting index price subscription for {}", instrument_id); + let context = StreamContext { + stream_config, + client, + data_sender, + clock, + cancellation_token, + data_farm_state, + recovery_scope: DataFarmRecoveryScope::MarketData, + instrument_id, + price_precision, + size_precision: 0, + }; - let mut farm_generation = data_farm_state.recovery_generation(); - - loop { - if cancellation_token.is_cancelled() { - break; - } - - let mut subscription = client - .market_data(&contract) - .streaming() - .subscribe() - .await - .context("Failed to create index market data subscription")?; - - loop { - tokio::select! { - () = cancellation_token.cancelled() => { - tracing::debug!("Index price subscription cancelled for {}", instrument_id); - subscription.cancel().await; - return Ok(()); - } - () = data_farm_state.wait_for_recovery_after(farm_generation) => { - farm_generation = data_farm_state.recovery_generation(); - subscription.cancel().await; - break; - } - tick_result = subscription.next() => { - let Some(tick_result) = tick_result else { - tracing::debug!("Index price subscription stream ended for {}", instrument_id); - break; - }; - - if let Err(e) = &tick_result - && is_subscription_disconnected_error(e) - { - data_farm_state.mark_degraded(farm_generation, clock.get_time_ns()); - tracing::warn!( - "Index price subscription disconnected for {}; waiting for data farm recovery: {:?}", - instrument_id, - e - ); - - if !wait_for_data_farm_recovery_or_cancel( - &data_farm_state, - farm_generation, - &cancellation_token, + run_stream( + &context, + |context| { + let contract = contract.clone(); + Box::pin(async move { + context + .client + .market_data(&contract) + .streaming() + .subscribe() + .await + .context("Failed to create index market data subscription") + }) + }, + move |subscription, context, farm_generation| { + Box::pin(process_market_data_stream( + subscription, + context, + farm_generation, + move |tick_result, context| { + Box::pin(async move { + process_index_price_tick_result( + tick_result, + context.instrument_id, + context.price_precision, + price_magnifier, + &context.data_sender, + context.clock, ) .await - { - subscription.cancel().await; - return Ok(()); - } - farm_generation = data_farm_state.recovery_generation(); - break; - } - - if let Ok(SubscriptionItem::Notice(notice)) = &tick_result { - data_farm_state.handle_notice(notice, clock); - } - - let action = process_index_price_tick_result( - tick_result, - instrument_id, - price_precision, - price_magnifier, - &data_sender, - clock, - ) - .await?; - - match action { - StreamAction::Continue => {} - StreamAction::Stop => return Ok(()), - StreamAction::Resubscribe => { - farm_generation = data_farm_state.recovery_generation(); - subscription.cancel().await; - break; - } - } - } - } - } - } - - Ok(()) + }) + }, + context.monitor("index_prices", farm_generation), + )) + }, + ) + .await } #[allow(clippy::too_many_arguments)] @@ -991,110 +1394,162 @@ pub(super) async fn handle_tick_by_tick_quote_subscription( cancellation_token: CancellationToken, price_magnifier: f64, data_farm_state: Arc, + stream_config: StreamConfig, ) -> anyhow::Result<()> { tracing::debug!( "Starting tick-by-tick quote subscription for {}", instrument_id ); - let mut farm_generation = data_farm_state.recovery_generation(); - - loop { - if cancellation_token.is_cancelled() { - break; - } + let context = StreamContext { + stream_config, + client, + data_sender, + clock, + cancellation_token, + data_farm_state, + recovery_scope: DataFarmRecoveryScope::MarketData, + instrument_id, + price_precision, + size_precision, + }; - let mut subscription = client - .tick_by_tick(&contract, 0) - .bid_ask(IgnoreSize::No) - .await - .context("Failed to create tick-by-tick bid/ask subscription")?; + run_stream( + &context, + |context| { + let contract = contract.clone(); + Box::pin(async move { + context + .client + .tick_by_tick(&contract, 0) + .bid_ask(IgnoreSize::No) + .await + .context("Failed to create tick-by-tick bid/ask subscription") + }) + }, + move |subscription, context, farm_generation| { + Box::pin(process_tick_by_tick_quote_stream( + subscription, + context, + farm_generation, + price_magnifier, + context.monitor("quotes", farm_generation), + )) + }, + ) + .await +} - loop { - tokio::select! { - () = cancellation_token.cancelled() => { - tracing::debug!("Tick-by-tick quote subscription cancelled for {}", instrument_id); - subscription.cancel().await; - return Ok(()); - } - () = data_farm_state.wait_for_recovery_after(farm_generation) => { - farm_generation = data_farm_state.recovery_generation(); - subscription.cancel().await; - break; +async fn process_tick_by_tick_quote_stream( + subscription: &mut S, + context: &StreamContext, + farm_generation: u64, + price_magnifier: f64, + mut monitor: SubscriptionMonitor, +) -> anyhow::Result +where + S: CancellableStream + + Stream, Error>> + + Unpin, +{ + loop { + tokio::select! { + biased; + () = context.cancellation_token.cancelled() => return Ok(StreamAction::Stop), + () = context.data_farm_state.wait_for_recovery_after(farm_generation) => { + return Ok(StreamAction::Resubscribe); + } + () = monitor.wait_for_idle() => { + if !monitor.notify_idle() { + return Ok(StreamAction::Stop); } - tick_result = subscription.next() => { - match tick_result { - Some(Ok(SubscriptionItem::Data(bid_ask))) => { - let ts_event = ib_timestamp_to_unix_nanos(&bid_ask.time); - let ts_init = clock.get_time_ns(); - - let bid_price = bid_ask.bid_price * price_magnifier; - let ask_price = bid_ask.ask_price * price_magnifier; - - match parse_quote_tick( - instrument_id, - Some(bid_price), - Some(ask_price), - Some(bid_ask.bid_size), - Some(bid_ask.ask_size), - price_precision, - size_precision, - ts_event, - ts_init, - ) { - Ok(quote_tick) => { - if data_sender - .send(DataEvent::Data(Data::Quote(quote_tick))) - .is_err() - { - return Ok(()); - } - } - Err(e) => tracing::warn!("Failed to parse quote tick: {:?}", e), - } - } - Some(Ok(SubscriptionItem::Notice(notice))) => { - data_farm_state.handle_notice(¬ice, clock); + } + tick_result = subscription.next() => { + match tick_result { + Some(Ok(SubscriptionItem::Data(bid_ask))) => { + monitor.received_data(); + + if is_sentinel_price(bid_ask.bid_price, Some(bid_ask.bid_size)) + || is_sentinel_price(bid_ask.ask_price, Some(bid_ask.ask_size)) + || !bid_ask.bid_size.is_finite() + || bid_ask.bid_size <= 0.0 + || !bid_ask.ask_size.is_finite() + || bid_ask.ask_size <= 0.0 + { tracing::debug!( - "IB tick-by-tick quote notice for {}: {} - {}", - instrument_id, - notice.code, - notice.message + "Ignoring incomplete IB quote for {}: bid={}@{}, ask={}@{}", + context.instrument_id, + bid_ask.bid_size, + bid_ask.bid_price, + bid_ask.ask_size, + bid_ask.ask_price, ); + continue; } - Some(Err(e)) if is_subscription_disconnected_error(&e) => { - data_farm_state.mark_degraded(farm_generation, clock.get_time_ns()); - tracing::warn!( - "Tick-by-tick quote subscription disconnected for {}; waiting for data farm recovery: {:?}", - instrument_id, - e - ); + let ts_event = ib_timestamp_to_unix_nanos(&bid_ask.time); + let ts_init = context.clock.get_time_ns(); + let quote_tick = parse_quote_tick( + context.instrument_id, + Some(bid_ask.bid_price * price_magnifier), + Some(bid_ask.ask_price * price_magnifier), + Some(bid_ask.bid_size), + Some(bid_ask.ask_size), + context.price_precision, + context.size_precision, + ts_event, + ts_init, + ); - if !wait_for_data_farm_recovery_or_cancel( - &data_farm_state, - farm_generation, - &cancellation_token, - ) - .await - { - subscription.cancel().await; - return Ok(()); + match quote_tick { + Ok(quote_tick) => { + if context + .data_sender + .send(DataEvent::Data(Data::Quote(quote_tick))) + .is_err() + { + return Ok(StreamAction::Stop); + } } - farm_generation = data_farm_state.recovery_generation(); - break; + Err(e) => log_tick_parse_error("quote", context.instrument_id, &e), } - Some(Err(e)) => { - tracing::debug!("Subscription error for {}: {:?}", instrument_id, e); - anyhow::bail!("Subscription error: {e:?}"); + } + Some(Ok(SubscriptionItem::Notice(notice))) => { + context.data_farm_state.handle_notice(¬ice, context.clock); + tracing::debug!( + "IB tick-by-tick quote notice for {}: {} - {}", + context.instrument_id, + notice.code, + notice.message, + ); + } + Some(Err(e)) if is_subscription_disconnected_error(&e) => { + context + .data_farm_state + .mark_degraded(farm_generation, context.clock.get_time_ns()); + if !wait_for_data_farm_recovery_or_cancel( + &context.data_farm_state, + farm_generation, + &context.cancellation_token, + ) + .await + { + return Ok(StreamAction::Stop); } - None => break, + return Ok(StreamAction::Resubscribe); + } + Some(Err(e)) => anyhow::bail!("Subscription error: {e:?}"), + None => { + tracing::warn!( + "IB tick-by-tick quote stream ended unexpectedly for {}; resubscribing", + context.instrument_id, + ); + + return Ok(StreamAction::Resubscribe); } } } } } - - Ok(()) } #[allow(clippy::too_many_arguments)] @@ -1108,42 +1563,52 @@ pub(super) async fn handle_trade_subscription( clock: &'static AtomicTime, cancellation_token: CancellationToken, data_farm_state: Arc, + stream_config: StreamConfig, ) -> anyhow::Result<()> { tracing::debug!("Starting trade subscription for {}", instrument_id); + let context = StreamContext { + stream_config, + client, + data_sender, + clock, + cancellation_token, + data_farm_state, + recovery_scope: DataFarmRecoveryScope::MarketData, + instrument_id, + price_precision, + size_precision, + }; - let mut farm_generation = data_farm_state.recovery_generation(); - - loop { - if cancellation_token.is_cancelled() { - break; - } - - let mut subscription = client - .tick_by_tick(&contract, 0) - .all_last() - .await - .context("Failed to create tick-by-tick trade subscription")?; - - let action = process_trade_stream( - &mut subscription, - instrument_id, - price_precision, - size_precision, - &data_sender, - clock, - &cancellation_token, - &data_farm_state, - farm_generation, - ) - .await?; - - match action { - StreamAction::Continue | StreamAction::Stop => break, - StreamAction::Resubscribe => farm_generation = data_farm_state.recovery_generation(), - } - } - - Ok(()) + run_stream( + &context, + |context| { + let contract = contract.clone(); + Box::pin(async move { + let builder = context.client.tick_by_tick(&contract, 0); + let subscription = if context.stream_config.all_last_trades { + builder.all_last().await + } else { + builder.last().await + }; + subscription.context("Failed to create tick-by-tick trade subscription") + }) + }, + |subscription, context, farm_generation| { + Box::pin(process_trade_stream( + subscription, + context.instrument_id, + context.price_precision, + context.size_precision, + &context.data_sender, + context.clock, + &context.cancellation_token, + &context.data_farm_state, + farm_generation, + context.monitor("trades", farm_generation), + )) + }, + ) + .await } #[allow(clippy::too_many_arguments)] @@ -1152,7 +1617,7 @@ pub(super) async fn handle_realtime_bars_subscription( contract: ibapi::contracts::Contract, bar_type: BarType, bar_type_str: String, - _instrument_id: InstrumentId, + instrument_id: InstrumentId, what_to_show: RealtimeWhatToShow, price_precision: u8, size_precision: u8, @@ -1164,6 +1629,7 @@ pub(super) async fn handle_realtime_bars_subscription( use_rth: bool, cancellation_token: CancellationToken, data_farm_state: Arc, + stream_config: StreamConfig, ) -> anyhow::Result<()> { tracing::debug!("Starting bars subscription for {}", bar_type); let trading_hours = if use_rth { @@ -1172,45 +1638,60 @@ pub(super) async fn handle_realtime_bars_subscription( TradingHours::Extended }; - let mut farm_generation = data_farm_state.recovery_generation(); - - loop { - if cancellation_token.is_cancelled() { - break; - } - - let mut subscription = client - .realtime_bars(&contract) - .what_to_show(what_to_show) - .trading_hours(trading_hours) - .subscribe() - .await - .context("Failed to create realtime bars subscription")?; - - let action = process_realtime_bar_stream( - &mut subscription, - bar_type, - &bar_type_str, - price_precision, - size_precision, - &data_sender, - &last_bars, - &bar_timeout_tasks, - handle_revised_bars, - &cancellation_token, - &data_farm_state, - farm_generation, - clock, - ) - .await?; - - match action { - StreamAction::Continue | StreamAction::Stop => break, - StreamAction::Resubscribe => farm_generation = data_farm_state.recovery_generation(), - } - } + let context = StreamContext { + stream_config, + client, + data_sender, + clock, + cancellation_token, + data_farm_state, + recovery_scope: DataFarmRecoveryScope::MarketData, + instrument_id, + price_precision, + size_precision, + }; - Ok(()) + run_stream( + &context, + |context| { + let contract = contract.clone(); + Box::pin(async move { + context + .client + .realtime_bars(&contract) + .what_to_show(what_to_show) + .trading_hours(trading_hours) + .subscribe() + .await + .context("Failed to create realtime bars subscription") + }) + }, + |subscription, context, farm_generation| { + let bar_type_str = bar_type_str.clone(); + let last_bars = Arc::clone(&last_bars); + let bar_timeout_tasks = Arc::clone(&bar_timeout_tasks); + Box::pin(async move { + process_realtime_bar_stream( + subscription, + bar_type, + &bar_type_str, + context.price_precision, + context.size_precision, + &context.data_sender, + &last_bars, + &bar_timeout_tasks, + handle_revised_bars, + &context.cancellation_token, + &context.data_farm_state, + farm_generation, + context.clock, + context.monitor(format!("bars:{bar_type}"), farm_generation), + ) + .await + }) + }, + ) + .await } async fn update_revised_bar_tracking( @@ -1227,8 +1708,8 @@ async fn update_revised_bar_tracking( } #[allow(clippy::too_many_arguments)] -async fn process_trade_stream( - subscription: &mut Subscription, +async fn process_trade_stream( + subscription: &mut S, instrument_id: InstrumentId, price_precision: u8, size_precision: u8, @@ -1237,9 +1718,16 @@ async fn process_trade_stream( cancellation_token: &CancellationToken, data_farm_state: &DataFarmConnectionState, farm_generation: u64, -) -> anyhow::Result { + mut monitor: SubscriptionMonitor, +) -> anyhow::Result +where + S: CancellableStream + + Stream, Error>> + + Unpin, +{ loop { tokio::select! { + biased; () = cancellation_token.cancelled() => { tracing::debug!("Trade subscription cancelled for {}", instrument_id); subscription.cancel().await; @@ -1249,9 +1737,28 @@ async fn process_trade_stream( subscription.cancel().await; return Ok(StreamAction::Resubscribe); } + () = monitor.wait_for_idle() => { + if !monitor.notify_idle() { + return Ok(StreamAction::Stop); + } + } tick_result = subscription.next() => { match tick_result { Some(Ok(SubscriptionItem::Data(tick))) => { + monitor.received_data(); + + if is_sentinel_price(tick.price, Some(tick.size)) + || !tick.size.is_finite() + || tick.size <= 0.0 + { + tracing::debug!( + "Ignoring IB trade sentinel for {}: price={}, size={}", + instrument_id, + tick.price, + tick.size + ); + continue; + } let ts_event = ib_timestamp_to_unix_nanos(&tick.time); let ts_init = clock.get_time_ns(); @@ -1270,7 +1777,7 @@ async fn process_trade_stream( return Ok(StreamAction::Stop); } } - Err(e) => tracing::warn!("Failed to parse trade tick: {:?}", e), + Err(e) => log_tick_parse_error("trade", instrument_id, &e), } } Some(Ok(SubscriptionItem::Notice(notice))) => { @@ -1305,7 +1812,14 @@ async fn process_trade_stream( Some(Err(e)) => { anyhow::bail!("Subscription error: {e:?}"); } - None => return Ok(StreamAction::Stop), + None => { + tracing::warn!( + "IB trade stream ended unexpectedly for {}; resubscribing", + instrument_id, + ); + + return Ok(StreamAction::Resubscribe); + } } } } @@ -1313,8 +1827,8 @@ async fn process_trade_stream( } #[allow(clippy::too_many_arguments)] -async fn process_realtime_bar_stream( - subscription: &mut Subscription, +async fn process_realtime_bar_stream( + subscription: &mut S, bar_type: BarType, bar_type_str: &str, price_precision: u8, @@ -1327,9 +1841,14 @@ async fn process_realtime_bar_stream( data_farm_state: &DataFarmConnectionState, farm_generation: u64, clock: &'static AtomicTime, -) -> anyhow::Result { + mut monitor: SubscriptionMonitor, +) -> anyhow::Result +where + S: CancellableStream + Stream, Error>> + Unpin, +{ loop { tokio::select! { + biased; () = cancellation_token.cancelled() => { tracing::debug!("Bars subscription cancelled for {}", bar_type); subscription.cancel().await; @@ -1339,9 +1858,15 @@ async fn process_realtime_bar_stream( subscription.cancel().await; return Ok(StreamAction::Resubscribe); } + () = monitor.wait_for_idle() => { + if !monitor.notify_idle() { + return Ok(StreamAction::Stop); + } + } bar_result = subscription.next() => { match bar_result { Some(Ok(SubscriptionItem::Data(bar))) => { + monitor.received_data(); let parsed_bar = ib_bar_to_nautilus_bar( &HistoricalBar { date: bar.date.into(), @@ -1404,7 +1929,14 @@ async fn process_realtime_bar_stream( Some(Err(e)) => { anyhow::bail!("Subscription error: {e:?}"); } - None => return Ok(StreamAction::Stop), + None => { + tracing::warn!( + "IB realtime bar stream ended unexpectedly for {}; resubscribing", + bar_type, + ); + + return Ok(StreamAction::Resubscribe); + } } } } @@ -1412,8 +1944,8 @@ async fn process_realtime_bar_stream( } #[allow(clippy::too_many_arguments)] -async fn process_market_depth_stream( - subscription: &mut Subscription, +async fn process_market_depth_stream( + subscription: &mut S, instrument_id: InstrumentId, price_precision: u8, size_precision: u8, @@ -1422,11 +1954,24 @@ async fn process_market_depth_stream( cancellation_token: &CancellationToken, data_farm_state: &DataFarmConnectionState, farm_generation: u64, -) -> anyhow::Result { + mut monitor: SubscriptionMonitor, +) -> anyhow::Result +where + S: CancellableStream + Stream, Error>> + Unpin, +{ let mut sequence: u64 = 0; + let mut l2_order_ids = AHashMap::new(); + let mut next_l2_order_id = 1_u64; + + // Each (re)established stream re-sends the full depth, so clear any levels + // left in the engine book by a previous stream before applying its deltas. + if !send_depth_stream_clear(instrument_id, sequence, data_sender, clock) { + return Ok(StreamAction::Stop); + } loop { tokio::select! { + biased; () = cancellation_token.cancelled() => { subscription.cancel().await; return Ok(StreamAction::Stop); @@ -1435,14 +1980,36 @@ async fn process_market_depth_stream( subscription.cancel().await; return Ok(StreamAction::Resubscribe); } + () = monitor.wait_for_idle() => { + if !monitor.notify_idle() { + return Ok(StreamAction::Stop); + } + } depth_result = subscription.next() => { match depth_result { + Some(Ok(SubscriptionItem::Data(MarketDepths::Reset))) => { + monitor.received_data(); + l2_order_ids.clear(); + sequence += 1; + + if !send_depth_stream_clear(instrument_id, sequence, data_sender, clock) { + return Ok(StreamAction::Stop); + } + } Some(Ok(SubscriptionItem::Data(MarketDepths::MarketDepth(depth)))) => { + monitor.received_data(); let ts_event = clock.get_time_ns(); let ts_init = ts_event; - sequence += 1; let order_side = if depth.side == 1 { OrderSide::Buy } else { OrderSide::Sell }; - let action = parse_market_depth_operation(depth.operation); + let Some(action) = parse_market_depth_operation(depth.operation) else { + tracing::warn!( + "Ignoring unknown IB depth operation {} for {}", + depth.operation, + instrument_id + ); + continue; + }; + sequence += 1; let price = Price::new(depth.price, price_precision); let size = Quantity::new(depth.size, size_precision); let order_id = depth.position as u64; @@ -1462,22 +2029,27 @@ async fn process_market_depth_stream( } } Some(Ok(SubscriptionItem::Data(MarketDepths::MarketDepthL2(depth)))) => { + monitor.received_data(); let ts_event = clock.get_time_ns(); let ts_init = ts_event; - sequence += 1; let order_side = if depth.side == 1 { OrderSide::Buy } else { OrderSide::Sell }; - let action = parse_market_depth_operation(depth.operation); + let Some(action) = parse_market_depth_operation(depth.operation) else { + tracing::warn!( + "Ignoring unknown IB L2 depth operation {} for {}", + depth.operation, + instrument_id + ); + continue; + }; + sequence += 1; let price = Price::new(depth.price, price_precision); let size = Quantity::new(depth.size, size_precision); - let order_id = (depth.position as u64 * 1000) - + (depth - .market_maker - .as_bytes() - .iter() - .take(3) - .map(|&b| b as u64) - .sum::() - % 1000); + let order_key = (depth.position, depth.market_maker.clone()); + let order_id = *l2_order_ids.entry(order_key.clone()).or_insert_with(|| { + let order_id = next_l2_order_id; + next_l2_order_id += 1; + order_id + }); let order = BookOrder::new(order_side, price, size, order_id); let delta = OrderBookDelta::new( instrument_id, @@ -1492,6 +2064,10 @@ async fn process_market_depth_stream( if data_sender.send(DataEvent::Data(Data::BookDelta(delta))).is_err() { return Ok(StreamAction::Stop); } + + if action == BookAction::Delete { + l2_order_ids.remove(&order_key); + } } Some(Ok(SubscriptionItem::Notice(notice))) => { data_farm_state.handle_notice(¬ice, clock); @@ -1523,13 +2099,33 @@ async fn process_market_depth_stream( return Ok(StreamAction::Resubscribe); } Some(Err(e)) => anyhow::bail!("Subscription error: {e:?}"), - None => return Ok(StreamAction::Stop), + None => { + tracing::warn!( + "IB market depth stream ended unexpectedly for {}; resubscribing", + instrument_id, + ); + + return Ok(StreamAction::Resubscribe); + } } } } } } +fn send_depth_stream_clear( + instrument_id: InstrumentId, + sequence: u64, + data_sender: &EventSender, + clock: &'static AtomicTime, +) -> bool { + let ts_clear = clock.get_time_ns(); + let clear = OrderBookDelta::clear(instrument_id, sequence, ts_clear, ts_clear); + data_sender + .send(DataEvent::Data(Data::BookDelta(clear))) + .is_ok() +} + #[allow(clippy::too_many_arguments)] pub(super) async fn handle_market_depth_subscription( client: Arc, @@ -1543,45 +2139,62 @@ pub(super) async fn handle_market_depth_subscription( clock: &'static AtomicTime, cancellation_token: CancellationToken, data_farm_state: Arc, + stream_config: StreamConfig, ) -> anyhow::Result<()> { - let mut farm_generation = data_farm_state.recovery_generation(); - - loop { - if cancellation_token.is_cancelled() { - break; - } + let context = StreamContext { + stream_config, + client, + data_sender, + clock, + cancellation_token, + data_farm_state, + recovery_scope: DataFarmRecoveryScope::MarketData, + instrument_id, + price_precision, + size_precision, + }; - let mut subscription = client - .market_depth(&contract, depth_rows) - .smart_depth(if is_smart_depth { - SmartDepth::Yes - } else { - SmartDepth::No + run_stream( + &context, + |context| { + let contract = contract.clone(); + Box::pin(async move { + let subscription = context + .client + .market_depth(&contract, depth_rows) + .smart_depth(if is_smart_depth { + SmartDepth::Yes + } else { + SmartDepth::No + }) + .subscribe() + .await + .context("Failed to create market depth subscription")?; + let ts_clear = context.clock.get_time_ns(); + let clear = OrderBookDelta::clear(context.instrument_id, 0, ts_clear, ts_clear); + context + .data_sender + .send(DataEvent::Data(Data::BookDelta(clear))) + .map_err(|e| anyhow::anyhow!("Failed to send order book clear: {e}"))?; + Ok(subscription) }) - .subscribe() - .await - .context("Failed to create market depth subscription")?; - - let action = process_market_depth_stream( - &mut subscription, - instrument_id, - price_precision, - size_precision, - &data_sender, - clock, - &cancellation_token, - &data_farm_state, - farm_generation, - ) - .await?; - - match action { - StreamAction::Continue | StreamAction::Stop => break, - StreamAction::Resubscribe => farm_generation = data_farm_state.recovery_generation(), - } - } - - Ok(()) + }, + |subscription, context, farm_generation| { + Box::pin(process_market_depth_stream( + subscription, + context.instrument_id, + context.price_precision, + context.size_precision, + &context.data_sender, + context.clock, + &context.cancellation_token, + &context.data_farm_state, + farm_generation, + context.monitor("order_book", farm_generation), + )) + }, + ) + .await } #[allow(clippy::too_many_arguments)] @@ -1766,6 +2379,14 @@ where Ok(SubscriptionItem::Data(TickTypes::Price(price))) if matches!(price.tick_type, TickType::Last) => { + if is_sentinel_price(price.price, None) { + tracing::debug!( + "Ignoring IB index price sentinel for {}: {}", + instrument_id, + price.price + ); + return Ok(StreamAction::Continue); + } let ts_event = clock.get_time_ns(); let ts_init = ts_event; let index_price = parse_index_price( @@ -1788,6 +2409,14 @@ where Ok(SubscriptionItem::Data(TickTypes::PriceSize(price_size))) if matches!(price_size.price_tick_type, TickType::Last) => { + if is_sentinel_price(price_size.price, Some(price_size.size)) { + tracing::debug!( + "Ignoring IB index price sentinel for {}: {}", + instrument_id, + price_size.price + ); + return Ok(StreamAction::Continue); + } let ts_event = clock.get_time_ns(); let ts_init = ts_event; let index_price = parse_index_price( @@ -1826,6 +2455,7 @@ fn update_quote_from_price_tick( TickType::Bid | TickType::DelayedBid => cache.update_bid_price( instrument_id, price.price, + None, price_precision, size_precision, ts_event, @@ -1834,6 +2464,7 @@ fn update_quote_from_price_tick( TickType::Ask | TickType::DelayedAsk => cache.update_ask_price( instrument_id, price.price, + None, price_precision, size_precision, ts_event, @@ -1892,6 +2523,7 @@ fn update_quote_from_price_size_tick( TickType::Bid | TickType::DelayedBid => cache.update_bid_price( instrument_id, price_size.price, + Some(price_size.size), price_precision, size_precision, ts_event, @@ -1900,6 +2532,7 @@ fn update_quote_from_price_size_tick( TickType::Ask | TickType::DelayedAsk => cache.update_ask_price( instrument_id, price_size.price, + Some(price_size.size), price_precision, size_precision, ts_event, @@ -1961,7 +2594,7 @@ fn send_option_greeks( ) -> StreamAction { if let Some(option_greeks) = greeks && data_sender - .send(DataEvent::OptionGreeks(option_greeks)) + .send(DataEvent::Data(Data::OptionGreeks(option_greeks))) .is_err() { tracing::warn!( @@ -2008,26 +2641,37 @@ mod tests { Bar as RealtimeBar, MarketDepth, MarketDepthL2, MarketDepths, TickAttribute, TickGeneric, TickPrice, TickPriceSize, TickSize, TickTypes, Trade, TradeAttribute, }, - subscriptions::{Subscription, SubscriptionItem}, + subscriptions::SubscriptionItem, }; use nautilus_common::messages::DataEvent; use nautilus_core::{UnixNanos, time::get_atomic_clock_realtime}; use nautilus_live::task::TaskSlot; use nautilus_model::{ - data::{BarType, Data}, + data::{Bar, BarType, Data}, + enums::BookAction, identifiers::{InstrumentId, Symbol, Venue}, + types::{Price, Quantity}, }; use rstest::rstest; use rust_decimal_macros::dec; use tokio_util::sync::CancellationToken; use super::{ - DataFarmConnectionState, DataFarmIdentity, DataFarmKind, DataFarmRecoveryScope, - OptionGreeksCache, QuoteCache, StreamAction, process_index_price_tick_result, - process_market_depth_stream, process_option_greeks_tick_result, process_quote_tick_result, - process_realtime_bar_stream, process_trade_stream, send_quote_tick, - update_quote_from_price_tick, update_revised_bar_tracking, + AtomicTime, CancellableStream, ClientId, DataFarmConnectionState, DataFarmIdentity, + DataFarmKind, DataFarmRecoveryScope, InteractiveBrokersSubscriptionIdle, OptionGreeksCache, + QuoteCache, StreamAction, StreamConfig, SubscriptionMonitor, + process_index_price_tick_result, process_market_depth_stream, + process_option_greeks_tick_result, process_quote_tick_result, process_realtime_bar_stream, + process_trade_stream, send_quote_tick, update_quote_from_price_tick, + update_revised_bar_tracking, }; + use crate::stubs::ChannelSubscription; + + impl CancellableStream for ChannelSubscription { + async fn cancel(&mut self) { + self.close(); + } + } fn instrument_id() -> InstrumentId { InstrumentId::new(Symbol::from("SPX"), Venue::from("CBOE")) @@ -2039,6 +2683,7 @@ mod tests { fn notice(code: i32, message: &str) -> Notice { Notice { + request_id: None, code, message: message.to_string(), error_time: None, @@ -2126,6 +2771,20 @@ mod tests { ); } + #[rstest] + fn test_data_farm_state_reports_pending_market_data_recovery() { + let clock = get_atomic_clock_realtime(); + let data_farm_state = DataFarmConnectionState::default(); + let recovered = notice(2104, "Market data farm connection is OK:usfarm"); + + assert!(!data_farm_state.recovery_pending(&recovered)); + data_farm_state.mark_degraded(data_farm_state.recovery_generation(), UnixNanos::from(10)); + assert!(data_farm_state.recovery_pending(&recovered)); + + data_farm_state.handle_notice(&recovered, clock); + assert!(!data_farm_state.recovery_pending(&recovered)); + } + #[tokio::test] async fn test_historical_bar_recovery_tracks_market_data_recovery() { let clock = get_atomic_clock_realtime(); @@ -2503,7 +3162,7 @@ mod tests { } #[tokio::test] - async fn test_process_quote_tick_result_emits_quote_after_bid_and_ask_prices() { + async fn test_process_quote_tick_result_waits_for_sizes_after_bid_and_ask_prices() { let instrument_id = instrument_id(); let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel::(); let clock = get_atomic_clock_realtime(); @@ -2545,17 +3204,11 @@ mod tests { assert!(matches!(bid_action, StreamAction::Continue)); assert!(matches!(ask_action, StreamAction::Continue)); - match receiver.recv().await.unwrap() { - DataEvent::Data(Data::Quote(quote)) => { - assert_eq!(quote.bid_price.as_f64(), 100.0); - assert_eq!(quote.ask_price.as_f64(), 101.0); - } - other => panic!("unexpected event: {other:?}"), - } + assert!(receiver.try_recv().is_err()); } #[tokio::test] - async fn test_process_quote_tick_result_emits_quote_from_delayed_price_ticks() { + async fn test_process_quote_tick_result_waits_for_delayed_sizes() { let instrument_id = instrument_id(); let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel::(); let clock = get_atomic_clock_realtime(); @@ -2597,13 +3250,7 @@ mod tests { assert!(matches!(bid_action, StreamAction::Continue)); assert!(matches!(ask_action, StreamAction::Continue)); - match receiver.recv().await.unwrap() { - DataEvent::Data(Data::Quote(quote)) => { - assert_eq!(quote.bid_price.as_decimal(), dec!(312.44)); - assert_eq!(quote.ask_price.as_decimal(), dec!(312.45)); - } - other => panic!("unexpected event: {other:?}"), - } + assert!(receiver.try_recv().is_err()); } #[tokio::test] @@ -2737,12 +3384,91 @@ mod tests { .await .unwrap(); process_quote_tick_result( - Ok::<_, &'static str>(TickTypes::PriceSize(TickPriceSize { - price_tick_type: TickType::Ask, - price: 100.5, + Ok::<_, &'static str>(TickTypes::PriceSize(TickPriceSize { + price_tick_type: TickType::Ask, + price: 100.5, + attributes: TickAttribute::default(), + size_tick_type: TickType::AskSize, + size: 9.0, + })), + instrument_id, + 2, + 0, + &sender.clone().into(), + "e_cache, + clock, + false, + ) + .await + .unwrap(); + + match receiver.recv().await.unwrap() { + DataEvent::Data(Data::Quote(quote)) => { + assert_eq!(quote.bid_price.as_f64(), 99.5); + assert_eq!(quote.bid_size.as_f64(), 7.0); + assert_eq!(quote.ask_price.as_f64(), 100.5); + assert_eq!(quote.ask_size.as_f64(), 9.0); + } + other => panic!("unexpected event: {other:?}"), + } + } + + #[tokio::test] + async fn test_quote_sentinel_clears_side_and_suppresses_emission() { + let instrument_id = instrument_id(); + let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel::(); + let clock = get_atomic_clock_realtime(); + let quote_cache = Arc::new(tokio::sync::Mutex::new(QuoteCache::new())); + + for (tick_type, size_type, price, size) in [ + (TickType::Bid, TickType::BidSize, 99.5, 7.0), + (TickType::Ask, TickType::AskSize, 100.5, 9.0), + ] { + process_quote_tick_result( + Ok::<_, &'static str>(TickTypes::PriceSize(TickPriceSize { + price_tick_type: tick_type, + price, + attributes: TickAttribute::default(), + size_tick_type: size_type, + size, + })), + instrument_id, + 2, + 0, + &sender.clone().into(), + "e_cache, + clock, + false, + ) + .await + .unwrap(); + } + assert!(matches!( + receiver.try_recv().unwrap(), + DataEvent::Data(Data::Quote(_)) + )); + + process_quote_tick_result( + Ok::<_, &'static str>(TickTypes::Price(TickPrice { + tick_type: TickType::Bid, + price: -1.0, + attributes: TickAttribute::default(), + })), + instrument_id, + 2, + 0, + &sender.clone().into(), + "e_cache, + clock, + false, + ) + .await + .unwrap(); + process_quote_tick_result( + Ok::<_, &'static str>(TickTypes::Price(TickPrice { + tick_type: TickType::Ask, + price: 101.0, attributes: TickAttribute::default(), - size_tick_type: TickType::AskSize, - size: 9.0, })), instrument_id, 2, @@ -2755,15 +3481,7 @@ mod tests { .await .unwrap(); - match receiver.recv().await.unwrap() { - DataEvent::Data(Data::Quote(quote)) => { - assert_eq!(quote.bid_price.as_f64(), 99.5); - assert_eq!(quote.bid_size.as_f64(), 7.0); - assert_eq!(quote.ask_price.as_f64(), 100.5); - assert_eq!(quote.ask_size.as_f64(), 9.0); - } - other => panic!("unexpected event: {other:?}"), - } + assert!(receiver.try_recv().is_err()); } #[tokio::test] @@ -2806,11 +3524,43 @@ mod tests { .await .unwrap(); - let initial = receiver.recv().await.unwrap(); - match initial { + assert!(receiver.try_recv().is_err()); + + process_quote_tick_result( + Ok::<_, &'static str>(TickTypes::Size(TickSize { + tick_type: TickType::BidSize, + size: 12.0, + })), + instrument_id, + 2, + 0, + &sender.clone().into(), + "e_cache, + clock, + true, + ) + .await + .unwrap(); + process_quote_tick_result( + Ok::<_, &'static str>(TickTypes::Size(TickSize { + tick_type: TickType::AskSize, + size: 13.0, + })), + instrument_id, + 2, + 0, + &sender.clone().into(), + "e_cache, + clock, + true, + ) + .await + .unwrap(); + + match receiver.recv().await.unwrap() { DataEvent::Data(Data::Quote(quote)) => { - assert_eq!(quote.bid_size.as_f64(), 0.0); - assert_eq!(quote.ask_size.as_f64(), 0.0); + assert_eq!(quote.bid_size.as_f64(), 12.0); + assert_eq!(quote.ask_size.as_f64(), 13.0); } other => panic!("unexpected event: {other:?}"), } @@ -2818,7 +3568,7 @@ mod tests { let action = process_quote_tick_result( Ok::<_, &'static str>(TickTypes::Size(TickSize { tick_type: TickType::BidSize, - size: 12.0, + size: 14.0, })), instrument_id, 2, @@ -2844,6 +3594,7 @@ mod tests { let action = process_quote_tick_result( Ok::<_, &'static str>(SubscriptionItem::Notice(Notice { + request_id: None, code: 162, message: String::from("Market data subscription cancelled"), error_time: None, @@ -2989,7 +3740,7 @@ mod tests { assert!(matches!(model_action, StreamAction::Continue)); match receiver.recv().await.unwrap() { - DataEvent::OptionGreeks(greeks) => { + DataEvent::Data(Data::OptionGreeks(greeks)) => { assert_eq!(greeks.delta, 0.55); assert_eq!(greeks.gamma, 0.02); assert_eq!(greeks.vega, 0.15); @@ -3014,6 +3765,7 @@ mod tests { let action = process_option_greeks_tick_result( Ok::<_, &'static str>(SubscriptionItem::Notice(Notice { + request_id: None, code: 162, message: String::from("Market data subscription cancelled"), error_time: None, @@ -3070,14 +3822,13 @@ mod tests { let instrument_id = instrument_id(); let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel::(); let (trade_sender, trade_receiver) = tokio::sync::mpsc::unbounded_channel(); - let mut subscription = Subscription::new(trade_receiver); + let mut subscription = ChannelSubscription::new(trade_receiver); let cancellation_token = CancellationToken::new(); let clock = get_atomic_clock_realtime(); let data_farm_state = DataFarmConnectionState::default(); trade_sender .send(Ok(Trade { - tick_type: String::from("Last"), time: time::OffsetDateTime::UNIX_EPOCH, price: 4500.25, size: 3.0, @@ -3101,6 +3852,7 @@ mod tests { &cancellation_token, &data_farm_state, data_farm_state.recovery_generation(), + disabled_monitor(), ) .await .unwrap(); @@ -3119,7 +3871,7 @@ mod tests { let instrument_id = instrument_id(); let (sender, _receiver) = tokio::sync::mpsc::unbounded_channel::(); let (trade_sender, trade_receiver) = tokio::sync::mpsc::unbounded_channel(); - let mut subscription = Subscription::new(trade_receiver); + let mut subscription = ChannelSubscription::new(trade_receiver); let cancellation_token = CancellationToken::new(); let clock = get_atomic_clock_realtime(); let data_farm_state = Arc::new(DataFarmConnectionState::default()); @@ -3162,6 +3914,7 @@ mod tests { &cancellation_token, &data_farm_state, farm_generation, + disabled_monitor(), ) .await .unwrap(); @@ -3177,7 +3930,7 @@ mod tests { let bar_type_str = bar_type.to_string(); let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel::(); let (bar_sender, bar_receiver) = tokio::sync::mpsc::unbounded_channel(); - let mut subscription = Subscription::new(bar_receiver); + let mut subscription = ChannelSubscription::new(bar_receiver); let cancellation_token = CancellationToken::new(); let last_bars = Arc::new(tokio::sync::Mutex::new(AHashMap::new())); let timeout_tasks = Arc::new(tokio::sync::Mutex::new(AHashMap::new())); @@ -3212,6 +3965,7 @@ mod tests { &data_farm_state, data_farm_state.recovery_generation(), clock, + disabled_monitor(), ) .await .unwrap(); @@ -3231,7 +3985,7 @@ mod tests { let instrument_id = instrument_id(); let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel::(); let (depth_sender, depth_receiver) = tokio::sync::mpsc::unbounded_channel(); - let mut subscription = Subscription::new(depth_receiver); + let mut subscription = ChannelSubscription::new(depth_receiver); let cancellation_token = CancellationToken::new(); let clock = get_atomic_clock_realtime(); let data_farm_state = DataFarmConnectionState::default(); @@ -3256,6 +4010,17 @@ mod tests { smart_depth: true, }))) .unwrap(); + depth_sender + .send(Ok(MarketDepths::MarketDepthL2(MarketDepthL2 { + position: 2, + market_maker: String::from("M1M"), + operation: 0, + side: 0, + price: 101.5, + size: 8.0, + smart_depth: true, + }))) + .unwrap(); drop(depth_sender); process_market_depth_stream( @@ -3268,10 +4033,19 @@ mod tests { &cancellation_token, &data_farm_state, data_farm_state.recovery_generation(), + disabled_monitor(), ) .await .unwrap(); + match receiver.recv().await.unwrap() { + DataEvent::Data(Data::BookDelta(delta)) => { + assert_eq!(delta.action, BookAction::Clear); + assert_eq!(delta.sequence, 0); + } + other => panic!("unexpected event: {other:?}"), + } + match receiver.recv().await.unwrap() { DataEvent::Data(Data::BookDelta(delta)) => { assert_eq!(delta.sequence, 1); @@ -3281,16 +4055,211 @@ mod tests { other => panic!("unexpected event: {other:?}"), } - match receiver.recv().await.unwrap() { + let first_l2_order_id = match receiver.recv().await.unwrap() { DataEvent::Data(Data::BookDelta(delta)) => { assert_eq!(delta.sequence, 2); assert_eq!(delta.order.price.as_f64(), 101.0); assert_eq!(delta.order.size.as_f64(), 7.0); + delta.order.order_id + } + other => panic!("unexpected event: {other:?}"), + }; + + match receiver.recv().await.unwrap() { + DataEvent::Data(Data::BookDelta(delta)) => { + assert_eq!(delta.sequence, 3); + assert_eq!(delta.order.price.as_f64(), 101.5); + assert_eq!(delta.order.size.as_f64(), 8.0); + assert_ne!(delta.order.order_id, first_l2_order_id); } other => panic!("unexpected event: {other:?}"), } } + #[tokio::test] + async fn test_process_market_depth_stream_clears_before_resubscribed_deltas() { + let instrument_id = instrument_id(); + let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel::(); + let cancellation_token = CancellationToken::new(); + let clock = get_atomic_clock_realtime(); + let data_farm_state = DataFarmConnectionState::default(); + + for _ in 0..2 { + let (depth_sender, depth_receiver) = tokio::sync::mpsc::unbounded_channel(); + let mut subscription = ChannelSubscription::new(depth_receiver); + depth_sender + .send(Ok(MarketDepths::MarketDepth(MarketDepth { + position: 1, + operation: 0, + side: 1, + price: 100.0, + size: 5.0, + }))) + .unwrap(); + drop(depth_sender); + + let action = process_market_depth_stream( + &mut subscription, + instrument_id, + 2, + 0, + &sender.clone().into(), + clock, + &cancellation_token, + &data_farm_state, + data_farm_state.recovery_generation(), + disabled_monitor(), + ) + .await + .unwrap(); + assert!(matches!(action, StreamAction::Resubscribe)); + } + + for _ in 0..2 { + match receiver.recv().await.unwrap() { + DataEvent::Data(Data::BookDelta(delta)) => { + assert_eq!(delta.action, BookAction::Clear); + assert_eq!(delta.sequence, 0); + } + other => panic!("unexpected event: {other:?}"), + } + + match receiver.recv().await.unwrap() { + DataEvent::Data(Data::BookDelta(delta)) => { + assert_eq!(delta.action, BookAction::Add); + assert_eq!(delta.sequence, 1); + assert_eq!(delta.order.price.as_f64(), 100.0); + } + other => panic!("unexpected event: {other:?}"), + } + } + } + + #[tokio::test] + async fn test_process_market_depth_stream_clears_book_on_reset() { + let instrument_id = instrument_id(); + let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel::(); + let cancellation_token = CancellationToken::new(); + let clock = get_atomic_clock_realtime(); + let data_farm_state = DataFarmConnectionState::default(); + let (depth_sender, depth_receiver) = tokio::sync::mpsc::unbounded_channel(); + let mut subscription = ChannelSubscription::new(depth_receiver); + depth_sender + .send(Ok(MarketDepths::MarketDepth(MarketDepth { + position: 1, + operation: 0, + side: 1, + price: 100.0, + size: 5.0, + }))) + .unwrap(); + depth_sender.send(Ok(MarketDepths::Reset)).unwrap(); + depth_sender + .send(Ok(MarketDepths::MarketDepth(MarketDepth { + position: 1, + operation: 0, + side: 1, + price: 101.0, + size: 6.0, + }))) + .unwrap(); + drop(depth_sender); + + let action = process_market_depth_stream( + &mut subscription, + instrument_id, + 2, + 0, + &sender.clone().into(), + clock, + &cancellation_token, + &data_farm_state, + data_farm_state.recovery_generation(), + disabled_monitor(), + ) + .await + .unwrap(); + assert!(matches!(action, StreamAction::Resubscribe)); + + let mut deltas = Vec::new(); + + while let Ok(event) = receiver.try_recv() { + match event { + DataEvent::Data(Data::BookDelta(delta)) => deltas.push(delta), + other => panic!("unexpected event: {other:?}"), + } + } + + let observed: Vec<_> = deltas + .iter() + .map(|delta| (delta.action, delta.sequence, delta.order.price.as_f64())) + .collect(); + assert_eq!( + observed, + vec![ + (BookAction::Clear, 0, 0.0), + (BookAction::Add, 1, 100.0), + (BookAction::Clear, 2, 0.0), + (BookAction::Add, 3, 101.0), + ] + ); + } + + #[tokio::test] + async fn test_process_trade_stream_emits_negative_price_and_skips_sentinel() { + let instrument_id = instrument_id(); + let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel::(); + let (trade_sender, trade_receiver) = tokio::sync::mpsc::unbounded_channel(); + let mut subscription = ChannelSubscription::new(trade_receiver); + let cancellation_token = CancellationToken::new(); + let clock = get_atomic_clock_realtime(); + let data_farm_state = DataFarmConnectionState::default(); + + for (price, size) in [(-1.0, 0.0), (-1.0, 3.0), (-1.25, 2.0)] { + trade_sender + .send(Ok(Trade { + time: time::OffsetDateTime::UNIX_EPOCH, + price, + size, + trade_attribute: TradeAttribute { + past_limit: false, + unreported: false, + }, + exchange: String::from("CBOE"), + special_conditions: String::new(), + })) + .unwrap(); + } + drop(trade_sender); + + process_trade_stream( + &mut subscription, + instrument_id, + 2, + 0, + &sender.clone().into(), + clock, + &cancellation_token, + &data_farm_state, + data_farm_state.recovery_generation(), + disabled_monitor(), + ) + .await + .unwrap(); + + let mut trades = Vec::new(); + + while let Ok(event) = receiver.try_recv() { + match event { + DataEvent::Data(Data::Trade(trade)) => { + trades.push((trade.price.as_f64(), trade.size.as_f64())); + } + other => panic!("unexpected event: {other:?}"), + } + } + assert_eq!(trades, vec![(-1.0, 3.0), (-1.25, 2.0)]); + } + #[rstest] fn test_send_quote_tick_returns_continue_for_none() { let instrument_id = instrument_id(); @@ -3320,4 +4289,323 @@ mod tests { ); assert!(quote.is_none()); } + fn idle_monitor( + timeout_secs: Option, + ) -> ( + SubscriptionMonitor, + tokio::sync::mpsc::UnboundedReceiver, + ) { + let (data_sender, receiver) = tokio::sync::mpsc::unbounded_channel(); + let timeout = timeout_secs.map(Duration::from_secs); + let clock = Box::leak(Box::new(AtomicTime::new(false, UnixNanos::from(101_u64)))); + let monitor = SubscriptionMonitor { + client_id: ClientId::from("IB-DATA-17"), + instrument_id: instrument_id(), + subscription: "trades".to_string(), + timeout, + deadline: timeout.and_then(|timeout| tokio::time::Instant::now().checked_add(timeout)), + last_data_received_ns: None, + data_sender: data_sender.into(), + clock, + cancellation_token: CancellationToken::new(), + data_farm_state: Arc::new(DataFarmConnectionState::default()), + recovery_scope: DataFarmRecoveryScope::MarketData, + farm_generation: 0, + }; + (monitor, receiver) + } + + fn disabled_monitor() -> SubscriptionMonitor { + idle_monitor(None).0 + } + + fn receive_idle( + receiver: &mut tokio::sync::mpsc::UnboundedReceiver, + ) -> InteractiveBrokersSubscriptionIdle { + let DataEvent::Data(Data::Custom(custom)) = receiver.try_recv().unwrap() else { + panic!("Expected a subscription idle custom event"); + }; + custom + .data + .as_any() + .downcast_ref::() + .unwrap() + .clone() + } + + #[tokio::test(start_paused = true)] + async fn idle_subscription_emits_once_and_rearms_after_data() { + let (mut monitor, mut receiver) = idle_monitor(Some(5)); + let started = tokio::time::Instant::now(); + tokio::time::advance(Duration::from_secs(5)).await; + monitor.clock.set_time(UnixNanos::from(501_u64)); + monitor.wait_for_idle().await; + assert!(monitor.notify_idle()); + assert_eq!( + receive_idle(&mut receiver), + InteractiveBrokersSubscriptionIdle { + client_id: ClientId::from("IB-DATA-17"), + instrument_id: instrument_id(), + subscription: "trades".to_string(), + idle_timeout_secs: 5, + last_data_received_ns: None, + ts_event: UnixNanos::from(501_u64), + ts_init: UnixNanos::from(501_u64), + } + ); + assert!(monitor.notify_idle()); + assert!(receiver.is_empty()); + monitor.clock.set_time(UnixNanos::from(601_u64)); + monitor.received_data(); + assert_eq!(monitor.deadline, Some(started + Duration::from_secs(10))); + tokio::time::advance(Duration::from_secs(5)).await; + monitor.clock.set_time(UnixNanos::from(1001_u64)); + monitor.wait_for_idle().await; + assert!(monitor.notify_idle()); + assert_eq!( + receive_idle(&mut receiver), + InteractiveBrokersSubscriptionIdle { + client_id: ClientId::from("IB-DATA-17"), + instrument_id: instrument_id(), + subscription: "trades".to_string(), + idle_timeout_secs: 5, + last_data_received_ns: Some(601), + ts_event: UnixNanos::from(1001_u64), + ts_init: UnixNanos::from(1001_u64), + } + ); + assert!(receiver.is_empty()); + } + + #[tokio::test(start_paused = true)] + async fn only_data_postpones_the_idle_deadline() { + let (mut first, mut first_events) = idle_monitor(Some(5)); + let (mut second, mut second_events) = idle_monitor(Some(5)); + second.subscription = "quotes".to_string(); + let started = tokio::time::Instant::now(); + tokio::time::advance(Duration::from_secs(2)).await; + first.received_data(); + second + .data_farm_state + .handle_notice(¬ice(2108, "Market data farm is inactive"), second.clock); + assert_eq!(first.deadline, Some(started + Duration::from_secs(7))); + assert_eq!(second.deadline, Some(started + Duration::from_secs(5))); + tokio::time::advance(Duration::from_secs(3)).await; + second.wait_for_idle().await; + assert!(second.notify_idle()); + assert_eq!(receive_idle(&mut second_events).subscription, "quotes"); + assert!(first_events.try_recv().is_err()); + assert!(second_events.is_empty()); + } + + #[rstest] + #[case("cancel")] + #[case("recovered")] + #[tokio::test(start_paused = true)] + async fn idle_notification_is_suppressed_during_recovery(#[case] state: &str) { + let (mut monitor, receiver) = idle_monitor(Some(5)); + tokio::time::advance(Duration::from_secs(5)).await; + + match state { + "cancel" => monitor.cancellation_token.cancel(), + "recovered" => { + monitor.data_farm_state.handle_notice( + ¬ice(2103, "Market data farm connection is broken:usfarm"), + monitor.clock, + ); + monitor.data_farm_state.handle_notice( + ¬ice(2104, "Market data farm connection is OK:usfarm"), + monitor.clock, + ); + } + _ => unreachable!(), + } + monitor.wait_for_idle().await; + assert!(monitor.notify_idle()); + assert!(receiver.is_empty()); + assert_eq!(monitor.deadline, None); + } + + #[tokio::test(start_paused = true)] + async fn disabled_idle_detection_stays_disabled() { + let (mut monitor, receiver) = idle_monitor(None); + monitor.received_data(); + tokio::time::advance(Duration::from_secs(60)).await; + assert!(monitor.notify_idle()); + assert_eq!(monitor.deadline, None); + assert_eq!(monitor.last_data_received_ns, None); + assert!(receiver.is_empty()); + } + + #[tokio::test(start_paused = true)] + async fn trade_farm_notices_do_not_hide_idle_subscription() { + let (monitor, mut receiver) = idle_monitor(Some(5)); + let (_trade_sender, trade_receiver) = tokio::sync::mpsc::unbounded_channel(); + let mut subscription = ChannelSubscription::new(trade_receiver); + let cancellation = monitor.cancellation_token.clone(); + let task_cancellation = cancellation.clone(); + let sender = monitor.data_sender.clone(); + let clock = monitor.clock; + let farm = Arc::clone(&monitor.data_farm_state); + let notices = Arc::clone(&farm); + + let task = tokio::spawn(async move { + process_trade_stream( + &mut subscription, + instrument_id(), + 2, + 0, + &sender, + clock, + &task_cancellation, + &farm, + 0, + monitor, + ) + .await + .unwrap() + }); + tokio::task::yield_now().await; + tokio::time::advance(Duration::from_secs(2)).await; + + for _ in 0..1000 { + notices.handle_notice(¬ice(2108, "Market data farm is inactive"), clock); + } + tokio::time::advance(Duration::from_secs(3)).await; + tokio::task::yield_now().await; + let event = receive_idle(&mut receiver); + cancellation.cancel(); + let action = task.await.unwrap(); + assert_eq!( + event, + InteractiveBrokersSubscriptionIdle { + client_id: ClientId::from("IB-DATA-17"), + instrument_id: instrument_id(), + subscription: "trades".to_string(), + idle_timeout_secs: 5, + last_data_received_ns: None, + ts_event: UnixNanos::from(101_u64), + ts_init: UnixNanos::from(101_u64), + } + ); + assert!(matches!(action, StreamAction::Stop)); + assert!(receiver.is_empty()); + } + + #[rstest] + #[case(true)] + #[case(false)] + fn stream_config_preserves_trade_selection_and_idle_interval(#[case] all_last_trades: bool) { + let config = crate::config::InteractiveBrokersDataClientConfig { + all_last_trades, + subscription_idle_timeout_secs: Some(17), + ..Default::default() + }; + let stream = StreamConfig::new(ClientId::from("IB-DATA-17"), &config); + assert_eq!(stream.client_id, ClientId::from("IB-DATA-17")); + assert_eq!(stream.all_last_trades, all_last_trades); + assert_eq!(stream.idle_timeout_secs, Some(17)); + } + + #[tokio::test(start_paused = true)] + async fn farm_notice_does_not_mask_subscription_inactivity() { + let (mut monitor, mut receiver) = idle_monitor(Some(5)); + monitor + .data_farm_state + .mark_degraded(0, UnixNanos::from(17_u64)); + tokio::time::advance(Duration::from_secs(5)).await; + monitor.wait_for_idle().await; + assert!(monitor.notify_idle()); + let event = receive_idle(&mut receiver); + assert_eq!(event.idle_timeout_secs, 5); + assert_eq!(event.last_data_received_ns, None); + assert_eq!(monitor.deadline, None); + } + + fn minute_bar(close_secs: u64, close: &str) -> Bar { + let ts = UnixNanos::from(close_secs * 1_000_000_000); + Bar::new( + minute_bar_type(), + Price::from("100.00"), + Price::from("101.00"), + Price::from("99.00"), + Price::from(close), + Quantity::from(5), + ts, + ts, + ) + } + + fn secs(value: f64) -> UnixNanos { + UnixNanos::from((value * 1_000_000_000.0) as u64) + } + + #[rstest] + fn test_historical_bar_completion_publishes_final_update_after_close() { + let mut completion = super::HistoricalBarCompletion::default(); + + let revisions = [ + completion.observe(minute_bar(120, "100.10"), secs(65.8)), + completion.observe(minute_bar(120, "100.20"), secs(115.4)), + ]; + let deadline = completion.deadline(); + let finals = completion.observe(minute_bar(120, "100.30"), secs(120.5)); + + assert_eq!(revisions, [vec![], vec![]]); + assert_eq!(deadline, Some(secs(122.0))); + assert_eq!(finals, vec![minute_bar(120, "100.30")]); + assert_eq!(completion.deadline(), None); + } + + #[rstest] + fn test_historical_bar_completion_publishes_pending_on_next_bar() { + let mut completion = super::HistoricalBarCompletion::default(); + completion.observe(minute_bar(120, "100.20"), secs(115.4)); + + let completed = completion.observe(minute_bar(180, "100.40"), secs(125.3)); + + assert_eq!(completed, vec![minute_bar(120, "100.20")]); + assert_eq!(completion.deadline(), Some(secs(182.0))); + } + + #[rstest] + fn test_historical_bar_completion_expires_pending_after_grace() { + let mut completion = super::HistoricalBarCompletion::default(); + completion.observe(minute_bar(120, "100.20"), secs(115.4)); + + let early = completion.expire(secs(121.9)); + let expired = completion.expire(secs(122.0)); + let late_update = completion.observe(minute_bar(120, "100.30"), secs(122.5)); + + assert_eq!(early, None); + assert_eq!(expired, Some(minute_bar(120, "100.20"))); + assert_eq!(late_update, vec![]); + assert_eq!(completion.deadline(), None); + } + + #[rstest] + fn test_historical_bar_completion_skips_replayed_bars() { + let mut completion = super::HistoricalBarCompletion::default(); + let history = [ + completion.observe(minute_bar(60, "100.10"), secs(130.0)), + completion.observe(minute_bar(120, "100.20"), secs(130.0)), + completion.observe(minute_bar(180, "100.30"), secs(130.0)), + ]; + + let replayed = [ + completion.observe(minute_bar(60, "100.10"), secs(140.0)), + completion.observe(minute_bar(120, "100.20"), secs(140.0)), + ]; + + assert_eq!( + history, + [ + vec![minute_bar(60, "100.10")], + vec![minute_bar(120, "100.20")], + vec![], + ] + ); + assert_eq!(replayed, [vec![], vec![]]); + assert_eq!(completion.deadline(), Some(secs(182.0))); + } } diff --git a/crates/adapters/interactive_brokers/src/data_types.rs b/crates/adapters/interactive_brokers/src/data_types.rs new file mode 100644 index 000000000000..873f15f04219 --- /dev/null +++ b/crates/adapters/interactive_brokers/src/data_types.rs @@ -0,0 +1,73 @@ +// ------------------------------------------------------------------------------------------------- +// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. +// https://nautechsystems.io +// +// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------------------------------- + +//! Interactive Brokers subscription diagnostics delivered as custom data. + +use nautilus_core::UnixNanos; +use nautilus_model::{ + custom_data, + identifiers::{ClientId, InstrumentId}, +}; + +/// A subscription receives no market data within its configured interval. +/// +/// This is an inactivity signal, not evidence that the connection or market is closed. +#[custom_data(pyo3, stub_module = "nautilus_trader.adapters.interactive_brokers")] +pub struct InteractiveBrokersSubscriptionIdle { + /// The Nautilus data client identifier. + pub client_id: ClientId, + /// The subscribed instrument identifier. + pub instrument_id: InstrumentId, + /// The subscription kind, including the complete bar type for bar subscriptions. + pub subscription: String, + /// The configured interval without market data. + pub idle_timeout_secs: u64, + /// The last local receipt time, or `None` when no market data has arrived. + #[custom_data_field(serde)] + pub last_data_received_ns: Option, + /// The UNIX timestamp (nanoseconds) when inactivity is detected. + pub ts_event: UnixNanos, + /// The UNIX timestamp (nanoseconds) when the event is created. + pub ts_init: UnixNanos, +} + +pub(crate) fn register_ib_custom_data() { + let _ = nautilus_model::data::ensure_custom_data_json_registered::< + InteractiveBrokersSubscriptionIdle, + >(); +} + +#[cfg(test)] +mod tests { + use rstest::rstest; + + use super::*; + + #[rstest] + fn subscription_idle_round_trip_preserves_every_field() { + let event = InteractiveBrokersSubscriptionIdle { + client_id: ClientId::from("IB-DATA-17"), + instrument_id: InstrumentId::from("AAPL=STK.SMART"), + subscription: "trades".to_string(), + idle_timeout_secs: 17, + last_data_received_ns: Some(123), + ts_event: UnixNanos::from(456_u64), + ts_init: UnixNanos::from(789_u64), + }; + let json = serde_json::to_string(&event).unwrap(); + let restored: InteractiveBrokersSubscriptionIdle = serde_json::from_str(&json).unwrap(); + assert_eq!(restored, event); + } +} diff --git a/crates/adapters/interactive_brokers/src/error.rs b/crates/adapters/interactive_brokers/src/error.rs deleted file mode 100644 index 66a34c2d782d..000000000000 --- a/crates/adapters/interactive_brokers/src/error.rs +++ /dev/null @@ -1,234 +0,0 @@ -// ------------------------------------------------------------------------------------------------- -// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -// https://nautechsystems.io -// -// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// ------------------------------------------------------------------------------------------------- - -//! Error types and classification for the Interactive Brokers adapter. - -use thiserror::Error; - -/// Errors that can occur in the Interactive Brokers adapter. -#[derive(Error, Debug)] -pub enum InteractiveBrokersError { - /// Connection error. - #[error("Connection error: {0}")] - Connection(String), - - /// Authentication error. - #[error("Authentication error: {0}")] - Authentication(String), - - /// Invalid configuration. - #[error("Invalid configuration: {0}")] - Configuration(String), - - /// API request error. - #[error("API request error: {0}")] - Request(String), - - /// Response parsing error. - #[error("Response parsing error: {0}")] - Parse(String), - - /// Instrument error. - #[error("Instrument error: {0}")] - Instrument(String), - - /// Order error. - #[error("Order error: {0}")] - Order(String), - - /// Market data error. - #[error("Market data error: {0}")] - MarketData(String), - - /// Generic error from rust-ibapi. - #[error("IB API error: {0}")] - IbApi(String), - - /// Internal error. - #[error("Internal error: {0}")] - Internal(String), -} - -impl InteractiveBrokersError { - /// Returns the payload-free error kind. - #[must_use] - pub const fn kind(&self) -> InteractiveBrokersErrorKind { - match self { - Self::Connection(_) => InteractiveBrokersErrorKind::Connection, - Self::Authentication(_) => InteractiveBrokersErrorKind::Authentication, - Self::Configuration(_) => InteractiveBrokersErrorKind::Configuration, - Self::Request(_) => InteractiveBrokersErrorKind::Request, - Self::Parse(_) => InteractiveBrokersErrorKind::Parse, - Self::Instrument(_) => InteractiveBrokersErrorKind::Instrument, - Self::Order(_) => InteractiveBrokersErrorKind::Order, - Self::MarketData(_) => InteractiveBrokersErrorKind::MarketData, - Self::IbApi(_) => InteractiveBrokersErrorKind::IbApi, - Self::Internal(_) => InteractiveBrokersErrorKind::Internal, - } - } -} - -/// Payload-free Interactive Brokers adapter error kind. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[cfg_attr( - feature = "python", - pyo3::pyclass( - module = "nautilus_trader.adapters.interactive_brokers", - from_py_object, - rename_all = "SCREAMING_SNAKE_CASE" - ) -)] -#[cfg_attr( - feature = "python", - pyo3_stub_gen::derive::gen_stub_pyclass_enum( - module = "nautilus_trader.adapters.interactive_brokers" - ) -)] -pub enum InteractiveBrokersErrorKind { - /// Connection error. - Connection, - /// Authentication error. - Authentication, - /// Invalid configuration. - Configuration, - /// API request error. - Request, - /// Response parsing error. - Parse, - /// Instrument error. - Instrument, - /// Order error. - Order, - /// Market data error. - MarketData, - /// Generic error from rust-ibapi. - IbApi, - /// Internal error. - Internal, -} - -/// Result type for Interactive Brokers operations. -pub type InteractiveBrokersResult = Result; - -/// IB API error code classification. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[cfg_attr( - feature = "python", - pyo3::pyclass( - module = "nautilus_trader.adapters.interactive_brokers", - from_py_object, - rename_all = "SCREAMING_SNAKE_CASE" - ) -)] -#[cfg_attr( - feature = "python", - pyo3_stub_gen::derive::gen_stub_pyclass_enum( - module = "nautilus_trader.adapters.interactive_brokers" - ) -)] -pub enum ErrorCategory { - /// Client/application error (should not retry). - ClientError, - /// Connectivity error (should retry with backoff). - ConnectivityError, - /// Subscription error (may need resubscription). - SubscriptionError, - /// Order error (may need special handling). - OrderError, - /// Market data error (may need resubscription). - MarketDataError, - /// Unknown/unclassified error. - Unknown, -} - -/// Classify an IB error code into a category. -pub fn classify_error_code(error_code: i32) -> ErrorCategory { - match error_code { - // Client errors - should not retry - 200..=299 => ErrorCategory::ClientError, - - // Connectivity errors - should retry - 326 | 502 | 503 | 504 | 1100 | 1101 | 1102 | 1300 | 1301 | 1302 => { - ErrorCategory::ConnectivityError - } - - // Subscription errors - may need resubscription - 10189 | 366 | 102 | 10182 => ErrorCategory::SubscriptionError, - - // Market data errors - 100..=199 if error_code != 10182 => ErrorCategory::MarketDataError, - - // Note: Order errors overlap with client errors range - // We handle order errors separately in the match - - // Unknown - _ => ErrorCategory::Unknown, - } -} - -/// Determine if an error is recoverable. -pub fn is_recoverable_error(error_code: i32) -> bool { - matches!( - classify_error_code(error_code), - ErrorCategory::ConnectivityError | ErrorCategory::SubscriptionError - ) -} - -/// Determine if an error requires subscription resubscription. -pub fn requires_resubscription(error_code: i32) -> bool { - matches!(error_code, 10189 | 366 | 102 | 10182) -} - -/// Get a human-readable error description. -pub fn format_error_message(error_code: i32, error_string: &str) -> String { - let category = classify_error_code(error_code); - let category_str = match category { - ErrorCategory::ClientError => "Client Error", - ErrorCategory::ConnectivityError => "Connectivity Error", - ErrorCategory::SubscriptionError => "Subscription Error", - ErrorCategory::OrderError => "Order Error", - ErrorCategory::MarketDataError => "Market Data Error", - ErrorCategory::Unknown => "Unknown Error", - }; - - format!( - "[{}] {} (Code: {}): {}", - category_str, error_string, error_code, error_string - ) -} - -#[cfg(test)] -mod tests { - use rstest::rstest; - - use super::{ErrorCategory, classify_error_code, is_recoverable_error}; - - #[rstest] - #[case(326, ErrorCategory::ConnectivityError)] - #[case(502, ErrorCategory::ConnectivityError)] - #[case(10182, ErrorCategory::SubscriptionError)] - #[case(200, ErrorCategory::ClientError)] - fn test_classify_error_code(#[case] error_code: i32, #[case] expected: ErrorCategory) { - assert_eq!(classify_error_code(error_code), expected); - } - - #[rstest] - #[case(326, true)] - #[case(10182, true)] - #[case(200, false)] - fn test_is_recoverable_error(#[case] error_code: i32, #[case] expected: bool) { - assert_eq!(is_recoverable_error(error_code), expected); - } -} diff --git a/crates/adapters/interactive_brokers/src/execution/account.rs b/crates/adapters/interactive_brokers/src/execution/account.rs index 0ea80aa96174..9801ed600b9f 100644 --- a/crates/adapters/interactive_brokers/src/execution/account.rs +++ b/crates/adapters/interactive_brokers/src/execution/account.rs @@ -15,18 +15,23 @@ //! Account management for Interactive Brokers execution client. -use std::{collections::HashMap, sync::Arc}; +use std::{collections::HashMap, sync::Arc, time::Duration}; use anyhow::Context; +use futures_util::Stream; use ibapi::{ accounts::{ - AccountSummary, AccountSummaryResult, AccountSummaryTags, + AccountSummary, AccountSummaryResult, AccountSummaryTags, AccountUpdate, AccountValue, types::{AccountGroup, AccountId as IbAccountId}, }, client::Client, + contracts::Contract, + orders::ExecutionSide, prelude::{StreamExt, SubscriptionItemStreamExt}, + subscriptions::{FilterDataStream, Subscription}, }; use nautilus_common::{ + cache::fifo::FifoCache, live::runner::get_exec_event_sender, messages::{ExecutionEvent, ExecutionReport}, }; @@ -40,25 +45,112 @@ use nautilus_model::{ types::{AccountBalance, Currency, MarginBalance, Money, Quantity}, }; use rust_decimal::{Decimal, prelude::ToPrimitive}; +use ustr::Ustr; -pub(crate) fn raw_ib_account_code(account_id: &AccountId) -> String { - account_id - .to_string() - .strip_prefix("IB-") - .unwrap_or(account_id.as_str()) - .to_string() +/// Derives the raw IB account code sent to TWS from the configured `account_id`. +/// +/// A configured bare code such as `DU123456` is used as is. A configured composite Nautilus +/// account ID such as `IB-DU123456`, or no configured value, falls back to the segment of +/// `account_id` after its last hyphen, because IB account codes contain no hyphen while client +/// names such as `IB-TEST` may. +pub(crate) fn ib_account_code(configured: Option<&str>, account_id: AccountId) -> Ustr { + match configured { + Some(code) if !code.contains('-') => Ustr::from(code), + _ => Ustr::from( + account_id + .as_str() + .rsplit_once('-') + .map_or(account_id.as_str(), |(_, code)| code), + ), + } +} + +// Rows of one IB refresh batch arrive within milliseconds of each other. +const ACCOUNT_SUMMARY_QUIET: Duration = Duration::from_secs(1); + +pub type AccountSnapshot = (Vec, Vec, Option); + +type AccountSummaryStream = FilterDataStream>; + +#[derive(Default)] +struct AccountSummaryAccumulator { + balances: Vec, + margins: Vec, + info: Params, } /// Subscribe to account summary and parse to balances and margins. /// +/// The returned `info` also carries one `reqAccountUpdates` snapshot, because IB serves +/// values such as `PostExpirationExcess` only on that stream and not as summary tags. +/// /// # Errors /// -/// Returns an error if subscription fails. +/// Returns an error if the account summary subscription fails. pub async fn subscribe_account_summary( client: &Arc, - account_id: AccountId, -) -> anyhow::Result<(Vec, Vec, Option)> { - let raw_account_id = raw_ib_account_code(&account_id); + ib_account: Ustr, +) -> anyhow::Result { + let mut subscription = open_account_summary(client, ib_account).await?; + let mut accumulator = AccountSummaryAccumulator::default(); + read_account_summary_cycle(&mut subscription, ib_account, &mut accumulator, None).await; + account_snapshot(client, ib_account, &accumulator).await +} + +/// Subscribe to account summary, deliver the first snapshot, and keep the subscription open. +/// +/// IB repeats the summary about every 3 minutes on a live `reqAccountSummary` subscription. A +/// session task delivers one further snapshot per refresh cycle through `on_snapshot` until +/// the subscription ends. +/// +/// # Errors +/// +/// Returns an error if the subscription fails or the first snapshot cannot be built. +pub async fn stream_account_summary( + client: &Arc, + ib_account: Ustr, + session_tasks: &TaskGroup, + on_snapshot: F, +) -> anyhow::Result<()> +where + F: Fn(AccountSnapshot) + Send + 'static, +{ + let mut subscription = open_account_summary(client, ib_account).await?; + let mut accumulator = AccountSummaryAccumulator::default(); + read_account_summary_cycle(&mut subscription, ib_account, &mut accumulator, None).await; + on_snapshot(account_snapshot(client, ib_account, &accumulator).await?); + + let client = Arc::clone(client); + let future = async move { + while read_account_summary_cycle( + &mut subscription, + ib_account, + &mut accumulator, + Some(ACCOUNT_SUMMARY_QUIET), + ) + .await + { + match account_snapshot(&client, ib_account, &accumulator).await { + Ok(snapshot) => on_snapshot(snapshot), + Err(e) => tracing::warn!("Failed to build account snapshot: {}", e), + } + } + tracing::warn!( + "IB account summary stream ended for account: {}", + ib_account + ); + }; + session_tasks + .spawn(future) + .context("Failed to register IB account summary task")?; + + Ok(()) +} + +async fn open_account_summary( + client: &Arc, + ib_account: Ustr, +) -> anyhow::Result { // Request key account summary tags (includes TotalCashValue to match Python account summary info dict). let tags = &[ AccountSummaryTags::NET_LIQUIDATION, @@ -78,71 +170,90 @@ pub async fn subscribe_account_summary( .account_summary(&group, tags) .await .context("Failed to subscribe to account summary")?; - let mut subscription = subscription.filter_data(); - tracing::debug!("Subscribed to account summary for account: {}", account_id); + tracing::debug!("Subscribed to account summary for account: {}", ib_account); - // Process initial account summary snapshot - // We collect all summary items until the API sends AccountSummaryResult::End, so the - // returned balances/margins are complete (matches Python behavior of waiting for all tags). - let mut balances: Vec = Vec::new(); - let mut margins: Vec = Vec::new(); - let mut info = Params::new(); + Ok(subscription.filter_data()) +} - while let Some(result) = subscription.next().await { - match result { - Ok(AccountSummaryResult::Summary(summary)) => { +// Folds summary rows into the accumulator until the API sends AccountSummaryResult::End, so +// each snapshot is complete (matches Python behavior of waiting for all tags). After the +// initial snapshot IB pushes changed values without a further End, so a refresh passes +// `quiet` and completes once rows stop arriving for that long. Returns false when the stream +// closes before a snapshot completes. +async fn read_account_summary_cycle( + subscription: &mut S, + ib_account: Ustr, + accumulator: &mut AccountSummaryAccumulator, + quiet: Option, +) -> bool +where + S: Stream> + Unpin, +{ + let raw_account_id = ib_account.as_str(); + let mut received_rows = false; + + loop { + let next = match quiet { + Some(quiet) if received_rows => { + match tokio::time::timeout(quiet, subscription.next()).await { + Ok(next) => next, + Err(_) => return true, + } + } + _ => subscription.next().await, + }; + + match next { + Some(Ok(AccountSummaryResult::Summary(summary))) => { // Filter for the specific account if summary.account != raw_account_id { continue; } + received_rows = true; + // Record the raw summary tag so the account state carries the // venue-reported values (for example TotalCashValue) that do not // map to the typed balances and margins. - info.insert( - summary.tag.to_string(), + accumulator.info.insert( + summary.tag.clone(), serde_json::Value::from(summary.value.as_str()), ); - match parse_account_summary_to_balance(&summary) { - Ok(balance) => { - // Check if balance already exists for this currency - if let Some(existing) = balances - .iter_mut() - .find(|b| b.total.currency == balance.total.currency) - { - if let Some(merged) = merge_account_summary_balance( - existing, - summary.tag.as_str(), - &summary.value, - &summary.currency, - )? { - *existing = merged; - } - } else { - balances.push(balance); - } - } - Err(e) => { - tracing::warn!("Failed to parse account summary: {}", e); - } + if let Err(e) = merge_account_summary_balance(&mut accumulator.balances, &summary) { + tracing::warn!("Failed to parse account summary: {}", e); } // Accumulate margin requirements by currency. IB reports INIT_MARGIN_REQ // and MAINT_MARGIN_REQ as separate summary entries; merge them into one // `MarginBalance` per currency so neither half overwrites the other when // the account-wide margin store keys by `Currency`. - merge_account_summary_margin(&mut margins, &summary); + merge_account_summary_margin(&mut accumulator.margins, &summary); } - Ok(AccountSummaryResult::End) => { - break; - } - Err(e) => { + Some(Ok(AccountSummaryResult::End)) => return true, + Some(Err(e)) => { tracing::warn!("Error receiving account summary: {}", e); } + None => return false, } } +} + +async fn account_snapshot( + client: &Arc, + ib_account: Ustr, + accumulator: &AccountSummaryAccumulator, +) -> anyhow::Result { + let mut info = accumulator.info.clone(); + + if let Err(e) = merge_account_updates_snapshot(client, ib_account.as_str(), &mut info).await { + tracing::warn!("Failed to collect account updates: {}", e); + } + + let balances = finalize_account_summary_balances(accumulator.balances.clone())?; + let mut margins = accumulator.margins.clone(); + margins.sort_by(|a, b| a.currency.code.as_str().cmp(b.currency.code.as_str())); tracing::debug!( "Received account summary: {} balances, {} margins", @@ -157,7 +268,61 @@ pub async fn subscribe_account_summary( )) } +// Drains one `reqAccountUpdates` snapshot into `info`, keyed by the raw IB key like the +// summary tags. Dropping the subscription sends the cancel, so no stream stays open. +async fn merge_account_updates_snapshot( + client: &Arc, + raw_account_id: &str, + info: &mut Params, +) -> anyhow::Result<()> { + let account = IbAccountId(raw_account_id.to_string()); + let subscription = client + .account_updates(&account) + .await + .context("Failed to subscribe to account updates")?; + let mut subscription = subscription.filter_data(); + + while let Some(result) = subscription.next().await { + match result { + Ok(AccountUpdate::AccountValue(value)) => { + merge_account_value(info, raw_account_id, &value); + } + Ok(AccountUpdate::End) => break, + Ok(AccountUpdate::PortfolioValue(_) | AccountUpdate::UpdateTime(_)) => {} + Err(e) => { + tracing::warn!("Error receiving account updates: {}", e); + } + } + } + + Ok(()) +} + +fn merge_account_value(info: &mut Params, raw_account_id: &str, value: &AccountValue) { + if value + .account + .as_deref() + .is_some_and(|account| account != raw_account_id) + { + return; + } + + info.insert( + value.key.clone(), + serde_json::Value::from(value.value.as_str()), + ); +} + fn merge_account_summary_margin(margins: &mut Vec, summary: &AccountSummary) { + let relevant = matches!( + summary.tag.as_str(), + AccountSummaryTags::INIT_MARGIN_REQ | AccountSummaryTags::MAINT_MARGIN_REQ + ); + + if !relevant { + return; + } + let currency = match parse_currency(&summary.currency) { Ok(currency) => currency, Err(e) => { @@ -188,37 +353,108 @@ fn merge_account_summary_margin(margins: &mut Vec, summary: &Acco Some(margin) => margin.maintenance = value, None => margins.push(MarginBalance::new(Money::zero(currency), value, None)), }, - _ => {} + _ => unreachable!("relevant account summary tag was checked above"), } } -fn merge_account_summary_balance( - existing: &AccountBalance, - tag: &str, - value: &str, - currency_code: &str, -) -> anyhow::Result> { - let currency = parse_currency(currency_code)?; - - match tag { - AccountSummaryTags::SETTLED_CASH => { - let settled_cash = parse_balance_decimal(value)?; - Ok(Some(AccountBalance::from_total_and_locked( - settled_cash, - Decimal::ZERO, - currency, - )?)) +#[derive(Clone)] +struct AccountSummaryBalance { + currency: Currency, + net_liquidation: Option, + settled_cash: Option, + total_cash_value: Option, + available_funds: Option, + buying_power: Option, +} + +impl AccountSummaryBalance { + fn new(currency: Currency) -> Self { + Self { + currency, + net_liquidation: None, + settled_cash: None, + total_cash_value: None, + available_funds: None, + buying_power: None, } - AccountSummaryTags::NET_LIQUIDATION => { - let net_liq = parse_balance_decimal(value)?; - Ok(Some(AccountBalance::from_total_and_free( - net_liq, - existing.free.as_decimal(), - currency, - )?)) + } + + fn into_balance(self) -> anyhow::Result> { + let Some(total) = self + .net_liquidation + .or(self.settled_cash) + .or(self.total_cash_value) + .or(self.available_funds) + .or(self.buying_power) + else { + return Ok(None); + }; + let free = self + .available_funds + .or(self.buying_power) + .or(self.settled_cash) + .or(self.total_cash_value) + .unwrap_or(total); + + Ok(Some(AccountBalance::from_total_and_free( + total, + free, + self.currency, + )?)) + } +} + +fn merge_account_summary_balance( + balances: &mut Vec, + summary: &AccountSummary, +) -> anyhow::Result<()> { + let relevant = matches!( + summary.tag.as_str(), + AccountSummaryTags::NET_LIQUIDATION + | AccountSummaryTags::SETTLED_CASH + | AccountSummaryTags::TOTAL_CASH_VALUE + | AccountSummaryTags::AVAILABLE_FUNDS + | AccountSummaryTags::BUYING_POWER + ); + + if !relevant { + return Ok(()); + } + + let currency = parse_currency(&summary.currency)?; + let value = parse_balance_decimal(&summary.value)?; + let balance = match balances.iter_mut().find(|b| b.currency == currency) { + Some(balance) => balance, + None => { + balances.push(AccountSummaryBalance::new(currency)); + balances.last_mut().expect("balance was just inserted") } - _ => Ok(None), + }; + + match summary.tag.as_str() { + AccountSummaryTags::NET_LIQUIDATION => balance.net_liquidation = Some(value), + AccountSummaryTags::SETTLED_CASH => balance.settled_cash = Some(value), + AccountSummaryTags::TOTAL_CASH_VALUE => balance.total_cash_value = Some(value), + AccountSummaryTags::AVAILABLE_FUNDS => balance.available_funds = Some(value), + AccountSummaryTags::BUYING_POWER => balance.buying_power = Some(value), + _ => unreachable!("relevant account summary tag was checked above"), } + + Ok(()) +} + +fn finalize_account_summary_balances( + summaries: Vec, +) -> anyhow::Result> { + let mut balances = summaries + .into_iter() + .map(AccountSummaryBalance::into_balance) + .collect::>>()? + .into_iter() + .flatten() + .collect::>(); + balances.sort_by(|a, b| a.currency.code.as_str().cmp(b.currency.code.as_str())); + Ok(balances) } /// Subscribe to PnL updates for the account. @@ -230,17 +466,17 @@ fn merge_account_summary_balance( /// Returns an error if subscription fails. pub async fn subscribe_pnl( client: &Arc, - account_id: AccountId, + ib_account: Ustr, session_tasks: &TaskGroup, ) -> anyhow::Result<()> { - let account = IbAccountId(raw_ib_account_code(&account_id)); + let account = IbAccountId(ib_account.to_string()); let subscription = client .pnl(&account, None) .await .context("Failed to subscribe to PnL")?; let mut subscription = subscription.filter_data(); - tracing::debug!("Subscribed to PnL updates for account: {}", account_id); + tracing::debug!("Subscribed to PnL updates for account: {}", ib_account); // Process PnL updates in background task let future = async move { @@ -270,12 +506,51 @@ pub async fn subscribe_pnl( Ok(()) } +#[derive(Debug)] +pub struct PositionTrackerState { + positions: HashMap, + own_fill_ids: FifoCache, +} + /// Track known positions for detecting external changes (e.g., option exercises). -pub type PositionTracker = Arc>>; +pub type PositionTracker = Arc>; /// Create a new position tracker. pub fn create_position_tracker() -> PositionTracker { - Arc::new(tokio::sync::Mutex::new(HashMap::new())) + Arc::new(tokio::sync::Mutex::new(PositionTrackerState { + positions: HashMap::new(), + own_fill_ids: FifoCache::new(), + })) +} + +pub async fn record_own_fill( + position_tracker: &PositionTracker, + execution_id: &str, + contract_id: i32, + side: ExecutionSide, + quantity: f64, +) -> anyhow::Result { + let quantity = Decimal::from_f64_retain(quantity) + .context("Failed to convert own fill quantity to Decimal")?; + let signed_quantity = match side { + ExecutionSide::Bought => quantity, + ExecutionSide::Sold => -quantity, + }; + let mut tracker = position_tracker.lock().await; + if !tracker.own_fill_ids.insert(execution_id.to_string()) { + return Ok(false); + } + + let position = tracker + .positions + .entry(contract_id) + .or_insert(Decimal::ZERO); + *position += signed_quantity; + if position.is_zero() { + tracker.positions.remove(&contract_id); + } + + Ok(true) } /// Check if a position update represents an external change (e.g., option exercise). @@ -285,7 +560,11 @@ pub async fn check_external_position_change( new_quantity: Decimal, ) -> Option<(bool, Decimal)> { let mut tracker = position_tracker.lock().await; - let known_quantity = tracker.get(&contract_id).copied().unwrap_or(Decimal::ZERO); + let known_quantity = tracker + .positions + .get(&contract_id) + .copied() + .unwrap_or(Decimal::ZERO); if new_quantity.is_zero() { return (!known_quantity.is_zero()).then_some((true, known_quantity)); @@ -300,7 +579,7 @@ pub async fn check_external_position_change( // This is a change - determine if it's external // External changes occur when position changes without a corresponding execution // Update tracked position - tracker.insert(contract_id, new_quantity); + tracker.positions.insert(contract_id, new_quantity); // If we had a known position and it changed, it's likely external if known_quantity != Decimal::ZERO && known_quantity != new_quantity { @@ -314,33 +593,33 @@ pub async fn check_external_position_change( /// Initialize position tracking with existing positions. /// /// This fetches all current positions and initializes the position tracker -/// to avoid processing duplicates from execDetails. +/// to avoid processing duplicates from execDetails. Returns the contracts of the +/// tracked positions so their instruments can be published before reconciliation. /// /// # Errors /// /// Returns an error if position request fails. pub async fn initialize_position_tracking( client: &Arc, - account_id: AccountId, + ib_account: Ustr, position_tracker: PositionTracker, -) -> anyhow::Result<()> { - let raw_account_id = raw_ib_account_code(&account_id); +) -> anyhow::Result> { let subscription = client .positions() .await .context("Failed to request positions")?; let mut subscription = subscription.filter_data(); - tracing::debug!("Initializing position tracking for account: {}", account_id); + tracing::debug!("Initializing position tracking for account: {}", ib_account); - let mut position_count = 0; + let mut contracts = Vec::new(); let mut tracker = position_tracker.lock().await; while let Some(result) = subscription.next().await { match result { Ok(ibapi::accounts::PositionUpdate::Position(position)) => { // Filter for the specific account - if position.account != raw_account_id { + if position.account != ib_account.as_str() { continue; } @@ -349,8 +628,8 @@ pub async fn initialize_position_tracking( // Only track non-zero positions if !quantity.is_zero() { - tracker.insert(contract_id, quantity); - position_count += 1; + tracker.positions.insert(contract_id, quantity); + contracts.push(position.contract); } } Ok(ibapi::accounts::PositionUpdate::PositionEnd) => { @@ -364,10 +643,10 @@ pub async fn initialize_position_tracking( tracing::debug!( "Initialized tracking for {} existing positions", - position_count + contracts.len() ); - Ok(()) + Ok(contracts) } /// Subscribe to real-time position updates for detecting external position changes (e.g., option exercises). @@ -381,18 +660,18 @@ pub async fn initialize_position_tracking( pub async fn subscribe_positions( client: &Arc, account_id: AccountId, + ib_account: Ustr, position_tracker: PositionTracker, instrument_provider: Arc, session_tasks: &TaskGroup, ) -> anyhow::Result<()> { - let raw_account_id = raw_ib_account_code(&account_id); let subscription = client .positions() .await .context("Failed to subscribe to positions")?; let mut subscription = subscription.filter_data(); - tracing::debug!("Subscribed to position updates for account: {}", account_id); + tracing::debug!("Subscribed to position updates for account: {}", ib_account); let exec_sender = get_exec_event_sender(); let clock = get_atomic_clock_realtime(); @@ -403,7 +682,7 @@ pub async fn subscribe_positions( while let Some(result) = subscription.next().await { match result { Ok(ibapi::accounts::PositionUpdate::Position(position)) => { - if position.account != raw_account_id { + if position.account != ib_account.as_str() { continue; } @@ -484,7 +763,11 @@ pub async fn subscribe_positions( ); } else { if new_quantity.is_zero() { - position_tracker.lock().await.remove(&contract_id); + position_tracker + .lock() + .await + .positions + .remove(&contract_id); } tracing::info!( @@ -509,7 +792,7 @@ pub async fn subscribe_positions( } } Ok(ibapi::accounts::PositionUpdate::PositionEnd) => { - break; + tracing::debug!("Received end of initial IB position snapshot"); } Err(e) => { tracing::warn!("Error receiving position update: {}", e); @@ -524,39 +807,10 @@ pub async fn subscribe_positions( Ok(()) } -/// Parse IB account summary to Nautilus AccountBalance. -fn parse_account_summary_to_balance(summary: &AccountSummary) -> anyhow::Result { - let currency = parse_currency(&summary.currency)?; - let balance = parse_balance_decimal(&summary.value)?; - - match summary.tag.as_str() { - AccountSummaryTags::SETTLED_CASH | AccountSummaryTags::TOTAL_CASH_VALUE => { - // Cash balance - free equals total for settled cash - AccountBalance::from_total_and_locked(balance, Decimal::ZERO, currency) - .map_err(Into::into) - } - AccountSummaryTags::NET_LIQUIDATION => { - // Net liquidation - represents total equity - // Free would be calculated from available funds - AccountBalance::from_total_and_locked(balance, Decimal::ZERO, currency) - .map_err(Into::into) - } - AccountSummaryTags::BUYING_POWER | AccountSummaryTags::AVAILABLE_FUNDS => { - // Available funds - this is the free amount - AccountBalance::from_total_and_free(balance, balance, currency).map_err(Into::into) - } - _ => { - // Default: treat as total balance - AccountBalance::from_total_and_locked(balance, Decimal::ZERO, currency) - .map_err(Into::into) - } - } -} - fn parse_balance_decimal(value: &str) -> anyhow::Result { value .parse::() - .context(format!("Failed to parse balance value: {}", value)) + .context(format!("Failed to parse balance value: {value}")) } fn parse_currency(currency: &str) -> anyhow::Result { @@ -566,14 +820,26 @@ fn parse_currency(currency: &str) -> anyhow::Result { #[cfg(test)] mod tests { - use ibapi::accounts::AccountSummary; - use nautilus_model::types::{AccountBalance, Currency, MarginBalance, Money}; + use std::time::Duration; + + use futures_util::{StreamExt, stream}; + use ibapi::{ + accounts::{AccountSummary, AccountSummaryResult, AccountValue}, + orders::ExecutionSide, + }; + use nautilus_core::Params; + use nautilus_model::{ + identifiers::AccountId, + types::{AccountBalance, Currency, MarginBalance, Money}, + }; use rstest::rstest; use rust_decimal::Decimal; use super::{ - AccountSummaryTags, check_external_position_change, create_position_tracker, - merge_account_summary_balance, merge_account_summary_margin, parse_currency, + AccountSummaryAccumulator, AccountSummaryTags, check_external_position_change, + create_position_tracker, finalize_account_summary_balances, ib_account_code, + merge_account_summary_balance, merge_account_summary_margin, merge_account_value, + parse_currency, read_account_summary_cycle, record_own_fill, }; fn margin_summary(tag: &str, value: &str, currency: &str) -> AccountSummary { @@ -585,6 +851,39 @@ mod tests { } } + fn account_value(account: Option<&str>, key: &str, value: &str) -> AccountValue { + AccountValue { + key: key.to_string(), + value: value.to_string(), + currency: "USD".to_string(), + account: account.map(str::to_string), + } + } + + fn balances_from_summaries(summaries: &[AccountSummary]) -> Vec { + let mut balances = Vec::new(); + for summary in summaries { + merge_account_summary_balance(&mut balances, summary).unwrap(); + } + finalize_account_summary_balances(balances).unwrap() + } + + #[rstest] + #[case(Some("U7654321"), "IB-TEST-U7654321", "U7654321")] + #[case(Some("IB_LIVE-U1234567"), "IB_LIVE-U1234567", "U1234567")] + #[case(Some("IB-TEST-U7654321"), "IB-TEST-U7654321", "U7654321")] + #[case(None, "IB-TEST-001", "001")] + #[case(None, "IB-001", "001")] + fn test_ib_account_code_prefers_configured_bare_code( + #[case] configured: Option<&str>, + #[case] account_id: &str, + #[case] expected: &str, + ) { + let code = ib_account_code(configured, AccountId::from(account_id)); + + assert_eq!(code.as_str(), expected); + } + /// Verifies the IB avg cost to Nautilus price conversion formula used in position parsing. /// Python: converted_avg_cost = avg_cost / (multiplier * price_magnifier) #[rstest] @@ -616,40 +915,222 @@ mod tests { #[tokio::test] async fn test_external_position_change_reports_tracked_zero_close() { let tracker = create_position_tracker(); - tracker.lock().await.insert(42, Decimal::new(5, 0)); + tracker + .lock() + .await + .positions + .insert(42, Decimal::new(5, 0)); let change = check_external_position_change(&tracker, 42, Decimal::ZERO).await; assert_eq!(change, Some((true, Decimal::new(5, 0)))); assert_eq!( - tracker.lock().await.get(&42).copied(), + tracker.lock().await.positions.get(&42).copied(), Some(Decimal::new(5, 0)) ); } #[rstest] - fn test_net_liquidation_merge_clamps_free_to_total() { - let existing = AccountBalance::from_total_and_free( - "120.00".parse().unwrap(), - "120.00".parse().unwrap(), - Currency::USD(), - ) - .unwrap(); + #[tokio::test] + async fn test_own_fill_position_change_is_not_external() { + let tracker = create_position_tracker(); + tracker + .lock() + .await + .positions + .insert(42, Decimal::new(5, 0)); + + assert!( + record_own_fill(&tracker, "EXEC-1", 42, ExecutionSide::Bought, 2.0) + .await + .unwrap() + ); + let change = check_external_position_change(&tracker, 42, Decimal::new(7, 0)).await; - let merged = merge_account_summary_balance( - &existing, - AccountSummaryTags::NET_LIQUIDATION, - "100.00", - "USD", - ) - .unwrap() - .unwrap(); + assert_eq!(change, None); + assert_eq!( + tracker.lock().await.positions.get(&42).copied(), + Some(Decimal::new(7, 0)) + ); + } + + #[rstest] + #[tokio::test] + async fn test_replayed_own_fill_does_not_advance_position_twice() { + let tracker = create_position_tracker(); + + assert!( + record_own_fill(&tracker, "EXEC-1", 42, ExecutionSide::Sold, 2.0) + .await + .unwrap() + ); + assert!( + !record_own_fill(&tracker, "EXEC-1", 42, ExecutionSide::Sold, 2.0) + .await + .unwrap() + ); + + assert_eq!( + tracker.lock().await.positions.get(&42).copied(), + Some(Decimal::new(-2, 0)) + ); + } + + #[rstest] + fn test_net_liquidation_merge_clamps_free_to_total() { + let merged = balances_from_summaries(&[ + margin_summary(AccountSummaryTags::AVAILABLE_FUNDS, "120.00", "USD"), + margin_summary(AccountSummaryTags::NET_LIQUIDATION, "100.00", "USD"), + ]) + .remove(0); assert_eq!(merged.total.as_decimal(), "100.00".parse().unwrap()); assert_eq!(merged.locked.as_decimal(), "0.00".parse().unwrap()); assert_eq!(merged.free.as_decimal(), "100.00".parse().unwrap()); } + fn net_liquidation_summary(value: &str) -> AccountSummaryResult { + AccountSummaryResult::Summary(margin_summary("NetLiquidation", value, "USD")) + } + + #[rstest] + #[tokio::test] + async fn test_read_account_summary_cycle_refreshes_on_each_end() { + let mut subscription = stream::iter([ + Ok(net_liquidation_summary("1000.50")), + Ok(AccountSummaryResult::End), + Ok(net_liquidation_summary("1100.25")), + Ok(AccountSummaryResult::End), + ]); + let ib_account = ustr::Ustr::from("DU123"); + let mut accumulator = AccountSummaryAccumulator::default(); + + let first = + read_account_summary_cycle(&mut subscription, ib_account, &mut accumulator, None).await; + let first_balances = + finalize_account_summary_balances(accumulator.balances.clone()).unwrap(); + let second = + read_account_summary_cycle(&mut subscription, ib_account, &mut accumulator, None).await; + let second_balances = + finalize_account_summary_balances(accumulator.balances.clone()).unwrap(); + let closed = + read_account_summary_cycle(&mut subscription, ib_account, &mut accumulator, None).await; + + assert!(first); + assert_eq!( + first_balances, + vec![ + AccountBalance::from_total_and_free( + Decimal::new(100050, 2), + Decimal::new(100050, 2), + Currency::USD() + ) + .unwrap() + ] + ); + assert!(second); + assert_eq!( + second_balances, + vec![ + AccountBalance::from_total_and_free( + Decimal::new(110025, 2), + Decimal::new(110025, 2), + Currency::USD() + ) + .unwrap() + ] + ); + assert!(!closed); + } + + #[rstest] + #[tokio::test(start_paused = true)] + async fn test_read_account_summary_cycle_flushes_refresh_without_end() { + let mut subscription = stream::iter([ + Ok(net_liquidation_summary("1000.50")), + Ok(net_liquidation_summary("1100.25")), + ]) + .chain(stream::pending()); + let ib_account = ustr::Ustr::from("DU123"); + let mut accumulator = AccountSummaryAccumulator::default(); + + let flushed = read_account_summary_cycle( + &mut subscription, + ib_account, + &mut accumulator, + Some(Duration::from_secs(1)), + ) + .await; + let balances = finalize_account_summary_balances(accumulator.balances.clone()).unwrap(); + + assert!(flushed); + assert_eq!( + balances, + vec![ + AccountBalance::from_total_and_free( + Decimal::new(110025, 2), + Decimal::new(110025, 2), + Currency::USD() + ) + .unwrap() + ] + ); + } + + #[rstest] + fn test_account_summary_balance_merge_is_order_independent() { + let summaries = [ + margin_summary(AccountSummaryTags::TOTAL_CASH_VALUE, "80.00", "USD"), + margin_summary(AccountSummaryTags::SETTLED_CASH, "75.00", "USD"), + margin_summary(AccountSummaryTags::AVAILABLE_FUNDS, "60.00", "USD"), + margin_summary(AccountSummaryTags::BUYING_POWER, "120.00", "USD"), + margin_summary(AccountSummaryTags::NET_LIQUIDATION, "100.00", "USD"), + ]; + let reversed = [ + margin_summary(AccountSummaryTags::NET_LIQUIDATION, "100.00", "USD"), + margin_summary(AccountSummaryTags::BUYING_POWER, "120.00", "USD"), + margin_summary(AccountSummaryTags::AVAILABLE_FUNDS, "60.00", "USD"), + margin_summary(AccountSummaryTags::SETTLED_CASH, "75.00", "USD"), + margin_summary(AccountSummaryTags::TOTAL_CASH_VALUE, "80.00", "USD"), + ]; + + let forward = balances_from_summaries(&summaries); + let reverse = balances_from_summaries(&reversed); + + assert_eq!(forward, reverse); + assert_eq!(forward.len(), 1); + assert_eq!(forward[0].total, Money::from("100.00 USD")); + assert_eq!(forward[0].locked, Money::from("40.00 USD")); + assert_eq!(forward[0].free, Money::from("60.00 USD")); + } + + #[rstest] + fn test_account_summary_rows_without_currency_are_ignored() { + // IB reports ratio tags such as `Cushion` with an empty currency. Neither merge may + // treat that as a parse failure; the account is built from the currency-bearing rows. + let cushion = margin_summary(AccountSummaryTags::CUSHION, "0.95", ""); + let mut margins: Vec = Vec::new(); + + merge_account_summary_margin(&mut margins, &cushion); + let balances = balances_from_summaries(&[ + cushion, + margin_summary(AccountSummaryTags::NET_LIQUIDATION, "1000.00", "EUR"), + ]); + + assert!(margins.is_empty()); + assert_eq!( + balances, + vec![ + AccountBalance::from_total_and_locked( + Decimal::new(100_000, 2), + Decimal::ZERO, + Currency::EUR(), + ) + .unwrap() + ] + ); + } + #[rstest] fn test_merge_account_summary_margin_combines_init_and_maint() { // Regression: `INIT_MARGIN_REQ` and `MAINT_MARGIN_REQ` arrive as separate @@ -720,4 +1201,54 @@ mod tests { assert_eq!(usd.initial, Money::from("500.00 USD")); assert_eq!(eur.initial, Money::from("400.00 EUR")); } + + #[rstest] + #[case(Some("DU123"), true)] + #[case(None, true)] + #[case(Some("DU999"), false)] + fn test_merge_account_value_filters_by_account( + #[case] account: Option<&str>, + #[case] expected_inserted: bool, + ) { + let mut info = Params::new(); + + merge_account_value( + &mut info, + "DU123", + &account_value(account, "PostExpirationExcess", "-326492.00"), + ); + + assert_eq!( + info.get_str("PostExpirationExcess"), + expected_inserted.then_some("-326492.00"), + ); + } + + #[rstest] + fn test_merge_account_value_keeps_summary_tags_and_overwrites_same_key() { + let mut info = Params::new(); + info.insert( + AccountSummaryTags::TOTAL_CASH_VALUE.to_string(), + serde_json::Value::from("1000.00"), + ); + + merge_account_value( + &mut info, + "DU123", + &account_value(Some("DU123"), "PostExpirationMargin", "391668.36"), + ); + merge_account_value( + &mut info, + "DU123", + &account_value( + Some("DU123"), + AccountSummaryTags::TOTAL_CASH_VALUE, + "1250.50", + ), + ); + + assert_eq!(info.len(), 2); + assert_eq!(info.get_str("TotalCashValue"), Some("1250.50")); + assert_eq!(info.get_str("PostExpirationMargin"), Some("391668.36")); + } } diff --git a/crates/adapters/interactive_brokers/src/execution/commands.rs b/crates/adapters/interactive_brokers/src/execution/commands.rs new file mode 100644 index 000000000000..a4345f99bc6d --- /dev/null +++ b/crates/adapters/interactive_brokers/src/execution/commands.rs @@ -0,0 +1,918 @@ +// ------------------------------------------------------------------------------------------------- +// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. +// https://nautechsystems.io +// +// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html +// ------------------------------------------------------------------------------------------------- + +//! Interactive Brokers execution command handling. + +use nautilus_common::live::sender::EventSender; + +use super::core::*; + +impl InteractiveBrokersExecutionClient { + #[allow(clippy::too_many_arguments)] // Command execution uses explicit client state. + pub(super) async fn handle_submit_order_async( + cmd: &SubmitOrder, + client: &Arc, + orders: &OrderTracker, + next_order_id: &Arc>, + instrument_provider: &Arc, + exec_sender: &EventSender, + clock: &'static AtomicTime, + account_id: AccountId, + ib_account: Ustr, + order_submit_lock: &Arc>, + ) -> anyhow::Result<()> { + if cmd.order_init.post_only { + let ts_event = clock.get_time_ns(); + let detail = "`post_only` not supported by Interactive Brokers"; + let reason = coded_denial_reason(DENIAL_POST_ONLY_UNSUPPORTED, detail); + Self::send_order_denied_to( + cmd.order_init.trader_id, + cmd.strategy_id, + cmd.instrument_id, + cmd.order_init.client_order_id, + &reason, + exec_sender, + ts_event, + )?; + anyhow::bail!(reason); + } + + let is_inverse = instrument_provider + .find(&cmd.instrument_id) + .is_some_and(|instrument| instrument.is_inverse()); + + if cmd.order_init.quote_quantity && !is_inverse { + let ts_event = clock.get_time_ns(); + let detail = "Quote quantity requires an inverse instrument"; + let reason = coded_denial_reason(DENIAL_QUOTE_QUANTITY_UNSUPPORTED, detail); + Self::send_order_denied_to( + cmd.order_init.trader_id, + cmd.strategy_id, + cmd.instrument_id, + cmd.order_init.client_order_id, + &reason, + exec_sender, + ts_event, + )?; + anyhow::bail!(reason); + } + + if let Some(reason) = Self::unsupported_trailing_offset_reason( + cmd.order_init.order_type, + cmd.order_init.trailing_offset_type, + ) { + let ts_event = clock.get_time_ns(); + Self::send_order_denied_to( + cmd.order_init.trader_id, + cmd.strategy_id, + cmd.instrument_id, + cmd.order_init.client_order_id, + &reason, + exec_sender, + ts_event, + )?; + anyhow::bail!("{reason}"); + } + + let contract = + Self::resolve_contract_for_instrument(cmd.instrument_id, instrument_provider)?; + let contract = Self::contract_with_order_exchange_param(contract, cmd.params.as_ref())?; + + let order_any = OrderAny::try_from(cmd.order_init.clone()) + .context("Failed to construct order from `OrderInitialized`")?; + let order_ref = cmd.order_init.client_order_id.to_string(); + let _submit_guard = order_submit_lock.lock().await; + let ib_order_id = Self::reserve_next_local_order_id(next_order_id)?; + let mut ib_order = + nautilus_order_to_ib_order(&order_any, instrument_provider, ib_order_id, &order_ref) + .context("Failed to transform order")?; + Self::assign_ib_account(&mut ib_order, ib_account); + + Self::cache_order_tracking( + ib_order_id, + cmd.order_init.client_order_id, + cmd.instrument_id, + cmd.order_init.trader_id, + cmd.strategy_id, + cmd.order_init.order_side, + cmd.order_init.order_type, + orders, + )?; + + let ts_event = clock.get_time_ns(); + let event = OrderSubmitted::new( + cmd.order_init.trader_id, + cmd.strategy_id, + cmd.instrument_id, + cmd.order_init.client_order_id, + account_id, + UUID4::new(), + ts_event, + ts_event, + ); + + exec_sender + .send(ExecutionEvent::Order(OrderEventAny::Submitted(event))) + .map_err(|e| anyhow::anyhow!("Failed to send order submitted event: {e}"))?; + + if let Err(e) = client.submit_order(ib_order_id, &contract, &ib_order).await { + return Self::handle_order_submit_failure( + &e, + "Failed to submit order", + ib_order_id, + account_id, + ts_event, + orders, + exec_sender, + clock, + ); + } + + tracing::debug!( + "Submitted order {} as IB order ID {}", + cmd.order_init.client_order_id, + ib_order_id + ); + + Ok(()) + } + + // IB trails by a price amount (`auxPrice`) or a percentage (`trailingPercent`) + pub(super) fn unsupported_trailing_offset_reason( + order_type: OrderType, + trailing_offset_type: Option, + ) -> Option { + if !matches!( + order_type, + OrderType::TrailingStopMarket | OrderType::TrailingStopLimit + ) { + return None; + } + + let trailing_offset_type = trailing_offset_type?; + if matches!( + trailing_offset_type, + TrailingOffsetType::Price | TrailingOffsetType::BasisPoints + ) { + return None; + } + + let detail = format!( + "`TrailingOffsetType` {trailing_offset_type:?} is not supported (only PRICE and BASIS_POINTS are supported)" + ); + Some(coded_denial_reason( + DENIAL_TRAILING_OFFSET_TYPE_UNSUPPORTED, + &detail, + )) + } + + // Modifies from IB's current copy of the open order so attributes Nautilus orders cannot + // carry (goodAfterTime, OCA group, outsideRth, conditions) survive, including for orders + // restored from a previous session. + pub(super) async fn handle_modify_order_async( + cmd: &ModifyOrder, + client: &Arc, + orders: &OrderTracker, + instrument_provider: &Arc, + ib_account: Ustr, + request_timeout_secs: u64, + ) -> anyhow::Result<()> { + let target_ib_order_id = Self::target_ib_order_id_for_modify( + cmd, + client, + orders, + ib_account, + request_timeout_secs, + ) + .await?; + + Self::handle_modify_open_order_async( + cmd, + client, + target_ib_order_id, + orders, + instrument_provider, + ib_account, + request_timeout_secs, + ) + .await + } + + async fn target_ib_order_id_for_modify( + cmd: &ModifyOrder, + client: &Arc, + orders: &OrderTracker, + ib_account: Ustr, + request_timeout_secs: u64, + ) -> anyhow::Result> { + if let Some(venue_order_id) = &cmd.venue_order_id { + let order_selector = IbOrderSelector::from_venue_order_id(venue_order_id)?; + let order_id = + Self::resolve_ib_order_id(client, order_selector, ib_account, request_timeout_secs) + .await?; + return Ok(Some(order_id)); + } + + Ok(orders + .lock()? + .order_id_map + .get(&cmd.client_order_id) + .copied()) + } + + pub(super) fn is_own_open_order( + order: &ibapi::orders::Order, + client_id: i32, + ib_account: Ustr, + ) -> bool { + order.client_id == client_id && order.account == ib_account + } + + pub(super) fn apply_modify_fields_to_ib_order( + cmd: &ModifyOrder, + ib_order: &mut ibapi::orders::Order, + instrument_provider: &Arc, + ) -> anyhow::Result<()> { + if let Some(quantity) = cmd.quantity { + ib_order.total_quantity = quantity.as_f64(); + } + + let price_magnifier = instrument_provider.get_price_magnifier(&cmd.instrument_id) as f64; + + if let Some(price) = cmd.price { + ib_order.limit_price = Some(price.as_f64() / price_magnifier); + } + + if let Some(trigger_price) = cmd.trigger_price { + let converted_trigger_price = trigger_price.as_f64() / price_magnifier; + + if matches!(ib_order.order_type.as_str(), "TRAIL" | "TRAIL LIMIT") { + // IB rejects a trailing stop price change that keeps the trailing amount (10067) + let trailing_offset = cmd + .params + .as_ref() + .and_then(|params| { + params + .get_f64(MODIFY_TRAILING_OFFSET_PARAM) + .or_else(|| params.get_str(MODIFY_TRAILING_OFFSET_PARAM)?.parse().ok()) + }) + .with_context(|| { + format!( + "IB requires a new trailing offset with a trailing stop trigger; \ + set params[\"{MODIFY_TRAILING_OFFSET_PARAM}\"] in the order's offset units" + ) + })?; + + // The order keeps its offset type: basis points convert to IB's percent + if ib_order.trailing_percent.is_some() { + ib_order.trailing_percent = Some(trailing_offset / 100.0); + } else { + ib_order.aux_price = Some(trailing_offset); + } + ib_order.trail_stop_price = Some(converted_trigger_price); + } else { + ib_order.aux_price = Some(converted_trigger_price); + } + } + + Ok(()) + } + + async fn handle_modify_open_order_async( + cmd: &ModifyOrder, + client: &Arc, + target_ib_order_id: Option, + orders: &OrderTracker, + instrument_provider: &Arc, + ib_account: Ustr, + request_timeout_secs: u64, + ) -> anyhow::Result<()> { + let timeout_dur = Duration::from_secs(request_timeout_secs); + let subscription = tokio::time::timeout(timeout_dur, client.all_open_orders()) + .await + .context("Timeout requesting open orders for modify")??; + let mut subscription = subscription.filter_data(); + + let client_order_id = cmd.client_order_id.to_string(); + + while let Some(order_result) = subscription.next().await { + match order_result { + Ok(Orders::OrderData(data)) => { + // Raw order IDs and references repeat across API clients and accounts, and + // IB lets only the placing client modify an order + if !Self::is_active_open_order(&data.order) + || !Self::is_own_open_order(&data.order, client.client_id(), ib_account) + { + continue; + } + + let matches_order_id = + target_ib_order_id.is_some_and(|order_id| data.order_id == order_id); + let matches_order_ref = parse::normalized_order_ref(&data.order.order_ref) + == Some(client_order_id.as_str()); + + if !matches_order_id && !matches_order_ref { + continue; + } + + let ib_order_id = data.order_id; + let contract = data.contract; + let contract = + Self::contract_with_order_exchange_param(contract, cmd.params.as_ref())?; + let mut ib_order = data.order; + + Self::apply_modify_fields_to_ib_order(cmd, &mut ib_order, instrument_provider)?; + + { + let mut state = orders.lock()?; + state.order_id_map.insert(cmd.client_order_id, ib_order_id); + state + .venue_order_id_map + .insert(ib_order_id, cmd.client_order_id); + if let Some(order) = state.active_orders.get_mut(&ib_order_id) { + order.instrument_id = cmd.instrument_id; + } + } + Self::mark_pending_modify(cmd, ib_order_id, orders, &ib_order)?; + + if let Err(e) = client.submit_order(ib_order_id, &contract, &ib_order).await { + if Self::is_definitive_order_submit_error(&e) { + Self::clear_pending_modify(ib_order_id, orders); + return Err(e) + .context("IB rejected the modified open order before sending it"); + } + + tracing::warn!( + "Modify outcome is unknown after attempting to send open order {} to IB: {e}", + cmd.client_order_id + ); + return Ok(()); + } + + tracing::debug!( + "Modified open order {} (IB order ID: {}) after cache miss", + cmd.client_order_id, + ib_order_id + ); + + return Ok(()); + } + Ok(_) => {} + Err(e) => { + tracing::warn!("Error receiving open order data for modify: {e}"); + } + } + } + + anyhow::bail!( + "Order not found for modify in IB open orders: client_order_id={}, venue_order_id={:?}", + cmd.client_order_id, + cmd.venue_order_id, + ) + } + + pub(super) fn mark_pending_modify( + cmd: &ModifyOrder, + ib_order_id: i32, + orders: &OrderTracker, + ib_order: &ibapi::orders::Order, + ) -> anyhow::Result<()> { + let mut state = orders.lock()?; + let order = state + .active_orders + .get_mut(&ib_order_id) + .ok_or_else(|| anyhow::anyhow!("IB order {ib_order_id} is not tracked for modify"))?; + anyhow::ensure!( + order.client_order_id == cmd.client_order_id, + "IB order {ib_order_id} is tracked as {}, not {}", + order.client_order_id, + cmd.client_order_id, + ); + anyhow::ensure!( + order.instrument_id == cmd.instrument_id, + "IB order {ib_order_id} is tracked for {}, not {}", + order.instrument_id, + cmd.instrument_id, + ); + anyhow::ensure!( + order.pending_modify.is_none(), + "IB order {ib_order_id} already has a pending modify", + ); + order.pending_modify = Some(PendingModifyValues { + total_quantity: ib_order.total_quantity, + limit_price: ib_order.limit_price, + aux_price: ib_order.aux_price, + // IB moves a trailing stop's trigger with the market, so only a requested one is checked + trail_stop_price: cmd.trigger_price.and(ib_order.trail_stop_price), + }); + Ok(()) + } + + fn clear_pending_modify(ib_order_id: i32, orders: &OrderTracker) { + if let Ok(mut state) = orders.lock() + && let Some(order) = state.active_orders.get_mut(&ib_order_id) + { + order.pending_modify = None; + } + } + + #[allow(clippy::too_many_arguments)] // Order-list submission shares explicit client state. + pub(super) async fn handle_submit_order_list_async( + cmd: &SubmitOrderList, + orders: &[OrderAny], + client: &Arc, + order_tracker: &OrderTracker, + next_order_id: &Arc>, + instrument_provider: &Arc, + exec_sender: &EventSender, + clock: &'static AtomicTime, + account_id: AccountId, + ib_account: Ustr, + strategy_id: StrategyId, + order_submit_lock: &Arc>, + ) -> anyhow::Result<()> { + let num_orders = orders.len(); + anyhow::ensure!(!orders.is_empty(), "Cannot submit an empty order list"); + + let _submit_guard = order_submit_lock.lock().await; + let mut ib_order_ids = AHashMap::with_capacity(num_orders); + + for order in orders { + let ib_order_id = Self::reserve_next_local_order_id(next_order_id)?; + ib_order_ids.insert(order.client_order_id(), ib_order_id); + } + + // Every order is prepared before the first reaches IB, so a list that cannot be + // prepared leaves nothing parked at the venue and is denied as a whole + let prepared = match Self::prepare_order_list( + cmd, + orders, + &ib_order_ids, + order_tracker, + instrument_provider, + ib_account, + ) { + Ok(prepared) => prepared, + Err(e) => { + let reason = coded_denial_reason(DENIAL_ORDER_LIST_INVALID, &format!("{e:#}")); + Self::deny_unsubmitted_order_list( + orders, + &reason, + strategy_id, + exec_sender, + clock, + )?; + return Err(e); + } + }; + + for (index, (order, (order_contract, ib_order))) in orders.iter().zip(prepared).enumerate() + { + let ib_order_id = ib_order_ids[&order.client_order_id()]; + + Self::cache_order_tracking( + ib_order_id, + order.client_order_id(), + order.instrument_id(), + order.trader_id(), + strategy_id, + order.order_side(), + order.order_type(), + order_tracker, + )?; + + let ts_event = clock.get_time_ns(); + let event = OrderSubmitted::new( + order.trader_id(), + strategy_id, + order.instrument_id(), + order.client_order_id(), + account_id, + UUID4::new(), + ts_event, + ts_event, + ); + + exec_sender + .send(ExecutionEvent::Order(OrderEventAny::Submitted(event))) + .map_err(|e| anyhow::anyhow!("Failed to send order submitted event: {e}"))?; + + if let Err(e) = client + .submit_order(ib_order_id, &order_contract, &ib_order) + .await + { + if let Err(denial_error) = Self::deny_unsubmitted_order_list( + &orders[index + 1..], + DENIAL_ORDER_LIST_SIBLING_SUBMIT_FAILED, + strategy_id, + exec_sender, + clock, + ) { + tracing::error!( + "Failed to deny unsubmitted order-list siblings after order {} failed: {denial_error}", + order.client_order_id() + ); + } + Self::cancel_untransmitted_order_list_predecessors( + &orders[..index], + &ib_order_ids, + client, + order_tracker, + strategy_id, + account_id, + exec_sender, + clock, + ) + .await; + return Self::handle_order_submit_failure( + &e, + "Failed to submit order from list", + ib_order_id, + account_id, + ts_event, + order_tracker, + exec_sender, + clock, + ); + } + + tracing::debug!( + "Submitted order {} from list as IB order ID {}", + order.client_order_id(), + ib_order_id, + ); + } + + Ok(()) + } + + pub(super) fn prepare_order_list( + cmd: &SubmitOrderList, + orders: &[OrderAny], + ib_order_ids: &AHashMap, + order_tracker: &OrderTracker, + instrument_provider: &Arc, + ib_account: Ustr, + ) -> anyhow::Result> { + let num_orders = orders.len(); + let mut prepared = Vec::with_capacity(num_orders); + + for (index, order) in orders.iter().enumerate() { + let ib_order_id = ib_order_ids[&order.client_order_id()]; + + let order_contract = + Self::resolve_contract_for_instrument(order.instrument_id(), instrument_provider)?; + let order_contract = + Self::contract_with_order_exchange_param(order_contract, cmd.params.as_ref())?; + + let order_ref = order.client_order_id().to_string(); + let mut ib_order = + nautilus_order_to_ib_order(order, instrument_provider, ib_order_id, &order_ref) + .context("Failed to transform order")?; + Self::assign_ib_account(&mut ib_order, ib_account); + ib_order.transmit = index == num_orders - 1; + + if let Some(parent_order_id) = order.parent_order_id() { + let parent_ib_order_id = match ib_order_ids.get(&parent_order_id) { + Some(parent_ib_order_id) => *parent_ib_order_id, + None => *order_tracker + .lock()? + .order_id_map + .get(&parent_order_id) + .with_context(|| { + format!( + "Parent order ID {parent_order_id} not found for order {}", + order.client_order_id() + ) + })?, + }; + ib_order.parent_id = parent_ib_order_id; + } + + prepared.push((order_contract, ib_order)); + } + + Ok(prepared) + } + + #[allow(clippy::too_many_arguments)] // Failure resolution shares the submit context. + async fn cancel_untransmitted_order_list_predecessors( + predecessors: &[OrderAny], + ib_order_ids: &AHashMap, + client: &Arc, + order_tracker: &OrderTracker, + strategy_id: StrategyId, + account_id: AccountId, + exec_sender: &EventSender, + clock: &'static AtomicTime, + ) { + // Predecessors were parked at IB with `transmit=false` and can no longer be + // activated once a later sibling failed, so cancel them at the venue and give + // each a definite terminal state. + let mut failed_cancels: AHashSet = AHashSet::new(); + + for order in predecessors { + let client_order_id = order.client_order_id(); + let Some(ib_order_id) = ib_order_ids.get(&client_order_id).copied() else { + continue; + }; + + if let Err(e) = client.cancel_order(ib_order_id, "").await { + tracing::error!( + "Failed to cancel untransmitted order-list predecessor {} (IB order ID {}): {e}", + client_order_id, + ib_order_id + ); + failed_cancels.insert(client_order_id); + } + } + + if let Err(e) = Self::resolve_failed_order_list_predecessors( + predecessors, + ib_order_ids, + &failed_cancels, + order_tracker, + strategy_id, + account_id, + exec_sender, + clock, + ) { + tracing::error!( + "Failed to resolve untransmitted order-list predecessors after sibling failure: {e}" + ); + } + } + + fn assign_ib_account(ib_order: &mut ibapi::orders::Order, ib_account: Ustr) { + ib_order.account = ib_account.to_string(); + ib_order.clearing_account = ib_account.to_string(); + } + + #[allow(clippy::too_many_arguments)] // Failure resolution shares the submit context. + pub(super) fn resolve_failed_order_list_predecessors( + predecessors: &[OrderAny], + ib_order_ids: &AHashMap, + failed_cancels: &AHashSet, + order_tracker: &OrderTracker, + strategy_id: StrategyId, + account_id: AccountId, + exec_sender: &EventSender, + clock: &'static AtomicTime, + ) -> anyhow::Result<()> { + for order in predecessors { + let client_order_id = order.client_order_id(); + let Some(ib_order_id) = ib_order_ids.get(&client_order_id).copied() else { + continue; + }; + + if failed_cancels.contains(&client_order_id) { + // The venue cancel failed, so the order may remain parked at TWS and + // manually transmittable; keep tracking instead of claiming terminal. + tracing::error!( + "Order-list predecessor {} (IB order ID {}) remains parked at IB after a failed cancel; keeping tracking until venue state resolves", + client_order_id, + ib_order_id + ); + continue; + } + + let perm_id = order_tracker.lock().ok().and_then(|state| { + state + .active_orders + .get(&ib_order_id) + .map(|tracked| tracked.perm_id) + }); + + let ts_event = clock.get_time_ns(); + let event = OrderCanceled::new( + order.trader_id(), + strategy_id, + order.instrument_id(), + client_order_id, + UUID4::new(), + ts_event, + ts_event, + false, + Some(ib_venue_order_id(ib_order_id, perm_id.unwrap_or(0))), + Some(account_id), + None, + ); + exec_sender + .send(ExecutionEvent::Order(OrderEventAny::Canceled(event))) + .map_err(|e| { + anyhow::anyhow!("Failed to send order-list predecessor canceled event: {e}") + })?; + + Self::remove_order_tracking(ib_order_id, client_order_id, order_tracker)?; + } + + Ok(()) + } + + pub(super) fn deny_unsubmitted_order_list( + orders: &[OrderAny], + reason: &str, + strategy_id: StrategyId, + exec_sender: &EventSender, + clock: &'static AtomicTime, + ) -> anyhow::Result<()> { + for order in orders { + let ts_event = clock.get_time_ns(); + Self::send_order_denied_to( + order.trader_id(), + strategy_id, + order.instrument_id(), + order.client_order_id(), + reason, + exec_sender, + ts_event, + )?; + } + + Ok(()) + } + + #[allow(clippy::too_many_arguments)] // Failure emission preserves the submit context. + pub(super) fn handle_order_submit_failure( + error: &ibapi::Error, + failure_prefix: &str, + ib_order_id: i32, + account_id: AccountId, + ts_event: UnixNanos, + orders: &OrderTracker, + exec_sender: &EventSender, + clock: &'static AtomicTime, + ) -> anyhow::Result<()> { + match Self::classify_order_submit_error(error) { + CommandFailure::Ambiguous(reason) => { + anyhow::bail!( + "{failure_prefix}; outcome is unknown after possible transmission: {reason}" + ); + } + CommandFailure::NotSent(reason) | CommandFailure::VenueRejected(reason) => { + let context = + Self::get_tracked_order_context(ib_order_id, orders)?.with_context(|| { + format!("Tracked order context not found for {ib_order_id}") + })?; + + Self::remove_order_tracking(ib_order_id, context.client_order_id, orders)?; + + let reason = format!("{failure_prefix}: {reason}"); + let event = OrderRejected::new( + context.trader_id, + context.strategy_id, + context.instrument_id, + context.client_order_id, + account_id, + Ustr::from(&reason), + UUID4::new(), + ts_event, + clock.get_time_ns(), + false, + false, + ); + exec_sender + .send(ExecutionEvent::Order(OrderEventAny::Rejected(event))) + .map_err(|e| anyhow::anyhow!("Failed to send order rejected event: {e}"))?; + anyhow::bail!(reason); + } + } + } +} + +#[cfg(test)] +mod tests { + use nautilus_core::Params; + use nautilus_model::identifiers::Symbol; + use serde_json::json; + + use super::*; + use crate::config::InteractiveBrokersInstrumentProviderConfig; + + fn modify_trigger_cmd() -> ModifyOrder { + ModifyOrder::new( + TraderId::from("TRADER-001"), + Some(ClientId::from("CLIENT-001")), + StrategyId::from("S-001"), + InstrumentId::new(Symbol::from("AAPL"), Venue::from("NASDAQ")), + ClientOrderId::from("O-001"), + Some(VenueOrderId::from("1")), + None, + None, + Some(Price::from("149.50")), + UUID4::new(), + UnixNanos::default(), + None, + None, + ) + } + + fn instrument_provider() -> Arc { + Arc::new(InteractiveBrokersInstrumentProvider::new( + InteractiveBrokersInstrumentProviderConfig::default(), + )) + } + + #[rstest::rstest] + #[case::price_offset(None, Some(json!(2.5)), Some(2.5), None)] + #[case::basis_point_offset(Some(0.25), Some(json!("30")), Some(0.5), Some(0.3))] + fn modify_trailing_stop_sends_trigger_with_new_offset( + #[case] trailing_percent: Option, + #[case] trailing_offset: Option, + #[case] expected_aux: Option, + #[case] expected_percent: Option, + ) { + let mut ib_order = ibapi::orders::Order { + order_type: "TRAIL".to_string(), + aux_price: Some(0.5), + trailing_percent, + ..Default::default() + }; + let mut cmd = modify_trigger_cmd(); + cmd.params = trailing_offset.map(|offset| { + let mut params = Params::new(); + params.insert(MODIFY_TRAILING_OFFSET_PARAM.to_string(), offset); + params + }); + + InteractiveBrokersExecutionClient::apply_modify_fields_to_ib_order( + &cmd, + &mut ib_order, + &instrument_provider(), + ) + .unwrap(); + + assert_eq!(ib_order.aux_price, expected_aux); + assert_eq!(ib_order.trailing_percent, expected_percent); + assert_eq!(ib_order.trail_stop_price, Some(149.5)); + } + + #[rstest::rstest] + fn modify_trailing_stop_without_offset_fails_before_sending() { + let original = ibapi::orders::Order { + order_type: "TRAIL".to_string(), + aux_price: Some(0.5), + trail_stop_price: Some(148.0), + ..Default::default() + }; + let mut ib_order = original.clone(); + + let error = InteractiveBrokersExecutionClient::apply_modify_fields_to_ib_order( + &modify_trigger_cmd(), + &mut ib_order, + &instrument_provider(), + ) + .unwrap_err(); + + assert_eq!( + error.to_string(), + "IB requires a new trailing offset with a trailing stop trigger; set params[\"trailing_offset\"] in the order's offset units" + ); + assert_eq!(ib_order, original); + } + + #[rstest::rstest] + #[case::own_order(7, "DU001", true)] + #[case::other_client(8, "DU001", false)] + #[case::other_account(7, "DU002", false)] + fn is_own_open_order_requires_client_and_account( + #[case] client_id: i32, + #[case] account: &str, + #[case] expected: bool, + ) { + let order = ibapi::orders::Order { + client_id, + account: account.to_string(), + ..Default::default() + }; + + assert_eq!( + InteractiveBrokersExecutionClient::is_own_open_order(&order, 7, Ustr::from("DU001")), + expected + ); + } + + #[rstest::rstest] + fn modify_stop_order_routes_trigger_to_aux_price() { + let mut ib_order = ibapi::orders::Order { + order_type: "STP".to_string(), + ..Default::default() + }; + + InteractiveBrokersExecutionClient::apply_modify_fields_to_ib_order( + &modify_trigger_cmd(), + &mut ib_order, + &instrument_provider(), + ) + .unwrap(); + + assert_eq!(ib_order.aux_price, Some(149.5)); + assert_eq!(ib_order.trail_stop_price, None); + } +} diff --git a/crates/adapters/interactive_brokers/src/execution/conditions.rs b/crates/adapters/interactive_brokers/src/execution/conditions.rs index c5de6ba1cd01..71421dd253be 100644 --- a/crates/adapters/interactive_brokers/src/execution/conditions.rs +++ b/crates/adapters/interactive_brokers/src/execution/conditions.rs @@ -44,10 +44,7 @@ pub fn create_ib_conditions(conditions_data: &Value) -> anyhow::Result condition_kind, Err(_) => { @@ -57,9 +54,7 @@ pub fn create_ib_conditions(conditions_data: &Value) -> anyhow::Result anyhow::Result { - let con_id = condition_dict - .get("conId") - .and_then(|v| v.as_i64()) - .unwrap_or(0) as i32; - let exchange = condition_dict - .get("exchange") - .and_then(|v| v.as_str()) - .unwrap_or("SMART"); - let price = condition_dict - .get("price") - .and_then(|v| v.as_f64()) - .unwrap_or(0.0); - let is_more = condition_dict - .get("isMore") - .and_then(|v| v.as_bool()) - .unwrap_or(true); - let trigger_method = condition_dict - .get("triggerMethod") - .and_then(|v| v.as_i64()) - .unwrap_or(0) as i32; + let con_id = json_i32(condition_dict, "conId").unwrap_or(0); + let exchange = json_str(condition_dict, "exchange").unwrap_or("SMART"); + let price = json_f64(condition_dict, "price").unwrap_or(0.0); + let is_more = json_bool(condition_dict, "isMore").unwrap_or(true); + let trigger_method = json_i32(condition_dict, "triggerMethod").unwrap_or(0); let mut builder = PriceCondition::builder(con_id, exchange); - if !is_more { - builder = builder.less_than(price); - } else { + if is_more { builder = builder.greater_than(price); + } else { + builder = builder.less_than(price); } - builder = builder - .trigger_method(IbTriggerMethod::from(trigger_method).ibapi_trigger_method()); + builder = builder.trigger_method( + IbTriggerMethod::try_from(trigger_method)?.ibapi_trigger_method(), + ); builder = builder.conjunction(is_conjunction); OrderCondition::Price(builder.build()) } IbConditionKind::Time => { - let time = condition_dict - .get("time") - .and_then(|v| v.as_str()) - .unwrap_or(""); - let is_more = condition_dict - .get("isMore") - .and_then(|v| v.as_bool()) - .unwrap_or(true); + let time = json_str(condition_dict, "time").unwrap_or(""); + let is_more = json_bool(condition_dict, "isMore").unwrap_or(true); let mut builder = TimeCondition::builder(); - if !is_more { - builder = builder.less_than(time); - } else { + if is_more { builder = builder.greater_than(time); + } else { + builder = builder.less_than(time); } builder = builder.conjunction(is_conjunction); OrderCondition::Time(builder.build()) } IbConditionKind::Margin => { - let percent = condition_dict - .get("percent") - .and_then(|v| v.as_i64()) - .unwrap_or(0) as i32; - let is_more = condition_dict - .get("isMore") - .and_then(|v| v.as_bool()) - .unwrap_or(true); + let percent = json_i32(condition_dict, "percent").unwrap_or(0); + let is_more = json_bool(condition_dict, "isMore").unwrap_or(true); let mut builder = MarginCondition::builder(); - if !is_more { - builder = builder.less_than(percent); - } else { + if is_more { builder = builder.greater_than(percent); + } else { + builder = builder.less_than(percent); } builder = builder.conjunction(is_conjunction); OrderCondition::Margin(builder.build()) } IbConditionKind::Execution => { - let symbol = condition_dict - .get("symbol") - .and_then(|v| v.as_str()) + let symbol = json_str(condition_dict, "symbol") .context("Missing symbol for execution condition")?; - let sec_type = condition_dict - .get("secType") - .and_then(|v| v.as_str()) - .unwrap_or("STK"); + let sec_type = json_str(condition_dict, "secType").unwrap_or("STK"); let sec_type = IbSecurityType::from_str(sec_type) .map_or_else(|_| sec_type.to_string(), |sec_type| sec_type.to_string()); - let exchange = condition_dict - .get("exchange") - .and_then(|v| v.as_str()) - .unwrap_or("SMART"); + let exchange = json_str(condition_dict, "exchange").unwrap_or("SMART"); let mut builder = ExecutionCondition::builder(symbol, sec_type.as_str(), exchange); builder = builder.conjunction(is_conjunction); OrderCondition::Execution(builder.build()) } IbConditionKind::Volume => { - let con_id = condition_dict - .get("conId") - .and_then(|v| v.as_i64()) - .unwrap_or(0) as i32; - let exchange = condition_dict - .get("exchange") - .and_then(|v| v.as_str()) - .unwrap_or("SMART"); - let volume = condition_dict - .get("volume") - .and_then(|v| v.as_i64()) - .unwrap_or(0) as i32; - let is_more = condition_dict - .get("isMore") - .and_then(|v| v.as_bool()) - .unwrap_or(true); + let con_id = json_i32(condition_dict, "conId").unwrap_or(0); + let exchange = json_str(condition_dict, "exchange").unwrap_or("SMART"); + let volume = json_i32(condition_dict, "volume").unwrap_or(0); + let is_more = json_bool(condition_dict, "isMore").unwrap_or(true); let mut builder = VolumeCondition::builder(con_id, exchange); - if !is_more { - builder = builder.less_than(volume); - } else { + if is_more { builder = builder.greater_than(volume); + } else { + builder = builder.less_than(volume); } builder = builder.conjunction(is_conjunction); OrderCondition::Volume(builder.build()) } IbConditionKind::PercentChange => { - let con_id = condition_dict - .get("conId") - .and_then(|v| v.as_i64()) - .unwrap_or(0) as i32; - let exchange = condition_dict - .get("exchange") - .and_then(|v| v.as_str()) - .unwrap_or("SMART"); - let change_percent = condition_dict - .get("changePercent") - .and_then(|v| v.as_f64()) - .unwrap_or(0.0); - let is_more = condition_dict - .get("isMore") - .and_then(|v| v.as_bool()) - .unwrap_or(true); + let con_id = json_i32(condition_dict, "conId").unwrap_or(0); + let exchange = json_str(condition_dict, "exchange").unwrap_or("SMART"); + let change_percent = json_f64(condition_dict, "changePercent").unwrap_or(0.0); + let is_more = json_bool(condition_dict, "isMore").unwrap_or(true); let mut builder = PercentChangeCondition::builder(con_id, exchange); - if !is_more { - builder = builder.less_than(change_percent); - } else { + if is_more { builder = builder.greater_than(change_percent); + } else { + builder = builder.less_than(change_percent); } builder = builder.conjunction(is_conjunction); OrderCondition::PercentChange(builder.build()) @@ -224,6 +161,25 @@ pub fn create_ib_conditions(conditions_data: &Value) -> anyhow::Result(value: &'a Value, key: &str) -> Option<&'a str> { + value.get(key).and_then(Value::as_str) +} + +fn json_i32(value: &Value, key: &str) -> Option { + value + .get(key) + .and_then(Value::as_i64) + .map(|value| value as i32) +} + +fn json_f64(value: &Value, key: &str) -> Option { + value.get(key).and_then(Value::as_f64) +} + +fn json_bool(value: &Value, key: &str) -> Option { + value.get(key).and_then(Value::as_bool) +} + #[cfg(test)] mod tests { use ibapi::orders::OrderCondition; diff --git a/crates/adapters/interactive_brokers/src/execution/core.rs b/crates/adapters/interactive_brokers/src/execution/core.rs index cf475bf6c2a2..c22f4818adb1 100644 --- a/crates/adapters/interactive_brokers/src/execution/core.rs +++ b/crates/adapters/interactive_brokers/src/execution/core.rs @@ -15,18 +15,10 @@ //! Core execution client implementation for Interactive Brokers. -#[path = "core_orders.rs"] -mod core_orders; -#[path = "core_tracking.rs"] -mod core_tracking; -#[path = "core_updates.rs"] -mod core_updates; #[cfg(test)] -#[path = "core_tests.rs"] mod tests; -use std::{ - collections::VecDeque, +pub(super) use std::{ fmt::Debug, str::FromStr, sync::{ @@ -36,26 +28,31 @@ use std::{ time::Duration, }; -use ahash::AHashMap; -use anyhow::Context; -use ibapi::{ +pub(super) use ahash::{AHashMap, AHashSet}; +pub(super) use anyhow::Context; +pub(super) use ibapi::{ + Notice, NoticeCategory, accounts::PositionUpdate, client::Client, - contracts::{Contract, SecurityType}, + contracts::{Contract, Exchange, SecurityType}, orders::{ - ExecutionData, ExecutionFilter, Executions, OrderStatus as IBOrderStatus, OrderUpdate, - Orders, + ExecutionData, ExecutionFilter, Executions, OrderStatus as IBOrderStatus, OrderStatusKind, + OrderUpdate, Orders, }, prelude::{StreamExt, SubscriptionItemStreamExt}, + subscriptions::SubscriptionItem, }; -use nautilus_common::{ +pub(super) use nautilus_common::{ cache::{Cache, fifo::FifoCacheMap}, - clients::ExecutionClient, + clients::{ExecutionClient, ExecutionReportTask}, enums::LogLevel, factories::OrderEventFactory, - live::{runner::get_exec_event_sender, sender::EventSender}, + live::{ + runner::{get_data_event_sender, get_exec_event_sender}, + sender::EventSender, + }, messages::{ - ExecutionEvent, + DataEvent, ExecutionEvent, execution::{ BatchCancelOrders, CancelAllOrders, CancelOrder, ExecutionReport, GenerateFillReports, GenerateFillReportsBuilder, GenerateOrderStatusReport, GenerateOrderStatusReports, @@ -64,27 +61,25 @@ use nautilus_common::{ SubmitOrder, SubmitOrderList, }, }, - msgbus::{send_account_state, switchboard::MessagingSwitchboard}, }; -use nautilus_core::{ +pub(super) use nautilus_core::{ DurationNanos, Params, UUID4, UnixNanos, time::{AtomicTime, get_atomic_clock_realtime}, }; -use nautilus_live::{ +pub(super) use nautilus_live::{ ExecutionClientCore, execution::failure::CommandFailure, - task::{TaskGroup, TaskGroupGuard}, + task::{TaskGroup, TaskGroupGuard, TaskSpawner}, }; -use nautilus_model::{ +pub(super) use nautilus_model::{ accounts::AccountAny, enums::{ - LiquiditySide, OmsType, OrderSide, OrderStatus, OrderType, PositionSide, TimeInForce, - TrailingOffsetType, + LiquiditySide, OmsType, OrderSide, OrderStatus, OrderType, PositionSide, TrailingOffsetType, }, events::{ - AccountState, OrderAccepted, OrderCancelRejected, OrderCanceled, OrderDenied, - OrderDeniedReason, OrderEventAny, OrderFilled, OrderModifyRejected, OrderPendingCancel, - OrderRejected, OrderSubmitted, OrderUpdated, + OrderAccepted, OrderCancelRejected, OrderCanceled, OrderDenied, OrderDeniedReason, + OrderEventAny, OrderFilled, OrderModifyRejected, OrderPendingCancel, OrderRejected, + OrderSubmitted, OrderUpdated, }, identifiers::{ AccountId, ClientId, ClientOrderId, InstrumentId, StrategyId, TradeId, TraderId, Venue, @@ -95,27 +90,47 @@ use nautilus_model::{ reports::{ExecutionMassStatus, FillReport, OrderStatusReport, PositionStatusReport}, types::{AccountBalance, Currency, MarginBalance, Money, Price, Quantity}, }; -use parking_lot::Mutex; -use rust_decimal::{Decimal, prelude::ToPrimitive}; -use ustr::Ustr; - -use super::{ - account::{PositionTracker, create_position_tracker, raw_ib_account_code}, +pub(super) use parking_lot::Mutex; +pub(super) use rust_decimal::Decimal; +pub(super) use tokio::sync::Mutex as AsyncMutex; +pub(super) use tokio_util::sync::CancellationToken; +pub(super) use ustr::Ustr; + +pub(super) use super::{ + account::{ + AccountSnapshot, PositionTracker, create_position_tracker, ib_account_code, + initialize_position_tracking, record_own_fill, stream_account_summary, + subscribe_account_summary, subscribe_positions, + }, + parse, parse::{ ib_venue_order_id, parse_execution_time, parse_execution_to_fill_report, parse_order_status_to_report, }, + reports::{IbReportClient, IbReportContext}, transform::nautilus_order_to_ib_order, }; -use crate::{ - common::{ - parse::{ib_contract_to_instrument_id_simple, is_spread_instrument_id}, - shared_client::SharedClientHandle, - }, +use super::{incarnations::OrderIncarnationGroup, parse::parse_order_data_to_report}; +pub(super) use crate::{ + common::shared_client::{SharedClientHandle, get_or_connect}, config::InteractiveBrokersExecutionClientConfig, providers::instruments::InteractiveBrokersInstrumentProvider, }; +pub(super) const MODIFY_TRAILING_OFFSET_PARAM: &str = "trailing_offset"; + +pub(super) const DENIAL_CLIENT_NOT_READY: &str = "IB_CLIENT_NOT_READY"; +pub(super) const DENIAL_ORDER_LIST_INVALID: &str = "ORDER_LIST_INVALID"; +pub(super) const DENIAL_ORDER_LIST_SIBLING_SUBMIT_FAILED: &str = "ORDER_LIST_SIBLING_SUBMIT_FAILED"; +pub(super) const DENIAL_POST_ONLY_UNSUPPORTED: &str = "UNSUPPORTED_POST_ONLY"; +pub(super) const DENIAL_QUOTE_QUANTITY_UNSUPPORTED: &str = "UNSUPPORTED_QUOTE_QUANTITY"; +pub(super) const DENIAL_TRAILING_OFFSET_TYPE_UNSUPPORTED: &str = "UNSUPPORTED_TRAILING_OFFSET_TYPE"; +const ORDER_ID_PARTITION_SIZE: i32 = 1_000_000; + +pub(super) fn coded_denial_reason(code: &str, detail: &str) -> String { + format!("{code}: {detail}") +} + /// Interactive Brokers execution client. /// /// This client provides order execution functionality using the `rust-ibapi` library. @@ -125,75 +140,241 @@ use crate::{ pyo3::pyclass(module = "nautilus_trader.adapters.interactive_brokers", unsendable) )] pub struct InteractiveBrokersExecutionClient { - core: ExecutionClientCore, - config: InteractiveBrokersExecutionClientConfig, - instrument_provider: Arc, - is_connected: AtomicBool, - ib_client: Option, - pending_tasks: TaskGroup, - next_order_id: Arc>, - order_submit_lock: Arc>, - session_tasks: TaskGroup, - order_id_map: Arc>>, - venue_order_id_map: Arc>>, - commission_cache: Arc>, - pending_execution_cache: Arc>, - instrument_id_map: Arc>>, - trader_id_map: Arc>>, - strategy_id_map: Arc>>, - active_order_contexts: Arc>>, - terminal_order_contexts: Arc>>, - spread_fill_tracking: Arc>>>, - position_tracker: PositionTracker, - order_avg_prices: Arc>>, - pending_combo_fills: Arc>>>, - pending_combo_fill_avgs: Arc>>>, - order_fill_progress: Arc>>, - pending_cancel_orders: Arc>>, + /// Core execution client functionality. + pub(super) core: ExecutionClientCore, + /// Configuration for the client. + pub(super) config: InteractiveBrokersExecutionClientConfig, + /// Raw IB account code sent to TWS, derived once from the configured `account_id`. + pub(super) ib_account: Ustr, + /// Instrument provider. + pub(super) instrument_provider: Arc, + /// Connection state. + pub(super) is_connected: Arc, + /// IB API client (shared per host/port/client_id when both data and execution connect). + pub(super) ib_client: Option, + /// Tasks serving execution commands. + pub(super) pending_tasks: TaskGroup, + /// Tasks serving the active connection session. + pub(super) session_tasks: TaskGroup, + /// Order ID counter. + pub(super) next_order_id: Arc>, + /// Serializes order submissions so TWS receives monotonically increasing order IDs. + pub(super) order_submit_lock: Arc>, + /// Per-order state and indexes. + pub(super) orders: OrderTracker, + /// Commission cache by execution ID (to merge with fill reports). + pub(super) commission_cache: Arc>, + /// Execution cache by execution ID while awaiting commission reports. + pub(super) pending_execution_cache: Arc>, + /// Position tracker for detecting external position changes (e.g., option exercises). + pub(super) position_tracker: PositionTracker, } -type CommissionCache = FifoCacheMap; -type PendingExecutionCache = FifoCacheMap; +pub(super) type CommissionCache = FifoCacheMap; +pub(super) type PendingExecutionCache = + FifoCacheMap; -#[derive(Clone, Debug)] -struct PendingComboFill { - trader_id: TraderId, - strategy_id: StrategyId, - account_id: AccountId, - instrument_id: InstrumentId, - venue_order_id: VenueOrderId, - trade_id: TradeId, - order_side: OrderSide, - order_type: OrderType, - last_qty: Quantity, - commission: Money, - liquidity_side: LiquiditySide, - quote_currency: Currency, - client_order_id: ClientOrderId, - ts_event: UnixNanos, - ts_init: UnixNanos, +#[derive(Clone)] +pub(super) struct OrderTracker { + pub(super) state: Arc>, +} + +pub(super) struct OrderTrackerState { + pub(super) groups: AHashMap, + pub(super) group_history: FifoCacheMap, + pub(super) incarnation_parents: AHashMap, + pub(super) order_snapshots: FifoCacheMap, + pub(super) auxiliary_orders: AHashMap, + pub(super) client_id: i32, + pub(super) order_id_map: AHashMap, + pub(super) venue_order_id_map: AHashMap, + pub(super) active_orders: AHashMap, + pub(super) terminal_orders: FifoCacheMap, + /// Fills held for untracked orders while their details resolve; a key marks a resolution + /// in flight. + pub(super) fills_held: AHashMap>, +} + +impl OrderTrackerState { + pub(super) fn order(&self, order_id: i32) -> Option<&TrackedOrder> { + self.active_orders + .get(&order_id) + .or_else(|| self.terminal_orders.get(&order_id)) + } + + pub(super) fn order_mut(&mut self, order_id: i32) -> Option<&mut TrackedOrder> { + if self.active_orders.contains_key(&order_id) { + self.active_orders.get_mut(&order_id) + } else { + self.terminal_orders.get_mut(&order_id) + } + } + + pub(super) fn order_by_client(&self, client_order_id: ClientOrderId) -> Option<&TrackedOrder> { + if let Some(order) = self.auxiliary_orders.get(&client_order_id) { + return Some(order); + } + self.order_id_map + .get(&client_order_id) + .and_then(|order_id| self.order(*order_id)) + } + + pub(super) fn order_by_client_mut( + &mut self, + client_order_id: ClientOrderId, + ) -> Option<&mut TrackedOrder> { + if self.auxiliary_orders.contains_key(&client_order_id) { + return self.auxiliary_orders.get_mut(&client_order_id); + } + let order_id = self.order_id_map.get(&client_order_id).copied()?; + self.order_mut(order_id) + } + + pub(super) fn cancel_selector( + &self, + client_order_id: ClientOrderId, + venue_order_id: Option<&VenueOrderId>, + ) -> anyhow::Result> { + // A duplicate member shares its raw order ID with another physical order, so + // only its permanent ID identifies it + if let Some(order) = self.auxiliary_orders.get(&client_order_id) + && order.perm_id > 0 + { + return Ok(Some(IbOrderSelector::PermId(order.perm_id))); + } + + if let Some(order_id) = self.order_id_map.get(&client_order_id) { + return Ok(Some(IbOrderSelector::OrderId(*order_id))); + } + + venue_order_id + .map(IbOrderSelector::from_venue_order_id) + .transpose() + } +} + +impl OrderTracker { + pub(super) fn new(client_id: i32) -> Self { + Self { + state: Arc::new(Mutex::new(OrderTrackerState { + groups: AHashMap::new(), + group_history: FifoCacheMap::new(), + incarnation_parents: AHashMap::new(), + order_snapshots: FifoCacheMap::new(), + auxiliary_orders: AHashMap::new(), + client_id, + order_id_map: AHashMap::new(), + venue_order_id_map: AHashMap::new(), + active_orders: AHashMap::new(), + terminal_orders: FifoCacheMap::new(), + fills_held: AHashMap::new(), + })), + } + } + + #[expect( + clippy::unnecessary_wraps, + reason = "Retain the fallible tracker interface used throughout execution callbacks" + )] + pub(super) fn lock(&self) -> anyhow::Result> { + Ok(self.state.lock()) + } +} + +#[derive(Clone, Copy)] +pub(super) struct SpreadFillContext<'a> { + pub(super) client_order_id: ClientOrderId, + pub(super) spread_instrument_id: InstrumentId, + pub(super) commission: f64, + pub(super) commission_currency: &'a str, + pub(super) ts_init: UnixNanos, + pub(super) account_id: AccountId, +} + +#[derive(Debug)] +pub(super) enum OrderCorrelation { + Tracked { + order_id: i32, + context: TrackedOrder, + }, + Duplicate { + order_id: i32, + context: TrackedOrder, + }, + Untracked { + client_order_id: Option, + }, } #[derive(Clone, Debug)] -struct TrackedOrderContext { - client_order_id: ClientOrderId, - trader_id: TraderId, - strategy_id: StrategyId, - instrument_id: InstrumentId, - order_side: OrderSide, - order_type: OrderType, - accepted: bool, - avg_px: Option, +pub(super) struct TrackedOrder { + pub(super) client_order_id: ClientOrderId, + pub(super) trader_id: TraderId, + pub(super) strategy_id: StrategyId, + pub(super) instrument_id: InstrumentId, + pub(super) order_side: OrderSide, + pub(super) order_type: OrderType, + pub(super) accepted: bool, + pub(super) avg_px: Option, + pub(super) pending_cancel: bool, + pub(super) pending_modify: Option, + pub(super) perm_id: i64, + pub(super) spread_fill_ids: ahash::AHashSet, + pub(super) spread_fills_held: HeldSpreadFills, + /// Quantity, price, and trigger price of the last `OrderUpdated` sent from an openOrder. + pub(super) last_update: Option<(Quantity, Option, Option)>, +} + +/// Spread fills waiting for their leg fills, so a spread fill is the last event of its group. +/// +/// IB reports the combo execution before the leg executions, and leg fills carry the account +/// changes, so a spread fill only goes out once the legs cover its quantity. +#[derive(Clone, Debug, Default)] +pub(super) struct HeldSpreadFills { + pub(super) fills: std::collections::VecDeque<(tokio::time::Instant, OrderFilled)>, + /// Leg quantity sent but not yet matched to a spread fill. + pub(super) leg_quantities: AHashMap, +} + +/// Requested modify values in IB units, kept until an openOrder reflects them or the +/// modify is rejected, so unrelated openOrder refreshes cannot clear the pending flag. +#[derive(Clone, Copy, Debug, PartialEq)] +pub(super) struct PendingModifyValues { + pub(super) total_quantity: f64, + pub(super) limit_price: Option, + pub(super) aux_price: Option, + /// Requested trailing stop trigger; `None` leaves the venue-moved trigger unchecked. + pub(super) trail_stop_price: Option, +} + +impl PendingModifyValues { + pub(super) fn matches(&self, order: &ibapi::orders::Order) -> bool { + const EPS: f64 = 1e-9; + let approx = |a: f64, b: f64| (a - b).abs() <= EPS; + let approx_opt = |a: Option, b: Option| match (a, b) { + (Some(a), Some(b)) => approx(a, b), + (None, None) => true, + _ => false, + }; + approx(self.total_quantity, order.total_quantity) + && approx_opt(self.limit_price, order.limit_price) + && approx_opt(self.aux_price, order.aux_price) + && self.trail_stop_price.is_none_or(|requested| { + order + .trail_stop_price + .is_some_and(|current| approx(requested, current)) + }) + } } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum IbOrderSelector { +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub(super) enum IbOrderSelector { OrderId(i32), PermId(i64), } impl IbOrderSelector { - fn from_venue_order_id(venue_order_id: &VenueOrderId) -> anyhow::Result { + pub(super) fn from_venue_order_id(venue_order_id: &VenueOrderId) -> anyhow::Result { let raw = venue_order_id.as_str(); if let Some(perm_id) = raw.strip_prefix("PERM-") { return Ok(Self::PermId(perm_id.parse::().with_context(|| { @@ -206,21 +387,21 @@ impl IbOrderSelector { })?)) } - fn matches(self, order_id: i32, perm_id: i64) -> bool { + pub(super) fn matches(self, order_id: i32, perm_id: i64) -> bool { match self { Self::OrderId(target_order_id) => order_id == target_order_id, Self::PermId(target_perm_id) => perm_id == target_perm_id, } } - fn venue_order_id(self) -> VenueOrderId { + pub(super) fn venue_order_id(self) -> VenueOrderId { match self { Self::OrderId(order_id) => VenueOrderId::from(order_id.to_string()), Self::PermId(perm_id) => VenueOrderId::from(format!("PERM-{perm_id}")), } } - fn label(self) -> String { + pub(super) fn label(self) -> String { self.venue_order_id().to_string() } } @@ -240,63 +421,156 @@ impl Debug for InteractiveBrokersExecutionClient { impl InteractiveBrokersExecutionClient { /// Creates a new [`InteractiveBrokersExecutionClient`]. /// - /// # Arguments - /// - /// * `core` - Core execution client functionality - /// * `config` - Configuration for the client - /// * `instrument_provider` - Instrument provider - /// /// # Errors /// /// Returns an error if client creation fails. pub fn new( - mut core: ExecutionClientCore, + core: ExecutionClientCore, config: InteractiveBrokersExecutionClientConfig, instrument_provider: Arc, ) -> anyhow::Result { anyhow::ensure!( !config.client_id.unsigned_abs().is_multiple_of(1000), - "Interactive Brokers execution client_id must not be a multiple of 1000 because order ID partitioning uses client_id % 1000; got {}", + "Interactive Brokers execution client_id must not be a multiple of 1000 because order ID partitioning uses client_id % 1000, was {}", config.client_id ); - // If account_id is provided in config, use it - if let Some(account_id) = &config.account_id { - core.account_id = AccountId::from(account_id.clone()); - } - + let ib_account = ib_account_code(config.account_id.as_deref(), core.account_id); let pending_tasks = TaskGroup::new(); let session_tasks = TaskGroup::new(); + let orders = OrderTracker::new(config.client_id); Ok(Self { core, config, + ib_account, instrument_provider, - is_connected: AtomicBool::new(false), + is_connected: Arc::new(AtomicBool::new(false)), ib_client: None, pending_tasks, - next_order_id: Arc::new(Mutex::new(0)), - order_submit_lock: Arc::new(tokio::sync::Mutex::new(())), session_tasks, - order_id_map: Arc::new(Mutex::new(AHashMap::new())), - venue_order_id_map: Arc::new(Mutex::new(AHashMap::new())), + next_order_id: Arc::new(Mutex::new(0)), + order_submit_lock: Arc::new(AsyncMutex::new(())), + orders, commission_cache: Arc::new(Mutex::new(CommissionCache::new())), pending_execution_cache: Arc::new(Mutex::new(PendingExecutionCache::new())), - instrument_id_map: Arc::new(Mutex::new(AHashMap::new())), - trader_id_map: Arc::new(Mutex::new(AHashMap::new())), - strategy_id_map: Arc::new(Mutex::new(AHashMap::new())), - active_order_contexts: Arc::new(Mutex::new(AHashMap::new())), - terminal_order_contexts: Arc::new(Mutex::new(FifoCacheMap::new())), - spread_fill_tracking: Arc::new(Mutex::new(AHashMap::new())), position_tracker: create_position_tracker(), - order_avg_prices: Arc::new(Mutex::new(AHashMap::new())), - pending_combo_fills: Arc::new(Mutex::new(AHashMap::new())), - pending_combo_fill_avgs: Arc::new(Mutex::new(AHashMap::new())), - order_fill_progress: Arc::new(Mutex::new(AHashMap::new())), - pending_cancel_orders: Arc::new(Mutex::new(ahash::AHashSet::new())), }) } + pub(super) async fn order_details_with_fills( + client: &Client, + target: IbOrderSelector, + instrument_id: InstrumentId, + account_id: AccountId, + ib_account: Ustr, + provider: &InteractiveBrokersInstrumentProvider, + ts_init: UnixNanos, + orders: &OrderTracker, + ) -> anyhow::Result)>> { + let mut found = None; + let mut found_completed = false; + + for completed in [false, true] { + let subscription = if completed { + client.completed_orders(false).await? + } else { + client.all_open_orders().await? + }; + let mut subscription = subscription.filter_data(); + while let Some(item) = subscription.next().await { + let Orders::OrderData(data) = item? else { + continue; + }; + + if data.order.account != ib_account + || !target.matches(data.order_id, data.order.perm_id) + { + continue; + } + + if let Some(previous) = &found { + let previous: &ibapi::orders::OrderData = previous; + anyhow::ensure!( + previous.order.perm_id == data.order.perm_id, + "IB API order ID identifies multiple permanent IDs" + ); + } + found_completed = completed; + found = Some(data); + } + + if found.is_some() { + break; + } + } + let Some(data) = found else { + return Ok(None); + }; + let resolved = provider.resolve_instrument_id_for_contract(&data.contract)?; + anyhow::ensure!( + resolved == instrument_id, + "IB order query resolved an unexpected instrument {resolved}" + ); + orders + .lock()? + .observe_order_data(&data, account_id, found_completed)?; + let mut report = + parse_order_data_to_report(&data, instrument_id, account_id, provider, ts_init)?; + let mut subscription = client + .executions(Self::execution_filter(ib_account, None)) + .await? + .filter_data(); + let mut executions = AHashMap::new(); + let mut commissions = AHashMap::new(); + + while let Some(item) = subscription.next().await { + match item? { + Executions::ExecutionData(data) + if data.execution.account_number == ib_account + && target.matches(data.execution.order_id, data.execution.perm_id) => + { + executions.insert(data.execution.execution_id.clone(), data); + } + Executions::CommissionReport(commission) => { + commissions.insert( + commission.execution_id, + (commission.commission, commission.currency), + ); + } + _ => {} + } + } + let mut fills = Vec::with_capacity(executions.len()); + for (id, data) in executions { + let (commission, currency) = commissions + .remove(&id) + .with_context(|| format!("IB execution {id} has no commission report"))?; + fills.push(parse_execution_to_fill_report( + &data.execution, + &data.contract, + commission, + ¤cy, + instrument_id, + account_id, + provider, + ts_init, + None, + )?); + } + fills.sort_by_key(|fill| (fill.ts_event, fill.trade_id)); + let filled = fills.iter().try_fold(Decimal::ZERO, |sum, fill| { + sum.checked_add(fill.last_qty.as_decimal()) + .context("IB execution quantity overflow") + })?; + let filled = Quantity::from_decimal_dp(filled, report.quantity.precision)?; + report.filled_qty = report.filled_qty.max(filled); + if report.quantity.is_positive() && report.filled_qty >= report.quantity { + report.order_status = OrderStatus::Filled; + } + Ok(Some((report, fills))) + } + fn submit_order_list_with_orders( &self, cmd: SubmitOrderList, @@ -304,18 +578,13 @@ impl InteractiveBrokersExecutionClient { ) -> anyhow::Result<()> { let client = self.ib_client.as_ref().context("IB client not connected")?; - let order_id_map = Arc::clone(&self.order_id_map); - let venue_order_id_map = Arc::clone(&self.venue_order_id_map); - let instrument_id_map = Arc::clone(&self.instrument_id_map); - let trader_id_map = Arc::clone(&self.trader_id_map); - let strategy_id_map = Arc::clone(&self.strategy_id_map); - let active_order_contexts = Arc::clone(&self.active_order_contexts); - let terminal_order_contexts = Arc::clone(&self.terminal_order_contexts); + let order_tracker = self.orders.clone(); let next_order_id = Arc::clone(&self.next_order_id); let instrument_provider = Arc::clone(&self.instrument_provider); let exec_sender = get_exec_event_sender(); let clock = get_atomic_clock_realtime(); let account_id = self.core.account_id; + let ib_account = self.ib_account; let strategy_id = cmd.strategy_id; let client_clone = client.as_arc().clone(); let order_submit_lock = Arc::clone(&self.order_submit_lock); @@ -325,18 +594,13 @@ impl InteractiveBrokersExecutionClient { &cmd, &orders, &client_clone, - &order_id_map, - &venue_order_id_map, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &active_order_contexts, - &terminal_order_contexts, + &order_tracker, &next_order_id, &instrument_provider, &exec_sender, clock, account_id, + ib_account, strategy_id, &order_submit_lock, ) @@ -353,11 +617,9 @@ impl InteractiveBrokersExecutionClient { Ok(()) } - fn cached_order_for_modify(&self, client_order_id: &ClientOrderId) -> Option { - self.core.cache().order(client_order_id).map(|o| o.clone()) - } - - fn reserve_next_local_order_id(next_order_id: &Arc>) -> anyhow::Result { + pub(super) fn reserve_next_local_order_id( + next_order_id: &Arc>, + ) -> anyhow::Result { let mut guard = next_order_id.lock(); anyhow::ensure!( *guard > 0, @@ -374,12 +636,23 @@ impl InteractiveBrokersExecutionClient { return next_id; } - let order_id_floor = (client_slot as i32) * 1_000_000; - if next_id > order_id_floor { + let order_id_floor = (client_slot as i32) * ORDER_ID_PARTITION_SIZE; + let order_id_ceiling = order_id_floor + ORDER_ID_PARTITION_SIZE; + if next_id > order_id_floor && next_id < order_id_ceiling { next_id } else { - order_id_floor.saturating_add(next_id.max(1)) + order_id_floor + next_id.rem_euclid(ORDER_ID_PARTITION_SIZE).max(1) + } + } + + fn is_order_id_in_client_partition(order_id: i32, client_id: i32) -> bool { + let client_slot = client_id.unsigned_abs() % 1000; + if client_slot == 0 { + return true; } + + let order_id_floor = (client_slot as i32) * ORDER_ID_PARTITION_SIZE; + order_id > order_id_floor && order_id < order_id_floor + ORDER_ID_PARTITION_SIZE } /// Gets the next valid order ID from IB. @@ -404,10 +677,23 @@ impl InteractiveBrokersExecutionClient { .context("Timeout requesting open orders for next order ID initialization")??; let mut subscription = subscription.filter_data(); let mut highest_order_id = None; + let mut snapshots = Vec::new(); while let Some(order_result) = subscription.next().await { match order_result { Ok(Orders::OrderData(data)) => { + if data.order.account == self.ib_account { + snapshots.push(data.clone()); + } + + if data.order.client_id != self.config.client_id + || !Self::is_order_id_in_client_partition( + data.order_id, + self.config.client_id, + ) + { + continue; + } highest_order_id = Some( highest_order_id .map_or(data.order_id, |current: i32| current.max(data.order_id)), @@ -422,6 +708,58 @@ impl InteractiveBrokersExecutionClient { } } + { + let cache = self.core.cache(); + let mut state = self.orders.lock()?; + + for data in &snapshots { + let venue_id = parse::ib_venue_order_id(data.order_id, data.order.perm_id); + let Some(id) = cache.client_order_id(&venue_id) else { + continue; + }; + let Some(order) = cache.order(id) else { + continue; + }; + + if cache.client_id(id) != Some(&self.core.client_id) + || order.account_id() != Some(self.core.account_id) + || data.order.client_id != self.config.client_id + { + continue; + } + + if let Some(existing) = state.venue_order_id_map.get(&data.order_id) { + if *existing != *id { + continue; + } + } + state.order_id_map.insert(*id, data.order_id); + state.venue_order_id_map.insert(data.order_id, *id); + state + .active_orders + .entry(data.order_id) + .or_insert_with(|| TrackedOrder { + client_order_id: *id, + trader_id: order.trader_id(), + strategy_id: order.strategy_id(), + instrument_id: order.instrument_id(), + order_side: order.order_side(), + order_type: order.order_type(), + accepted: true, + avg_px: None, + pending_cancel: false, + pending_modify: None, + perm_id: data.order.perm_id, + spread_fill_ids: AHashSet::new(), + spread_fills_held: HeldSpreadFills::default(), + last_update: Some((order.quantity(), order.price(), order.trigger_price())), + }); + } + + for data in &snapshots { + state.observe_order_data(data, self.core.account_id, false)?; + } + } Ok(highest_order_id) } @@ -560,13 +898,7 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { let client = self.ib_client.as_ref().context("IB client not connected")?; - let order_id_map = Arc::clone(&self.order_id_map); - let venue_order_id_map = Arc::clone(&self.venue_order_id_map); - let instrument_id_map = Arc::clone(&self.instrument_id_map); - let trader_id_map = Arc::clone(&self.trader_id_map); - let strategy_id_map = Arc::clone(&self.strategy_id_map); - let active_order_contexts = Arc::clone(&self.active_order_contexts); - let terminal_order_contexts = Arc::clone(&self.terminal_order_contexts); + let orders = self.orders.clone(); let next_order_id = Arc::clone(&self.next_order_id); let instrument_provider = Arc::clone(&self.instrument_provider); let exec_sender = get_exec_event_sender(); @@ -576,23 +908,19 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { let client_clone = client.as_arc().clone(); let account_id = self.core.account_id; + let ib_account = self.ib_account; let future = async move { if let Err(e) = Self::handle_submit_order_async( &cmd, &client_clone, - &order_id_map, - &venue_order_id_map, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &active_order_contexts, - &terminal_order_contexts, + &orders, &next_order_id, &instrument_provider, &exec_sender, clock, account_id, + ib_account, &order_submit_lock, ) .await @@ -622,6 +950,7 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { TaskGroupGuard::new(&[&self.session_tasks, &self.pending_tasks], move || {}); tracing::info!("Connecting Interactive Brokers execution client..."); + log::debug!( "Execution client config host={} port={} client_id={} account_id={:?} request_timeout={} connection_timeout={} fetch_all_open_orders={} track_option_exercise_from_position_update={}", self.config.host, @@ -634,7 +963,7 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { self.config.track_option_exercise_from_position_update ); - let handle = crate::common::shared_client::get_or_connect( + let handle = get_or_connect( &self.config.host, self.config.port, self.config.client_id, @@ -672,9 +1001,8 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { let session_result = async { - log::debug!("Preloading cached spread instruments for execution client"); - self.preload_cached_spread_instruments(client.as_ref()) - .await?; + log::debug!("Preloading cached instruments for execution client"); + self.preload_cached_instruments(client.as_ref()).await; // Get initial next order ID (uses self.ib_client internally) log::debug!("Requesting next valid IB order ID"); @@ -683,20 +1011,19 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { let highest_open_order_id = self.get_highest_open_order_id(client.as_ref()).await?; let client_scoped_next_id = Self::apply_client_order_id_floor(next_id, self.config.client_id); - let starting_order_id = highest_open_order_id - .map(|order_id| next_id.max(order_id.saturating_add(1))) - .unwrap_or(next_id) - .max(client_scoped_next_id); + let starting_order_id = highest_open_order_id.map_or(client_scoped_next_id, |order_id| { + client_scoped_next_id.max(order_id.saturating_add(1)) + }); - if starting_order_id != next_id { + if starting_order_id == next_id { tracing::debug!( - "Adjusted next Interactive Brokers order ID from {} to {} based on client ID/open orders", - next_id, + "Initialized next Interactive Brokers order ID to {}", starting_order_id ); } else { tracing::debug!( - "Initialized next Interactive Brokers order ID to {}", + "Adjusted next Interactive Brokers order ID from {} to {} based on client ID/open orders", + next_id, starting_order_id ); } @@ -711,34 +1038,22 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { log::debug!("Starting IB order update stream"); self.start_order_updates().await?; - // Subscribe to account summary and generate initial account state - // Wait for initial account summary to load before proceeding - let client_for_account = Arc::clone(&client); + // Subscribe to account summary and keep it open so the account state follows the broker + // Wait for the initial account summary to load before proceeding let account_id = self.core.account_id; - let _exec_client_core = self.core.clone(); // Clone core to generate account state - log::debug!("Subscribing to IB account summary for {}", account_id); - match crate::execution::account::subscribe_account_summary(&client_for_account, account_id) - .await - { - Ok((balances, margins, info)) => { - tracing::debug!( - "Received account summary: {} balances, {} margins", - balances.len(), - margins.len() - ); - // Generate account state event like Python version - let ts_event = get_atomic_clock_realtime().get_time_ns(); + log::debug!("Subscribing to IB account summary for {account_id}"); - if let Err(e) = ExecutionClient::generate_account_state( - self, balances, margins, true, // reported - ts_event, info, - ) { - tracing::warn!("Failed to generate account state: {}", e); - } - } - Err(e) => { - tracing::warn!("Failed to subscribe to account summary: {}", e); - } + let send_account_state = self.account_state_sender(); + + if let Err(e) = stream_account_summary( + &client, + self.ib_account, + &self.session_tasks, + send_account_state, + ) + .await + { + tracing::warn!("Failed to subscribe to account summary: {}", e); } // Initialize position tracking with existing positions @@ -747,16 +1062,23 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { let position_tracker_init = Arc::clone(&self.position_tracker); log::debug!("Initializing IB execution position tracking"); - if let Err(e) = crate::execution::account::initialize_position_tracking( + + match initialize_position_tracking( &client_for_positions_init, - self.core.account_id, + self.ib_account, position_tracker_init, ) .await { - tracing::warn!("Failed to initialize position tracking: {}", e); + Ok(contracts) => { + self.publish_position_instruments(client.as_ref(), contracts) + .await; + } + Err(e) => tracing::warn!("Failed to initialize position tracking: {}", e), } + self.publish_combo_order_instruments(client.as_ref()).await; + // Subscribe to PnL updates let client_for_pnl = Arc::clone(&client); // Clone Arc @@ -764,7 +1086,7 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { if let Err(e) = crate::execution::account::subscribe_pnl( &client_for_pnl, - self.core.account_id, + self.ib_account, &self.session_tasks, ) .await @@ -780,9 +1102,10 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { log::debug!("Subscribing to IB position updates for option exercise tracking"); - if let Err(e) = crate::execution::account::subscribe_positions( + if let Err(e) = subscribe_positions( &client_for_positions, self.core.account_id, + self.ib_account, position_tracker_clone, instrument_provider_clone, &self.session_tasks, @@ -834,457 +1157,94 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { Ok(()) } + fn generate_order_status_report_task( + &self, + cmd: &GenerateOrderStatusReport, + ) -> Option>> { + let report_client = self.report_client().ok()?; + let command = cmd.clone(); + + Some(ExecutionReportTask::new( + async move { report_client.generate_order_status_report(&command).await }, + Ok, + )) + } + async fn generate_order_status_report( &self, cmd: &GenerateOrderStatusReport, ) -> anyhow::Result> { - let plural_cmd = GenerateOrderStatusReports { - command_id: cmd.command_id, - ts_init: cmd.ts_init, - open_only: false, - instrument_id: cmd.instrument_id, - start: None, - end: None, - params: cmd.params.clone(), - log_receipt_level: LogLevel::Info, - correlation_id: cmd.correlation_id, - causation_id: cmd.causation_id, - }; - - let reports = self.generate_order_status_reports(&plural_cmd).await?; - - // Filter by client_order_id and venue_order_id - let report = reports.into_iter().find(|r| { - let matches_client = if let Some(filter_client_id) = cmd.client_order_id { - r.client_order_id == Some(filter_client_id) - } else { - true - }; - let matches_venue = if let Some(filter_venue_id) = cmd.venue_order_id { - r.venue_order_id == filter_venue_id - } else { - true - }; - matches_client && matches_venue - }); + self.report_client()? + .generate_order_status_report(cmd) + .await + } - Ok(report) + fn generate_order_status_reports_task( + &self, + cmd: &GenerateOrderStatusReports, + ) -> Option>> { + let report_client = self.report_client().ok()?; + let command = cmd.clone(); + + Some(ExecutionReportTask::new( + async move { report_client.generate_order_status_reports(&command).await }, + Ok, + )) } async fn generate_order_status_reports( &self, cmd: &GenerateOrderStatusReports, ) -> anyhow::Result> { - let client = self.ib_client.as_ref().context("IB client not connected")?; - - let timeout_dur = Duration::from_secs(self.config.request_timeout); - let subscription = tokio::time::timeout(timeout_dur, client.all_open_orders()) + self.report_client()? + .generate_order_status_reports(cmd) .await - .context("Timeout requesting open orders")??; - let mut subscription = subscription.filter_data(); - let mut reports = Vec::new(); - let mut open_order_fills: AHashMap = AHashMap::new(); - let ts_init = get_atomic_clock_realtime().get_time_ns(); - let raw_account_id = raw_ib_account_code(&self.core.account_id); - - while let Some(order_result) = subscription.next().await { - match order_result { - Ok(Orders::OrderData(data)) => { - if !data.order.account.is_empty() && data.order.account != raw_account_id { - continue; - } - - // Convert IB contract to instrument ID - let instrument_id = - match self.resolve_report_contract_instrument_id(&data.contract) { - Ok(instrument_id) => instrument_id, - Err(e) => { - tracing::warn!( - order_id = data.order_id, - sec_type = ?data.contract.security_type, - symbol = data.contract.symbol.as_str(), - con_id = data.contract.contract_id, - error = %e, - "Failed to resolve IBKR order status report instrument ID", - ); - continue; - } - }; - - // Filter by instrument_id if specified - if let Some(filter_id) = cmd.instrument_id { - if instrument_id != filter_id { - continue; - } - } - - // Parse to order status report using minimal OrderStatus - // Note: OrderState doesn't have filled/average_fill_price, so we use defaults - match parse_order_status_to_report( - &IBOrderStatus { - order_id: data.order_id, - status: data.order_state.status, - filled: data.order.filled_quantity, - remaining: (data.order.total_quantity - data.order.filled_quantity) - .max(0.0), - average_fill_price: None, // Not available in OrderState - perm_id: data.order.perm_id, - parent_id: 0, // Not available in OrderState - last_fill_price: None, // Not available in OrderState - client_id: data.order.client_id, - why_held: String::new(), // Not available in OrderState - market_cap_price: None, // Not available in OrderState - }, - Some(&data.order), - instrument_id, - self.core.account_id, - &self.instrument_provider, - ts_init, - ) { - Ok(report) => { - if !cmd.open_only && report.filled_qty.as_decimal() > Decimal::ZERO { - let signed_filled = if report.order_side == Some(OrderSide::Buy) { - report.filled_qty.as_decimal() - } else { - -report.filled_qty.as_decimal() - }; - open_order_fills - .entry(report.instrument_id) - .and_modify(|qty| *qty += signed_filled) - .or_insert(signed_filled); - } - reports.push(report); - } - Err(e) => { - tracing::warn!("Failed to parse order status report: {e}"); - } - } - } - Ok(_) => { - // Ignore other order types - } - Err(e) => { - tracing::warn!("Error receiving order data: {e}"); - } - } - } - - if !cmd.open_only { - let positions = tokio::time::timeout(timeout_dur, client.positions()) - .await - .context("Timeout requesting positions for synthetic order reports")??; - let mut positions = positions.filter_data(); - - while let Some(position_result) = positions.next().await { - match position_result { - Ok(PositionUpdate::Position(position)) => { - if position.account != raw_account_id { - continue; - } - - let instrument = match self - .instrument_provider - .get_instrument(client.as_arc().as_ref(), &position.contract) - .await - { - Ok(Some(instrument)) => instrument, - Ok(None) => { - tracing::warn!( - con_id = position.contract.contract_id, - sec_type = ?position.contract.security_type, - "Cannot generate synthetic order report: instrument not found", - ); - continue; - } - Err(e) => { - tracing::warn!( - con_id = position.contract.contract_id, - sec_type = ?position.contract.security_type, - error = %e, - "Failed to resolve instrument for synthetic order report", - ); - continue; - } - }; - - let instrument_id = instrument.id(); - if let Some(filter_id) = cmd.instrument_id - && instrument_id != filter_id - { - continue; - } - - let position_qty = - Decimal::from_f64_retain(position.position).unwrap_or_default(); - let open_fills = open_order_fills - .get(&instrument_id) - .copied() - .unwrap_or_default(); - let adjusted_qty = position_qty - open_fills; - if adjusted_qty.is_zero() { - continue; - } - - let quantity = Quantity::new( - adjusted_qty.abs().to_f64().unwrap_or_default(), - instrument.size_precision(), - ); - let order_side = if adjusted_qty > Decimal::ZERO { - OrderSide::Buy - } else { - OrderSide::Sell - }; - let id = instrument_id.to_string(); - let mut report = OrderStatusReport::new( - self.core.account_id, - instrument_id, - Some(ClientOrderId::new(id.clone())), - VenueOrderId::new(id), - order_side.into(), - OrderType::Market, - TimeInForce::Fok, - OrderStatus::Filled, - quantity, - quantity, - ts_init, - ts_init, - ts_init, - Some(UUID4::new()), - ); - report.avg_px = self.position_avg_px_open( - &instrument_id, - &instrument, - position.average_cost, - ); - reports.push(report); - } - Ok(PositionUpdate::PositionEnd) => break, - Err(e) => tracing::warn!( - "Error receiving position data for synthetic order report: {e}" - ), - } - } - } + } - Ok(reports) + fn generate_fill_reports_task( + &self, + cmd: &GenerateFillReports, + ) -> Option>> { + let report_client = self.report_client().ok()?; + let command = cmd.clone(); + + Some(ExecutionReportTask::new( + async move { report_client.generate_fill_reports(command).await }, + Ok, + )) } async fn generate_fill_reports( &self, cmd: GenerateFillReports, ) -> anyhow::Result> { - let client = self.ib_client.as_ref().context("IB client not connected")?; - - // Get account code from account ID - let account_code = self.core.account_id.to_string(); + self.report_client()?.generate_fill_reports(cmd).await + } - // Build time filter from start if provided. - let time_filter = if let Some(start) = cmd.start { - let start_dt = start.to_datetime_utc(); - start_dt.strftime("%Y%m%d-%H:%M:%S").to_string() - } else { - String::new() - }; - - let filter = ExecutionFilter { - client_id: None, - account_code, - time: time_filter, - symbol: String::new(), - security_type: String::new(), - exchange: String::new(), - side: None, - last_n_days: 0, - specific_dates: Vec::new(), - }; - - let timeout_dur = Duration::from_secs(self.config.request_timeout); - let subscription = tokio::time::timeout(timeout_dur, client.executions(filter)) - .await - .context("Timeout requesting executions")??; - let mut subscription = subscription.filter_data(); - let mut reports = Vec::new(); - let ts_init = get_atomic_clock_realtime().get_time_ns(); - let mut pending_exec_data: AHashMap = AHashMap::new(); - let mut pending_commissions: AHashMap = AHashMap::new(); - - while let Some(exec_result) = subscription.next().await { - match exec_result { - Ok(Executions::ExecutionData(exec_data)) => { - let execution_id = exec_data.execution.execution_id.clone(); - if let Some((commission, commission_currency)) = - pending_commissions.remove(&execution_id) - { - if let Some(report) = self.parse_historical_fill_report( - &cmd, - &exec_data, - commission, - &commission_currency, - ts_init, - ) { - reports.push(report); - } - } else { - pending_exec_data.insert(execution_id, exec_data); - } - } - Ok(Executions::CommissionReport(commission)) => { - if let Some(exec_data) = pending_exec_data.remove(&commission.execution_id) { - if let Some(report) = self.parse_historical_fill_report( - &cmd, - &exec_data, - commission.commission, - &commission.currency, - ts_init, - ) { - reports.push(report); - } - } else { - pending_commissions.insert( - commission.execution_id, - (commission.commission, commission.currency), - ); - } - } - Err(e) => { - tracing::warn!("Error receiving execution data: {e}"); - } - } - } - - if !pending_exec_data.is_empty() { - tracing::warn!( - "Skipped {} historical fill reports because IB did not provide matching commission reports", - pending_exec_data.len() - ); - } - - Ok(reports) - } + fn generate_position_status_reports_task( + &self, + cmd: &GeneratePositionStatusReports, + ) -> Option>> { + let report_client = self.report_client().ok()?; + let command = cmd.clone(); + + Some(ExecutionReportTask::new( + async move { + report_client + .generate_position_status_reports(&command) + .await + }, + Ok, + )) + } async fn generate_position_status_reports( &self, cmd: &GeneratePositionStatusReports, ) -> anyhow::Result> { - let client = self.ib_client.as_ref().context("IB client not connected")?; - - let timeout_dur = Duration::from_secs(self.config.request_timeout); - let subscription = tokio::time::timeout(timeout_dur, client.positions()) + self.report_client()? + .generate_position_status_reports(cmd) .await - .context("Timeout requesting positions")??; - let mut subscription = subscription.filter_data(); - let mut reports = Vec::new(); - let ts_init = get_atomic_clock_realtime().get_time_ns(); - let raw_account_id = raw_ib_account_code(&self.core.account_id); - - // Process positions until PositionEnd; return empty list when none (reconciliation parity: - // never return None/missing for "no positions"). - while let Some(position_result) = subscription.next().await { - match position_result { - Ok(PositionUpdate::Position(position)) => { - // Filter for the specific account - if position.account != raw_account_id { - continue; - } - - let instrument = match self - .instrument_provider - .get_instrument(client.as_arc().as_ref(), &position.contract) - .await - { - Ok(Some(instrument)) => instrument, - Ok(None) => { - tracing::warn!( - con_id = position.contract.contract_id, - sec_type = ?position.contract.security_type, - "Cannot generate position status report: instrument not found", - ); - continue; - } - Err(e) => { - tracing::warn!( - con_id = position.contract.contract_id, - sec_type = ?position.contract.security_type, - error = %e, - "Failed to resolve position instrument", - ); - continue; - } - }; - let instrument_id = instrument.id(); - - // Filter by instrument_id if specified - if let Some(filter_id) = cmd.instrument_id - && instrument_id != filter_id - { - continue; - } - - // Determine position side - let position_side = if position.position == 0.0 { - PositionSide::Flat - } else if position.position > 0.0 { - PositionSide::Long - } else { - PositionSide::Short - }; - - let quantity = - Quantity::new(position.position.abs(), instrument.size_precision()); - - // Convert IB avg_cost to Nautilus Price, accounting for price magnifier and multiplier - // Python: converted_avg_cost = avg_cost / (multiplier * price_magnifier) - let avg_px_open = self.position_avg_px_open( - &instrument_id, - &instrument, - position.average_cost, - ); - - let report = PositionStatusReport::new( - self.core.account_id, - instrument_id, - position_side, - quantity, - ts_init, // ts_last - ts_init, // ts_init - None, // report_id: auto-generated - None, // venue_position_id - avg_px_open, - ); - - reports.push(report); - } - Ok(PositionUpdate::PositionEnd) => { - // End of position list - break; - } - Err(e) => { - tracing::warn!("Error receiving position data: {e}"); - } - } - } - - if reports.is_empty() - && let Some(instrument_id) = cmd.instrument_id - { - let precision = self - .instrument_provider - .find(&instrument_id) - .map_or(0, |instrument| instrument.size_precision()); - reports.push(PositionStatusReport::new( - self.core.account_id, - instrument_id, - PositionSide::Flat, - Quantity::zero(precision), - ts_init, - ts_init, - None, - None, - None, - )); - } - - Ok(reports) } async fn generate_mass_status( @@ -1316,7 +1276,7 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { .build() .map_err(|e| anyhow::anyhow!("{e}"))?; - let (order_reports, fill_reports, position_reports) = tokio::try_join!( + let (mut order_reports, fill_reports, mut position_reports) = tokio::try_join!( self.generate_order_status_reports(&order_cmd), self.generate_fill_reports(fill_cmd), self.generate_position_status_reports(&position_cmd), @@ -1336,7 +1296,13 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { ts_now, Some(UUID4::new()), ); + // IB `reqExecutions` and completed orders are session-scoped, so without a + // configured lookback the actual venue bound is unknown and the report set + // cannot be claimed complete. + mass_status.set_report_window(start, start.is_some()); + parse::fill_missing_avg_px(&mut order_reports, &fill_reports); + parse::fill_position_avg_px_from_fills(&mut position_reports, &fill_reports); mass_status.add_order_reports(order_reports); mass_status.add_fill_reports(fill_reports); mass_status.add_position_reports(position_reports); @@ -1348,41 +1314,20 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { let client = self.ib_client.as_ref().context("IB client not connected")?; let client_clone = client.as_arc().clone(); - let account_id = self.core.account_id; - let account_type = self.core.account_type; - let base_currency = self.core.base_currency; - let clock = get_atomic_clock_realtime(); + let ib_account = self.ib_account; let request_timeout_secs = self.config.request_timeout; + let send_account_state = self.account_state_sender(); let future = async move { let timeout_dur = Duration::from_secs(request_timeout_secs); let result = tokio::time::timeout( timeout_dur, - crate::execution::account::subscribe_account_summary(&client_clone, account_id), + subscribe_account_summary(&client_clone, ib_account), ) .await; match result { - Ok(Ok((balances, margins, info))) => { - let ts_event = clock.get_time_ns(); - let ts_now = clock.get_time_ns(); - - let account_state = AccountState::new( - account_id, - account_type, - balances, - margins, - true, - UUID4::new(), - ts_event, - ts_now, - base_currency, - ) - .with_info(info); - - let endpoint = MessagingSwitchboard::portfolio_update_account(); - send_account_state(endpoint, &account_state); - } + Ok(Ok(snapshot)) => send_account_state(snapshot), Ok(Err(e)) => { tracing::error!("Failed to query account state: {e}"); } @@ -1409,22 +1354,29 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { let target_order = if let Some(venue_order_id) = &cmd.venue_order_id { IbOrderSelector::from_venue_order_id(venue_order_id)? } else { - let map = self.order_id_map.lock(); + let state = self.orders.lock()?; IbOrderSelector::OrderId( - *map.get(&cmd.client_order_id) + *state + .order_id_map + .get(&cmd.client_order_id) .context("No venue order id for client_order_id")?, ) }; + let order_quantity = self + .core + .cache() + .order(&cmd.client_order_id) + .map(|order| order.quantity().as_decimal()); + let client_clone = client.as_arc().clone(); - let instrument_id_map = Arc::clone(&self.instrument_id_map); + let orders = self.orders.clone(); let instrument_provider = Arc::clone(&self.instrument_provider); let account_id = self.core.account_id; let exec_sender = get_exec_event_sender(); let ts_init = get_atomic_clock_realtime().get_time_ns(); let request_timeout_secs = self.config.request_timeout; - let pending_cancel_orders = Arc::clone(&self.pending_cancel_orders); - let raw_account_id = raw_ib_account_code(&self.core.account_id); + let ib_account = self.ib_account; let future = async move { let timeout_dur = Duration::from_secs(request_timeout_secs); @@ -1444,7 +1396,7 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { while let Some(order_result) = subscription.next().await { if let Ok(Orders::OrderData(data)) = order_result { - if !data.order.account.is_empty() && data.order.account != raw_account_id { + if !data.order.account.is_empty() && data.order.account != ib_account { continue; } @@ -1452,7 +1404,10 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { continue; } - let instrument_id = instrument_id_map.lock().get(&data.order_id).copied(); + let instrument_id = match orders.lock() { + Ok(state) => state.order(data.order_id).map(|order| order.instrument_id), + Err(_) => None, + }; let instrument_id = match instrument_id { Some(id) => id, None => match instrument_provider @@ -1466,22 +1421,8 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { }, }; - let report = match parse_order_status_to_report( - &IBOrderStatus { - order_id: data.order_id, - status: data.order_state.status, - filled: data.order.filled_quantity, - remaining: (data.order.total_quantity - data.order.filled_quantity) - .max(0.0), - average_fill_price: None, - perm_id: data.order.perm_id, - parent_id: 0, - last_fill_price: None, - client_id: data.order.client_id, - why_held: String::new(), - market_cap_price: None, - }, - Some(&data.order), + let report = match parse_order_data_to_report( + &data, instrument_id, account_id, &instrument_provider, @@ -1506,35 +1447,202 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { } } - let was_pending_cancel = pending_cancel_orders.lock().remove(&client_order_id); + let is_pending_cancel = orders.lock().is_ok_and(|state| { + state + .order_by_client(client_order_id) + .is_some_and(|order| order.pending_cancel) + }); + + if is_pending_cancel { + let filter = Self::execution_filter(ib_account, None); + let executions = match tokio::time::timeout( + timeout_dur, + client_clone.executions(filter), + ) + .await + { + Ok(Ok(subscription)) => subscription, + Ok(Err(e)) => { + tracing::error!( + "query_order: failed to request executions before inferring cancel: {e}" + ); + return; + } + Err(_) => { + tracing::error!( + "query_order: timeout requesting executions before inferring cancel" + ); + return; + } + }; + let mut executions = executions.filter_data(); + let mut execution_data = AHashMap::new(); + let mut commissions = AHashMap::new(); + + while let Some(result) = executions.next().await { + match result { + Ok(Executions::ExecutionData(data)) + if target_order + .matches(data.execution.order_id, data.execution.perm_id) => + { + execution_data.insert(data.execution.execution_id.clone(), data); + } + Ok(Executions::CommissionReport(commission)) => { + commissions.insert( + commission.execution_id, + (commission.commission, commission.currency), + ); + } + Ok(_) => {} + Err(e) => { + tracing::error!( + "query_order: execution query failed before cancel resolution: {e}" + ); + return; + } + } + } + + if !execution_data.is_empty() { + let cumulative_filled = execution_data + .values() + .map(|data| data.execution.cumulative_quantity) + .fold(0.0_f64, f64::max); + + for (execution_id, data) in execution_data { + let Some((commission, currency)) = commissions.remove(&execution_id) else { + tracing::error!( + "query_order: execution {execution_id} has no commission report; cancel remains unresolved" + ); + return; + }; + let report = match parse_execution_to_fill_report( + &data.execution, + &data.contract, + commission, + ¤cy, + instrument_id, + account_id, + &instrument_provider, + ts_init, + None, + ) { + Ok(report) => report, + Err(e) => { + tracing::error!( + "query_order: failed to parse execution {execution_id}: {e}" + ); + return; + } + }; + let context = + Self::get_tracked_order_context(data.execution.order_id, &orders) + .ok() + .flatten(); + let event = if let Some(context) = context { + let quote_currency = instrument_provider + .find(&context.instrument_id) + .map_or(report.commission.currency, |instrument| { + instrument.quote_currency() + }); + ExecutionEvent::Order(OrderEventAny::Filled(OrderFilled::new( + context.trader_id, + context.strategy_id, + context.instrument_id, + context.client_order_id, + report.venue_order_id, + report.account_id, + report.trade_id, + context.order_side, + context.order_type, + report.last_qty, + report.last_px, + quote_currency, + report.liquidity_side, + UUID4::new(), + report.ts_event, + report.ts_init, + false, + report.venue_position_id, + Some(report.commission), + None, + ))) + } else { + ExecutionEvent::Report(ExecutionReport::Fill(Box::new(report))) + }; + + if exec_sender.send(event).is_err() { + tracing::error!( + "query_order: failed to send fill resolved from execution history" + ); + return; + } + } + + if let Ok(mut state) = orders.lock() + && let Some(order) = state.order_by_client_mut(client_order_id) + { + order.pending_cancel = false; + } + + // The order is absent from open orders, so any unfilled remainder + // was cancelled at the venue. Quantize to the instrument's size + // precision so float dust from the f64 cumulative quantity cannot + // register as a phantom remainder after a full fill. + let size_precision = instrument_provider + .find(&instrument_id) + .map(|instrument| u32::from(instrument.size_precision())); + let remainder = order_quantity.and_then(|total| { + Decimal::from_f64_retain(cumulative_filled).map(|filled| { + let remainder = total - filled; + size_precision.map_or(remainder, |dp| remainder.round_dp(dp)) + }) + }); + + match remainder { + Some(remainder) if remainder > Decimal::ZERO => { + Self::send_inferred_order_canceled( + trader_id, + strategy_id, + instrument_id, + client_order_id, + target_order.venue_order_id(), + account_id, + ts_init, + &exec_sender, + "cancelled remainder after partial-fill recovery", + ); + } + Some(_) => {} + None => { + tracing::debug!( + "query_order: cannot determine cancelled remainder for {} (order not in cache)", + client_order_id + ); + } + } + return; + } + } + + let was_pending_cancel = orders.lock().is_ok_and(|mut state| { + state + .order_by_client_mut(client_order_id) + .is_some_and(|order| std::mem::replace(&mut order.pending_cancel, false)) + }); if was_pending_cancel { - let event = OrderCanceled::new( + Self::send_inferred_order_canceled( trader_id, strategy_id, instrument_id, client_order_id, - UUID4::new(), - ts_init, + target_order.venue_order_id(), + account_id, ts_init, - false, - Some(target_order.venue_order_id()), - Some(account_id), - None, + &exec_sender, + "missing open order", ); - - if exec_sender - .send(ExecutionEvent::Order(OrderEventAny::Canceled(event))) - .is_err() - { - tracing::error!("query_order: failed to send inferred order canceled event"); - } else { - tracing::debug!( - "query_order: inferred cancel for {} from missing open order {}", - client_order_id, - target_order.label() - ); - } return; } @@ -1554,12 +1662,13 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { fn submit_order_list(&self, cmd: SubmitOrderList) -> anyhow::Result<()> { let orders = self.core.get_orders_for_list(&cmd.order_list)?; if let Some(reason) = orders.iter().find_map(|order| validate_order(order).err()) { - self.deny_submit_order_list_not_ready(&cmd, &reason.to_string())?; + self.deny_submit_order_list(&cmd, &reason.to_string())?; return Ok(()); } if let Err(reason) = self.ensure_client_ready_for_order_request("submit order list") { - self.deny_submit_order_list_not_ready(&cmd, &reason)?; + let reason = coded_denial_reason(DENIAL_CLIENT_NOT_READY, &reason); + self.deny_submit_order_list(&cmd, &reason)?; return Ok(()); } @@ -1567,6 +1676,22 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { } fn modify_order(&self, cmd: ModifyOrder) -> anyhow::Result<()> { + if self + .orders + .lock()? + .group_for_client(cmd.client_order_id) + .is_some() + { + Self::send_order_modify_rejected( + &cmd, + "Cannot modify an order with unresolved duplicate broker identities; cancel and reconcile it first", + &get_exec_event_sender(), + get_atomic_clock_realtime().get_time_ns(), + self.core.account_id, + )?; + return Ok(()); + } + if let Err(reason) = self.ensure_client_ready_for_order_request("modify order") { Self::send_order_modify_rejected( &cmd, @@ -1580,38 +1705,21 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { let client = self.ib_client.as_ref().context("IB client not connected")?; - let order_id_map = Arc::clone(&self.order_id_map); - let venue_order_id_map = Arc::clone(&self.venue_order_id_map); - let instrument_id_map = Arc::clone(&self.instrument_id_map); + let orders = self.orders.clone(); let instrument_provider = Arc::clone(&self.instrument_provider); let exec_sender = get_exec_event_sender(); let clock = get_atomic_clock_realtime(); let account_id = self.core.account_id; + let ib_account = self.ib_account; let client_clone = client.as_arc().clone(); let request_timeout_secs = self.config.request_timeout; - let original_order = self - .cached_order_for_modify(&cmd.client_order_id) - .map(Arc::new); - - if original_order.is_none() { - tracing::debug!( - "Order {} not found in cache for modify; querying IB open orders", - cmd.client_order_id - ); - } - let future = async move { if let Err(e) = Self::handle_modify_order_async( &cmd, &client_clone, - &order_id_map, - &venue_order_id_map, - &instrument_id_map, + &orders, &instrument_provider, - &exec_sender, - clock, - account_id, - original_order.as_ref(), + ib_account, request_timeout_secs, ) .await @@ -1668,12 +1776,12 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { let client = self.ib_client.as_ref().context("IB client not connected")?; - let order_id_map = Arc::clone(&self.order_id_map); - let venue_order_id_map = Arc::clone(&self.venue_order_id_map); - let instrument_id_map = Arc::clone(&self.instrument_id_map); - let trader_id_map = Arc::clone(&self.trader_id_map); - let strategy_id_map = Arc::clone(&self.strategy_id_map); - let pending_cancel_orders = Arc::clone(&self.pending_cancel_orders); + let orders = self.orders.clone(); + let instrument_provider = Arc::clone(&self.instrument_provider); + let exec_sender = get_exec_event_sender(); + let clock = get_atomic_clock_realtime(); + let account_id = self.core.account_id; + let ib_account = self.ib_account; let client_clone = client.as_arc().clone(); let request_timeout_secs = self.config.request_timeout; @@ -1682,15 +1790,12 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { &cmd, &target_order, &client_clone, - &order_id_map, - &venue_order_id_map, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &pending_cancel_orders, + &orders, + &instrument_provider, &exec_sender, clock.get_time_ns(), account_id, + ib_account, request_timeout_secs, ) .await @@ -1717,69 +1822,14 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { } fn cancel_all_orders(&self, cmd: CancelAllOrders) -> anyhow::Result<()> { - // Warn if order_side is specified (IB doesn't support side filtering) - if cmd.order_side.is_some() { - tracing::warn!( - "Interactive Brokers does not support order_side filtering for cancel all orders; \ - ignoring order_side={:?} and canceling all orders", - cmd.order_side - ); - } - - // Not-ready warning already logged; a whole-request failure must not - // fan out per-order rejections. if self .ensure_client_ready_for_order_request("cancel orders") .is_err() { return Ok(()); } - let client = self.ib_client.as_ref().context("IB client not connected")?; - - // Get open orders from cache before spawning async task (Rc doesn't work across async boundaries) - // Note: In Rust, instrument_id is always required, so we always filter by it - let orders_to_cancel: Vec<(ClientOrderId, Option)> = { - let cache = self.core.cache(); - let mut orders_to_cancel: Vec<(ClientOrderId, Option)> = cache - .orders_open( - None, // venue - Some(&cmd.instrument_id), // instrument_id (always filter by it in Rust) - None, // strategy_id - None, // account_id - None, // side (IB doesn't support side filtering) - ) - .iter() - .map(|order| (order.client_order_id(), order.venue_order_id())) - .collect(); - - if orders_to_cancel.is_empty() { - let ib_order_ids: Vec = { - let instrument_id_map = self.instrument_id_map.lock(); - instrument_id_map - .iter() - .filter_map(|(order_id, instrument_id)| { - (*instrument_id == cmd.instrument_id).then_some(*order_id) - }) - .collect() - }; - - let venue_map = self.venue_order_id_map.lock(); - - orders_to_cancel.extend(ib_order_ids.into_iter().filter_map(|ib_order_id| { - venue_map.get(&ib_order_id).copied().map(|client_order_id| { - ( - client_order_id, - Some(VenueOrderId::from(ib_order_id.to_string())), - ) - }) - })); - } - - orders_to_cancel.sort_by_key(|(client_order_id, _)| client_order_id.to_string()); - orders_to_cancel.dedup_by_key(|(client_order_id, _)| *client_order_id); - orders_to_cancel - }; + let orders_to_cancel = self.cancel_all_targets(&cmd)?; if orders_to_cancel.is_empty() { tracing::debug!("No open orders to cancel"); @@ -1793,29 +1843,26 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { ); let client_clone = client.as_arc().clone(); - let order_id_map = Arc::clone(&self.order_id_map); - let instrument_id_map = Arc::clone(&self.instrument_id_map); - let trader_id_map = Arc::clone(&self.trader_id_map); - let strategy_id_map = Arc::clone(&self.strategy_id_map); - let pending_cancel_orders = Arc::clone(&self.pending_cancel_orders); + let orders = self.orders.clone(); let exec_sender = get_exec_event_sender(); let clock = get_atomic_clock_realtime(); let account_id = self.core.account_id; + let ib_account = self.ib_account; let request_timeout_secs = self.config.request_timeout; + let provider = Arc::clone(&self.instrument_provider); let future = async move { if let Err(e) = Self::handle_cancel_all_orders_async( &client_clone, - &order_id_map, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &pending_cancel_orders, + &orders, &exec_sender, clock.get_time_ns(), account_id, + ib_account, request_timeout_secs, orders_to_cancel, + &provider, + cmd.order_side, ) .await { @@ -1837,6 +1884,67 @@ impl ExecutionClient for InteractiveBrokersExecutionClient { } Ok(()) } + + // Reconciliation creates orders a previous session left working at IB; without tracking, + // their IB status updates are dropped as untracked. + fn register_external_order( + &self, + client_order_id: ClientOrderId, + venue_order_id: VenueOrderId, + _instrument_id: InstrumentId, + _strategy_id: StrategyId, + _ts_init: UnixNanos, + ) { + let order = match self.core.get_order(&client_order_id) { + Ok(order) if order.is_open() => order, + Ok(_) => return, + Err(e) => { + tracing::warn!("Cannot track external IB order {client_order_id}: {e}"); + return; + } + }; + let order_selector = match IbOrderSelector::from_venue_order_id(&venue_order_id) { + Ok(order_selector) => order_selector, + Err(e) => { + tracing::warn!("Cannot track external IB order {client_order_id}: {e}"); + return; + } + }; + let Some(client) = self.ib_client.as_ref() else { + tracing::warn!( + "Cannot track external IB order {client_order_id}: IB client not connected" + ); + return; + }; + + let client = client.as_arc().clone(); + let orders = self.orders.clone(); + let ib_account = self.ib_account; + let request_timeout_secs = self.config.request_timeout; + + let future = async move { + let result = Self::resolve_ib_order_id( + &client, + order_selector, + ib_account, + request_timeout_secs, + ) + .await + .and_then(|ib_order_id| { + Self::cache_recovered_order_tracking(ib_order_id, &order, &orders) + }); + + // A filled order awaiting its deferred fills is still open in the cache but no + // longer listed by IB; cancels resolve `PERM-` IDs on demand either way + if let Err(e) = result { + tracing::debug!("Cannot track external IB order {client_order_id}: {e:#}"); + } + }; + + if let Err(e) = self.pending_tasks.spawn(future) { + tracing::warn!("Cannot track external IB order {client_order_id}: {e}"); + } + } } fn validate_order(order: &impl Order) -> Result<(), OrderDeniedReason> { @@ -1848,6 +1956,63 @@ fn validate_order(order: &impl Order) -> Result<(), OrderDeniedReason> { } impl InteractiveBrokersExecutionClient { + // Captures the thread-local exec event sender here so tokio worker threads, which have no + // sender or message bus of their own, can still deliver account states. + fn account_state_sender(&self) -> impl Fn(AccountSnapshot) + Send + 'static { + let factory = OrderEventFactory::new( + self.core.trader_id, + self.core.account_id, + self.core.account_type, + self.core.base_currency, + ); + let exec_sender = get_exec_event_sender(); + let clock = get_atomic_clock_realtime(); + + move |(balances, margins, info)| { + let ts_now = clock.get_time_ns(); + let state = + factory.generate_account_state(balances, margins, true, ts_now, ts_now, info); + + if let Err(e) = exec_sender.send(ExecutionEvent::Account(state)) { + tracing::warn!("Failed to send account state: {e}"); + } + } + } + + // Returns an error while disconnected, so the report hooks fall back to the inline path + // that reports the same error. + fn report_client(&self) -> anyhow::Result { + let client = self.ib_client.as_ref().context("IB client not connected")?; + + Ok(IbReportClient::new( + client.as_arc().clone(), + IbReportContext::new(Arc::clone(&self.instrument_provider), self.core.account_id), + self.ib_account, + Duration::from_secs(self.config.request_timeout), + )) + } + + pub(super) fn execution_filter(ib_account: Ustr, start: Option) -> ExecutionFilter { + let time = start.map_or_else(String::new, |start| { + start + .to_datetime_utc() + .strftime("%Y%m%d-%H:%M:%S") + .to_string() + }); + + ExecutionFilter { + client_id: None, + account_code: ib_account.to_string(), + time, + symbol: String::new(), + security_type: String::new(), + exchange: String::new(), + side: None, + last_n_days: 0, + specific_dates: Vec::new(), + } + } + fn is_ready_for_order_request(&self) -> bool { if !self.is_connected.load(Ordering::Relaxed) { return false; @@ -1864,6 +2029,43 @@ impl InteractiveBrokersExecutionClient { *self.next_order_id.lock() > 0 } + // Selects open orders for the instrument and account from the cache, the tracker, and + // incarnation groups, keeping only the requested side when one is set + fn cancel_all_targets( + &self, + cmd: &CancelAllOrders, + ) -> anyhow::Result)>> { + let cache = self.core.cache(); + let mut selected: Vec<_> = cache + .orders_open( + None, + Some(&cmd.instrument_id), + None, + Some(&self.core.account_id), + cmd.order_side, + ) + .iter() + .map(|order| (order.client_order_id(), order.venue_order_id())) + .collect(); + let state = self.orders.lock()?; + selected.extend(state.active_orders.iter().filter_map(|(order_id, order)| { + (order.instrument_id == cmd.instrument_id + && cmd.order_side.is_none_or(|side| side == order.order_side)) + .then_some(( + order.client_order_id, + Some(parse::ib_venue_order_id(*order_id, order.perm_id)), + )) + })); + selected.extend( + state + .group_cancel_candidates(cmd.instrument_id, self.core.account_id, cmd.order_side) + .map(|parent| (parent, None)), + ); + selected.sort_by_key(|(id, _)| id.to_string()); + selected.dedup_by_key(|(id, _)| *id); + Ok(selected) + } + fn ensure_client_ready_for_order_request(&self, request: &str) -> Result<(), String> { if self.is_ready_for_order_request() { return Ok(()); @@ -1875,20 +2077,17 @@ impl InteractiveBrokersExecutionClient { } fn deny_submit_order_not_ready(&self, cmd: &SubmitOrder, reason: &str) -> anyhow::Result<()> { + let reason = coded_denial_reason(DENIAL_CLIENT_NOT_READY, reason); Self::send_order_denied( cmd.order_init.trader_id, cmd.strategy_id, cmd.instrument_id, cmd.order_init.client_order_id, - reason, + &reason, ) } - fn deny_submit_order_list_not_ready( - &self, - cmd: &SubmitOrderList, - reason: &str, - ) -> anyhow::Result<()> { + fn deny_submit_order_list(&self, cmd: &SubmitOrderList, reason: &str) -> anyhow::Result<()> { for order_init in &cmd.order_inits { Self::send_order_denied( order_init.trader_id, @@ -1902,6 +2101,46 @@ impl InteractiveBrokersExecutionClient { Ok(()) } + #[allow(clippy::too_many_arguments)] // Event construction preserves the query context. + fn send_inferred_order_canceled( + trader_id: TraderId, + strategy_id: StrategyId, + instrument_id: InstrumentId, + client_order_id: ClientOrderId, + venue_order_id: VenueOrderId, + account_id: AccountId, + ts_init: UnixNanos, + exec_sender: &EventSender, + context: &str, + ) { + let event = OrderCanceled::new( + trader_id, + strategy_id, + instrument_id, + client_order_id, + UUID4::new(), + ts_init, + ts_init, + false, + Some(venue_order_id), + Some(account_id), + None, + ); + + if exec_sender + .send(ExecutionEvent::Order(OrderEventAny::Canceled(event))) + .is_err() + { + tracing::error!("query_order: failed to send inferred order canceled event"); + } else { + tracing::debug!( + "query_order: inferred cancel for {} ({})", + client_order_id, + context + ); + } + } + fn send_order_denied( trader_id: TraderId, strategy_id: StrategyId, @@ -1910,6 +2149,27 @@ impl InteractiveBrokersExecutionClient { reason: &str, ) -> anyhow::Result<()> { let ts_event = get_atomic_clock_realtime().get_time_ns(); + let exec_sender = get_exec_event_sender(); + Self::send_order_denied_to( + trader_id, + strategy_id, + instrument_id, + client_order_id, + reason, + &exec_sender, + ts_event, + ) + } + + pub(super) fn send_order_denied_to( + trader_id: TraderId, + strategy_id: StrategyId, + instrument_id: InstrumentId, + client_order_id: ClientOrderId, + reason: &str, + exec_sender: &EventSender, + ts_event: UnixNanos, + ) -> anyhow::Result<()> { let event = OrderDenied::new( trader_id, strategy_id, @@ -1921,7 +2181,7 @@ impl InteractiveBrokersExecutionClient { ts_event, ); - get_exec_event_sender() + exec_sender .send(ExecutionEvent::Order(OrderEventAny::Denied(event))) .map_err(|e| anyhow::anyhow!("Failed to send order denied event: {e}")) } @@ -1977,128 +2237,7 @@ impl InteractiveBrokersExecutionClient { } } -#[allow(dead_code)] impl InteractiveBrokersExecutionClient { - fn parse_historical_fill_report( - &self, - cmd: &GenerateFillReports, - exec_data: &ExecutionData, - commission: f64, - commission_currency: &str, - ts_init: UnixNanos, - ) -> Option { - let instrument_id = match self.resolve_historical_execution_instrument_id(exec_data) { - Ok(instrument_id) => instrument_id, - Err(e) => { - Self::warn_historical_fill_report_parse_error(exec_data, &e); - return None; - } - }; - - if let Some(filter_id) = cmd.instrument_id - && instrument_id != filter_id - { - return None; - } - - if let Some(filter_venue_order_id) = cmd.venue_order_id - && ib_venue_order_id(exec_data.execution.order_id, exec_data.execution.perm_id) - != filter_venue_order_id - { - return None; - } - - if let Some(end) = cmd.end { - match parse_execution_time(&exec_data.execution.time) { - Ok(ts_event) if ts_event > end => return None, - Ok(_) => {} - Err(e) => { - Self::warn_historical_fill_report_parse_error(exec_data, &e); - return None; - } - } - } - - match parse_execution_to_fill_report( - &exec_data.execution, - &exec_data.contract, - commission, - commission_currency, - instrument_id, - self.core.account_id, - &self.instrument_provider, - ts_init, - None, // avg_px (not available in historical fills) - ) { - Ok(report) => Some(report), - Err(e) => { - Self::warn_historical_fill_report_parse_error(exec_data, &e); - None - } - } - } - - fn resolve_historical_execution_instrument_id( - &self, - exec_data: &ExecutionData, - ) -> anyhow::Result { - self.resolve_report_contract_instrument_id(&exec_data.contract) - } - - fn resolve_report_contract_instrument_id( - &self, - contract: &Contract, - ) -> anyhow::Result { - match self - .instrument_provider - .resolve_instrument_id_for_contract(contract) - { - Ok(instrument_id) => Ok(instrument_id), - Err(provider_error) if contract.security_type != SecurityType::Spread => { - ib_contract_to_instrument_id_simple(contract).with_context(|| { - format!( - "Failed to resolve IBKR contract to instrument ID using provider ({provider_error}) or simple conversion", - ) - }) - } - Err(provider_error) => Err(provider_error) - .context("Failed to resolve BAG contract to spread instrument ID"), - } - } - - fn position_avg_px_open( - &self, - instrument_id: &InstrumentId, - instrument: &InstrumentAny, - average_cost: f64, - ) -> Option { - if average_cost <= 0.0 { - return None; - } - - let price_magnifier = self.instrument_provider.get_price_magnifier(instrument_id) as f64; - let multiplier = instrument.multiplier().as_f64(); - let converted_avg_cost = average_cost / (multiplier * price_magnifier); - Decimal::from_f64_retain(converted_avg_cost) - .map(|price| price.round_dp(instrument.price_precision() as u32)) - } - - fn warn_historical_fill_report_parse_error(exec_data: &ExecutionData, error: &anyhow::Error) { - tracing::warn!( - symbol = exec_data.contract.symbol.as_str(), - sec_type = ?exec_data.contract.security_type, - exchange = exec_data.contract.exchange.as_str(), - primary_exchange = exec_data.contract.primary_exchange.as_str(), - local_symbol = exec_data.contract.local_symbol.as_str(), - con_id = exec_data.contract.contract_id, - order_id = exec_data.execution.order_id, - order_ref = exec_data.execution.order_reference.as_str(), - execution_id = exec_data.execution.execution_id.as_str(), - error = %error, - "Failed to parse IBKR historical fill report", - ); - } - fn validate_cancel_order_target( cmd: &CancelOrder, target_order: &OrderAny, @@ -2116,7 +2255,7 @@ impl InteractiveBrokersExecutionClient { target_order.instrument_id() ); - // Command actor IDs identify the requester and are not ownership evidence + // Command actor IDs identify the requester and are not ownership evidence. if let (Some(command_venue_order_id), Some(target_venue_order_id)) = (cmd.venue_order_id.as_ref(), target_order.venue_order_id()) { @@ -2129,72 +2268,123 @@ impl InteractiveBrokersExecutionClient { Ok(()) } - /// Handles cancel order asynchronously. + /// Handles a cancel order asynchronously. /// /// # Errors /// /// Returns an error if broker order resolution or identity caching fails. + #[allow( + clippy::too_many_arguments, + reason = "cancel routing requires the request, target order, venue client, and event context" + )] async fn handle_cancel_order_async( cmd: &CancelOrder, target_order: &OrderAny, client: &Arc, - order_id_map: &Arc>>, - venue_order_id_map: &Arc>>, - instrument_id_map: &Arc>>, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - pending_cancel_orders: &Arc>>, + orders: &OrderTracker, + instrument_provider: &Arc, exec_sender: &EventSender, ts_init: UnixNanos, account_id: AccountId, + ib_account: Ustr, request_timeout_secs: u64, ) -> anyhow::Result<()> { + if orders.lock()?.groups.contains_key(&cmd.client_order_id) { + return Self::cancel_incarnation_group( + cmd.client_order_id, + client, + orders, + instrument_provider, + exec_sender, + ts_init, + account_id, + ib_account, + request_timeout_secs, + None, + target_order.is_pending_cancel(), + ) + .await; + } let order_selector = if let Some(venue_order_id) = &cmd.venue_order_id { IbOrderSelector::from_venue_order_id(venue_order_id)? } else { - let map = order_id_map.lock(); + let state = orders.lock()?; IbOrderSelector::OrderId( - *map.get(&cmd.client_order_id) + *state + .order_id_map + .get(&cmd.client_order_id) .context("No IB order ID mapping found for client order ID")?, ) }; let ib_order_id = - Self::resolve_ib_order_id(client, order_selector, account_id, request_timeout_secs) + Self::resolve_ib_order_id(client, order_selector, ib_account, request_timeout_secs) .await?; - Self::cache_cancel_order_tracking( - ib_order_id, - cmd, - target_order, - order_id_map, - venue_order_id_map, - instrument_id_map, - trader_id_map, - strategy_id_map, - )?; + Self::cache_recovered_order_tracking(ib_order_id, target_order, orders)?; + let venue_order_id = target_order + .venue_order_id() + .unwrap_or_else(|| VenueOrderId::from(ib_order_id.to_string())); + + let timeout_dur = Duration::from_secs(request_timeout_secs); + let send_error = + match tokio::time::timeout(timeout_dur, client.cancel_order(ib_order_id, "")).await { + Ok(Ok(_subscription)) => None, + Ok(Err(e)) => match Self::classify_order_submit_error(&e) { + CommandFailure::NotSent(reason) | CommandFailure::VenueRejected(reason) => { + anyhow::bail!(reason) + } + CommandFailure::Ambiguous(reason) => Some(reason), + }, + Err(_) => Some(format!( + "cancel request timed out after {request_timeout_secs} seconds" + )), + }; - if let Err(e) = client.cancel_order(ib_order_id, "").await { + if let Some(send_error) = send_error { tracing::warn!( - "Cancel outcome is unknown after attempting to send order {} to IB: {e}", + "Cancel outcome is unknown after attempting to send order {} to IB: {}; querying venue state", + cmd.client_order_id, + send_error + ); + return Self::resolve_ambiguous_cancel( + target_order, + client, + order_selector, + account_id, + ib_account, + request_timeout_secs, + &send_error, + exec_sender, + ts_init, + ) + .await; + } + + // IB's terminal status can evict an order while its execution details still wait for + // the commission report, so a cancel sent in that gap has no pending state to record + if orders.lock()?.terminal_orders.contains_key(&ib_order_id) { + tracing::debug!( + "Cancel request for order {} was sent after IB reported it terminal", cmd.client_order_id ); return Ok(()); } - let venue_order_id = target_order - .venue_order_id() - .unwrap_or_else(|| VenueOrderId::from(ib_order_id.to_string())); - if let Err(e) = Self::emit_order_pending_cancel( - ib_order_id, - cmd.client_order_id, - venue_order_id, - instrument_id_map, - trader_id_map, - strategy_id_map, - pending_cancel_orders, - exec_sender, - ts_init, - account_id, - ) { + // A strategy cancel already published PENDING_CANCEL, so only the tracking records it + let result = if target_order.is_pending_cancel() { + Self::track_pending_cancel(cmd.client_order_id, orders).map(|_| ()) + } else { + Self::emit_order_pending_cancel( + ib_order_id, + cmd.client_order_id, + venue_order_id, + orders, + exec_sender, + ts_init, + account_id, + ) + }; + + if let Err(e) = result { tracing::error!( "Cancel request for order {} was sent, but OrderPendingCancel emission failed: {e}", cmd.client_order_id @@ -2204,67 +2394,164 @@ impl InteractiveBrokersExecutionClient { Ok(()) } - async fn resolve_ib_order_id( + #[allow(clippy::too_many_arguments)] // Resolution needs the original cancel context. + async fn resolve_ambiguous_cancel( + target_order: &OrderAny, client: &Arc, order_selector: IbOrderSelector, account_id: AccountId, + ib_account: Ustr, request_timeout_secs: u64, - ) -> anyhow::Result { - let target_perm_id = match order_selector { - IbOrderSelector::OrderId(order_id) => return Ok(order_id), - IbOrderSelector::PermId(perm_id) => perm_id, - }; - + send_error: &str, + exec_sender: &EventSender, + ts_init: UnixNanos, + ) -> anyhow::Result<()> { let timeout_dur = Duration::from_secs(request_timeout_secs); - let raw_account_id = raw_ib_account_code(&account_id); - let subscription = match tokio::time::timeout(timeout_dur, client.all_open_orders()).await { - Ok(Ok(subscription)) => subscription, - Ok(Err(e)) => anyhow::bail!("Failed to request open orders for perm_id lookup: {e}"), - Err(_) => anyhow::bail!("Timed out requesting open orders for perm_id lookup"), - }; - let mut subscription = subscription.filter_data(); + let remains_open = tokio::time::timeout(timeout_dur, async { + let mut subscription = client.all_open_orders().await?.filter_data(); + while let Some(result) = subscription.next().await { + let Orders::OrderData(data) = result? else { + continue; + }; - while let Some(order_result) = subscription.next().await { - let Orders::OrderData(data) = order_result? else { - continue; - }; + if !data.order.account.is_empty() && data.order.account != ib_account { + continue; + } - if !Self::is_active_open_order(&data.order) { - continue; + if order_selector.matches(data.order_id, data.order.perm_id) { + return Ok::(true); + } } + Ok(false) + }) + .await + .context("Timed out querying open orders after ambiguous cancel")??; - if !data.order.account.is_empty() && data.order.account != raw_account_id { - continue; - } + if remains_open { + let reason = format!( + "Cancel was not confirmed after transport error; order remains open at IB: {send_error}" + ); + return Self::send_order_cancel_rejected( + target_order, + &reason, + exec_sender, + ts_init, + account_id, + ); + } - if data.order.perm_id != target_perm_id { - continue; - } + let completed_status = tokio::time::timeout(timeout_dur, async { + let mut subscription = client.completed_orders(false).await?.filter_data(); + while let Some(result) = subscription.next().await { + let Orders::OrderData(data) = result? else { + continue; + }; - if data.order_id == 0 { - anyhow::bail!( - "Cannot resolve PERM-{target_perm_id}: matching open order has no IB order_id" - ); + if !data.order.account.is_empty() && data.order.account != ib_account { + continue; + } + let matches_order = order_selector.matches(data.order_id, data.order.perm_id); + if matches_order { + return Ok::, ibapi::Error>(Some( + data.order_state.status, + )); + } } + Ok(None) + }) + .await + .context("Timed out querying completed orders after ambiguous cancel")??; - return Ok(data.order_id); + if completed_status.as_ref().is_some_and(|status| { + matches!( + status, + OrderStatusKind::Cancelled | OrderStatusKind::ApiCancelled + ) + }) { + let event = OrderCanceled::new( + target_order.trader_id(), + target_order.strategy_id(), + target_order.instrument_id(), + target_order.client_order_id(), + UUID4::new(), + ts_init, + ts_init, + false, + Some(order_selector.venue_order_id()), + Some(account_id), + None, + ); + exec_sender + .send(ExecutionEvent::Order(OrderEventAny::Canceled(event))) + .map_err(|e| { + anyhow::anyhow!("Failed to send resolved order canceled event: {e}") + })?; + return Ok(()); } - anyhow::bail!("Cannot resolve PERM-{target_perm_id}: no matching open order found") + let reason = completed_status.map_or_else( + || { + format!( + "Cancel outcome could not be resolved from IB open or completed orders: {send_error}" + ) + }, + |status| { + format!( + "Cancel did not complete; IB reports order status {}", + status.as_str() + ) + }, + ); + Self::send_order_cancel_rejected(target_order, &reason, exec_sender, ts_init, account_id) } - fn is_active_open_order(order: &ibapi::orders::Order) -> bool { + pub(super) async fn resolve_ib_order_id( + client: &Arc, + order_selector: IbOrderSelector, + ib_account: Ustr, + request_timeout_secs: u64, + ) -> anyhow::Result { + let target_perm_id = match order_selector { + IbOrderSelector::OrderId(order_id) => return Ok(order_id), + IbOrderSelector::PermId(perm_id) => perm_id, + }; + + let timeout_dur = Duration::from_secs(request_timeout_secs); + tokio::time::timeout(timeout_dur, async { + let mut subscription = client.all_open_orders().await?.filter_data(); + let mut routes = AHashMap::<(i32, i32), AHashSet>::new(); + let mut target_routes = AHashSet::new(); + + while let Some(result) = subscription.next().await { + let Orders::OrderData(data) = result? else { continue; }; + if data.order.account != ib_account || !Self::is_active_open_order(&data.order) { continue; } + let route = (data.order.client_id, data.order_id); + routes.entry(route).or_default().insert(data.order.perm_id); + if data.order.perm_id == target_perm_id { target_routes.insert(route); } + } + anyhow::ensure!(target_routes.len() == 1, + "Cannot resolve PERM-{target_perm_id}: expected one broker route, found {}", target_routes.len()); + let route = *target_routes.iter().next().expect("one route was checked"); + anyhow::ensure!(route.0 == client.client_id() && route.1 != 0, + "Cannot resolve PERM-{target_perm_id}: order is not bound to this API client"); + anyhow::ensure!(routes.get(&route).is_some_and(|ids| ids.len() == 1), + "Cannot resolve PERM-{target_perm_id}: broker route is shared by distinct permanent IDs"); + Ok(route.1) + }).await.context("timed out resolving IB permanent order identity")? + } + + pub(super) fn is_active_open_order(order: &ibapi::orders::Order) -> bool { !order.deactivate } - fn is_definitive_order_submit_error(error: &ibapi::Error) -> bool { + pub(super) fn is_definitive_order_submit_error(error: &ibapi::Error) -> bool { matches!( error, ibapi::Error::InvalidArgument(_) | ibapi::Error::ServerVersion(_, _, _) ) } - fn classify_order_submit_error(error: &ibapi::Error) -> CommandFailure { + pub(super) fn classify_order_submit_error(error: &ibapi::Error) -> CommandFailure { let reason = error.to_string(); if Self::is_definitive_order_submit_error(error) { @@ -2282,50 +2569,62 @@ impl InteractiveBrokersExecutionClient { async fn handle_cancel_all_orders_async( client: &Arc, - order_id_map: &Arc>>, - instrument_id_map: &Arc>>, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - pending_cancel_orders: &Arc>>, + orders: &OrderTracker, exec_sender: &EventSender, ts_init: UnixNanos, account_id: AccountId, + ib_account: Ustr, request_timeout_secs: u64, orders_to_cancel: Vec<(ClientOrderId, Option)>, + provider: &Arc, + order_side: Option, ) -> anyhow::Result<()> { + let mut handled = AHashSet::new(); + let mut individual = Vec::new(); + + for (id, venue_id) in orders_to_cancel { + let parent = orders.lock()?.group_for_client(id); + if let Some(parent) = parent { + if handled.insert(parent) { + Self::cancel_incarnation_group( + parent, + client, + orders, + provider, + exec_sender, + ts_init, + account_id, + ib_account, + request_timeout_secs, + order_side, + false, + ) + .await?; + } + } else { + individual.push((id, venue_id)); + } + } + let orders_to_cancel = individual; // Get all IB order selectors first, then drop the guard before awaiting let order_selectors: Vec<(ClientOrderId, IbOrderSelector, Option)> = { - let order_id_map_guard = order_id_map.lock(); + let state = orders.lock()?; orders_to_cancel .into_iter() .filter_map(|(client_order_id, venue_order_id)| { - if let Some(venue_order_id) = venue_order_id { - match IbOrderSelector::from_venue_order_id(&venue_order_id) { - Ok(order_selector) => { - return Some((client_order_id, order_selector, Some(venue_order_id))); - } - Err(e) => { - tracing::error!( - "Failed resolve cancel-all order {} from venue order ID {}: {e}", - client_order_id, - venue_order_id - ); - return None; - } + match state.cancel_selector(client_order_id, venue_order_id.as_ref()) { + Ok(Some(order_selector)) => { + Some((client_order_id, order_selector, venue_order_id)) + } + Ok(None) => None, + Err(e) => { + tracing::error!( + "Failed to resolve cancel-all order {client_order_id}: {e}" + ); + None } } - - order_id_map_guard - .get(&client_order_id) - .copied() - .map(|ib_order_id| { - ( - client_order_id, - IbOrderSelector::OrderId(ib_order_id), - None, - ) - }) }) .collect() }; @@ -2335,7 +2634,7 @@ impl InteractiveBrokersExecutionClient { let ib_order_id = match Self::resolve_ib_order_id( client, order_selector, - account_id, + ib_account, request_timeout_secs, ) .await @@ -2360,10 +2659,7 @@ impl InteractiveBrokersExecutionClient { ib_order_id, client_order_id, venue_order_id, - instrument_id_map, - trader_id_map, - strategy_id_map, - pending_cancel_orders, + orders, exec_sender, ts_init, account_id, @@ -2387,29 +2683,20 @@ impl InteractiveBrokersExecutionClient { Ok(()) } - #[allow(clippy::too_many_arguments)] - fn emit_order_pending_cancel( - order_id: i32, + pub(super) fn emit_order_pending_cancel( + _order_id: i32, client_order_id: ClientOrderId, venue_order_id: VenueOrderId, - instrument_id_map: &Arc>>, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - pending_cancel_orders: &Arc>>, + orders: &OrderTracker, exec_sender: &EventSender, ts_init: UnixNanos, account_id: AccountId, ) -> anyhow::Result<()> { - let mut pending = pending_cancel_orders.lock(); - if !pending.insert(client_order_id) { + let Some((trader_id, strategy_id, instrument_id)) = + Self::track_pending_cancel(client_order_id, orders)? + else { return Ok(()); - } - drop(pending); - - let instrument_id = Self::get_mapped_instrument_id(order_id, instrument_id_map) - .context("Instrument ID not found for pending cancel order")?; - let (trader_id, strategy_id) = - Self::get_required_order_actor_ids(order_id, trader_id_map, strategy_id_map)?; + }; let event = OrderPendingCancel::new( trader_id, @@ -2430,4 +2717,24 @@ impl InteractiveBrokersExecutionClient { Ok(()) } + + // Marks a tracked order as pending cancel, returning its identity when it was not already. + pub(super) fn track_pending_cancel( + client_order_id: ClientOrderId, + orders: &OrderTracker, + ) -> anyhow::Result> { + let mut state = orders.lock()?; + let order = state + .order_by_client_mut(client_order_id) + .context("Tracked state not found for pending cancel order")?; + if order.pending_cancel { + return Ok(None); + } + order.pending_cancel = true; + Ok(Some(( + order.trader_id, + order.strategy_id, + order.instrument_id, + ))) + } } diff --git a/crates/adapters/interactive_brokers/src/execution/core/tests.rs b/crates/adapters/interactive_brokers/src/execution/core/tests.rs new file mode 100644 index 000000000000..cccd458c68b6 --- /dev/null +++ b/crates/adapters/interactive_brokers/src/execution/core/tests.rs @@ -0,0 +1,4952 @@ +// ------------------------------------------------------------------------------------------------- +// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. +// https://nautechsystems.io +// +// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html +// ------------------------------------------------------------------------------------------------- + +//! Tests for Interactive Brokers execution client behavior. + +use std::{cell::RefCell, rc::Rc, str::FromStr}; + +use ibapi::{ + contracts::{ + Contract, Currency as IBCurrency, Exchange, LegAction, OptionRight, SecurityType, + Symbol as IBSymbol, + }, + orders::{ + CommissionReport, Execution, ExecutionData, ExecutionSide, Liquidity, Order as IBOrder, + OrderData as IBOrderData, OrderState, OrderStatus as IBOrderStatus, OrderStatusKind, + OrderUpdate, + }, +}; +use nautilus_common::{ + cache::Cache, + live::{runner::replace_exec_event_sender, sender::EventSender}, +}; +use nautilus_core::Params; +use nautilus_live::{ExecutionClientCore, execution::failure::CommandFailure}; +use nautilus_model::{ + enums::{AccountType, AssetClass, OmsType, OrderSide, OrderType}, + events::OrderInitialized, + identifiers::{ + AccountId, ClientOrderId, InstrumentId, OrderListId, StrategyId, Symbol, TradeId, TraderId, + Venue, VenueOrderId, + }, + instruments::{InstrumentAny, OptionSpread, stubs::equity_aapl}, + orders::{OrderList, builder::OrderTestBuilder}, + types::{Currency, Money, Price, Quantity}, +}; +use rstest::rstest; +use rust_decimal::Decimal; +use ustr::Ustr; + +use super::*; +use crate::{ + common::consts::{IB_CLIENT_ID, IB_VENUE}, + config::InteractiveBrokersInstrumentProviderConfig, + execution::account::check_external_position_change, + stubs::ChannelSubscription, +}; + +const NATIVE_CLIENT_ID: i32 = 1; + +fn create_test_instrument_provider() -> Arc { + let config = InteractiveBrokersInstrumentProviderConfig::default(); + Arc::new(InteractiveBrokersInstrumentProvider::new(config)) +} + +fn create_test_execution_client() -> ( + InteractiveBrokersExecutionClient, + tokio::sync::mpsc::UnboundedReceiver, + Rc>, +) { + let trader_id = TraderId::from("TESTER-001"); + let account_id = AccountId::from("IB-001"); + let cache = Rc::new(RefCell::new(Cache::default())); + let core = ExecutionClientCore::new( + trader_id, + *IB_CLIENT_ID, + *IB_VENUE, + OmsType::Netting, + account_id, + AccountType::Margin, + None, + cache.clone(), + ); + let instrument_provider = create_test_instrument_provider(); + let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); + replace_exec_event_sender(tx); + let client = InteractiveBrokersExecutionClient::new( + core, + InteractiveBrokersExecutionClientConfig::default(), + instrument_provider, + ) + .unwrap(); + + (client, rx, cache) +} + +fn create_test_spread_instrument() -> InstrumentId { + InstrumentId::new( + Symbol::from("(1)SPY C400___((1))SPY C410"), + Venue::from("SMART"), + ) +} + +fn create_test_leg_instrument() -> InstrumentId { + InstrumentId::new(Symbol::from("SPY C400"), Venue::from("SMART")) +} + +fn create_test_stock_instrument() -> InstrumentId { + InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")) +} + +fn create_test_limit_order(client_order_id: ClientOrderId) -> OrderAny { + OrderTestBuilder::new(OrderType::Limit) + .instrument_id(create_test_stock_instrument()) + .client_order_id(client_order_id) + .side(OrderSide::Buy) + .price(Price::from("100.00")) + .quantity(Quantity::from(1)) + .submit(true) + .build() +} + +fn create_tracked_order_context( + client_order_id: ClientOrderId, + instrument_id: InstrumentId, +) -> TrackedOrder { + TrackedOrder { + client_order_id, + trader_id: TraderId::from("TRADER-001"), + strategy_id: StrategyId::from("STRATEGY-001"), + instrument_id, + order_side: OrderSide::Buy, + order_type: OrderType::Limit, + accepted: false, + avg_px: None, + pending_cancel: false, + pending_modify: None, + perm_id: 0, + spread_fill_ids: ahash::AHashSet::new(), + spread_fills_held: HeldSpreadFills::default(), + last_update: None, + } +} + +fn insert_tracked_order(orders: &OrderTracker, order_id: i32, order: TrackedOrder) { + let mut state = orders.lock().unwrap(); + state.order_id_map.insert(order.client_order_id, order_id); + state + .venue_order_id_map + .insert(order_id, order.client_order_id); + state.active_orders.insert(order_id, order); +} + +struct SubmitTrackingState(OrderTracker); + +impl SubmitTrackingState { + fn new() -> Self { + Self(OrderTracker::new(NATIVE_CLIENT_ID)) + } + + fn cache( + &self, + order_id: i32, + client_order_id: ClientOrderId, + instrument_id: InstrumentId, + trader_id: TraderId, + strategy_id: StrategyId, + ) { + InteractiveBrokersExecutionClient::cache_order_tracking( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + OrderSide::Buy, + OrderType::Limit, + &self.0, + ) + .unwrap(); + } + + fn assert_active( + &self, + order_id: i32, + client_order_id: ClientOrderId, + instrument_id: InstrumentId, + trader_id: TraderId, + strategy_id: StrategyId, + accepted: bool, + ) { + let state = self.0.lock().unwrap(); + assert_eq!(state.order_id_map.get(&client_order_id), Some(&order_id)); + assert_eq!( + state.venue_order_id_map.get(&order_id), + Some(&client_order_id) + ); + let context = state.active_orders.get(&order_id).unwrap(); + assert_eq!(context.client_order_id, client_order_id); + assert_eq!(context.instrument_id, instrument_id); + assert_eq!(context.trader_id, trader_id); + assert_eq!(context.strategy_id, strategy_id); + assert_eq!(context.order_side, OrderSide::Buy); + assert_eq!(context.order_type, OrderType::Limit); + assert_eq!(context.accepted, accepted); + assert_eq!(context.avg_px, None); + assert!(state.terminal_orders.get(&order_id).is_none()); + } + + fn emit_accepted( + &self, + order_id: i32, + account_id: AccountId, + exec_sender: &EventSender, + ) -> bool { + InteractiveBrokersExecutionClient::emit_order_accepted_if_needed( + order_id, + VenueOrderId::from(order_id.to_string()), + account_id, + UnixNanos::new(29), + &self.0, + exec_sender, + ) + .unwrap() + } + + fn assert_absent(&self, order_id: i32, client_order_id: ClientOrderId) { + let state = self.0.lock().unwrap(); + assert_eq!(state.order_id_map.get(&client_order_id), None); + assert_eq!(state.venue_order_id_map.get(&order_id), None); + assert!(state.active_orders.get(&order_id).is_none()); + assert!(state.terminal_orders.get(&order_id).is_none()); + } +} + +async fn process_submitted_status( + order_id: i32, + state: &SubmitTrackingState, + exec_sender: &EventSender, +) { + InteractiveBrokersExecutionClient::handle_order_status( + &create_test_order_status(order_id, "Submitted"), + &state.0, + &create_test_instrument_provider(), + exec_sender, + UnixNanos::new(27), + AccountId::from("IB-001"), + Ustr::from("001"), + None, + ) + .await + .unwrap(); +} + +fn next_order_event( + rx: &mut tokio::sync::mpsc::UnboundedReceiver, +) -> OrderEventAny { + match rx.try_recv().unwrap() { + ExecutionEvent::Order(event) => event, + event => panic!("Expected order event, was {event:?}"), + } +} + +#[rstest] +#[case(1, 0, 1)] +#[case(1, 310, 310_000_001)] +#[case(42, 1_402, 402_000_042)] +#[case(450_000_123, 402, 402_000_123)] +#[case(1, -12, 12_000_001)] +fn apply_client_order_id_floor( + #[case] next_id: i32, + #[case] client_id: i32, + #[case] expected: i32, +) { + assert_eq!( + InteractiveBrokersExecutionClient::apply_client_order_id_floor(next_id, client_id), + expected + ); +} + +#[rstest] +fn stop_cancels_execution_lifecycle_and_aborts_tracked_tasks() { + let (mut client, _receiver, _cache) = create_test_execution_client(); + client.is_connected.store(true, Ordering::Relaxed); + client.core.set_connected(); + let cancellation = client.pending_tasks.cancellation_token(); + client + .pending_tasks + .spawn(async { std::future::pending::<()>().await }) + .unwrap(); + + ExecutionClient::stop(&mut client).unwrap(); + + assert!(cancellation.is_cancelled()); + assert!(!client.pending_tasks.is_open()); + assert!(!client.is_connected.load(Ordering::Relaxed)); + assert!(client.core.is_disconnected()); +} + +#[rstest] +fn order_id_partition_rejects_foreign_open_order() { + assert!(InteractiveBrokersExecutionClient::is_order_id_in_client_partition(402_000_123, 402)); + assert!(!InteractiveBrokersExecutionClient::is_order_id_in_client_partition(450_000_123, 402)); +} + +#[rstest] +fn new_derives_ib_account_from_configured_code_for_non_default_client_name() { + let core = ExecutionClientCore::new( + TraderId::from("TESTER-001"), + ClientId::from("IB-TEST"), + *IB_VENUE, + OmsType::Netting, + AccountId::from("IB-TEST-U7654321"), + AccountType::Margin, + None, + Rc::new(RefCell::new(Cache::default())), + ); + let config = InteractiveBrokersExecutionClientConfig { + account_id: Some(String::from("U7654321")), + ..Default::default() + }; + + let client = + InteractiveBrokersExecutionClient::new(core, config, create_test_instrument_provider()) + .unwrap(); + + assert_eq!(client.core.account_id, AccountId::from("IB-TEST-U7654321")); + assert_eq!(client.ib_account, Ustr::from("U7654321")); +} + +#[rstest] +fn execution_filter_uses_raw_ib_account_code() { + let filter = InteractiveBrokersExecutionClient::execution_filter(Ustr::from("DU123456"), None); + + assert_eq!(filter.client_id, None); + assert_eq!(filter.account_code, "DU123456"); + assert_eq!(filter.time, ""); + assert_eq!(filter.symbol, ""); + assert_eq!(filter.security_type, ""); + assert_eq!(filter.exchange, ""); + assert_eq!(filter.side, None); + assert_eq!(filter.last_n_days, 0); + assert!(filter.specific_dates.is_empty()); +} + +#[rstest] +fn ib_order_selector_parses_numeric_venue_order_id() { + let selector = IbOrderSelector::from_venue_order_id(&VenueOrderId::from("123")).unwrap(); + + assert_eq!(selector, IbOrderSelector::OrderId(123)); + assert!(selector.matches(123, 456)); + assert!(!selector.matches(124, 456)); + assert_eq!(selector.venue_order_id(), VenueOrderId::from("123")); +} + +#[rstest] +fn ib_order_selector_parses_perm_venue_order_id() { + let selector = IbOrderSelector::from_venue_order_id(&VenueOrderId::from("PERM-456")).unwrap(); + + assert_eq!(selector, IbOrderSelector::PermId(456)); + assert!(selector.matches(0, 456)); + assert!(selector.matches(123, 456)); + assert!(!selector.matches(123, 457)); + assert_eq!(selector.venue_order_id(), VenueOrderId::from("PERM-456")); +} + +#[rstest] +#[case( + "PERM-invalid", + "Failed to parse venue_order_id \"PERM-invalid\" as IB perm_id" +)] +#[case("invalid", "Failed to parse venue_order_id \"invalid\" as IB order_id")] +fn ib_order_selector_rejects_invalid_venue_order_id( + #[case] venue_order_id: &str, + #[case] expected: &str, +) { + let result = IbOrderSelector::from_venue_order_id(&VenueOrderId::from(venue_order_id)); + + assert_eq!(result.unwrap_err().to_string(), expected); +} + +#[rstest] +#[case::tracked(false, IbOrderSelector::OrderId(123))] +#[case::duplicate_member(true, IbOrderSelector::PermId(789))] +fn cancel_selector_prefers_tracked_order_id( + #[case] duplicate_member: bool, + #[case] expected: IbOrderSelector, +) { + let client_order_id = ClientOrderId::from("O-19700101-000000-001-001-1"); + let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let mut tracked = create_tracked_order_context(client_order_id, instrument_id); + tracked.perm_id = 789; + + if duplicate_member { + orders + .lock() + .unwrap() + .auxiliary_orders + .insert(client_order_id, tracked); + } else { + insert_tracked_order(&orders, 123, tracked); + } + + let selector = orders + .lock() + .unwrap() + .cancel_selector(client_order_id, Some(&VenueOrderId::from("PERM-456"))) + .unwrap(); + + assert_eq!(selector, Some(expected)); +} + +#[rstest] +fn cancel_selector_falls_back_to_venue_order_id() { + let client_order_id = ClientOrderId::from("O-19700101-000000-001-001-1"); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + + let selector = orders + .lock() + .unwrap() + .cancel_selector(client_order_id, Some(&VenueOrderId::from("PERM-456"))) + .unwrap(); + + assert_eq!(selector, Some(IbOrderSelector::PermId(456))); +} + +#[rstest] +#[case(false, true)] +#[case(true, false)] +fn active_open_order_excludes_deactivated_records( + #[case] deactivate: bool, + #[case] expected: bool, +) { + let order = IBOrder { + deactivate, + ..Default::default() + }; + + assert_eq!( + InteractiveBrokersExecutionClient::is_active_open_order(&order), + expected + ); +} + +#[rstest] +fn order_submit_error_classifies_by_delivery_evidence() { + let invalid = ibapi::Error::InvalidArgument("invalid quantity".to_string()); + let unsupported = ibapi::Error::ServerVersion(100, 99, "feature".to_string()); + let rejection = ibapi::Error::Notice(ibapi::Notice { + request_id: None, + code: 201, + message: "Order rejected".to_string(), + error_time: None, + advanced_order_reject_json: String::new(), + }); + let cancellation = ibapi::Error::Notice(ibapi::Notice { + request_id: None, + code: 202, + message: "Order cancelled".to_string(), + error_time: None, + advanced_order_reject_json: String::new(), + }); + + assert_eq!( + InteractiveBrokersExecutionClient::classify_order_submit_error(&invalid), + CommandFailure::not_sent(invalid.to_string()) + ); + assert_eq!( + InteractiveBrokersExecutionClient::classify_order_submit_error(&unsupported), + CommandFailure::not_sent(unsupported.to_string()) + ); + assert_eq!( + InteractiveBrokersExecutionClient::classify_order_submit_error(&rejection), + CommandFailure::venue_rejected(rejection.to_string()) + ); + assert_eq!( + InteractiveBrokersExecutionClient::classify_order_submit_error(&cancellation), + CommandFailure::ambiguous(cancellation.to_string()) + ); + + for ambiguous in [ + ibapi::Error::ConnectionReset, + ibapi::Error::EndOfStream, + ibapi::Error::Io(std::io::Error::new( + std::io::ErrorKind::BrokenPipe, + "partial write", + )), + ibapi::Error::Io(std::io::Error::new( + std::io::ErrorKind::TimedOut, + "write timed out", + )), + ] { + assert_eq!( + InteractiveBrokersExecutionClient::classify_order_submit_error(&ambiguous), + CommandFailure::ambiguous(ambiguous.to_string()) + ); + } + + assert!(InteractiveBrokersExecutionClient::is_definitive_order_submit_error(&invalid)); + assert!(InteractiveBrokersExecutionClient::is_definitive_order_submit_error(&unsupported)); + assert!( + !InteractiveBrokersExecutionClient::is_definitive_order_submit_error( + &ibapi::Error::ConnectionReset + ) + ); +} + +#[rstest] +fn single_submit_definitive_failure_rejects_and_removes_tracking() { + let state = SubmitTrackingState::new(); + let order_id = 7101; + let client_order_id = ClientOrderId::from("O-SINGLE-NOT-SENT"); + let instrument_id = InstrumentId::new(Symbol::from("MSFT"), Venue::from("SMART")); + let trader_id = TraderId::from("TRADER-SINGLE-001"); + let strategy_id = StrategyId::from("STRATEGY-SINGLE-001"); + let account_id = AccountId::from("IB-SINGLE-001"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + state.cache( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + ); + + let result = InteractiveBrokersExecutionClient::handle_order_submit_failure( + &ibapi::Error::InvalidArgument("quantity must be positive".to_string()), + "Failed to submit order", + order_id, + account_id, + UnixNanos::new(19), + &state.0, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + ); + + assert_eq!( + result.unwrap_err().to_string(), + "Failed to submit order: InvalidArgument: quantity must be positive" + ); + state.assert_absent(order_id, client_order_id); + + match exec_receiver.try_recv().unwrap() { + ExecutionEvent::Order(OrderEventAny::Rejected(event)) => { + assert_eq!(event.trader_id, trader_id); + assert_eq!(event.strategy_id, strategy_id); + assert_eq!(event.instrument_id, instrument_id); + assert_eq!(event.client_order_id, client_order_id); + assert_eq!(event.account_id, account_id); + assert_eq!( + event.reason, + "Failed to submit order: InvalidArgument: quantity must be positive" + ); + assert_eq!(event.ts_event, UnixNanos::new(19)); + assert!(!event.reconciliation); + assert!(!event.due_post_only); + } + event => panic!("Expected rejected order event, was {event:?}"), + } + assert!(exec_receiver.try_recv().is_err()); +} + +#[tokio::test] +async fn single_submit_ambiguous_failure_retains_tracking_for_status_resolution() { + let state = SubmitTrackingState::new(); + let order_id = 7102; + let client_order_id = ClientOrderId::from("O-SINGLE-AMBIGUOUS"); + let instrument_id = InstrumentId::new(Symbol::from("NVDA"), Venue::from("SMART")); + let trader_id = TraderId::from("TRADER-SINGLE-002"); + let strategy_id = StrategyId::from("STRATEGY-SINGLE-002"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + state.cache( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + ); + + let result = InteractiveBrokersExecutionClient::handle_order_submit_failure( + &ibapi::Error::Io(std::io::Error::new( + std::io::ErrorKind::TimedOut, + "socket write timed out", + )), + "Failed to submit order", + order_id, + AccountId::from("IB-001"), + UnixNanos::new(23), + &state.0, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + ); + + assert_eq!( + result.unwrap_err().to_string(), + "Failed to submit order; outcome is unknown after possible transmission: socket write timed out" + ); + assert!(exec_receiver.try_recv().is_err()); + state.assert_active( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + false, + ); + + process_submitted_status(order_id, &state, &exec_sender.clone().into()).await; + + match exec_receiver.try_recv().unwrap() { + ExecutionEvent::Order(OrderEventAny::Accepted(event)) => { + assert_eq!(event.trader_id, trader_id); + assert_eq!(event.strategy_id, strategy_id); + assert_eq!(event.instrument_id, instrument_id); + assert_eq!(event.client_order_id, client_order_id); + assert_eq!( + event.venue_order_id, + VenueOrderId::from(order_id.to_string()) + ); + assert_eq!(event.account_id, AccountId::from("IB-001")); + assert_eq!(event.ts_event, UnixNanos::new(27)); + } + event => panic!("Expected accepted order event, was {event:?}"), + } + assert!(exec_receiver.try_recv().is_err()); + state.assert_active( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + true, + ); +} + +#[tokio::test] +async fn inactive_order_status_emits_rejected_before_terminal_eviction() { + let state = SubmitTrackingState::new(); + let order_id = 7103; + let client_order_id = ClientOrderId::from("O-SINGLE-INACTIVE"); + let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")); + let trader_id = TraderId::from("TRADER-SINGLE-003"); + let strategy_id = StrategyId::from("STRATEGY-SINGLE-003"); + let account_id = AccountId::from("IB-001"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + state.cache( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + ); + let mut status = create_test_order_status(order_id, "Inactive"); + status.why_held = "Order rejected by venue".to_string(); + + InteractiveBrokersExecutionClient::handle_order_status( + &status, + &state.0, + &create_test_instrument_provider(), + &exec_sender.clone().into(), + UnixNanos::new(41), + account_id, + Ustr::from("001"), + None, + ) + .await + .unwrap(); + + match exec_receiver.try_recv().unwrap() { + ExecutionEvent::Order(OrderEventAny::Rejected(event)) => { + assert_eq!(event.trader_id, trader_id); + assert_eq!(event.strategy_id, strategy_id); + assert_eq!(event.instrument_id, instrument_id); + assert_eq!(event.client_order_id, client_order_id); + assert_eq!(event.account_id, account_id); + assert_eq!(event.reason.as_str(), "Order rejected by venue"); + assert_eq!(event.ts_event, UnixNanos::new(41)); + assert_eq!(event.ts_init, UnixNanos::new(41)); + assert!(!event.reconciliation); + assert!(!event.due_post_only); + } + event => panic!("Expected rejected order event, was {event:?}"), + } + assert!(exec_receiver.try_recv().is_err()); + let tracker = state.0.lock().unwrap(); + assert_eq!(tracker.order_id_map.get(&client_order_id), None); + assert_eq!(tracker.venue_order_id_map.get(&order_id), None); + assert!(tracker.active_orders.get(&order_id).is_none()); + assert_eq!( + tracker + .terminal_orders + .get(&order_id) + .map(|context| context.client_order_id), + Some(client_order_id) + ); +} + +#[rstest] +#[tokio::test] +async fn inactive_partially_filled_order_emits_canceled() { + let state = SubmitTrackingState::new(); + let order_id = 7104; + let client_order_id = ClientOrderId::from("O-PARTIAL-INACTIVE"); + let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")); + let trader_id = TraderId::from("TRADER-SINGLE-004"); + let strategy_id = StrategyId::from("STRATEGY-SINGLE-004"); + let account_id = AccountId::from("IB-001"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + state.cache( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + ); + + let mut status = create_test_order_status(order_id, "Inactive"); + status.filled = 1.0; + status.remaining = 1.0; + status.why_held = "Order inactive after partial fill".to_string(); + + InteractiveBrokersExecutionClient::handle_order_status( + &status, + &state.0, + &create_test_instrument_provider(), + &exec_sender.clone().into(), + UnixNanos::new(42), + account_id, + Ustr::from("001"), + None, + ) + .await + .unwrap(); + + match exec_receiver.try_recv().unwrap() { + ExecutionEvent::Order(OrderEventAny::Canceled(event)) => { + assert_eq!(event.trader_id, trader_id); + assert_eq!(event.strategy_id, strategy_id); + assert_eq!(event.instrument_id, instrument_id); + assert_eq!(event.client_order_id, client_order_id); + assert_eq!(event.account_id, Some(account_id)); + assert_eq!(event.ts_event, UnixNanos::new(42)); + } + event => panic!("Expected canceled order event, was {event:?}"), + } + assert!(exec_receiver.try_recv().is_err()); +} + +#[rstest] +#[case::pending_modify(true, 0.0)] +#[case::no_pending_modify(false, 0.0)] +#[case::partially_filled_pending_modify(true, 1.0)] +#[tokio::test] +async fn inactive_accepted_order_stays_working(#[case] pending_modify: bool, #[case] filled: f64) { + let state = SubmitTrackingState::new(); + let order_id = 7105; + let client_order_id = ClientOrderId::from("O-ACCEPTED-INACTIVE"); + let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")); + let trader_id = TraderId::from("TRADER-SINGLE-005"); + let strategy_id = StrategyId::from("STRATEGY-SINGLE-005"); + let account_id = AccountId::from("IB-001"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + state.cache( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + ); + { + let mut tracker = state.0.lock().unwrap(); + let order = tracker.active_orders.get_mut(&order_id).unwrap(); + order.accepted = true; + order.pending_modify = pending_modify.then_some(PendingModifyValues { + total_quantity: 1.0, + limit_price: Some(5000.0), + aux_price: None, + trail_stop_price: None, + }); + } + + let mut status = create_test_order_status(order_id, "Inactive"); + status.why_held = "Modify refused by venue".to_string(); + status.filled = filled; + + InteractiveBrokersExecutionClient::handle_order_status( + &status, + &state.0, + &create_test_instrument_provider(), + &exec_sender.clone().into(), + UnixNanos::new(46), + account_id, + Ustr::from("001"), + None, + ) + .await + .unwrap(); + + if pending_modify { + match exec_receiver.try_recv().unwrap() { + ExecutionEvent::Order(OrderEventAny::ModifyRejected(event)) => { + assert_eq!(event.trader_id, trader_id); + assert_eq!(event.strategy_id, strategy_id); + assert_eq!(event.client_order_id, client_order_id); + assert_eq!(event.account_id, Some(account_id)); + assert_eq!(event.reason.as_str(), "Modify refused by venue"); + assert_eq!(event.ts_event, UnixNanos::new(46)); + } + event => panic!("Expected modify rejected event, was {event:?}"), + } + } + assert!(exec_receiver.try_recv().is_err()); + state.assert_active( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + true, + ); + let tracker = state.0.lock().unwrap(); + assert!(tracker.active_orders[&order_id].pending_modify.is_none()); +} + +#[rstest] +fn order_notice_rejection_emits_terminal_event_after_informational_notices() { + let state = SubmitTrackingState::new(); + let order_id = 7104; + let client_order_id = ClientOrderId::from("O-SINGLE-NOTICE"); + let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")); + let trader_id = TraderId::from("TRADER-SINGLE-004"); + let strategy_id = StrategyId::from("STRATEGY-SINGLE-004"); + let account_id = AccountId::from("IB-001"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + state.cache( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + ); + + for notice in [ + Notice { + request_id: Some(order_id), + code: 2109, + message: "Order event attribute ignored".to_string(), + error_time: None, + advanced_order_reject_json: String::new(), + }, + Notice { + request_id: Some(order_id), + code: 399, + message: "Order Message:\nSELL 1 ES DEC'26\nWarning: Your order will not be placed at the exchange until 2026-08-17 08:30:00 US/Central.".to_string(), + error_time: None, + advanced_order_reject_json: String::new(), + }, + Notice { + request_id: Some(order_id), + code: 202, + message: "Order cancelled".to_string(), + error_time: None, + advanced_order_reject_json: String::new(), + }, + Notice { + request_id: None, + code: 201, + message: "Request-less rejection".to_string(), + error_time: None, + advanced_order_reject_json: String::new(), + }, + ] { + InteractiveBrokersExecutionClient::handle_order_notice( + ¬ice, + &state.0, + &exec_sender.clone().into(), + UnixNanos::new(42), + account_id, + None, + ) + .unwrap(); + } + + assert!(exec_receiver.try_recv().is_err()); + state.assert_active( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + false, + ); + + let rejection = Notice { + request_id: Some(order_id), + code: 201, + message: "Order rejected: insufficient margin".to_string(), + error_time: None, + advanced_order_reject_json: r#"{"errorCode":"IBDBUYTX"}"#.to_string(), + }; + InteractiveBrokersExecutionClient::handle_order_notice( + &rejection, + &state.0, + &exec_sender.clone().into(), + UnixNanos::new(43), + account_id, + None, + ) + .unwrap(); + + match exec_receiver.try_recv().unwrap() { + ExecutionEvent::Order(OrderEventAny::Rejected(event)) => { + assert_eq!(event.trader_id, trader_id); + assert_eq!(event.strategy_id, strategy_id); + assert_eq!(event.instrument_id, instrument_id); + assert_eq!(event.client_order_id, client_order_id); + assert_eq!(event.account_id, account_id); + assert_eq!(event.reason.as_str(), "Order rejected: insufficient margin"); + assert_eq!(event.ts_event, UnixNanos::new(43)); + assert_eq!(event.ts_init, UnixNanos::new(43)); + assert!(!event.reconciliation); + assert!(!event.due_post_only); + } + event => panic!("Expected rejected order event, was {event:?}"), + } + assert!(exec_receiver.try_recv().is_err()); + let tracker = state.0.lock().unwrap(); + assert_eq!(tracker.order_id_map.get(&client_order_id), None); + assert_eq!(tracker.venue_order_id_map.get(&order_id), None); + assert!(tracker.active_orders.get(&order_id).is_none()); + assert_eq!( + tracker + .terminal_orders + .get(&order_id) + .map(|context| context.client_order_id), + Some(client_order_id) + ); +} + +#[rstest] +fn order_notice_sub_200_error_rejects_pre_acceptance_order() { + let state = SubmitTrackingState::new(); + let order_id = 7105; + let client_order_id = ClientOrderId::from("O-SUB-200"); + let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")); + let trader_id = TraderId::from("TRADER-SUB-200"); + let strategy_id = StrategyId::from("STRATEGY-SUB-200"); + let account_id = AccountId::from("IB-001"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + state.cache( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + ); + + let notice = Notice { + request_id: Some(order_id), + code: 110, + message: "The price does not conform to the minimum price variation".to_string(), + error_time: None, + advanced_order_reject_json: String::new(), + }; + InteractiveBrokersExecutionClient::handle_order_notice( + ¬ice, + &state.0, + &exec_sender.clone().into(), + UnixNanos::new(44), + account_id, + None, + ) + .unwrap(); + + match exec_receiver.try_recv().unwrap() { + ExecutionEvent::Order(OrderEventAny::Rejected(event)) => { + assert_eq!(event.client_order_id, client_order_id); + assert_eq!(event.account_id, account_id); + assert_eq!( + event.reason.as_str(), + "The price does not conform to the minimum price variation" + ); + assert_eq!(event.ts_event, UnixNanos::new(44)); + } + event => panic!("Expected rejected order event, was {event:?}"), + } + assert!(exec_receiver.try_recv().is_err()); + + let tracker = state.0.lock().unwrap(); + assert!(tracker.active_orders.get(&order_id).is_none()); + assert_eq!( + tracker + .terminal_orders + .get(&order_id) + .map(|context| context.client_order_id), + Some(client_order_id) + ); +} + +#[rstest] +fn order_notice_for_accepted_order_with_pending_modify_emits_modify_rejected() { + let state = SubmitTrackingState::new(); + let order_id = 7106; + let client_order_id = ClientOrderId::from("O-MODIFY-REJECT"); + let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")); + let trader_id = TraderId::from("TRADER-MODIFY"); + let strategy_id = StrategyId::from("STRATEGY-MODIFY"); + let account_id = AccountId::from("IB-001"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + state.cache( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + ); + { + let mut tracker = state.0.lock().unwrap(); + let order = tracker.active_orders.get_mut(&order_id).unwrap(); + order.accepted = true; + order.pending_modify = Some(PendingModifyValues { + total_quantity: 1.0, + limit_price: Some(5000.0), + aux_price: None, + trail_stop_price: None, + }); + } + + let notice = Notice { + request_id: Some(order_id), + code: 201, + message: "Order rejected - reason: modify would violate margin".to_string(), + error_time: None, + advanced_order_reject_json: String::new(), + }; + InteractiveBrokersExecutionClient::handle_order_notice( + ¬ice, + &state.0, + &exec_sender.clone().into(), + UnixNanos::new(45), + account_id, + None, + ) + .unwrap(); + + match exec_receiver.try_recv().unwrap() { + ExecutionEvent::Order(OrderEventAny::ModifyRejected(event)) => { + assert_eq!(event.client_order_id, client_order_id); + assert_eq!(event.account_id, Some(account_id)); + assert_eq!( + event.reason.as_str(), + "Order rejected - reason: modify would violate margin" + ); + assert_eq!(event.ts_event, UnixNanos::new(45)); + } + event => panic!("Expected modify rejected event, was {event:?}"), + } + assert!(exec_receiver.try_recv().is_err()); + + let tracker = state.0.lock().unwrap(); + let order = tracker.active_orders.get(&order_id).unwrap(); + assert!(order.accepted); + assert!(order.pending_modify.is_none()); +} + +#[rstest] +fn order_notice_for_accepted_order_with_pending_cancel_emits_cancel_rejected() { + let state = SubmitTrackingState::new(); + let order_id = 7108; + let client_order_id = ClientOrderId::from("O-CANCEL-REJECT"); + let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")); + let trader_id = TraderId::from("TRADER-CANCEL"); + let strategy_id = StrategyId::from("STRATEGY-CANCEL"); + let account_id = AccountId::from("IB-001"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + state.cache( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + ); + { + let mut tracker = state.0.lock().unwrap(); + let order = tracker.active_orders.get_mut(&order_id).unwrap(); + order.accepted = true; + order.pending_cancel = true; + order.perm_id = 9_108; + } + + let notice = Notice { + request_id: Some(order_id), + code: 161, + message: "Cancel attempted when order is not in a cancellable state".to_string(), + error_time: None, + advanced_order_reject_json: String::new(), + }; + InteractiveBrokersExecutionClient::handle_order_notice( + ¬ice, + &state.0, + &exec_sender.clone().into(), + UnixNanos::new(47), + account_id, + None, + ) + .unwrap(); + + match exec_receiver.try_recv().unwrap() { + ExecutionEvent::Order(OrderEventAny::CancelRejected(event)) => { + assert_eq!(event.trader_id, trader_id); + assert_eq!(event.strategy_id, strategy_id); + assert_eq!(event.instrument_id, instrument_id); + assert_eq!(event.client_order_id, client_order_id); + assert_eq!(event.venue_order_id, Some(VenueOrderId::new("PERM-9108"))); + assert_eq!(event.account_id, Some(account_id)); + assert_eq!( + event.reason.as_str(), + "Cancel attempted when order is not in a cancellable state" + ); + assert_eq!(event.ts_event, UnixNanos::new(47)); + } + event => panic!("Expected cancel rejected event, was {event:?}"), + } + assert!(exec_receiver.try_recv().is_err()); + + let tracker = state.0.lock().unwrap(); + let order = tracker.active_orders.get(&order_id).unwrap(); + assert!(order.accepted); + assert!(!order.pending_cancel); +} + +#[rstest] +fn order_notice_for_accepted_order_without_pending_modify_stays_non_terminal() { + let state = SubmitTrackingState::new(); + let order_id = 7107; + let client_order_id = ClientOrderId::from("O-ACCEPTED-NOTICE"); + let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")); + let trader_id = TraderId::from("TRADER-ACCEPTED"); + let strategy_id = StrategyId::from("STRATEGY-ACCEPTED"); + let account_id = AccountId::from("IB-001"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + state.cache( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + ); + { + let mut tracker = state.0.lock().unwrap(); + tracker.active_orders.get_mut(&order_id).unwrap().accepted = true; + } + + for code in [110, 201] { + let notice = Notice { + request_id: Some(order_id), + code, + message: format!("Venue notice {code}"), + error_time: None, + advanced_order_reject_json: String::new(), + }; + InteractiveBrokersExecutionClient::handle_order_notice( + ¬ice, + &state.0, + &exec_sender.clone().into(), + UnixNanos::new(46), + account_id, + None, + ) + .unwrap(); + } + + assert!(exec_receiver.try_recv().is_err()); + state.assert_active( + order_id, + client_order_id, + instrument_id, + trader_id, + strategy_id, + true, + ); +} + +#[rstest] +fn resolve_failed_order_list_predecessors_cancels_and_clears_tracking() { + let state = SubmitTrackingState::new(); + let first_id = 7301; + let second_id = 7302; + let first_client_id = ClientOrderId::from("O-PRED-FIRST"); + let second_client_id = ClientOrderId::from("O-PRED-SECOND"); + let strategy_id = StrategyId::from("STRATEGY-PRED-001"); + let account_id = AccountId::from("IB-001"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + + let first_order = create_test_limit_order(first_client_id); + let second_order = create_test_limit_order(second_client_id); + let mut ib_order_ids = AHashMap::new(); + ib_order_ids.insert(first_client_id, first_id); + ib_order_ids.insert(second_client_id, second_id); + for (order, order_id) in [(&first_order, first_id), (&second_order, second_id)] { + state.cache( + order_id, + order.client_order_id(), + order.instrument_id(), + order.trader_id(), + strategy_id, + ); + } + + InteractiveBrokersExecutionClient::resolve_failed_order_list_predecessors( + &[first_order, second_order], + &ib_order_ids, + &AHashSet::new(), + &state.0, + strategy_id, + account_id, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + ) + .unwrap(); + + for expected_client_id in [first_client_id, second_client_id] { + match exec_receiver.try_recv().unwrap() { + ExecutionEvent::Order(OrderEventAny::Canceled(event)) => { + assert_eq!(event.client_order_id, expected_client_id); + assert_eq!(event.strategy_id, strategy_id); + assert_eq!(event.account_id, Some(account_id)); + } + event => panic!("Expected canceled order event, was {event:?}"), + } + } + assert!(exec_receiver.try_recv().is_err()); + + let tracker = state.0.lock().unwrap(); + assert!(tracker.active_orders.is_empty()); + assert!(tracker.order_id_map.is_empty()); + assert!(tracker.venue_order_id_map.is_empty()); +} + +#[rstest] +fn resolve_failed_order_list_predecessors_skips_failed_cancels() { + let state = SubmitTrackingState::new(); + let first_id = 7311; + let second_id = 7312; + let first_client_id = ClientOrderId::from("O-PRED-PARKED"); + let second_client_id = ClientOrderId::from("O-PRED-CANCELLED"); + let strategy_id = StrategyId::from("STRATEGY-PRED-002"); + let account_id = AccountId::from("IB-001"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + + let first_order = create_test_limit_order(first_client_id); + let second_order = create_test_limit_order(second_client_id); + let mut ib_order_ids = AHashMap::new(); + ib_order_ids.insert(first_client_id, first_id); + ib_order_ids.insert(second_client_id, second_id); + for (order, order_id) in [(&first_order, first_id), (&second_order, second_id)] { + state.cache( + order_id, + order.client_order_id(), + order.instrument_id(), + order.trader_id(), + strategy_id, + ); + } + + let mut failed_cancels = AHashSet::new(); + failed_cancels.insert(first_client_id); + + InteractiveBrokersExecutionClient::resolve_failed_order_list_predecessors( + &[first_order, second_order], + &ib_order_ids, + &failed_cancels, + &state.0, + strategy_id, + account_id, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + ) + .unwrap(); + + match exec_receiver.try_recv().unwrap() { + ExecutionEvent::Order(OrderEventAny::Canceled(event)) => { + assert_eq!(event.client_order_id, second_client_id); + } + event => panic!("Expected canceled order event, was {event:?}"), + } + assert!(exec_receiver.try_recv().is_err()); + + let tracker = state.0.lock().unwrap(); + assert!(tracker.active_orders.contains_key(&first_id)); + assert_eq!(tracker.order_id_map.get(&first_client_id), Some(&first_id)); + assert!(!tracker.active_orders.contains_key(&second_id)); +} + +#[rstest] +fn absent_order_terminal_event_maps_completed_status() { + let client_order_id = ClientOrderId::from("O-ABSENT"); + let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")); + let tracked = create_tracked_order_context(client_order_id, instrument_id); + let account_id = AccountId::from("IB-001"); + let ts_init = UnixNanos::new(50); + + let canceled = InteractiveBrokersExecutionClient::absent_order_terminal_event( + Some((OrderStatusKind::Cancelled, 9_900, 0.0)), + None, + &tracked, + 7401, + account_id, + ts_init, + ); + + match canceled { + Some(OrderEventAny::Canceled(event)) => { + assert_eq!(event.client_order_id, client_order_id); + assert_eq!(event.venue_order_id, Some(VenueOrderId::new("PERM-9900"))); + assert_eq!(event.account_id, Some(account_id)); + assert_eq!(event.ts_event, ts_init); + } + event => panic!("Expected canceled event, was {event:?}"), + } + + let rejected = InteractiveBrokersExecutionClient::absent_order_terminal_event( + Some((OrderStatusKind::Inactive, 0, 0.0)), + None, + &tracked, + 7401, + account_id, + ts_init, + ); + + match rejected { + Some(OrderEventAny::Rejected(event)) => { + assert_eq!(event.client_order_id, client_order_id); + assert_eq!( + event.reason.as_str(), + "IB reports Inactive after a venue notice" + ); + } + event => panic!("Expected rejected event, was {event:?}"), + } + + let filled = InteractiveBrokersExecutionClient::absent_order_terminal_event( + Some((OrderStatusKind::Filled, 9_900, 1.0)), + None, + &tracked, + 7401, + account_id, + ts_init, + ); + assert!(filled.is_none()); + + let partially_filled_inactive = InteractiveBrokersExecutionClient::absent_order_terminal_event( + Some((OrderStatusKind::Inactive, 9_900, 1.0)), + None, + &tracked, + 7401, + account_id, + ts_init, + ); + + match partially_filled_inactive { + Some(OrderEventAny::Canceled(event)) => { + assert_eq!(event.client_order_id, client_order_id); + assert_eq!(event.venue_order_id, Some(VenueOrderId::new("PERM-9900"))); + assert_eq!(event.account_id, Some(account_id)); + assert_eq!(event.ts_event, ts_init); + } + event => panic!("Expected canceled event, was {event:?}"), + } + + let unknown = InteractiveBrokersExecutionClient::absent_order_terminal_event( + None, None, &tracked, 7401, account_id, ts_init, + ); + assert!(unknown.is_none()); + + let filled_before_acceptance = InteractiveBrokersExecutionClient::absent_order_terminal_event( + Some((OrderStatusKind::Filled, 9_900, 1.0)), + Some(Ustr::from("Order preset notice")), + &tracked, + 7401, + account_id, + ts_init, + ); + assert!(filled_before_acceptance.is_none()); + + let never_listed = InteractiveBrokersExecutionClient::absent_order_terminal_event( + None, + Some(Ustr::from("Price does not conform to the minimum tick")), + &tracked, + 7401, + account_id, + ts_init, + ); + + match never_listed { + Some(OrderEventAny::Rejected(event)) => { + assert_eq!(event.client_order_id, client_order_id); + assert_eq!(event.account_id, account_id); + assert_eq!( + event.reason.as_str(), + "Price does not conform to the minimum tick" + ); + assert_eq!(event.ts_event, ts_init); + } + event => panic!("Expected rejected event, was {event:?}"), + } +} + +#[rstest] +#[case(1.0, None, true)] +#[case(2.0, Some(101.0), false)] +#[tokio::test] +async fn open_order_refresh_clears_pending_modify_only_on_match( + #[case] refresh_quantity: f64, + #[case] refresh_limit: Option, + #[case] expect_pending_after: bool, +) { + let order_id = 7501; + let client_order_id = ClientOrderId::from("O-PENDING-MODIFY"); + let equity = equity_aapl(); + let instrument_id = equity.id(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let instrument_provider = create_test_instrument_provider(); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); + let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); + let position_tracker = create_position_tracker(); + + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), 12345, 1); + let mut tracked = create_tracked_order_context(client_order_id, instrument_id); + tracked.accepted = true; + tracked.pending_modify = Some(PendingModifyValues { + total_quantity: 2.0, + limit_price: Some(101.0), + aux_price: None, + trail_stop_price: None, + }); + insert_tracked_order(&orders, order_id, tracked); + + let mut open_order = create_test_open_order(order_id, "Submitted", client_order_id.as_str()); + open_order.order.total_quantity = refresh_quantity; + open_order.order.limit_price = refresh_limit; + open_order.order.order_type = "LMT".to_string(); + + InteractiveBrokersExecutionClient::handle_order_update( + &OrderUpdate::OpenOrder(open_order), + &orders, + &instrument_provider, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + AccountId::from("IB-001"), + Ustr::from("001"), + &commission_cache, + &pending_execution_cache, + &position_tracker, + None, + ) + .await + .unwrap(); + + while exec_receiver.try_recv().is_ok() {} + let state = orders.lock().unwrap(); + let order = state.active_orders.get(&order_id).unwrap(); + assert_eq!(order.pending_modify.is_some(), expect_pending_after); +} + +#[tokio::test] +async fn trailing_stop_modify_is_acknowledged_by_its_trail_stop_price() { + let order_id = 7503; + let client_order_id = ClientOrderId::from("O-TRAIL-MODIFY"); + let equity = equity_aapl(); + let instrument_id = equity.id(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let instrument_provider = create_test_instrument_provider(); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); + let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); + let position_tracker = create_position_tracker(); + + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), 12345, 1); + let mut tracked = create_tracked_order_context(client_order_id, instrument_id); + tracked.order_type = OrderType::TrailingStopMarket; + tracked.accepted = true; + tracked.last_update = Some((Quantity::from(2), None, Some(Price::from("96.00")))); + tracked.pending_modify = Some(PendingModifyValues { + total_quantity: 2.0, + limit_price: None, + aux_price: Some(1.5), + trail_stop_price: Some(95.0), + }); + insert_tracked_order(&orders, order_id, tracked); + + let mut open_order = create_test_open_order(order_id, "Submitted", client_order_id.as_str()); + open_order.order.order_type = "TRAIL".to_string(); + open_order.order.total_quantity = 2.0; + open_order.order.limit_price = None; + open_order.order.aux_price = Some(1.5); + open_order.order.trail_stop_price = Some(95.0); + + InteractiveBrokersExecutionClient::handle_order_update( + &OrderUpdate::OpenOrder(open_order), + &orders, + &instrument_provider, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + AccountId::from("IB-001"), + Ustr::from("001"), + &commission_cache, + &pending_execution_cache, + &position_tracker, + None, + ) + .await + .unwrap(); + + let mut updates = Vec::new(); + + while let Ok(event) = exec_receiver.try_recv() { + if let ExecutionEvent::Order(OrderEventAny::Updated(event)) = event { + updates.push((event.quantity, event.price, event.trigger_price)); + } + } + + assert_eq!( + updates, + vec![(Quantity::from(2), None, Some(Price::from("95.00")))] + ); + let state = orders.lock().unwrap(); + assert!(state.active_orders[&order_id].pending_modify.is_none()); +} + +#[rstest] +#[case::tracked(false, vec![Price::from("100.00"), Price::from("101.00")])] +#[case::recovered(true, vec![Price::from("101.00")])] +#[tokio::test] +async fn open_order_refresh_emits_order_updated_only_on_change( + #[case] recovered: bool, + #[case] expected_prices: Vec, +) { + let order_id = 7502; + let client_order_id = ClientOrderId::from("O-OPEN-REFRESH"); + let equity = equity_aapl(); + let instrument_id = equity.id(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let instrument_provider = create_test_instrument_provider(); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); + let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); + let position_tracker = create_position_tracker(); + + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), 12345, 1); + + if recovered { + let cached = OrderTestBuilder::new(OrderType::Limit) + .instrument_id(instrument_id) + .client_order_id(client_order_id) + .side(OrderSide::Buy) + .price(Price::from("100.00")) + .quantity(Quantity::from(2)) + .submit(true) + .build(); + InteractiveBrokersExecutionClient::cache_recovered_order_tracking( + order_id, &cached, &orders, + ) + .unwrap(); + } else { + let mut tracked = create_tracked_order_context(client_order_id, instrument_id); + tracked.accepted = true; + insert_tracked_order(&orders, order_id, tracked); + } + + let mut updates = Vec::new(); + + for limit_price in [100.0, 100.0, 101.0] { + let mut open_order = + create_test_open_order(order_id, "PreSubmitted", client_order_id.as_str()); + open_order.order.total_quantity = 2.0; + open_order.order.limit_price = Some(limit_price); + open_order.order.order_type = "LMT".to_string(); + + InteractiveBrokersExecutionClient::handle_order_update( + &OrderUpdate::OpenOrder(open_order), + &orders, + &instrument_provider, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + AccountId::from("IB-001"), + Ustr::from("001"), + &commission_cache, + &pending_execution_cache, + &position_tracker, + None, + ) + .await + .unwrap(); + + while let Ok(event) = exec_receiver.try_recv() { + if let ExecutionEvent::Order(OrderEventAny::Updated(event)) = event { + updates.push((event.client_order_id, event.quantity, event.price)); + } + } + } + + let expected: Vec<_> = expected_prices + .into_iter() + .map(|price| (client_order_id, Quantity::from(2), Some(price))) + .collect(); + assert_eq!(updates, expected); +} + +#[rstest] +#[case::limit("LMT", Some(Price::from("101.00")), None, Some(101.0), Some(1.5), None)] +#[case::stop("STP", None, Some(Price::from("95.00")), None, Some(95.0), None)] +#[case::trailing("TRAIL", None, Some(Price::from("95.00")), None, Some(2.5), Some(95.0))] +fn modify_changes_only_requested_fields_of_the_open_order( + #[case] order_type: &str, + #[case] price: Option, + #[case] trigger_price: Option, + #[case] expected_limit: Option, + #[case] expected_aux: Option, + #[case] expected_trail_stop: Option, +) { + let equity = equity_aapl(); + let instrument_id = equity.id(); + let instrument_provider = create_test_instrument_provider(); + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), 12345, 1); + let original = IBOrder { + order_type: order_type.to_string(), + total_quantity: 1.0, + limit_price: None, + aux_price: Some(1.5), + tif: ibapi::orders::TimeInForce::GoodTillCanceled, + good_after_time: "20260924 12:31:13 UTC".to_string(), + oca_group: "PROTECT-1".to_string(), + oca_type: ibapi::orders::OcaType::ReduceWithBlock, + outside_rth: true, + order_ref: "O-RESTORED-1".to_string(), + account: "DU123".to_string(), + ..Default::default() + }; + let mut params = Params::new(); + params.insert( + MODIFY_TRAILING_OFFSET_PARAM.to_string(), + serde_json::json!(2.5), + ); + let cmd = ModifyOrder::new( + TraderId::from("TRADER-001"), + Some(ClientId::from("CLIENT-001")), + StrategyId::from("STRATEGY-001"), + instrument_id, + ClientOrderId::from("O-RESTORED-1"), + None, + Some(Quantity::from(2)), + price, + trigger_price, + UUID4::new(), + UnixNanos::default(), + Some(params), + None, + ); + let mut modified = original.clone(); + + InteractiveBrokersExecutionClient::apply_modify_fields_to_ib_order( + &cmd, + &mut modified, + &instrument_provider, + ) + .unwrap(); + + let expected = IBOrder { + total_quantity: 2.0, + limit_price: expected_limit, + aux_price: expected_aux, + trail_stop_price: expected_trail_stop, + ..original + }; + assert_eq!(modified, expected); +} + +#[rstest] +#[case::quantity_modify_ignores_moved_trigger(None, Some(94.0), true)] +#[case::requested_trigger_matches(Some(95.0), Some(95.0), true)] +#[case::requested_trigger_differs(Some(95.0), Some(94.0), false)] +#[case::requested_trigger_missing(Some(95.0), None, false)] +fn pending_modify_checks_trailing_trigger_only_when_requested( + #[case] requested: Option, + #[case] reported: Option, + #[case] expected: bool, +) { + let pending = PendingModifyValues { + total_quantity: 2.0, + limit_price: None, + aux_price: Some(1.5), + trail_stop_price: requested, + }; + let order = IBOrder { + order_type: "TRAIL".to_string(), + total_quantity: 2.0, + limit_price: None, + aux_price: Some(1.5), + trail_stop_price: reported, + ..Default::default() + }; + + assert_eq!(pending.matches(&order), expected); +} + +#[rstest] +fn pending_modify_is_marked_once_for_matching_tracked_order() { + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let order_id = 7120; + let client_order_id = ClientOrderId::from("O-MODIFY-ATOMIC"); + let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")); + insert_tracked_order( + &orders, + order_id, + create_tracked_order_context(client_order_id, instrument_id), + ); + let cmd = ModifyOrder::new( + TraderId::from("TRADER-001"), + Some(ClientId::from("CLIENT-001")), + StrategyId::from("STRATEGY-001"), + instrument_id, + client_order_id, + Some(VenueOrderId::from(order_id.to_string())), + Some(Quantity::from(2)), + Some(Price::from("101.00")), + None, + UUID4::new(), + UnixNanos::default(), + None, + None, + ); + let ib_order = IBOrder { + total_quantity: 2.0, + limit_price: Some(101.0), + ..Default::default() + }; + + InteractiveBrokersExecutionClient::mark_pending_modify(&cmd, order_id, &orders, &ib_order) + .unwrap(); + + assert_eq!( + orders + .lock() + .unwrap() + .order(order_id) + .unwrap() + .pending_modify, + Some(PendingModifyValues { + total_quantity: 2.0, + limit_price: Some(101.0), + aux_price: None, + trail_stop_price: None, + }), + ); + assert_eq!( + InteractiveBrokersExecutionClient::mark_pending_modify(&cmd, order_id, &orders, &ib_order,) + .unwrap_err() + .to_string(), + "IB order 7120 already has a pending modify", + ); +} + +// Replies to `reqExecutions` reach the update stream with the request's positive ID +#[rstest] +#[case(-1, None)] +#[case(0, None)] +#[case(9001, Some((false, Decimal::ZERO)))] +#[tokio::test] +async fn execution_data_records_own_fill_before_commission_report( + #[case] request_id: i32, + #[case] expected_external_change: Option<(bool, Decimal)>, +) { + let order_id = 7401; + let client_order_id = ClientOrderId::from("O-OWN-FILL-EAGER"); + let equity = equity_aapl(); + let instrument_id = equity.id(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let instrument_provider = create_test_instrument_provider(); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); + let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); + let position_tracker = create_position_tracker(); + + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), 12345, 1); + insert_tracked_order( + &orders, + order_id, + create_tracked_order_context(client_order_id, instrument_id), + ); + + let mut exec_data = create_test_execution_data(order_id, "EXEC-EAGER-1", 2.0, 100.0, "BOT"); + exec_data.request_id = request_id; + let contract_id = exec_data.contract.contract_id; + InteractiveBrokersExecutionClient::handle_order_update( + &OrderUpdate::ExecutionData(exec_data), + &orders, + &instrument_provider, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + AccountId::from("IB-001"), + Ustr::from("001"), + &commission_cache, + &pending_execution_cache, + &position_tracker, + None, + ) + .await + .unwrap(); + + // No commission yet: the fill is buffered and no event is emitted, but the + // position stream must already observe the post-fill quantity as its own. + assert!(exec_receiver.try_recv().is_err()); + assert_eq!( + check_external_position_change(&position_tracker, contract_id, Decimal::from(2)).await, + expected_external_change + ); +} + +#[rstest] +fn list_submit_definitive_partial_failure_preserves_prefix_and_omits_tail() { + let state = SubmitTrackingState::new(); + let prior_id = 7201; + let current_id = 7202; + let tail_id = 7203; + let prior_client_id = ClientOrderId::from("O-LIST-PRIOR"); + let current_client_id = ClientOrderId::from("O-LIST-NOT-SENT"); + let tail_client_id = ClientOrderId::from("O-LIST-TAIL"); + let prior_instrument_id = InstrumentId::new(Symbol::from("AMD"), Venue::from("SMART")); + let current_instrument_id = InstrumentId::new(Symbol::from("INTC"), Venue::from("SMART")); + let trader_id = TraderId::from("TRADER-LIST-001"); + let strategy_id = StrategyId::from("STRATEGY-LIST-001"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + state.cache( + prior_id, + prior_client_id, + prior_instrument_id, + trader_id, + strategy_id, + ); + state.cache( + current_id, + current_client_id, + current_instrument_id, + trader_id, + strategy_id, + ); + assert!(state.emit_accepted( + prior_id, + AccountId::from("IB-LIST-001"), + &exec_sender.clone().into() + )); + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Accepted(event)) + if event.client_order_id == prior_client_id + && event.venue_order_id == VenueOrderId::from(prior_id.to_string()) + )); + + let error = ibapi::Error::ServerVersion(170, 169, "order feature".to_string()); + let result = InteractiveBrokersExecutionClient::handle_order_submit_failure( + &error, + "Failed to submit order from list", + current_id, + AccountId::from("IB-LIST-001"), + UnixNanos::new(31), + &state.0, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + ); + + assert_eq!( + result.unwrap_err().to_string(), + format!("Failed to submit order from list: {error}") + ); + state.assert_active( + prior_id, + prior_client_id, + prior_instrument_id, + trader_id, + strategy_id, + true, + ); + state.assert_absent(current_id, current_client_id); + state.assert_absent(tail_id, tail_client_id); + + match exec_receiver.try_recv().unwrap() { + ExecutionEvent::Order(OrderEventAny::Rejected(event)) => { + assert_eq!(event.client_order_id, current_client_id); + assert_eq!(event.instrument_id, current_instrument_id); + assert_eq!(event.ts_event, UnixNanos::new(31)); + } + event => panic!("Expected rejected order event, was {event:?}"), + } + assert!(exec_receiver.try_recv().is_err()); +} + +#[rstest] +fn list_submit_failure_denies_every_unsubmitted_tail_order() { + let first_id = ClientOrderId::from("O-LIST-TAIL-001"); + let second_id = ClientOrderId::from("O-LIST-TAIL-002"); + let orders = vec![ + create_test_limit_order(first_id), + create_test_limit_order(second_id), + ]; + let strategy_id = StrategyId::from("STRATEGY-LIST-001"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + + InteractiveBrokersExecutionClient::deny_unsubmitted_order_list( + &orders, + DENIAL_ORDER_LIST_SIBLING_SUBMIT_FAILED, + strategy_id, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + ) + .unwrap(); + + for expected_id in [first_id, second_id] { + match exec_receiver.try_recv().unwrap() { + ExecutionEvent::Order(OrderEventAny::Denied(event)) => { + assert_eq!(event.client_order_id, expected_id); + assert_eq!(event.strategy_id, strategy_id); + assert_eq!(event.reason.as_str(), "ORDER_LIST_SIBLING_SUBMIT_FAILED"); + } + event => panic!("Expected denied order event, was {event:?}"), + } + } + assert!(exec_receiver.try_recv().is_err()); +} + +#[rstest] +fn prepare_order_list_fails_before_submission_on_an_untransformable_child() { + let equity = equity_aapl(); + let instrument_id = equity.id(); + let instrument_provider = create_test_instrument_provider(); + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), 12345, 1); + let entry = OrderTestBuilder::new(OrderType::Limit) + .instrument_id(instrument_id) + .client_order_id(ClientOrderId::from("O-LIST-ENTRY")) + .side(OrderSide::Buy) + .price(Price::from("100.00")) + .quantity(Quantity::from(1)) + .build(); + let stop = OrderTestBuilder::new(OrderType::TrailingStopMarket) + .instrument_id(instrument_id) + .client_order_id(ClientOrderId::from("O-LIST-STOP")) + .parent_order_id(entry.client_order_id()) + .side(OrderSide::Sell) + .quantity(Quantity::from(1)) + .trigger_price(Price::from("95.00")) + .trailing_offset(Decimal::from(5)) + .trailing_offset_type(TrailingOffsetType::Ticks) + .build(); + let orders = vec![entry.clone(), stop.clone()]; + let cmd = SubmitOrderList::new( + TraderId::from("TRADER-001"), + Some(*IB_CLIENT_ID), + entry.strategy_id(), + OrderList::new( + OrderListId::from("OL-IB-UNTRANSFORMABLE"), + instrument_id, + entry.strategy_id(), + vec![entry.client_order_id(), stop.client_order_id()], + UnixNanos::default(), + ), + vec![ + OrderInitialized::from(&entry), + OrderInitialized::from(&stop), + ], + None, + None, + None, + UUID4::new(), + UnixNanos::default(), + None, + ); + let ib_order_ids = AHashMap::from([ + (entry.client_order_id(), 7401), + (stop.client_order_id(), 7402), + ]); + + let error = InteractiveBrokersExecutionClient::prepare_order_list( + &cmd, + &orders, + &ib_order_ids, + &OrderTracker::new(NATIVE_CLIENT_ID), + &instrument_provider, + Ustr::from("DU001"), + ) + .expect_err("an untransformable child fails preparation"); + + assert_eq!( + format!("{error:#}"), + "Failed to transform order: `TrailingOffsetType` Ticks is not supported" + ); +} + +#[tokio::test] +async fn list_submit_ambiguous_partial_failure_retains_attempted_children_only() { + let state = SubmitTrackingState::new(); + let prior_id = 7301; + let current_id = 7302; + let tail_id = 7303; + let prior_client_id = ClientOrderId::from("O-LIST-AMBIGUOUS-PRIOR"); + let current_client_id = ClientOrderId::from("O-LIST-AMBIGUOUS-CURRENT"); + let tail_client_id = ClientOrderId::from("O-LIST-AMBIGUOUS-TAIL"); + let prior_instrument_id = InstrumentId::new(Symbol::from("META"), Venue::from("SMART")); + let current_instrument_id = InstrumentId::new(Symbol::from("GOOG"), Venue::from("SMART")); + let trader_id = TraderId::from("TRADER-LIST-002"); + let strategy_id = StrategyId::from("STRATEGY-LIST-002"); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + state.cache( + prior_id, + prior_client_id, + prior_instrument_id, + trader_id, + strategy_id, + ); + state.cache( + current_id, + current_client_id, + current_instrument_id, + trader_id, + strategy_id, + ); + assert!(state.emit_accepted( + prior_id, + AccountId::from("IB-001"), + &exec_sender.clone().into() + )); + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Accepted(event)) + if event.client_order_id == prior_client_id + && event.venue_order_id == VenueOrderId::from(prior_id.to_string()) + )); + + let result = InteractiveBrokersExecutionClient::handle_order_submit_failure( + &ibapi::Error::Io(std::io::Error::new( + std::io::ErrorKind::BrokenPipe, + "socket closed after partial write", + )), + "Failed to submit order from list", + current_id, + AccountId::from("IB-001"), + UnixNanos::new(37), + &state.0, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + ); + + assert_eq!( + result.unwrap_err().to_string(), + "Failed to submit order from list; outcome is unknown after possible transmission: socket closed after partial write" + ); + assert!(exec_receiver.try_recv().is_err()); + state.assert_active( + prior_id, + prior_client_id, + prior_instrument_id, + trader_id, + strategy_id, + true, + ); + state.assert_active( + current_id, + current_client_id, + current_instrument_id, + trader_id, + strategy_id, + false, + ); + state.assert_absent(tail_id, tail_client_id); + + process_submitted_status(current_id, &state, &exec_sender.clone().into()).await; + + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Accepted(event)) + if event.client_order_id == current_client_id + && event.venue_order_id == VenueOrderId::from(current_id.to_string()) + )); + assert!(exec_receiver.try_recv().is_err()); + state.assert_active( + prior_id, + prior_client_id, + prior_instrument_id, + trader_id, + strategy_id, + true, + ); + state.assert_active( + current_id, + current_client_id, + current_instrument_id, + trader_id, + strategy_id, + true, + ); + state.assert_absent(tail_id, tail_client_id); +} + +#[rstest] +#[case::price(OrderType::TrailingStopMarket, Some(TrailingOffsetType::Price), None)] +#[case::basis_points( + OrderType::TrailingStopLimit, + Some(TrailingOffsetType::BasisPoints), + None +)] +#[case::unset(OrderType::TrailingStopMarket, None, None)] +#[case::not_trailing(OrderType::Limit, Some(TrailingOffsetType::Ticks), None)] +#[case::ticks( + OrderType::TrailingStopMarket, + Some(TrailingOffsetType::Ticks), + Some( + "UNSUPPORTED_TRAILING_OFFSET_TYPE: `TrailingOffsetType` Ticks is not supported (only PRICE and BASIS_POINTS are supported)" + ) +)] +fn unsupported_trailing_offset_reason_allows_price_and_basis_points( + #[case] order_type: OrderType, + #[case] trailing_offset_type: Option, + #[case] expected: Option<&str>, +) { + let reason = InteractiveBrokersExecutionClient::unsupported_trailing_offset_reason( + order_type, + trailing_offset_type, + ); + + assert_eq!(reason.as_deref(), expected); +} + +#[rstest] +fn submit_order_denies_reduce_only() { + let (client, mut rx, cache) = create_test_execution_client(); + let order = OrderTestBuilder::new(OrderType::Limit) + .instrument_id(create_test_stock_instrument()) + .client_order_id(ClientOrderId::from("O-IB-REDUCE-ONLY")) + .side(OrderSide::Sell) + .price(Price::from("100.00")) + .quantity(Quantity::from(1)) + .reduce_only(true) + .submit(true) + .build(); + cache + .borrow_mut() + .add_order(order.clone(), None, Some(*IB_CLIENT_ID), false) + .unwrap(); + let cmd = SubmitOrder::from_order( + &order, + client.core.trader_id, + Some(client.core.client_id), + None, + UUID4::new(), + UnixNanos::default(), + ); + + client.submit_order(cmd).unwrap(); + + match next_order_event(&mut rx) { + OrderEventAny::Denied(event) => { + assert_eq!(event.client_order_id, order.client_order_id()); + assert_eq!(event.reason, "UNSUPPORTED_REDUCE_ONLY"); + } + event => panic!("Expected OrderDenied, was {event:?}"), + } + assert!(rx.try_recv().is_err()); +} + +#[rstest] +fn submit_order_denies_when_client_not_ready() { + let (client, mut rx, cache) = create_test_execution_client(); + let order = create_test_limit_order(ClientOrderId::from("O-IB-001")); + cache + .borrow_mut() + .add_order(order.clone(), None, Some(*IB_CLIENT_ID), false) + .unwrap(); + let cmd = SubmitOrder::from_order( + &order, + client.core.trader_id, + Some(client.core.client_id), + None, + UUID4::new(), + UnixNanos::default(), + ); + + client.submit_order(cmd).unwrap(); + + match next_order_event(&mut rx) { + OrderEventAny::Denied(event) => { + assert_eq!(event.client_order_id, order.client_order_id()); + assert_eq!( + event.reason.to_string(), + "IB_CLIENT_NOT_READY: Interactive Brokers client is not ready; refusing to submit order" + ); + } + event => panic!("Expected OrderDenied, was {event:?}"), + } +} + +#[rstest] +fn submit_order_list_denies_all_orders_when_reduce_only_is_present() { + let (client, mut rx, cache) = create_test_execution_client(); + let reduce_only = OrderTestBuilder::new(OrderType::Limit) + .instrument_id(create_test_stock_instrument()) + .client_order_id(ClientOrderId::from("O-IB-REDUCE-ONLY")) + .side(OrderSide::Sell) + .price(Price::from("100.00")) + .quantity(Quantity::from(1)) + .reduce_only(true) + .submit(true) + .build(); + let regular = create_test_limit_order(ClientOrderId::from("O-IB-REGULAR")); + for order in [&reduce_only, ®ular] { + cache + .borrow_mut() + .add_order(order.clone(), None, Some(*IB_CLIENT_ID), false) + .unwrap(); + } + let order_list = OrderList::new( + OrderListId::from("OL-IB-REDUCE-ONLY"), + reduce_only.instrument_id(), + reduce_only.strategy_id(), + vec![reduce_only.client_order_id(), regular.client_order_id()], + UnixNanos::default(), + ); + let cmd = SubmitOrderList::new( + client.core.trader_id, + Some(client.core.client_id), + reduce_only.strategy_id(), + order_list, + vec![ + OrderInitialized::from(&reduce_only), + OrderInitialized::from(®ular), + ], + None, + None, + None, + UUID4::new(), + UnixNanos::default(), + None, + ); + + client.submit_order_list(cmd).unwrap(); + + for client_order_id in [reduce_only.client_order_id(), regular.client_order_id()] { + match next_order_event(&mut rx) { + OrderEventAny::Denied(event) => { + assert_eq!(event.client_order_id, client_order_id); + assert_eq!(event.reason, "UNSUPPORTED_REDUCE_ONLY"); + } + event => panic!("Expected OrderDenied, was {event:?}"), + } + } + assert!(rx.try_recv().is_err()); +} + +#[rstest] +fn submit_order_list_denies_all_orders_when_client_not_ready() { + let (client, mut rx, cache) = create_test_execution_client(); + let order1 = create_test_limit_order(ClientOrderId::from("O-IB-001")); + let order2 = create_test_limit_order(ClientOrderId::from("O-IB-002")); + for order in [&order1, &order2] { + cache + .borrow_mut() + .add_order(order.clone(), None, Some(*IB_CLIENT_ID), false) + .unwrap(); + } + let order_list = OrderList::new( + OrderListId::from("OL-IB-001"), + order1.instrument_id(), + order1.strategy_id(), + vec![order1.client_order_id(), order2.client_order_id()], + UnixNanos::default(), + ); + let cmd = SubmitOrderList::new( + client.core.trader_id, + Some(client.core.client_id), + order1.strategy_id(), + order_list, + vec![ + OrderInitialized::from(&order1), + OrderInitialized::from(&order2), + ], + None, + None, + None, + UUID4::new(), + UnixNanos::default(), + None, + ); + + client.submit_order_list(cmd).unwrap(); + + for expected_client_order_id in [order1.client_order_id(), order2.client_order_id()] { + match next_order_event(&mut rx) { + OrderEventAny::Denied(event) => { + assert_eq!(event.client_order_id, expected_client_order_id); + assert_eq!( + event.reason.to_string(), + "IB_CLIENT_NOT_READY: Interactive Brokers client is not ready; refusing to submit order list" + ); + } + event => panic!("Expected OrderDenied, was {event:?}"), + } + } +} + +#[rstest] +fn modify_order_rejects_when_client_not_ready() { + let (client, mut rx, _) = create_test_execution_client(); + let order = create_test_limit_order(ClientOrderId::from("O-IB-001")); + let cmd = ModifyOrder::new( + client.core.trader_id, + Some(client.core.client_id), + order.strategy_id(), + order.instrument_id(), + order.client_order_id(), + Some(VenueOrderId::from("1001")), + Some(Quantity::from(2)), + Some(Price::from("101.00")), + None, + UUID4::new(), + UnixNanos::default(), + None, + None, + ); + + client.modify_order(cmd).unwrap(); + + match next_order_event(&mut rx) { + OrderEventAny::ModifyRejected(event) => { + assert_eq!(event.trader_id, client.core.trader_id); + assert_eq!(event.client_order_id, order.client_order_id()); + assert_eq!(event.instrument_id, order.instrument_id()); + assert_eq!(event.strategy_id, order.strategy_id()); + assert_eq!(event.venue_order_id, Some(VenueOrderId::from("1001"))); + assert_eq!(event.account_id, Some(client.core.account_id)); + assert_eq!(event.ts_init, event.ts_event); + assert!(!event.reconciliation); + assert_eq!(event.causation_id, None); + assert_eq!( + event.reason.to_string(), + "Interactive Brokers client is not ready; refusing to modify order" + ); + } + event => panic!("Expected OrderModifyRejected, was {event:?}"), + } + assert!(rx.try_recv().is_err()); +} + +#[rstest] +fn cancel_order_rejects_when_client_not_ready() { + let (client, mut rx, cache) = create_test_execution_client(); + let order = create_test_limit_order(ClientOrderId::from("O-IB-001")); + let accepted = OrderEventAny::Accepted(OrderAccepted::new( + order.trader_id(), + order.strategy_id(), + order.instrument_id(), + order.client_order_id(), + VenueOrderId::from("1001"), + client.core.account_id, + UUID4::new(), + UnixNanos::default(), + UnixNanos::default(), + false, + )); + { + let mut cache = cache.borrow_mut(); + cache + .add_order(order.clone(), None, Some(client.core.client_id), false) + .unwrap(); + cache.update_order(&accepted).unwrap(); + } + let cmd = CancelOrder::new( + client.core.trader_id, + Some(client.core.client_id), + order.strategy_id(), + order.instrument_id(), + order.client_order_id(), + Some(VenueOrderId::from("1001")), + UUID4::new(), + UnixNanos::default(), + None, + None, + ); + + client.cancel_order(cmd).unwrap(); + + match next_order_event(&mut rx) { + OrderEventAny::CancelRejected(event) => { + assert_eq!(event.trader_id, order.trader_id()); + assert_eq!(event.client_order_id, order.client_order_id()); + assert_eq!(event.instrument_id, order.instrument_id()); + assert_eq!(event.strategy_id, order.strategy_id()); + assert_eq!(event.venue_order_id, Some(VenueOrderId::from("1001"))); + assert_eq!(event.account_id, Some(client.core.account_id)); + assert_eq!(event.ts_init, event.ts_event); + assert!(!event.reconciliation); + assert_eq!(event.causation_id, None); + assert_eq!( + event.reason.to_string(), + "Interactive Brokers client is not ready; refusing to cancel order" + ); + } + event => panic!("Expected OrderCancelRejected, was {event:?}"), + } + assert!(rx.try_recv().is_err()); +} + +#[rstest] +fn cancel_all_orders_emits_no_events_when_client_not_ready() { + let (client, mut rx, cache) = create_test_execution_client(); + let order = create_test_limit_order(ClientOrderId::from("O-IB-001")); + let accepted = OrderEventAny::Accepted(OrderAccepted::new( + order.trader_id(), + order.strategy_id(), + order.instrument_id(), + order.client_order_id(), + VenueOrderId::from("1001"), + client.core.account_id, + UUID4::new(), + UnixNanos::default(), + UnixNanos::default(), + false, + )); + { + let mut cache = cache.borrow_mut(); + cache + .add_order(order.clone(), None, Some(client.core.client_id), false) + .unwrap(); + cache.update_order(&accepted).unwrap(); + } + let cmd = CancelAllOrders::new( + client.core.trader_id, + Some(client.core.client_id), + order.strategy_id(), + order.instrument_id(), + None, + UUID4::new(), + UnixNanos::default(), + None, + None, + ); + + client.cancel_all_orders(cmd).unwrap(); + + // A whole-request local failure must not become one rejection per order + assert!(rx.try_recv().is_err(), "expected no events"); +} + +fn add_accepted_cache_order( + cache: &Rc>, + client_order_id: &str, + instrument_id: InstrumentId, + side: OrderSide, + strategy_id: &str, + venue_order_id: &str, + account_id: AccountId, +) { + let order = OrderTestBuilder::new(OrderType::Limit) + .instrument_id(instrument_id) + .client_order_id(ClientOrderId::from(client_order_id)) + .strategy_id(StrategyId::from(strategy_id)) + .side(side) + .price(Price::from("100.00")) + .quantity(Quantity::from(1)) + .submit(true) + .build(); + let accepted = OrderEventAny::Accepted(OrderAccepted::new( + order.trader_id(), + order.strategy_id(), + order.instrument_id(), + order.client_order_id(), + VenueOrderId::from(venue_order_id), + account_id, + UUID4::new(), + UnixNanos::default(), + UnixNanos::default(), + false, + )); + let mut cache = cache.borrow_mut(); + cache + .add_order(order, None, Some(*IB_CLIENT_ID), false) + .unwrap(); + cache.update_order(&accepted).unwrap(); +} + +fn insert_side_tracked_order( + client: &InteractiveBrokersExecutionClient, + order_id: i32, + client_order_id: &str, + instrument_id: InstrumentId, + side: OrderSide, + strategy_id: &str, + perm_id: i64, +) { + let mut order = + create_tracked_order_context(ClientOrderId::from(client_order_id), instrument_id); + order.order_side = side; + order.strategy_id = StrategyId::from(strategy_id); + order.accepted = true; + order.perm_id = perm_id; + insert_tracked_order(&client.orders, order_id, order); +} + +fn record_side_group( + client: &InteractiveBrokersExecutionClient, + client_order_id: &str, + order_id: i32, + perm_id: i64, + side: OrderSide, + account_id: AccountId, +) { + let mut parent = create_tracked_order_context( + ClientOrderId::from(client_order_id), + create_test_stock_instrument(), + ); + parent.order_side = side; + parent.perm_id = perm_id; + let mut sibling = create_test_open_order(order_id + 1, "Submitted", client_order_id); + sibling.order.perm_id = perm_id + 1; + client.orders.lock().unwrap().record_incarnation( + order_id, + &parent, + account_id, + perm_id + 1, + Some(&sibling), + false, + ); +} + +#[rstest] +#[case::unsided( + None, + vec![ + ("O-CACHE-BUY", Some("1001")), + ("O-CACHE-SELL", Some("1002")), + ("O-GROUP-SELL", None), + ("O-TRACK-BUY", Some("21")), + ("O-TRACK-SELL", Some("PERM-222")), + ], +)] +#[case::buy( + Some(OrderSide::Buy), + vec![("O-CACHE-BUY", Some("1001")), ("O-TRACK-BUY", Some("21"))], +)] +#[case::sell( + Some(OrderSide::Sell), + vec![ + ("O-CACHE-SELL", Some("1002")), + ("O-GROUP-SELL", None), + ("O-TRACK-SELL", Some("PERM-222")), + ], +)] +fn cancel_all_targets_select_requested_side_across_sources( + #[case] order_side: Option, + #[case] expected: Vec<(&str, Option<&str>)>, +) { + let (client, _rx, cache) = create_test_execution_client(); + let account_id = client.core.account_id; + let aapl = create_test_stock_instrument(); + let msft = InstrumentId::from("MSFT.SMART"); + add_accepted_cache_order( + &cache, + "O-CACHE-BUY", + aapl, + OrderSide::Buy, + "S-001", + "1001", + account_id, + ); + add_accepted_cache_order( + &cache, + "O-CACHE-SELL", + aapl, + OrderSide::Sell, + "S-002", + "1002", + account_id, + ); + add_accepted_cache_order( + &cache, + "O-CACHE-MSFT", + msft, + OrderSide::Sell, + "S-002", + "1003", + account_id, + ); + add_accepted_cache_order( + &cache, + "O-CACHE-OTHER-ACCOUNT", + aapl, + OrderSide::Sell, + "S-002", + "1004", + AccountId::from("IB-002"), + ); + // Tracked but not cached: the tracker is the fallback selection path + insert_side_tracked_order(&client, 11, "O-CACHE-BUY", aapl, OrderSide::Buy, "S-001", 0); + insert_side_tracked_order(&client, 21, "O-TRACK-BUY", aapl, OrderSide::Buy, "S-003", 0); + insert_side_tracked_order( + &client, + 22, + "O-TRACK-SELL", + aapl, + OrderSide::Sell, + "S-004", + 222, + ); + insert_side_tracked_order( + &client, + 23, + "O-TRACK-MSFT", + msft, + OrderSide::Buy, + "S-003", + 0, + ); + record_side_group( + &client, + "O-GROUP-SELL", + 31, + 301, + OrderSide::Sell, + account_id, + ); + record_side_group( + &client, + "O-GROUP-OTHER-ACCOUNT", + 41, + 401, + OrderSide::Sell, + AccountId::from("IB-002"), + ); + let cmd = CancelAllOrders::new( + client.core.trader_id, + Some(client.core.client_id), + StrategyId::from("S-001"), + aapl, + order_side, + UUID4::new(), + UnixNanos::default(), + None, + None, + ); + + let targets = client.cancel_all_targets(&cmd).unwrap(); + + let expected: Vec<_> = expected + .into_iter() + .map(|(id, venue_id)| (ClientOrderId::from(id), venue_id.map(VenueOrderId::from))) + .collect(); + assert_eq!(targets, expected); +} + +#[rstest] +fn cancel_all_targets_sided_request_without_matches_selects_nothing() { + let (client, _rx, cache) = create_test_execution_client(); + let account_id = client.core.account_id; + let aapl = create_test_stock_instrument(); + add_accepted_cache_order( + &cache, + "O-CACHE-BUY", + aapl, + OrderSide::Buy, + "S-001", + "1001", + account_id, + ); + insert_side_tracked_order(&client, 21, "O-TRACK-BUY", aapl, OrderSide::Buy, "S-002", 0); + record_side_group(&client, "O-GROUP-BUY", 31, 301, OrderSide::Buy, account_id); + let cmd = CancelAllOrders::new( + client.core.trader_id, + Some(client.core.client_id), + StrategyId::from("S-001"), + aapl, + Some(OrderSide::Sell), + UUID4::new(), + UnixNanos::default(), + None, + None, + ); + + let targets = client.cancel_all_targets(&cmd).unwrap(); + + assert_eq!(targets, vec![]); +} + +fn create_test_execution_data( + order_id: i32, + execution_id: &str, + shares: f64, + price: f64, + side: &str, +) -> ExecutionData { + let contract = Contract { + contract_id: 12345, + symbol: IBSymbol::from("SPY"), + security_type: SecurityType::Option, + last_trade_date_or_contract_month: String::from("20250101"), + strike: 400.0, + right: Some(OptionRight::Call), + multiplier: String::from("100"), + exchange: Exchange::from("SMART"), + currency: IBCurrency::from("USD"), + local_symbol: String::from("SPY C400"), + trading_class: String::new(), + combo_legs: vec![], + ..Default::default() + }; + + let execution = Execution { + execution_id: execution_id.to_string(), + order_id, + time: String::from("20250101 08:00:00"), + side: if side == "BOT" { + ExecutionSide::Bought + } else { + ExecutionSide::Sold + }, + shares, + price, + perm_id: 0, + client_id: NATIVE_CLIENT_ID, + liquidation: 0, + account_number: String::from("001"), + exchange: String::new(), + cumulative_quantity: shares, + average_price: price, + order_reference: String::new(), + ev_rule: String::new(), + ev_multiplier: None, + model_code: String::new(), + last_liquidity: Liquidity::None, + pending_price_revision: false, + submitter: String::new(), + }; + + ExecutionData { + request_id: 0, + contract, + execution, + } +} + +fn create_test_stock_execution_data( + contract_id: i32, + order_id: i32, + execution_id: &str, +) -> ExecutionData { + let contract = Contract { + contract_id, + symbol: IBSymbol::from("AAPL"), + security_type: SecurityType::Stock, + exchange: Exchange::from("SMART"), + currency: IBCurrency::from("USD"), + ..Default::default() + }; + + let execution = Execution { + execution_id: execution_id.to_string(), + order_id, + time: String::from("20250101 08:00:00"), + side: ExecutionSide::Bought, + shares: 10.0, + price: 150.25, + perm_id: 0, + client_id: NATIVE_CLIENT_ID, + liquidation: 0, + account_number: String::from("001"), + exchange: String::new(), + cumulative_quantity: 10.0, + average_price: 150.25, + order_reference: String::from("O-IB-001"), + ev_rule: String::new(), + ev_multiplier: None, + model_code: String::new(), + last_liquidity: Liquidity::None, + pending_price_revision: false, + submitter: String::new(), + }; + + ExecutionData { + request_id: 0, + contract, + execution, + } +} + +fn create_test_bag_execution_data(order_id: i32, execution_id: &str) -> ExecutionData { + let contract = Contract { + symbol: IBSymbol::from("SPY"), + security_type: SecurityType::Spread, + exchange: Exchange::from("SMART"), + currency: IBCurrency::from("USD"), + combo_legs: vec![ + ibapi::contracts::ComboLeg { + contract_id: 12345, + ratio: 1, + action: LegAction::Buy, + exchange: String::from("SMART"), + open_close: ibapi::contracts::ComboLegOpenClose::Same, + short_sale_slot: 0, + designated_location: String::new(), + exempt_code: 0, + }, + ibapi::contracts::ComboLeg { + contract_id: 67890, + ratio: 1, + action: LegAction::Sell, + exchange: String::from("SMART"), + open_close: ibapi::contracts::ComboLegOpenClose::Same, + short_sale_slot: 0, + designated_location: String::new(), + exempt_code: 0, + }, + ], + ..Default::default() + }; + + let execution = Execution { + execution_id: execution_id.to_string(), + order_id, + time: String::from("20250101 08:00:00"), + side: ExecutionSide::Bought, + shares: 1.0, + price: 1.25, + perm_id: 0, + client_id: NATIVE_CLIENT_ID, + liquidation: 0, + account_number: String::from("001"), + exchange: String::new(), + cumulative_quantity: 1.0, + average_price: 1.25, + order_reference: String::from("O-IB-SPREAD"), + ev_rule: String::new(), + ev_multiplier: None, + model_code: String::new(), + last_liquidity: Liquidity::None, + pending_price_revision: false, + submitter: String::new(), + }; + + ExecutionData { + request_id: 0, + contract, + execution, + } +} + +fn create_test_option_spread() -> OptionSpread { + OptionSpread::builder() + .instrument_id(create_test_spread_instrument()) + .raw_symbol(Symbol::from("(1)SPY C400_((1))SPY C410")) + .asset_class(AssetClass::Equity) + .exchange(Ustr::from("SMART")) + .underlying(Ustr::from("SPY")) + .strategy_type(Ustr::from("VERTICAL")) + .activation_ns(UnixNanos::new(0)) + .expiration_ns(UnixNanos::new(0)) + .currency(Currency::USD()) + .price_precision(2) + .price_increment(Price::from("0.01")) + .multiplier(Quantity::from(100)) + .lot_size(Quantity::from(1)) + .ts_event(UnixNanos::new(0)) + .ts_init(UnixNanos::new(0)) + .build() + .unwrap() +} + +fn create_test_order_status(order_id: i32, status: &str) -> IBOrderStatus { + IBOrderStatus { + order_id, + status: OrderStatusKind::from_str(status).unwrap(), + filled: 0.0, + remaining: 0.0, + average_fill_price: Some(0.0), + perm_id: 0, + parent_id: 0, + last_fill_price: Some(0.0), + client_id: NATIVE_CLIENT_ID, + why_held: String::new(), + market_cap_price: Some(0.0), + } +} + +fn create_test_open_order(order_id: i32, status: &str, order_ref: &str) -> IBOrderData { + IBOrderData { + order_id, + contract: Contract { + contract_id: 12345, + symbol: IBSymbol::from("AAPL"), + security_type: SecurityType::Stock, + exchange: Exchange::from("SMART"), + currency: IBCurrency::from("USD"), + ..Default::default() + }, + order: IBOrder { + account: String::from("001"), + client_id: NATIVE_CLIENT_ID, + order_ref: order_ref.to_string(), + ..Default::default() + }, + order_state: OrderState { + status: OrderStatusKind::from_str(status).unwrap(), + ..Default::default() + }, + } +} + +#[rstest] +#[case(false, "Submitted")] +#[case(true, "PreSubmitted")] +#[tokio::test] +async fn handle_order_update_ignores_deactivated_open_order( + #[case] what_if: bool, + #[case] status: &str, +) { + let order_id = 7009; + let client_order_id = ClientOrderId::from("O-DEACTIVATED"); + let equity = equity_aapl(); + let instrument_id = equity.id(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let instrument_provider = create_test_instrument_provider(); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); + let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); + let position_tracker = create_position_tracker(); + let mut open_order = create_test_open_order(order_id, status, client_order_id.as_str()); + open_order.order.what_if = what_if; + open_order.order.deactivate = true; + open_order.order.total_quantity = 1.0; + + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), 12345, 1); + insert_tracked_order( + &orders, + order_id, + create_tracked_order_context(client_order_id, instrument_id), + ); + + let result = InteractiveBrokersExecutionClient::handle_order_update( + &OrderUpdate::OpenOrder(open_order), + &orders, + &instrument_provider, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + AccountId::from("IB-001"), + Ustr::from("001"), + &commission_cache, + &pending_execution_cache, + &position_tracker, + None, + ) + .await; + + result.unwrap(); + assert!(exec_receiver.try_recv().is_err()); + let state = orders.lock().unwrap(); + assert_eq!(state.order_id_map.get(&client_order_id), Some(&order_id)); + assert_eq!( + state.venue_order_id_map.get(&order_id), + Some(&client_order_id) + ); + let order = state.active_orders.get(&order_id).unwrap(); + assert_eq!(order.instrument_id, instrument_id); + assert!(!order.accepted); +} + +#[rstest] +fn test_remove_order_tracking_clears_submit_identity() { + let order_id = 7008; + let client_order_id = ClientOrderId::from("O-SUBMIT-FAIL"); + let instrument_id = create_test_stock_instrument(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + + InteractiveBrokersExecutionClient::cache_order_tracking( + order_id, + client_order_id, + instrument_id, + TraderId::from("TRADER-001"), + StrategyId::from("STRATEGY-001"), + OrderSide::Buy, + OrderType::Limit, + &orders, + ) + .unwrap(); + + InteractiveBrokersExecutionClient::remove_order_tracking(order_id, client_order_id, &orders) + .unwrap(); + + let state = orders.lock().unwrap(); + assert!(state.order_id_map.is_empty()); + assert!(state.venue_order_id_map.is_empty()); + assert!(state.active_orders.is_empty()); + assert!(state.terminal_orders.is_empty()); +} + +#[tokio::test] +async fn test_get_leg_position_standard_spread() { + let spread_id = InstrumentId::new( + Symbol::from("(1)SPY C400___((1))SPY C410"), + Venue::from("SMART"), + ); + let leg_id = InstrumentId::new(Symbol::from("SPY C400"), Venue::from("SMART")); + + let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id); + assert_eq!(result, 0); // First leg is at position 0 +} + +#[tokio::test] +async fn test_get_leg_position_second_leg() { + let spread_id = InstrumentId::new( + Symbol::from("(1)SPY C400___((1))SPY C410"), + Venue::from("SMART"), + ); + let leg_id = InstrumentId::new(Symbol::from("SPY C410"), Venue::from("SMART")); + + let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id); + assert_eq!(result, 1); // Second leg is at position 1 +} + +#[tokio::test] +async fn test_get_leg_position_ratio_spread() { + let spread_id = InstrumentId::new( + Symbol::from("(1)E4DN5 P6350___((2))E4DN5 P6355"), + Venue::from("XCME"), + ); + let leg_id = InstrumentId::new(Symbol::from("E4DN5 P6350"), Venue::from("XCME")); + + let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id); + assert_eq!(result, 0); +} + +#[tokio::test] +async fn test_get_leg_position_not_found() { + let spread_id = InstrumentId::new( + Symbol::from("(1)SPY C400___((1))SPY C410"), + Venue::from("SMART"), + ); + let leg_id = InstrumentId::new(Symbol::from("SPY C420"), Venue::from("SMART")); + + let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id); + // Should fallback to position 0 + assert_eq!(result, 0); +} + +#[rstest] +fn test_cached_instrument_ids_for_preload_deduplicates_spread_orders() { + let instrument_provider = create_test_instrument_provider(); + let mut cache = Cache::default(); + let spread_instrument_id = create_test_spread_instrument(); + + let order_one = OrderTestBuilder::new(OrderType::Limit) + .instrument_id(spread_instrument_id) + .side(OrderSide::Buy) + .price(Price::from("1.00")) + .quantity(Quantity::from(1)) + .build(); + let order_two = OrderTestBuilder::new(OrderType::Limit) + .instrument_id(spread_instrument_id) + .client_order_id(ClientOrderId::from("O-SPREAD-002")) + .side(OrderSide::Buy) + .price(Price::from("1.00")) + .quantity(Quantity::from(2)) + .build(); + + cache.add_order(order_one, None, None, false).unwrap(); + cache.add_order(order_two, None, None, false).unwrap(); + + let spread_ids = InteractiveBrokersExecutionClient::cached_instrument_ids_for_preload( + &cache, + &instrument_provider, + *IB_CLIENT_ID, + AccountId::from("IB-DU123456"), + ); + + assert_eq!(spread_ids, vec![spread_instrument_id]); +} + +#[rstest] +fn test_cached_instrument_ids_for_preload_includes_non_spread_orders() { + let instrument_provider = create_test_instrument_provider(); + let mut cache = Cache::default(); + let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")); + + let order = OrderTestBuilder::new(OrderType::Limit) + .instrument_id(instrument_id) + .side(OrderSide::Buy) + .price(Price::from("1.00")) + .quantity(Quantity::from(1)) + .build(); + + cache.add_order(order, None, None, false).unwrap(); + + let spread_ids = InteractiveBrokersExecutionClient::cached_instrument_ids_for_preload( + &cache, + &instrument_provider, + *IB_CLIENT_ID, + AccountId::from("IB-DU123456"), + ); + + assert_eq!(spread_ids, vec![instrument_id]); +} + +fn report_context(client: &InteractiveBrokersExecutionClient) -> IbReportContext { + IbReportContext::new( + Arc::clone(&client.instrument_provider), + client.core.account_id, + ) +} + +#[rstest] +fn test_parse_historical_fill_report_uses_provider_resolved_stock_venue() { + let (client, _, _) = create_test_execution_client(); + let equity = equity_aapl(); + let instrument_id = equity.id(); + client + .instrument_provider + .insert_test_instrument(InstrumentAny::from(equity), 265598, 1); + let exec_data = create_test_stock_execution_data(0, 123, "exec-aapl-001"); + let cmd = GenerateFillReportsBuilder::default() + .ts_init(UnixNanos::default()) + .build() + .unwrap(); + + let report = report_context(&client) + .parse_historical_fill_report(&cmd, &exec_data, 1.25, "USD", UnixNanos::default()) + .unwrap(); + + assert_eq!(report.instrument_id, instrument_id); + assert_eq!( + report.client_order_id, + Some(ClientOrderId::from("O-IB-001")) + ); + assert_eq!(report.trade_id, TradeId::from("exec-aapl-001")); + assert_eq!(report.venue_order_id, VenueOrderId::from("123")); + assert_eq!(report.last_qty, Quantity::from(10)); + assert_eq!(report.last_px, Price::from("150.25")); +} + +#[rstest] +fn test_report_contract_resolution_preserves_canonical_opra_id() { + let (client, _, _) = create_test_execution_client(); + let instrument_id = InstrumentId::from("SPY 250101C00400000.OPRA"); + client + .instrument_provider + .insert_test_contract_id_mapping(12_345, instrument_id); + let exec_data = create_test_execution_data(123, "exec-opra-001", 1.0, 1.25, "BOT"); + + let resolved = report_context(&client) + .resolve_report_contract_instrument_id(&exec_data.contract) + .unwrap(); + + assert_eq!(resolved, instrument_id); +} + +#[rstest] +fn test_parse_historical_fill_report_uses_cached_bag_spread_id() { + let (client, _, _) = create_test_execution_client(); + let spread = create_test_option_spread(); + let instrument_id = spread.id; + client + .instrument_provider + .insert_test_instrument(InstrumentAny::from(spread), 54321, 1); + client + .instrument_provider + .insert_test_contract_id_mapping(12345, create_test_leg_instrument()); + client.instrument_provider.insert_test_contract_id_mapping( + 67890, + InstrumentId::new(Symbol::from("SPY C410"), Venue::from("SMART")), + ); + let exec_data = create_test_bag_execution_data(7001, "exec-spread-001"); + let cmd = GenerateFillReportsBuilder::default() + .ts_init(UnixNanos::default()) + .build() + .unwrap(); + + let report = report_context(&client) + .parse_historical_fill_report(&cmd, &exec_data, 2.00, "USD", UnixNanos::default()) + .unwrap(); + + assert_eq!(report.instrument_id, instrument_id); + assert_eq!( + report.client_order_id, + Some(ClientOrderId::from("O-IB-SPREAD")) + ); + assert_eq!(report.trade_id, TradeId::from("exec-spread-001")); + assert_eq!(report.venue_order_id, VenueOrderId::from("7001")); + assert_eq!(report.last_qty, Quantity::from(1)); + assert_eq!(report.last_px, Price::from("1.25")); +} + +#[tokio::test] +async fn test_handle_spread_execution_emits_only_leg_fill_event() { + let instrument_provider = create_test_instrument_provider(); + let mut leg = equity_aapl(); + leg.id = create_test_leg_instrument(); + let spread = create_test_option_spread(); + let spread_instrument_id = spread.id; + let instrument_id = leg.id; + instrument_provider.insert_test_instrument(InstrumentAny::from(leg), 12345, 1); + instrument_provider.insert_test_instrument(InstrumentAny::from(spread), 54321, 1); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + + let exec_data = create_test_execution_data(213, "exec-001", 3.0, 5.25, "BOT"); + let client_order_id = ClientOrderId::from("O-001"); + let context = create_tracked_order_context(client_order_id, spread_instrument_id); + let account_id = AccountId::from("IB-001"); + let ts_init = UnixNanos::new(0); + + insert_tracked_order(&orders, exec_data.execution.order_id, context.clone()); + + let fill = SpreadFillContext { + client_order_id, + spread_instrument_id, + commission: 1.0, + commission_currency: "USD", + ts_init, + account_id, + }; + InteractiveBrokersExecutionClient::handle_spread_execution( + &exec_data, + &fill, + &instrument_provider, + &exec_sender.clone().into(), + &orders, + &context, + ) + .await + .unwrap(); + + let leg_event = exec_receiver.try_recv().unwrap(); + let leg_position = + InteractiveBrokersExecutionClient::get_leg_position(&spread_instrument_id, &instrument_id); + + match leg_event { + ExecutionEvent::Order(OrderEventAny::Filled(fill)) => { + assert_eq!(fill.trader_id, context.trader_id); + assert_eq!(fill.strategy_id, context.strategy_id); + assert_eq!(fill.instrument_id, instrument_id); + assert_eq!( + fill.client_order_id, + ClientOrderId::from("O-001-LEG-SPY C400") + ); + assert_eq!( + fill.venue_order_id, + VenueOrderId::new(format!( + "{}-LEG-{leg_position}", + exec_data.execution.order_id + )) + ); + assert_eq!(fill.account_id, account_id); + assert_eq!( + fill.trade_id, + TradeId::new(format!("exec-001-{leg_position}")) + ); + assert_eq!(fill.order_side, OrderSide::Buy); + assert_eq!(fill.last_qty, Quantity::from(3)); + assert_eq!(fill.last_px, Price::from("5.25")); + assert_eq!(fill.commission, Some(Money::from("1.00 USD"))); + assert_eq!(fill.position_id, None); + } + other => panic!("unexpected leg event: {other:?}"), + } + assert!(exec_receiver.try_recv().is_err()); + let state = orders.lock().unwrap(); + let order = state.order(exec_data.execution.order_id).unwrap(); + assert_eq!(order.spread_fill_ids.len(), 1); +} + +#[tokio::test] +async fn test_handle_spread_execution_rejects_non_leg_execution() { + let instrument_provider = create_test_instrument_provider(); + let equity = equity_aapl(); + let spread = create_test_option_spread(); + let spread_instrument_id = spread.id; + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), 12345, 1); + instrument_provider.insert_test_instrument(InstrumentAny::from(spread), 54321, 1); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let exec_data = create_test_execution_data(213, "exec-001", 3.0, 5.25, "BOT"); + let client_order_id = ClientOrderId::from("O-001"); + let context = create_tracked_order_context(client_order_id, spread_instrument_id); + insert_tracked_order(&orders, exec_data.execution.order_id, context.clone()); + let fill = SpreadFillContext { + client_order_id, + spread_instrument_id, + commission: 1.0, + commission_currency: "USD", + ts_init: UnixNanos::new(0), + account_id: AccountId::from("IB-001"), + }; + + let error = InteractiveBrokersExecutionClient::handle_spread_execution( + &exec_data, + &fill, + &instrument_provider, + &exec_sender.clone().into(), + &orders, + &context, + ) + .await + .unwrap_err(); + + assert_eq!( + error.to_string(), + format!("Execution instrument AAPL.XNAS is not a leg of spread {spread_instrument_id}") + ); + assert!(exec_receiver.try_recv().is_err()); +} + +#[tokio::test] +async fn test_handle_spread_execution_duplicate_detection() { + let instrument_provider = create_test_instrument_provider(); + let (exec_sender, _exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + + let exec_data = create_test_execution_data(213, "exec-001", 3.0, 5.25, "BOT"); + let client_order_id = ClientOrderId::from("O-001"); + let spread_instrument_id = create_test_spread_instrument(); + let context = create_tracked_order_context(client_order_id, spread_instrument_id); + let account_id = AccountId::from("IB-001"); + let ts_init = UnixNanos::new(0); + + insert_tracked_order(&orders, exec_data.execution.order_id, context.clone()); + // Pre-populate tracking with the fill ID to simulate duplicate + { + let mut state = orders.lock().unwrap(); + state + .order_mut(exec_data.execution.order_id) + .unwrap() + .spread_fill_ids + .insert("exec-001".to_string()); + } + + let fill = SpreadFillContext { + client_order_id, + spread_instrument_id, + commission: 1.0, + commission_currency: "USD", + ts_init, + account_id, + }; + let result = InteractiveBrokersExecutionClient::handle_spread_execution( + &exec_data, + &fill, + &instrument_provider, + &exec_sender.clone().into(), + &orders, + &context, + ) + .await; + + // Should return Ok(()) immediately without processing duplicate + assert!(result.is_ok()); +} + +#[rstest] +fn test_update_order_avg_price_allows_negative_spread_avg_fill_price() { + let order_id = 7002; + let instrument_provider = create_test_instrument_provider(); + let spread = create_test_option_spread(); + let spread_instrument_id = spread.id; + let client_order_id = ClientOrderId::from("O-COMBO-NEG-001"); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + + instrument_provider.insert_test_instrument(InstrumentAny::from(spread), 54321, 1); + insert_tracked_order( + &orders, + order_id, + create_tracked_order_context(client_order_id, spread_instrument_id), + ); + + InteractiveBrokersExecutionClient::update_order_avg_price( + order_id, + &spread_instrument_id, + -2.25, + 3.0, + &instrument_provider, + &orders, + ) + .unwrap(); + + let state = orders.lock().unwrap(); + let order = state.order(order_id).unwrap(); + let avg_px = order.avg_px.unwrap(); + assert_eq!(avg_px, Price::from("-2.25")); +} + +#[rstest] +fn test_track_pending_cancel_marks_once_without_emitting() { + let order_id = 7002; + let client_order_id = ClientOrderId::from("O-CANCEL-002"); + let instrument_id = create_test_spread_instrument(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let tracked = create_tracked_order_context(client_order_id, instrument_id); + let (trader_id, strategy_id) = (tracked.trader_id, tracked.strategy_id); + insert_tracked_order(&orders, order_id, tracked); + + let first = + InteractiveBrokersExecutionClient::track_pending_cancel(client_order_id, &orders).unwrap(); + let second = + InteractiveBrokersExecutionClient::track_pending_cancel(client_order_id, &orders).unwrap(); + + assert_eq!(first, Some((trader_id, strategy_id, instrument_id))); + assert_eq!(second, None); + assert!(orders.lock().unwrap().active_orders[&order_id].pending_cancel); +} + +#[rstest] +fn test_emit_order_pending_cancel_is_idempotent() { + let order_id = 7001; + let client_order_id = ClientOrderId::from("O-CANCEL-001"); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + + insert_tracked_order( + &orders, + order_id, + create_tracked_order_context(client_order_id, create_test_spread_instrument()), + ); + + InteractiveBrokersExecutionClient::emit_order_pending_cancel( + order_id, + client_order_id, + VenueOrderId::from(order_id.to_string()), + &orders, + &exec_sender.clone().into(), + UnixNanos::new(1), + AccountId::from("IB-001"), + ) + .unwrap(); + InteractiveBrokersExecutionClient::emit_order_pending_cancel( + order_id, + client_order_id, + VenueOrderId::from(order_id.to_string()), + &orders, + &exec_sender.clone().into(), + UnixNanos::new(1), + AccountId::from("IB-001"), + ) + .unwrap(); + + let first = exec_receiver.try_recv().unwrap(); + assert!(matches!( + first, + ExecutionEvent::Order(OrderEventAny::PendingCancel(_)) + )); + assert!(exec_receiver.try_recv().is_err()); + assert!( + orders + .lock() + .unwrap() + .order(order_id) + .unwrap() + .pending_cancel + ); +} + +#[tokio::test] +async fn test_handle_order_status_canceled_emits_canceled_event() { + let instrument_provider = create_test_instrument_provider(); + let spread = create_test_option_spread(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let order_id = 7001; + let client_order_id = ClientOrderId::from("O-CANCEL-002"); + let instrument_id = spread.id; + + instrument_provider.insert_test_instrument(InstrumentAny::from(spread), 54321, 1); + + let mut order = create_tracked_order_context(client_order_id, instrument_id); + order.pending_cancel = true; + insert_tracked_order(&orders, order_id, order); + let mut status = create_test_order_status(order_id, "Cancelled"); + status.filled = 1.0; + status.average_fill_price = Some(2.25); + + InteractiveBrokersExecutionClient::handle_order_status( + &status, + &orders, + &instrument_provider, + &exec_sender.clone().into(), + UnixNanos::new(1), + AccountId::from("IB-001"), + Ustr::from("001"), + None, + ) + .await + .unwrap(); + + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Accepted(event)) + if event.client_order_id == client_order_id + )); + + match exec_receiver.try_recv().unwrap() { + ExecutionEvent::Order(OrderEventAny::Canceled(event)) => { + assert_eq!(event.client_order_id, client_order_id); + assert_eq!(event.instrument_id, instrument_id); + } + other => panic!("unexpected event: {other:?}"), + } + let state = orders.lock().unwrap(); + assert!(state.order_id_map.is_empty()); + assert!(state.venue_order_id_map.is_empty()); + assert!(state.active_orders.is_empty()); + let order = state.terminal_orders.get(&order_id).unwrap(); + assert!(!order.pending_cancel); +} + +#[tokio::test] +async fn test_opra_cancel_status_preserves_canonical_instrument_identity() { + let state = SubmitTrackingState::new(); + let order_id = 7_002; + let client_order_id = ClientOrderId::from("O-OPRA-CANCEL-001"); + let instrument_id = InstrumentId::from("SPY 250101C00400000.OPRA"); + state.cache( + order_id, + client_order_id, + instrument_id, + TraderId::from("TRADER-001"), + StrategyId::from("STRATEGY-001"), + ); + + let instrument_provider = create_test_instrument_provider(); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + + InteractiveBrokersExecutionClient::handle_order_status( + &create_test_order_status(order_id, "Cancelled"), + &state.0, + &instrument_provider, + &exec_sender.clone().into(), + UnixNanos::new(1), + AccountId::from("IB-001"), + Ustr::from("001"), + None, + ) + .await + .unwrap(); + + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Accepted(event)) + if event.instrument_id == instrument_id + )); + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Canceled(event)) + if event.instrument_id == instrument_id + && event.client_order_id == client_order_id + )); + assert!(exec_receiver.try_recv().is_err()); + let state = state.0.lock().unwrap(); + assert_eq!( + state.terminal_orders.get(&order_id).unwrap().instrument_id, + instrument_id + ); +} + +#[tokio::test] +async fn test_process_order_update_stream_emits_accepted_then_canceled() { + let instrument_provider = create_test_instrument_provider(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); + let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); + let position_tracker = create_position_tracker(); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let (update_sender, update_receiver) = tokio::sync::mpsc::unbounded_channel(); + let mut subscription = ChannelSubscription::new(update_receiver); + let order_id = 7002; + let client_order_id = ClientOrderId::from("O-STREAM-001"); + let instrument_id = create_test_spread_instrument(); + + insert_tracked_order( + &orders, + order_id, + create_tracked_order_context(client_order_id, instrument_id), + ); + + update_sender + .send(Ok(OrderUpdate::OpenOrder(create_test_open_order( + order_id, + "Submitted", + "", + )))) + .unwrap(); + update_sender + .send(Ok(OrderUpdate::OrderStatus(create_test_order_status( + order_id, + "Cancelled", + )))) + .unwrap(); + drop(update_sender); + + InteractiveBrokersExecutionClient::process_order_update_stream( + &mut subscription, + &orders, + &instrument_provider, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + AccountId::from("IB-001"), + Ustr::from("001"), + &commission_cache, + &pending_execution_cache, + &position_tracker, + None, + ) + .await; + + let accepted_event = exec_receiver.try_recv().unwrap(); + assert!(matches!( + accepted_event, + ExecutionEvent::Order(OrderEventAny::Accepted(_)) + )); + + let canceled_event = exec_receiver.try_recv().unwrap(); + assert!(matches!( + canceled_event, + ExecutionEvent::Order(OrderEventAny::Canceled(_)) + )); +} + +#[tokio::test] +async fn test_process_order_update_stream_clears_market_order_update_prices() { + let instrument_provider = create_test_instrument_provider(); + let equity = equity_aapl(); + let order_id = 7005; + let contract_id = 12347; + let client_order_id = ClientOrderId::from("O-STREAM-MKT-UPDATE"); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); + let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let (update_sender, update_receiver) = tokio::sync::mpsc::unbounded_channel(); + let mut subscription = ChannelSubscription::new(update_receiver); + + let instrument_id = equity.id(); + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), contract_id, 1); + insert_tracked_order( + &orders, + order_id, + create_tracked_order_context(client_order_id, instrument_id), + ); + + let mut open_order = create_test_open_order(order_id, "Submitted", ""); + open_order.contract.contract_id = contract_id; + open_order.order.total_quantity = 10.0; + open_order.order.order_type = "MKT".to_string(); + open_order.order.limit_price = Some(150.25); + open_order.order.aux_price = Some(149.75); + + update_sender + .send(Ok(OrderUpdate::OpenOrder(open_order))) + .unwrap(); + drop(update_sender); + + InteractiveBrokersExecutionClient::process_order_update_stream( + &mut subscription, + &orders, + &instrument_provider, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + AccountId::from("IB-001"), + Ustr::from("001"), + &commission_cache, + &pending_execution_cache, + &create_position_tracker(), + None, + ) + .await; + + let accepted_event = exec_receiver.try_recv().unwrap(); + assert!(matches!( + accepted_event, + ExecutionEvent::Order(OrderEventAny::Accepted(_)) + )); + + let updated_event = exec_receiver.try_recv().unwrap(); + match updated_event { + ExecutionEvent::Order(OrderEventAny::Updated(event)) => { + assert_eq!(event.client_order_id, client_order_id); + assert_eq!(event.quantity, Quantity::from(10)); + assert_eq!(event.price, None); + assert_eq!(event.trigger_price, None); + } + other => panic!("unexpected event: {other:?}"), + } +} + +#[rstest] +#[tokio::test] +async fn execution_without_commission_flushes_with_zero_marker() { + let instrument_provider = create_test_instrument_provider(); + let equity = equity_aapl(); + let order_id = 7008; + let contract_id = 12349; + let client_order_id = ClientOrderId::from("O-STREAM-NO-COMMISSION"); + let position_tracker = create_position_tracker(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); + let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let instrument_id = equity.id(); + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), contract_id, 1); + insert_tracked_order( + &orders, + order_id, + create_tracked_order_context(client_order_id, instrument_id), + ); + + let mut exec_data = + create_test_execution_data(order_id, "exec-no-commission", 1.0, 50.0, "BOT"); + exec_data.contract.contract_id = contract_id; + exec_data.contract.security_type = SecurityType::Stock; + exec_data.contract.symbol = IBSymbol::from("AAPL"); + exec_data.contract.exchange = Exchange::from("SMART"); + exec_data.contract.currency = IBCurrency::from("USD"); + pending_execution_cache.lock().insert( + exec_data.execution.execution_id.clone(), + ( + tokio::time::Instant::now() - Duration::from_secs(5), + exec_data, + ), + ); + + InteractiveBrokersExecutionClient::flush_executions_without_commission( + &orders, + &instrument_provider, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + AccountId::from("IB-001"), + Ustr::from("001"), + &commission_cache, + &pending_execution_cache, + &position_tracker, + None, + ) + .await + .unwrap(); + + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Accepted(event)) + if event.client_order_id == client_order_id + )); + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Filled(event)) + if event.client_order_id == client_order_id + && event.commission == Some(Money::new(0.0, Currency::USD())) + )); + assert!(exec_receiver.try_recv().is_err()); + assert!(pending_execution_cache.lock().is_empty()); + assert!(commission_cache.lock().is_empty()); +} + +#[rstest] +#[case(false)] +#[case(true)] +#[tokio::test] +async fn test_process_order_update_stream_emits_fill_after_commission_report( + #[case] already_accepted: bool, +) { + let instrument_provider = create_test_instrument_provider(); + let equity = equity_aapl(); + let order_id = 7003; + let contract_id = 12345; + let client_order_id = ClientOrderId::from("O-STREAM-002"); + let position_tracker = create_position_tracker(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); + let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let (update_sender, update_receiver) = tokio::sync::mpsc::unbounded_channel(); + let mut subscription = ChannelSubscription::new(update_receiver); + + let instrument_id = equity.id(); + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), contract_id, 1); + let mut context = create_tracked_order_context(client_order_id, instrument_id); + context.accepted = already_accepted; + insert_tracked_order(&orders, order_id, context); + + let mut exec_data = create_test_execution_data(order_id, "exec-stream-001", 100.0, 50.0, "BOT"); + exec_data.contract.contract_id = contract_id; + exec_data.contract.security_type = SecurityType::Stock; + exec_data.contract.symbol = IBSymbol::from("AAPL"); + exec_data.contract.exchange = Exchange::from("SMART"); + exec_data.contract.currency = IBCurrency::from("USD"); + exec_data.execution.order_reference.clear(); + + update_sender + .send(Ok(OrderUpdate::ExecutionData(exec_data))) + .unwrap(); + update_sender + .send(Ok(OrderUpdate::CommissionReport(CommissionReport { + execution_id: String::from("exec-stream-001"), + commission: 1.25, + currency: String::from("USD"), + realized_pnl: None, + yields: None, + yield_redemption_date: String::new(), + }))) + .unwrap(); + drop(update_sender); + + InteractiveBrokersExecutionClient::process_order_update_stream( + &mut subscription, + &orders, + &instrument_provider, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + AccountId::from("IB-001"), + Ustr::from("001"), + &commission_cache, + &pending_execution_cache, + &position_tracker, + None, + ) + .await; + + if !already_accepted { + let accepted_event = exec_receiver.try_recv().unwrap(); + assert!(matches!( + accepted_event, + ExecutionEvent::Order(OrderEventAny::Accepted(event)) + if event.client_order_id == client_order_id + )); + } + + let fill_event = exec_receiver.try_recv().unwrap(); + match fill_event { + ExecutionEvent::Order(OrderEventAny::Filled(fill)) => { + assert_eq!(fill.trader_id, TraderId::from("TRADER-001")); + assert_eq!(fill.strategy_id, StrategyId::from("STRATEGY-001")); + assert_eq!(fill.client_order_id, client_order_id); + assert_eq!(fill.instrument_id, instrument_id); + assert_eq!(fill.trade_id, TradeId::from("exec-stream-001")); + assert_eq!(fill.order_side, OrderSide::Buy); + assert_eq!(fill.order_type, OrderType::Limit); + assert_eq!(fill.last_qty, Quantity::from(100)); + assert_eq!(fill.last_px, Price::from("50")); + assert_eq!(fill.currency, Currency::USD()); + assert_eq!(fill.commission, Some(Money::new(1.25, Currency::USD()))); + } + other => panic!("unexpected event: {other:?}"), + } + assert!(commission_cache.lock().is_empty()); + assert_eq!( + check_external_position_change(&position_tracker, contract_id, Decimal::from(100),).await, + None, + ); + assert!(exec_receiver.try_recv().is_err()); +} + +#[tokio::test] +async fn test_process_order_update_stream_retains_terminal_identity_for_late_fill() { + let instrument_provider = create_test_instrument_provider(); + let equity = equity_aapl(); + let order_id = 7006; + let contract_id = 12348; + let client_order_id = ClientOrderId::from("O-STREAM-LATE-FILL"); + let instrument_id = equity.id(); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); + let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let (update_sender, update_receiver) = tokio::sync::mpsc::unbounded_channel(); + let mut subscription = ChannelSubscription::new(update_receiver); + + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), contract_id, 1); + insert_tracked_order( + &orders, + order_id, + create_tracked_order_context(client_order_id, instrument_id), + ); + + let mut exec_data = + create_test_execution_data(order_id, "exec-stream-late", 100.0, 50.0, "BOT"); + exec_data.contract.contract_id = contract_id; + exec_data.contract.security_type = SecurityType::Stock; + exec_data.contract.symbol = IBSymbol::from("AAPL"); + exec_data.contract.exchange = Exchange::from("SMART"); + exec_data.contract.currency = IBCurrency::from("USD"); + exec_data.execution.order_reference.clear(); + + update_sender + .send(Ok(OrderUpdate::OrderStatus(create_test_order_status( + order_id, "Filled", + )))) + .unwrap(); + update_sender + .send(Ok(OrderUpdate::ExecutionData(exec_data))) + .unwrap(); + drop(update_sender); + + InteractiveBrokersExecutionClient::process_order_update_stream( + &mut subscription, + &orders, + &instrument_provider, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + AccountId::from("IB-001"), + Ustr::from("001"), + &commission_cache, + &pending_execution_cache, + &create_position_tracker(), + None, + ) + .await; + + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Accepted(event)) + if event.client_order_id == client_order_id + )); + assert!( + pending_execution_cache + .lock() + .contains_key(&String::from("exec-stream-late")) + ); + assert!(exec_receiver.try_recv().is_err()); + + let (update_sender, update_receiver) = tokio::sync::mpsc::unbounded_channel(); + let mut subscription = ChannelSubscription::new(update_receiver); + update_sender + .send(Ok(OrderUpdate::CommissionReport(CommissionReport { + execution_id: String::from("exec-stream-late"), + commission: 1.25, + currency: String::from("USD"), + realized_pnl: None, + yields: None, + yield_redemption_date: String::new(), + }))) + .unwrap(); + drop(update_sender); + + InteractiveBrokersExecutionClient::process_order_update_stream( + &mut subscription, + &orders, + &instrument_provider, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + AccountId::from("IB-001"), + Ustr::from("001"), + &commission_cache, + &pending_execution_cache, + &create_position_tracker(), + None, + ) + .await; + + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Filled(event)) + if event.client_order_id == client_order_id + && event.trade_id == TradeId::from("exec-stream-late") + )); + assert!(exec_receiver.try_recv().is_err()); + assert!(pending_execution_cache.lock().is_empty()); + let state = orders.lock().unwrap(); + assert!(state.active_orders.is_empty()); + let terminal_context = state.terminal_orders.get(&order_id).unwrap(); + assert!(terminal_context.accepted); + assert_eq!(terminal_context.client_order_id, client_order_id); +} + +#[tokio::test] +async fn test_process_order_update_stream_retains_terminal_combo_routing() { + let instrument_provider = create_test_instrument_provider(); + let spread = create_test_option_spread(); + let order_id = 7007; + let client_order_id = ClientOrderId::from("O-STREAM-LATE-COMBO"); + let instrument_id = spread.id; + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); + let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + + let (long_leg, short_leg) = insert_test_spread_legs(&instrument_provider); + instrument_provider.insert_test_instrument(InstrumentAny::from(spread), 54321, 1); + insert_tracked_order( + &orders, + order_id, + create_tracked_order_context(client_order_id, instrument_id), + ); + + let mut status = create_test_order_status(order_id, "Filled"); + status.filled = 1.0; + status.average_fill_price = Some(2.25); + let executions = create_test_combo_executions(order_id, "exec-stream-late-combo", 1.0); + let replayed_executions = executions.clone(); + let mut updates = vec![OrderUpdate::OrderStatus(status)]; + updates.extend(executions); + + run_order_update_stream( + updates, + &orders, + &instrument_provider, + &exec_sender, + &commission_cache, + &pending_execution_cache, + ) + .await; + + // IB reports the combo execution first; its fill goes out after both leg fills + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Accepted(event)) + if event.client_order_id == client_order_id + )); + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Filled(event)) + if event.instrument_id == short_leg + && event.order_side == OrderSide::Sell + && event.last_px == Price::from("1.25") + && event.commission == Some(Money::from("0.80 USD")) + )); + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Filled(event)) + if event.instrument_id == long_leg + && event.order_side == OrderSide::Buy + && event.last_px == Price::from("3.50") + && event.commission == Some(Money::from("0.80 USD")) + )); + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Filled(event)) + if event.client_order_id == client_order_id + && event.instrument_id == instrument_id + && event.last_px == Price::from("2.25") + && event.commission == Some(Money::from("0.00 USD")) + )); + assert!(exec_receiver.try_recv().is_err()); + { + let state = orders.lock().unwrap(); + assert!(state.active_orders.is_empty()); + assert!(state.terminal_orders.contains_key(&order_id)); + } + + run_order_update_stream( + replayed_executions, + &orders, + &instrument_provider, + &exec_sender, + &commission_cache, + &pending_execution_cache, + ) + .await; + + assert!(exec_receiver.try_recv().is_err()); +} + +#[tokio::test] +async fn test_spread_fill_waits_for_split_leg_executions() { + let instrument_provider = create_test_instrument_provider(); + let spread = create_test_option_spread(); + let order_id = 7008; + let client_order_id = ClientOrderId::from("O-SPLIT-LEGS"); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let exec_sender: EventSender = exec_sender.into(); + let (long_leg, short_leg) = insert_test_spread_legs(&instrument_provider); + let fill = create_test_held_spread_fill(client_order_id, spread.id, 2); + insert_tracked_order( + &orders, + order_id, + create_tracked_order_context(client_order_id, spread.id), + ); + + InteractiveBrokersExecutionClient::hold_spread_fill( + &orders, + order_id, + fill.clone(), + &exec_sender, + ) + .unwrap(); + InteractiveBrokersExecutionClient::record_spread_leg_fill( + &orders, + order_id, + long_leg, + 2.0, + &exec_sender, + ) + .unwrap(); + InteractiveBrokersExecutionClient::record_spread_leg_fill( + &orders, + order_id, + short_leg, + 1.0, + &exec_sender, + ) + .unwrap(); + let held_after_partial_leg = exec_receiver.try_recv().is_err(); + InteractiveBrokersExecutionClient::record_spread_leg_fill( + &orders, + order_id, + short_leg, + 1.0, + &exec_sender, + ) + .unwrap(); + + assert!(held_after_partial_leg); + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Filled(released)) if released == fill + )); + let state = orders.lock().unwrap(); + let held = &state.order(order_id).unwrap().spread_fills_held; + assert!(held.fills.is_empty()); + assert!(held.leg_quantities.is_empty()); +} + +#[tokio::test(start_paused = true)] +async fn test_spread_fill_without_legs_is_flushed_after_timeout() { + let instrument_provider = create_test_instrument_provider(); + let spread = create_test_option_spread(); + let order_id = 7009; + let client_order_id = ClientOrderId::from("O-MISSING-LEGS"); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let exec_sender: EventSender = exec_sender.into(); + insert_test_spread_legs(&instrument_provider); + let fill = create_test_held_spread_fill(client_order_id, spread.id, 1); + insert_tracked_order( + &orders, + order_id, + create_tracked_order_context(client_order_id, spread.id), + ); + + InteractiveBrokersExecutionClient::hold_spread_fill( + &orders, + order_id, + fill.clone(), + &exec_sender, + ) + .unwrap(); + tokio::time::advance(Duration::from_millis(4_999)).await; + InteractiveBrokersExecutionClient::flush_spread_fills_without_legs(&orders, &exec_sender) + .unwrap(); + let held_before_timeout = exec_receiver.try_recv().is_err(); + tokio::time::advance(Duration::from_millis(1)).await; + InteractiveBrokersExecutionClient::flush_spread_fills_without_legs(&orders, &exec_sender) + .unwrap(); + + assert!(held_before_timeout); + assert!(matches!( + exec_receiver.try_recv().unwrap(), + ExecutionEvent::Order(OrderEventAny::Filled(released)) if released == fill + )); + assert!(exec_receiver.try_recv().is_err()); +} + +// Inserts the `SPY C400` and `SPY C410` legs of the test spread under contract IDs 12345 and +// 67890, returning their instrument IDs +fn insert_test_spread_legs( + instrument_provider: &Arc, +) -> (InstrumentId, InstrumentId) { + let mut long_leg = equity_aapl(); + long_leg.id = create_test_leg_instrument(); + let mut short_leg = equity_aapl(); + short_leg.id = InstrumentId::new(Symbol::from("SPY C410"), Venue::from("SMART")); + let ids = (long_leg.id, short_leg.id); + instrument_provider.insert_test_instrument(InstrumentAny::from(long_leg), 12345, 1); + instrument_provider.insert_test_instrument(InstrumentAny::from(short_leg), 67890, 1); + ids +} + +// IB's order for a filled combo: the combo execution without a commission report, then each +// leg execution followed by its commission report +fn create_test_combo_executions( + order_id: i32, + execution_prefix: &str, + quantity: f64, +) -> Vec { + let mut combo = create_test_execution_data( + order_id, + &format!("{execution_prefix}.01"), + quantity, + 2.25, + "BOT", + ); + combo.contract.contract_id = 54321; + combo.contract.security_type = SecurityType::Spread; + combo.contract.combo_legs = create_test_bag_execution_data(order_id, "unused") + .contract + .combo_legs; + combo.execution.order_reference.clear(); + let mut short_leg = create_test_execution_data( + order_id, + &format!("{execution_prefix}.02"), + quantity, + 1.25, + "SLD", + ); + short_leg.contract.contract_id = 67890; + short_leg.contract.local_symbol = String::from("SPY C410"); + short_leg.execution.order_reference.clear(); + let mut long_leg = create_test_execution_data( + order_id, + &format!("{execution_prefix}.03"), + quantity, + 3.50, + "BOT", + ); + long_leg.execution.order_reference.clear(); + let commission = |execution_id: String| { + OrderUpdate::CommissionReport(CommissionReport { + execution_id, + commission: 0.80, + currency: String::from("USD"), + realized_pnl: None, + yields: None, + yield_redemption_date: String::new(), + }) + }; + + vec![ + OrderUpdate::ExecutionData(combo), + OrderUpdate::ExecutionData(short_leg.clone()), + commission(short_leg.execution.execution_id), + OrderUpdate::ExecutionData(long_leg.clone()), + commission(long_leg.execution.execution_id), + ] +} + +fn create_test_held_spread_fill( + client_order_id: ClientOrderId, + spread_instrument_id: InstrumentId, + quantity: u64, +) -> OrderFilled { + OrderFilled::new( + TraderId::from("TRADER-001"), + StrategyId::from("STRATEGY-001"), + spread_instrument_id, + client_order_id, + VenueOrderId::from("PERM-1"), + AccountId::from("IB-001"), + TradeId::from("exec-combo"), + OrderSide::Buy, + OrderType::Limit, + Quantity::from(quantity), + Price::from("2.25"), + Currency::USD(), + LiquiditySide::NoLiquiditySide, + UUID4::new(), + UnixNanos::new(1), + UnixNanos::new(2), + false, + None, + Some(Money::from("0.00 USD")), + None, + ) +} + +async fn run_order_update_stream( + updates: Vec, + orders: &OrderTracker, + instrument_provider: &Arc, + exec_sender: &tokio::sync::mpsc::UnboundedSender, + commission_cache: &Arc>, + pending_execution_cache: &Arc>, +) { + let (update_sender, update_receiver) = tokio::sync::mpsc::unbounded_channel(); + let mut subscription = ChannelSubscription::new(update_receiver); + + for update in updates { + update_sender.send(Ok(update)).unwrap(); + } + drop(update_sender); + + InteractiveBrokersExecutionClient::process_order_update_stream( + &mut subscription, + orders, + instrument_provider, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + AccountId::from("IB-001"), + Ustr::from("001"), + commission_cache, + pending_execution_cache, + &create_position_tracker(), + None, + ) + .await; +} + +#[tokio::test] +async fn test_process_order_update_stream_preserves_execution_reference_without_changing_routes() { + let instrument_provider = create_test_instrument_provider(); + let equity = equity_aapl(); + let order_id = 7004; + let contract_id = 12346; + let client_order_id = ClientOrderId::from("O-STREAM-EXEC-REF"); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); + let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let (update_sender, update_receiver) = tokio::sync::mpsc::unbounded_channel(); + let mut subscription = ChannelSubscription::new(update_receiver); + + let instrument_id = equity.id(); + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), contract_id, 1); + + let mut exec_data = create_test_execution_data(order_id, "exec-stream-002", 100.0, 50.0, "BOT"); + exec_data.contract.contract_id = contract_id; + exec_data.contract.security_type = SecurityType::Stock; + exec_data.contract.symbol = IBSymbol::from("AAPL"); + exec_data.contract.exchange = Exchange::from("SMART"); + exec_data.contract.currency = IBCurrency::from("USD"); + exec_data.execution.order_reference = client_order_id.to_string(); + exec_data.execution.perm_id = 90210; + + update_sender + .send(Ok(OrderUpdate::ExecutionData(exec_data))) + .unwrap(); + update_sender + .send(Ok(OrderUpdate::CommissionReport(CommissionReport { + execution_id: String::from("exec-stream-002"), + commission: 1.25, + currency: String::from("USD"), + realized_pnl: None, + yields: None, + yield_redemption_date: String::new(), + }))) + .unwrap(); + drop(update_sender); + + InteractiveBrokersExecutionClient::process_order_update_stream( + &mut subscription, + &orders, + &instrument_provider, + &exec_sender.clone().into(), + nautilus_core::time::get_atomic_clock_realtime(), + AccountId::from("IB-001"), + Ustr::from("001"), + &commission_cache, + &pending_execution_cache, + &create_position_tracker(), + None, + ) + .await; + + let fill_event = exec_receiver.try_recv().unwrap(); + match fill_event { + ExecutionEvent::Report(ExecutionReport::Fill(fill)) => { + assert_eq!(fill.client_order_id, Some(client_order_id)); + assert_eq!(fill.instrument_id, instrument_id); + assert_eq!(fill.account_id, AccountId::from("IB-001")); + assert_eq!(fill.venue_order_id, VenueOrderId::from("PERM-90210")); + assert_eq!(fill.trade_id, TradeId::from("exec-stream-002")); + assert_eq!(fill.order_side, OrderSide::Buy); + assert_eq!(fill.last_qty, Quantity::from(100)); + assert_eq!(fill.last_px, Price::from("50.00")); + assert_eq!(fill.commission, Money::from("1.25 USD")); + } + other => panic!("unexpected event: {other:?}"), + } + let state = orders.lock().unwrap(); + assert_eq!(state.venue_order_id_map.get(&order_id), None); + assert_eq!(state.order_id_map.get(&client_order_id), None); +} + +#[rstest] +fn test_get_leg_position_edge_cases() { + // Test with single component (no spread) + let spread_id = InstrumentId::new(Symbol::from("SPY C400"), Venue::from("SMART")); + let leg_id = InstrumentId::new(Symbol::from("SPY C400"), Venue::from("SMART")); + let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id); + assert_eq!(result, 0); // Should fallback to 0 + + // Test with invalid format + let spread_id = InstrumentId::new(Symbol::from("INVALID_FORMAT"), Venue::from("SMART")); + let leg_id = InstrumentId::new(Symbol::from("SPY C400"), Venue::from("SMART")); + let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id); + assert_eq!(result, 0); // Should fallback to 0 +} + +#[rstest] +fn test_get_leg_position_three_leg_spread() { + let spread_id = InstrumentId::new( + Symbol::from("(1)LEG1___((1))LEG2___((2))LEG3"), + Venue::from("SMART"), + ); + + // Test first leg + let leg_id1 = InstrumentId::new(Symbol::from("LEG1"), Venue::from("SMART")); + let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id1); + assert_eq!(result, 0); + + // Test second leg + let leg_id2 = InstrumentId::new(Symbol::from("LEG2"), Venue::from("SMART")); + let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id2); + assert_eq!(result, 1); + + // Test third leg + let leg_id3 = InstrumentId::new(Symbol::from("LEG3"), Venue::from("SMART")); + let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id3); + assert_eq!(result, 2); +} + +#[rstest] +#[case(1, 22, vec![(101, 11), (202, 22)], vec![])] +#[case(1, 11, vec![], vec![101, 202])] +#[case(9, 22, vec![(101, 11)], vec![202])] +fn duplicate_orders_cancel_only_unique_owned_routes( + #[case] sibling_client: i32, + #[case] sibling_raw_id: i32, + #[case] expected_routes: Vec<(i64, i32)>, + #[case] expected_unresolved: Vec, +) { + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let parent_id = ClientOrderId::from("O-PRIMARY"); + let mut parent = create_tracked_order_context(parent_id, create_test_stock_instrument()); + parent.perm_id = 101; + insert_tracked_order(&orders, 11, parent.clone()); + let mut primary = create_test_open_order(11, "Submitted", parent_id.as_str()); + primary.order.perm_id = 101; + let mut sibling = create_test_open_order(sibling_raw_id, "Submitted", parent_id.as_str()); + sibling.order.client_id = sibling_client; + sibling.order.perm_id = 202; + let mut state = orders.lock().unwrap(); + state.record_incarnation( + 11, + &parent, + AccountId::from("IB-001"), + 101, + Some(&primary), + false, + ); + state.record_incarnation( + 11, + &parent, + AccountId::from("IB-001"), + 202, + Some(&sibling), + false, + ); + assert_eq!( + state.group_cancel_routes(parent_id, None), + (expected_routes, expected_unresolved) + ); +} + +#[rstest] +fn binding_notification_resolves_a_duplicate_order_route() { + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let parent_id = ClientOrderId::from("O-PRIMARY"); + let mut parent = create_tracked_order_context(parent_id, create_test_stock_instrument()); + parent.perm_id = 101; + let mut data = create_test_open_order(11, "Submitted", parent_id.as_str()); + data.order.perm_id = 101; + let mut state = orders.lock().unwrap(); + state.record_incarnation( + 11, + &parent, + AccountId::from("IB-001"), + 101, + Some(&data), + false, + ); + data.order.perm_id = 202; + state.record_incarnation( + 11, + &parent, + AccountId::from("IB-001"), + 202, + Some(&data), + false, + ); + assert_eq!( + state.group_cancel_routes(parent_id, None), + (vec![], vec![101, 202]) + ); + state.observe_order_binding(&ibapi::orders::OrderBound { + perm_id: 202, + client_id: NATIVE_CLIENT_ID, + order_id: 22, + }); + assert_eq!( + state.group_cancel_routes(parent_id, None), + (vec![(101, 11), (202, 22)], vec![]) + ); +} + +#[rstest] +#[case::unsided(None, vec![(101, 11), (202, 22), (303, 33)])] +#[case::buy(Some(OrderSide::Buy), vec![(101, 11)])] +#[case::sell(Some(OrderSide::Sell), vec![(303, 33)])] +fn group_cancel_routes_skip_members_off_the_requested_side( + #[case] order_side: Option, + #[case] expected_routes: Vec<(i64, i32)>, +) { + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let parent_id = ClientOrderId::from("O-PRIMARY"); + let mut parent = create_tracked_order_context(parent_id, create_test_stock_instrument()); + parent.perm_id = 101; + let mut primary = create_test_open_order(11, "Submitted", parent_id.as_str()); + primary.order.perm_id = 101; + primary.order.action = ibapi::orders::Action::Buy; + let mut sell = create_test_open_order(33, "Submitted", parent_id.as_str()); + sell.order.perm_id = 303; + sell.order.action = ibapi::orders::Action::Sell; + let account_id = AccountId::from("IB-001"); + let mut state = orders.lock().unwrap(); + state.record_incarnation(11, &parent, account_id, 101, Some(&primary), false); + // Bound without order data, so its broker side is unknown + state.record_incarnation(11, &parent, account_id, 202, None, false); + state.observe_order_binding(&ibapi::orders::OrderBound { + perm_id: 202, + client_id: NATIVE_CLIENT_ID, + order_id: 22, + }); + state.record_incarnation(11, &parent, account_id, 303, Some(&sell), false); + + assert_eq!( + state.group_cancel_routes(parent_id, order_side), + (expected_routes, vec![]) + ); +} + +#[rstest] +fn conflicting_routes_for_one_permanent_id_remain_unresolved() { + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let parent_id = ClientOrderId::from("O-PRIMARY"); + let mut parent = create_tracked_order_context(parent_id, create_test_stock_instrument()); + parent.perm_id = 101; + let mut data = create_test_open_order(11, "Submitted", parent_id.as_str()); + data.order.perm_id = 101; + let mut state = orders.lock().unwrap(); + state.record_incarnation( + 11, + &parent, + AccountId::from("IB-001"), + 101, + Some(&data), + false, + ); + data.order_id = 22; + state.record_incarnation( + 11, + &parent, + AccountId::from("IB-001"), + 101, + Some(&data), + false, + ); + assert_eq!( + state.group_cancel_routes(parent_id, None), + (vec![], vec![101]) + ); +} + +#[rstest] +fn callback_identity_does_not_alias_another_api_clients_raw_id() { + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let id = ClientOrderId::from("O-PRIMARY"); + let mut parent = create_tracked_order_context(id, create_test_stock_instrument()); + parent.perm_id = 101; + insert_tracked_order(&orders, 11, parent); + let state = orders.lock().unwrap(); + assert!(matches!( + state.correlate(9, 11, 202, "").unwrap(), + OrderCorrelation::Untracked { + client_order_id: None + } + )); + assert!(state.correlate(1, 11, 101, "ANOTHER-ORDER").is_err()); + let OrderCorrelation::Tracked { order_id, context } = + state.correlate(9, 77, 101, id.as_str()).unwrap() + else { + panic!("known permanent ID must retain its original context"); + }; + assert_eq!( + (order_id, context.client_order_id, context.perm_id), + (11, id, 101) + ); + assert_eq!(state.order_id_map.get(&id), Some(&11)); +} + +#[rstest] +#[case(OrderStatusKind::Submitted)] +#[case(OrderStatusKind::Cancelled)] +fn late_sibling_status_does_not_reopen_a_filled_order(#[case] late: OrderStatusKind) { + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let id = ClientOrderId::from("O-PRIMARY"); + let mut parent = create_tracked_order_context(id, create_test_stock_instrument()); + parent.perm_id = 101; + let mut data = create_test_open_order(22, "Filled", id.as_str()); + data.order.perm_id = 202; + data.order.filled_quantity = 2.0; + let mut state = orders.lock().unwrap(); + state.record_incarnation( + 11, + &parent, + AccountId::from("IB-001"), + 202, + Some(&data), + true, + ); + let status = IBOrderStatus { + order_id: 22, + perm_id: 202, + client_id: NATIVE_CLIENT_ID, + status: late, + filled: 0.0, + ..Default::default() + }; + let (_, snapshot) = state.observe_incarnation_status(&status).unwrap(); + assert_eq!(snapshot.order_state.status, OrderStatusKind::Filled); + assert_eq!(snapshot.order.filled_quantity, 2.0); +} + +#[rstest] +fn cancel_all_includes_working_siblings_after_the_parent_closes() { + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let id = ClientOrderId::from("O-PRIMARY"); + let instrument_id = create_test_stock_instrument(); + let account_id = AccountId::from("IB-001"); + let mut parent = create_tracked_order_context(id, instrument_id); + parent.perm_id = 101; + let mut original = create_test_open_order(11, "Cancelled", id.as_str()); + original.order.perm_id = 101; + let mut sibling = create_test_open_order(22, "Submitted", id.as_str()); + sibling.order.perm_id = 202; + let mut state = orders.lock().unwrap(); + state.record_incarnation(11, &parent, account_id, 101, Some(&original), true); + state.record_incarnation(11, &parent, account_id, 202, Some(&sibling), false); + assert!(state.active_orders.is_empty()); + assert_eq!( + state + .group_cancel_candidates(instrument_id, account_id, None) + .collect::>(), + vec![id] + ); + assert_eq!( + state + .group_cancel_candidates(InstrumentId::from("MSFT.SMART"), account_id, None) + .count(), + 0 + ); + assert_eq!( + state + .group_cancel_candidates(instrument_id, AccountId::from("IB-002"), None) + .count(), + 0 + ); + state.observe_incarnation_status(&IBOrderStatus { + order_id: 22, + perm_id: 202, + client_id: NATIVE_CLIENT_ID, + status: OrderStatusKind::Cancelled, + ..Default::default() + }); + assert_eq!( + state + .group_cancel_candidates(instrument_id, account_id, None) + .count(), + 0 + ); +} + +#[rstest] +fn completed_duplicate_group_removes_only_its_child_routes() { + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let parent_id = ClientOrderId::from("O-PRIMARY"); + let account_id = AccountId::from("IB-001"); + let child_id = ClientOrderId::from("DUP-6:IB-001:PERM-202"); + let mut parent = create_tracked_order_context(parent_id, create_test_stock_instrument()); + parent.perm_id = 101; + insert_tracked_order(&orders, 11, parent.clone()); + let mut child = parent.clone(); + child.client_order_id = child_id; + child.perm_id = 202; + insert_tracked_order(&orders, 22, child); + let mut original = create_test_open_order(11, "Cancelled", parent_id.as_str()); + original.order.perm_id = 101; + let mut sibling = create_test_open_order(22, "Cancelled", parent_id.as_str()); + sibling.order.perm_id = 202; + let mut state = orders.lock().unwrap(); + state.record_incarnation(11, &parent, account_id, 101, Some(&original), true); + state.record_incarnation(11, &parent, account_id, 202, Some(&sibling), true); + state.archive_finished_groups(); + assert_eq!(state.order_id_map.get(&parent_id), Some(&11)); + assert_eq!(state.venue_order_id_map.get(&11), Some(&parent_id)); + assert_eq!(state.order_id_map.get(&child_id), None); + assert_eq!(state.venue_order_id_map.get(&22), None); + assert!(!state.active_orders.contains_key(&22)); + assert_eq!( + state.terminal_orders.get(&22).unwrap().client_order_id, + child_id + ); + assert!(state.groups.is_empty()); + assert!(state.group_history.contains_key(&parent_id)); +} + +#[tokio::test] +async fn duplicate_execution_reports_under_its_own_client_order_id() { + let instrument_provider = create_test_instrument_provider(); + let equity = equity_aapl(); + let instrument_id = equity.id(); + let contract_id = 12346; + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), contract_id, 1); + let parent_id = ClientOrderId::from("O-PRIMARY"); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let mut parent = create_tracked_order_context(parent_id, instrument_id); + parent.perm_id = 101; + insert_tracked_order(&orders, 7005, parent); + let mut exec_data = create_test_execution_data(7005, "exec-dup-001", 2.0, 50.0, "SLD"); + exec_data.contract.contract_id = contract_id; + exec_data.contract.security_type = SecurityType::Stock; + exec_data.execution.order_reference = parent_id.to_string(); + exec_data.execution.perm_id = 202; + let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); + commission_cache + .lock() + .insert(String::from("exec-dup-001"), (0.5, String::from("USD"))); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + + InteractiveBrokersExecutionClient::handle_execution_data( + &exec_data, + &orders, + &instrument_provider, + &exec_sender.into(), + UnixNanos::new(31), + AccountId::from("IB-001"), + Ustr::from("001"), + &commission_cache, + &create_position_tracker(), + None, + ) + .await + .unwrap(); + + let ExecutionEvent::Report(ExecutionReport::Fill(fill)) = exec_receiver.try_recv().unwrap() + else { + panic!("expected a fill report"); + }; + assert_eq!( + fill.client_order_id, + Some(ClientOrderId::from("DUP-6:IB-001:PERM-202")) + ); + assert_eq!(fill.venue_order_id, VenueOrderId::from("PERM-202")); + assert_eq!(fill.trade_id, TradeId::from("exec-dup-001")); + assert_eq!(fill.order_side, OrderSide::Sell); + assert_eq!(fill.last_qty, Quantity::from(2)); + assert!(exec_receiver.try_recv().is_err()); +} + +#[tokio::test] +async fn duplicate_status_reports_under_its_own_client_order_id() { + let instrument_provider = create_test_instrument_provider(); + let equity = equity_aapl(); + let instrument_id = equity.id(); + instrument_provider.insert_test_instrument(InstrumentAny::from(equity), 12345, 1); + let parent_id = ClientOrderId::from("O-PRIMARY"); + let account_id = AccountId::from("IB-001"); + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let mut parent = create_tracked_order_context(parent_id, instrument_id); + parent.perm_id = 101; + let mut sibling = create_test_open_order(22, "Submitted", parent_id.as_str()); + sibling.order.perm_id = 202; + orders + .lock() + .unwrap() + .record_incarnation(11, &parent, account_id, 202, Some(&sibling), false); + let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); + let status = IBOrderStatus { + perm_id: 202, + ..create_test_order_status(22, "Submitted") + }; + + InteractiveBrokersExecutionClient::handle_order_status( + &status, + &orders, + &instrument_provider, + &exec_sender.into(), + UnixNanos::new(32), + account_id, + Ustr::from("001"), + None, + ) + .await + .unwrap(); + + let ExecutionEvent::Report(ExecutionReport::Order(report)) = exec_receiver.try_recv().unwrap() + else { + panic!("expected an order status report"); + }; + assert_eq!( + report.client_order_id, + Some(ClientOrderId::from("DUP-6:IB-001:PERM-202")) + ); + assert_eq!(report.venue_order_id, VenueOrderId::from("PERM-202")); + assert_eq!(report.instrument_id, instrument_id); + assert_eq!(report.order_status, OrderStatus::Accepted); + assert!(exec_receiver.try_recv().is_err()); +} + +#[rstest] +fn duplicate_fills_stay_unresolved_until_their_executions_are_delivered() { + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let mut parent = create_tracked_order_context( + ClientOrderId::from("O-PRIMARY"), + create_test_stock_instrument(), + ); + parent.perm_id = 101; + let mut state = orders.lock().unwrap(); + state.record_incarnation(11, &parent, AccountId::from("IB-001"), 202, None, false); + let before = ( + state.duplicate_fills_unresolved(202, 0.0), + state.duplicate_fills_unresolved(202, 2.0), + ); + + state.observe_duplicate_fills_resolved(202, 2.0); + + assert_eq!(before, (false, true)); + assert!(!state.duplicate_fills_unresolved(202, 2.0)); + assert!(state.duplicate_fills_unresolved(202, 3.0)); + assert!(!state.duplicate_fills_unresolved(303, 3.0)); +} + +#[rstest] +fn held_fills_start_one_resolution_per_order() { + let orders = OrderTracker::new(NATIVE_CLIENT_ID); + let target = IbOrderSelector::PermId(202); + let fill = |trade_id: &str| { + FillReport::new( + AccountId::from("IB-001"), + create_test_stock_instrument(), + VenueOrderId::from("PERM-202"), + TradeId::from(trade_id), + OrderSide::Buy, + Quantity::from(1), + Price::from("50.00"), + Money::from("0.50 USD"), + LiquiditySide::Taker, + None, + None, + UnixNanos::new(33), + UnixNanos::new(34), + None, + ) + }; + let mut state = orders.lock().unwrap(); + + let starts = [ + state.hold_fill(target, Some(fill("T-1"))), + state.hold_fill(target, None), + state.hold_fill(target, Some(fill("T-2"))), + state.hold_fill(IbOrderSelector::PermId(303), None), + ]; + + let held: Vec<_> = state.fills_held[&target] + .iter() + .map(|fill| fill.trade_id) + .collect(); + assert_eq!(starts, [true, false, false, true]); + assert_eq!(held, vec![TradeId::from("T-1"), TradeId::from("T-2")]); + assert!(state.fills_held[&IbOrderSelector::PermId(303)].is_empty()); +} + +#[rstest] +fn test_account_state_sender_delivers_from_worker_thread() { + let (client, mut rx, _cache) = create_test_execution_client(); + let send_account_state = client.account_state_sender(); + let balances = vec![ + AccountBalance::from_total_and_free( + Decimal::new(100050, 2), + Decimal::new(90025, 2), + Currency::USD(), + ) + .unwrap(), + ]; + + std::thread::spawn({ + let balances = balances.clone(); + move || send_account_state((balances, Vec::new(), None)) + }) + .join() + .unwrap(); + + match rx.try_recv().unwrap() { + ExecutionEvent::Account(state) => { + assert_eq!(state.account_id, AccountId::from("IB-001")); + assert_eq!(state.balances, balances); + assert!(state.is_reported); + } + other => panic!("expected account state, was {other:?}"), + } +} + +#[rstest] +fn test_report_hooks_fall_back_to_inline_collection_while_disconnected() { + let (client, _, _) = create_test_execution_client(); + let order_cmd = GenerateOrderStatusReport::new( + UUID4::new(), + UnixNanos::default(), + None, + None, + None, + None, + None, + ); + let orders_cmd = GenerateOrderStatusReportsBuilder::default() + .ts_init(UnixNanos::default()) + .open_only(false) + .build() + .unwrap(); + let fills_cmd = GenerateFillReportsBuilder::default() + .ts_init(UnixNanos::default()) + .build() + .unwrap(); + let positions_cmd = GeneratePositionStatusReportsBuilder::default() + .ts_init(UnixNanos::default()) + .build() + .unwrap(); + + assert!( + client + .generate_order_status_report_task(&order_cmd) + .is_none() + ); + assert!( + client + .generate_order_status_reports_task(&orders_cmd) + .is_none() + ); + assert!(client.generate_fill_reports_task(&fills_cmd).is_none()); + assert!( + client + .generate_position_status_reports_task(&positions_cmd) + .is_none() + ); +} + +#[rstest] +#[tokio::test] +async fn test_inline_reports_report_not_connected_while_disconnected() { + let (client, _, _) = create_test_execution_client(); + let fills_cmd = GenerateFillReportsBuilder::default() + .ts_init(UnixNanos::default()) + .build() + .unwrap(); + + let error = client.generate_fill_reports(fills_cmd).await.unwrap_err(); + + assert_eq!(error.to_string(), "IB client not connected"); +} diff --git a/crates/adapters/interactive_brokers/src/execution/core_orders.rs b/crates/adapters/interactive_brokers/src/execution/core_orders.rs deleted file mode 100644 index a903f1eee915..000000000000 --- a/crates/adapters/interactive_brokers/src/execution/core_orders.rs +++ /dev/null @@ -1,692 +0,0 @@ -// ------------------------------------------------------------------------------------------------- -// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -// https://nautechsystems.io -// -// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html -// ------------------------------------------------------------------------------------------------- - -use nautilus_common::live::sender::EventSender; - -use super::*; - -#[allow(dead_code)] -impl InteractiveBrokersExecutionClient { - #[allow(clippy::too_many_arguments)] - pub(super) async fn handle_submit_order_async( - cmd: &SubmitOrder, - client: &Arc, - order_id_map: &Arc>>, - venue_order_id_map: &Arc>>, - instrument_id_map: &Arc>>, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - active_order_contexts: &Arc>>, - terminal_order_contexts: &Arc>>, - next_order_id: &Arc>, - instrument_provider: &Arc, - exec_sender: &EventSender, - clock: &'static AtomicTime, - account_id: AccountId, - order_submit_lock: &Arc>, - ) -> anyhow::Result<()> { - if cmd.order_init.post_only { - let ts_event = clock.get_time_ns(); - let event = OrderDenied::new( - cmd.order_init.trader_id, - cmd.strategy_id, - cmd.instrument_id, - cmd.order_init.client_order_id, - Ustr::from("`post_only` not supported by Interactive Brokers"), - UUID4::new(), - ts_event, - ts_event, - ); - exec_sender - .send(ExecutionEvent::Order(OrderEventAny::Denied(event))) - .map_err(|e| anyhow::anyhow!("Failed to send order denied event: {e}"))?; - anyhow::bail!("`post_only` not supported by Interactive Brokers"); - } - - let is_inverse = instrument_provider - .find(&cmd.instrument_id) - .map(|instrument| instrument.is_inverse()) - .unwrap_or(false); - - if cmd.order_init.quote_quantity && !is_inverse { - let ts_event = clock.get_time_ns(); - let event = OrderDenied::new( - cmd.order_init.trader_id, - cmd.strategy_id, - cmd.instrument_id, - cmd.order_init.client_order_id, - Ustr::from("UNSUPPORTED_QUOTE_QUANTITY"), - UUID4::new(), - ts_event, - ts_event, - ); - exec_sender - .send(ExecutionEvent::Order(OrderEventAny::Denied(event))) - .map_err(|e| anyhow::anyhow!("Failed to send order denied event: {e}"))?; - anyhow::bail!("UNSUPPORTED_QUOTE_QUANTITY"); - } - - if matches!( - cmd.order_init.order_type, - OrderType::TrailingStopMarket | OrderType::TrailingStopLimit - ) && let Some(trailing_offset_type) = cmd.order_init.trailing_offset_type - && trailing_offset_type != TrailingOffsetType::Price - { - let ts_event = clock.get_time_ns(); - let reason = format!( - "`TrailingOffsetType` {:?} is not supported (only PRICE is supported)", - trailing_offset_type - ); - let event = OrderDenied::new( - cmd.order_init.trader_id, - cmd.strategy_id, - cmd.instrument_id, - cmd.order_init.client_order_id, - Ustr::from(&reason), - UUID4::new(), - ts_event, - ts_event, - ); - exec_sender - .send(ExecutionEvent::Order(OrderEventAny::Denied(event))) - .map_err(|e| anyhow::anyhow!("Failed to send order denied event: {e}"))?; - anyhow::bail!("{}", reason); - } - - let contract = - Self::resolve_contract_for_instrument(cmd.instrument_id, instrument_provider)?; - let contract = Self::contract_with_order_exchange_param(contract, cmd.params.as_ref())?; - - let order_any = OrderAny::try_from(cmd.order_init.clone()) - .context("Failed to construct order from `OrderInitialized`")?; - let order_ref = cmd.order_init.client_order_id.to_string(); - let _submit_guard = order_submit_lock.lock().await; - let ib_order_id = Self::reserve_next_local_order_id(next_order_id)?; - let mut ib_order = nautilus_order_to_ib_order( - &order_any, - &contract, - instrument_provider, - ib_order_id, - &order_ref, - ) - .context("Failed to transform order")?; - let ib_account = account_id - .to_string() - .split_once('-') - .map_or_else(|| account_id.to_string(), |(_, value)| value.to_string()); - ib_order.account = ib_account.clone(); - ib_order.clearing_account = ib_account; - - Self::cache_order_tracking( - ib_order_id, - cmd.order_init.client_order_id, - cmd.instrument_id, - cmd.order_init.trader_id, - cmd.strategy_id, - cmd.order_init.order_side, - cmd.order_init.order_type, - order_id_map, - venue_order_id_map, - instrument_id_map, - trader_id_map, - strategy_id_map, - active_order_contexts, - terminal_order_contexts, - ); - - let ts_event = clock.get_time_ns(); - let event = OrderSubmitted::new( - cmd.order_init.trader_id, - cmd.strategy_id, - cmd.instrument_id, - cmd.order_init.client_order_id, - account_id, - UUID4::new(), - ts_event, - ts_event, - ); - - exec_sender - .send(ExecutionEvent::Order(OrderEventAny::Submitted(event))) - .map_err(|e| anyhow::anyhow!("Failed to send order submitted event: {e}"))?; - - if let Err(e) = client.submit_order(ib_order_id, &contract, &ib_order).await { - return Self::handle_order_submit_failure( - &e, - "Failed to submit order", - ib_order_id, - account_id, - ts_event, - order_id_map, - venue_order_id_map, - instrument_id_map, - trader_id_map, - strategy_id_map, - active_order_contexts, - terminal_order_contexts, - exec_sender, - clock, - ); - } - - Self::emit_order_accepted_if_needed( - ib_order_id, - VenueOrderId::from(ib_order_id.to_string()), - account_id, - ts_event, - active_order_contexts, - exec_sender, - )?; - - tracing::debug!( - "Submitted order {} as IB order ID {}", - cmd.order_init.client_order_id, - ib_order_id - ); - - Ok(()) - } - - #[allow(clippy::too_many_arguments)] - pub(super) async fn handle_modify_order_async( - cmd: &ModifyOrder, - client: &Arc, - order_id_map: &Arc>>, - venue_order_id_map: &Arc>>, - instrument_id_map: &Arc>>, - instrument_provider: &Arc, - _exec_sender: &EventSender, - _clock: &'static AtomicTime, - account_id: AccountId, - original_order: Option<&Arc>, - request_timeout_secs: u64, - ) -> anyhow::Result<()> { - let target_ib_order_id = Self::target_ib_order_id_for_modify( - cmd, - client, - order_id_map, - account_id, - request_timeout_secs, - ) - .await?; - - if let Some(original_order) = original_order { - let ib_order_id = target_ib_order_id.context("Order ID not found in mapping")?; - let contract = - Self::resolve_contract_for_instrument(cmd.instrument_id, instrument_provider)?; - let contract = Self::contract_with_order_exchange_param(contract, cmd.params.as_ref())?; - - let order_ref = original_order.client_order_id().to_string(); - let mut ib_order = nautilus_order_to_ib_order( - original_order, - &contract, - instrument_provider, - ib_order_id, - &order_ref, - ) - .context("Failed to transform order to IB order")?; - - Self::apply_modify_fields_to_ib_order(cmd, &mut ib_order, instrument_provider); - - if let Err(e) = client.submit_order(ib_order_id, &contract, &ib_order).await { - if Self::is_definitive_order_submit_error(&e) { - return Err(e).context("IB rejected the modified order before sending it"); - } - - tracing::warn!( - "Modify outcome is unknown after attempting to send order {} to IB: {e}", - cmd.client_order_id - ); - return Ok(()); - } - - tracing::debug!( - "Modified order {} (IB order ID: {})", - cmd.client_order_id, - ib_order_id - ); - - return Ok(()); - } - - Self::handle_modify_open_order_async( - cmd, - client, - target_ib_order_id, - order_id_map, - venue_order_id_map, - instrument_id_map, - instrument_provider, - request_timeout_secs, - ) - .await - } - - async fn target_ib_order_id_for_modify( - cmd: &ModifyOrder, - client: &Arc, - order_id_map: &Arc>>, - account_id: AccountId, - request_timeout_secs: u64, - ) -> anyhow::Result> { - if let Some(venue_order_id) = &cmd.venue_order_id { - let order_selector = IbOrderSelector::from_venue_order_id(venue_order_id)?; - let order_id = - Self::resolve_ib_order_id(client, order_selector, account_id, request_timeout_secs) - .await?; - return Ok(Some(order_id)); - } - - let map = order_id_map.lock(); - Ok(map.get(&cmd.client_order_id).copied()) - } - - fn apply_modify_fields_to_ib_order( - cmd: &ModifyOrder, - ib_order: &mut ibapi::orders::Order, - instrument_provider: &Arc, - ) { - if let Some(quantity) = cmd.quantity { - ib_order.total_quantity = quantity.as_f64(); - } - - let price_magnifier = instrument_provider.get_price_magnifier(&cmd.instrument_id) as f64; - - if let Some(price) = cmd.price { - ib_order.limit_price = Some(price.as_f64() / price_magnifier); - } - - if let Some(trigger_price) = cmd.trigger_price { - let converted_trigger_price = trigger_price.as_f64() / price_magnifier; - if matches!(ib_order.order_type.as_str(), "TRAIL" | "TRAIL LIMIT") { - ib_order.trail_stop_price = Some(converted_trigger_price); - } else { - ib_order.aux_price = Some(converted_trigger_price); - } - } - } - - #[allow(clippy::too_many_arguments)] - async fn handle_modify_open_order_async( - cmd: &ModifyOrder, - client: &Arc, - target_ib_order_id: Option, - order_id_map: &Arc>>, - venue_order_id_map: &Arc>>, - instrument_id_map: &Arc>>, - instrument_provider: &Arc, - request_timeout_secs: u64, - ) -> anyhow::Result<()> { - let timeout_dur = Duration::from_secs(request_timeout_secs); - let subscription = tokio::time::timeout(timeout_dur, client.all_open_orders()) - .await - .context("Timeout requesting open orders for modify")??; - let mut subscription = subscription.filter_data(); - - let client_order_id = cmd.client_order_id.to_string(); - - while let Some(order_result) = subscription.next().await { - match order_result { - Ok(Orders::OrderData(data)) => { - if !Self::is_active_open_order(&data.order) { - continue; - } - - let matches_order_id = - target_ib_order_id.is_some_and(|order_id| data.order_id == order_id); - let matches_order_ref = data.order.order_ref == client_order_id; - - if !matches_order_id && !matches_order_ref { - continue; - } - - let ib_order_id = data.order_id; - let contract = data.contract; - let contract = - Self::contract_with_order_exchange_param(contract, cmd.params.as_ref())?; - let mut ib_order = data.order; - - Self::apply_modify_fields_to_ib_order(cmd, &mut ib_order, instrument_provider); - - { - let mut map = order_id_map.lock(); - map.insert(cmd.client_order_id, ib_order_id); - } - { - let mut map = venue_order_id_map.lock(); - map.insert(ib_order_id, cmd.client_order_id); - } - { - let mut map = instrument_id_map.lock(); - map.insert(ib_order_id, cmd.instrument_id); - } - - if let Err(e) = client.submit_order(ib_order_id, &contract, &ib_order).await { - if Self::is_definitive_order_submit_error(&e) { - return Err(e) - .context("IB rejected the modified open order before sending it"); - } - - tracing::warn!( - "Modify outcome is unknown after attempting to send open order {} to IB: {e}", - cmd.client_order_id - ); - return Ok(()); - } - - tracing::debug!( - "Modified open order {} (IB order ID: {}) after cache miss", - cmd.client_order_id, - ib_order_id - ); - - return Ok(()); - } - Ok(_) => {} - Err(e) => { - tracing::warn!("Error receiving open order data for modify: {e}"); - } - } - } - - anyhow::bail!( - "Order not found for modify in IB open orders: client_order_id={}, venue_order_id={:?}", - cmd.client_order_id, - cmd.venue_order_id, - ) - } - - #[allow(clippy::too_many_arguments)] - pub(super) async fn handle_submit_order_list_async( - cmd: &SubmitOrderList, - orders: &[OrderAny], - client: &Arc, - order_id_map: &Arc>>, - venue_order_id_map: &Arc>>, - instrument_id_map: &Arc>>, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - active_order_contexts: &Arc>>, - terminal_order_contexts: &Arc>>, - next_order_id: &Arc>, - instrument_provider: &Arc, - exec_sender: &EventSender, - clock: &'static AtomicTime, - account_id: AccountId, - strategy_id: StrategyId, - order_submit_lock: &Arc>, - ) -> anyhow::Result<()> { - let num_orders = orders.len(); - anyhow::ensure!(!orders.is_empty(), "Cannot submit an empty order list"); - - let _submit_guard = order_submit_lock.lock().await; - let ib_account = account_id - .to_string() - .split_once('-') - .map_or_else(|| account_id.to_string(), |(_, value)| value.to_string()); - let mut ib_order_ids = AHashMap::with_capacity(num_orders); - - for order in orders { - let ib_order_id = Self::reserve_next_local_order_id(next_order_id)?; - ib_order_ids.insert(order.client_order_id(), ib_order_id); - } - - for order in orders { - if let Some(parent_order_id) = order.parent_order_id() - && !ib_order_ids.contains_key(&parent_order_id) - { - let map = order_id_map.lock(); - anyhow::ensure!( - map.contains_key(&parent_order_id), - "Parent order ID {parent_order_id} not found for order {}", - order.client_order_id(), - ); - } - } - - for (index, order) in orders.iter().enumerate() { - let is_last = index == num_orders - 1; - let ib_order_id = ib_order_ids[&order.client_order_id()]; - - let order_contract = - Self::resolve_contract_for_instrument(order.instrument_id(), instrument_provider)?; - let order_contract = - Self::contract_with_order_exchange_param(order_contract, cmd.params.as_ref())?; - - let order_ref = order.client_order_id().to_string(); - let mut ib_order = nautilus_order_to_ib_order( - order, - &order_contract, - instrument_provider, - ib_order_id, - &order_ref, - ) - .context("Failed to transform order")?; - ib_order.account = ib_account.clone(); - ib_order.clearing_account = ib_account.clone(); - ib_order.transmit = is_last; - - if let Some(parent_order_id) = order.parent_order_id() { - let parent_ib_order_id = ib_order_ids - .get(&parent_order_id) - .copied() - .or_else(|| order_id_map.lock().get(&parent_order_id).copied()); - - if let Some(parent_ib_order_id) = parent_ib_order_id { - ib_order.parent_id = parent_ib_order_id; - } - } - - Self::cache_order_tracking( - ib_order_id, - order.client_order_id(), - order.instrument_id(), - order.trader_id(), - strategy_id, - order.order_side(), - order.order_type(), - order_id_map, - venue_order_id_map, - instrument_id_map, - trader_id_map, - strategy_id_map, - active_order_contexts, - terminal_order_contexts, - ); - - let ts_event = clock.get_time_ns(); - let event = OrderSubmitted::new( - order.trader_id(), - strategy_id, - order.instrument_id(), - order.client_order_id(), - account_id, - UUID4::new(), - ts_event, - ts_event, - ); - - exec_sender - .send(ExecutionEvent::Order(OrderEventAny::Submitted(event))) - .map_err(|e| anyhow::anyhow!("Failed to send order submitted event: {e}"))?; - - if let Err(e) = client - .submit_order(ib_order_id, &order_contract, &ib_order) - .await - { - return Self::handle_order_submit_failure( - &e, - "Failed to submit order from list", - ib_order_id, - account_id, - ts_event, - order_id_map, - venue_order_id_map, - instrument_id_map, - trader_id_map, - strategy_id_map, - active_order_contexts, - terminal_order_contexts, - exec_sender, - clock, - ); - } - - Self::emit_order_accepted_if_needed( - ib_order_id, - VenueOrderId::from(ib_order_id.to_string()), - account_id, - ts_event, - active_order_contexts, - exec_sender, - )?; - - tracing::debug!( - "Submitted order {} from list as IB order ID {}", - order.client_order_id(), - ib_order_id, - ); - } - - Ok(()) - } - - #[allow(clippy::too_many_arguments)] - pub(super) fn handle_order_submit_failure( - error: &ibapi::Error, - failure_prefix: &str, - ib_order_id: i32, - account_id: AccountId, - ts_event: UnixNanos, - order_id_map: &Arc>>, - venue_order_id_map: &Arc>>, - instrument_id_map: &Arc>>, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - active_order_contexts: &Arc>>, - terminal_order_contexts: &Arc>>, - exec_sender: &EventSender, - clock: &'static AtomicTime, - ) -> anyhow::Result<()> { - match Self::classify_order_submit_error(error) { - CommandFailure::Ambiguous(reason) => { - anyhow::bail!( - "{failure_prefix}; outcome is unknown after possible transmission: {reason}" - ); - } - CommandFailure::NotSent(reason) | CommandFailure::VenueRejected(reason) => { - let context = Self::get_tracked_order_context( - ib_order_id, - active_order_contexts, - terminal_order_contexts, - ) - .with_context(|| format!("Tracked order context not found for {ib_order_id}"))?; - - Self::remove_order_tracking( - ib_order_id, - context.client_order_id, - order_id_map, - venue_order_id_map, - instrument_id_map, - trader_id_map, - strategy_id_map, - active_order_contexts, - terminal_order_contexts, - ); - - let reason = format!("{failure_prefix}: {reason}"); - let event = OrderRejected::new( - context.trader_id, - context.strategy_id, - context.instrument_id, - context.client_order_id, - account_id, - Ustr::from(&reason), - UUID4::new(), - ts_event, - clock.get_time_ns(), - false, - false, - ); - exec_sender - .send(ExecutionEvent::Order(OrderEventAny::Rejected(event))) - .map_err(|e| anyhow::anyhow!("Failed to send order rejected event: {e}"))?; - anyhow::bail!(reason); - } - } - } -} - -#[cfg(test)] -mod tests { - use nautilus_model::identifiers::Symbol; - - use super::*; - - fn modify_trigger_cmd() -> ModifyOrder { - ModifyOrder::new( - TraderId::from("TRADER-001"), - Some(ClientId::from("CLIENT-001")), - StrategyId::from("S-001"), - InstrumentId::new(Symbol::from("AAPL"), Venue::from("NASDAQ")), - ClientOrderId::from("O-001"), - Some(VenueOrderId::from("1")), - None, - None, - Some(Price::from("149.50")), - UUID4::new(), - UnixNanos::default(), - None, - None, - ) - } - - fn instrument_provider() -> Arc { - Arc::new(InteractiveBrokersInstrumentProvider::new( - crate::config::InteractiveBrokersInstrumentProviderConfig::default(), - )) - } - - #[rstest::rstest] - fn modify_trailing_stop_routes_trigger_to_trail_stop_price() { - let mut ib_order = ibapi::orders::Order { - order_type: "TRAIL".to_string(), - aux_price: Some(0.5), - trailing_percent: Some(0.25), - ..Default::default() - }; - - InteractiveBrokersExecutionClient::apply_modify_fields_to_ib_order( - &modify_trigger_cmd(), - &mut ib_order, - &instrument_provider(), - ); - - assert_eq!(ib_order.aux_price, Some(0.5)); - assert_eq!(ib_order.trailing_percent, Some(0.25)); - assert_eq!(ib_order.trail_stop_price, Some(149.5)); - } - - #[rstest::rstest] - fn modify_stop_order_routes_trigger_to_aux_price() { - let mut ib_order = ibapi::orders::Order { - order_type: "STP".to_string(), - ..Default::default() - }; - - InteractiveBrokersExecutionClient::apply_modify_fields_to_ib_order( - &modify_trigger_cmd(), - &mut ib_order, - &instrument_provider(), - ); - - assert_eq!(ib_order.aux_price, Some(149.5)); - assert_eq!(ib_order.trail_stop_price, None); - } -} diff --git a/crates/adapters/interactive_brokers/src/execution/core_tests.rs b/crates/adapters/interactive_brokers/src/execution/core_tests.rs deleted file mode 100644 index 05c80243c7db..000000000000 --- a/crates/adapters/interactive_brokers/src/execution/core_tests.rs +++ /dev/null @@ -1,3222 +0,0 @@ -// ------------------------------------------------------------------------------------------------- -// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -// https://nautechsystems.io -// -// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html -// ------------------------------------------------------------------------------------------------- - -use std::{cell::RefCell, rc::Rc, str::FromStr}; - -use ibapi::{ - contracts::{ - Contract, Currency as IBCurrency, Exchange, LegAction, OptionRight, SecurityType, - Symbol as IBSymbol, - }, - orders::{ - CommissionReport, Execution, ExecutionData, ExecutionSide, Liquidity, Order as IBOrder, - OrderData as IBOrderData, OrderState, OrderStatus as IBOrderStatus, OrderStatusKind, - OrderUpdate, - }, - subscriptions::Subscription, -}; -use nautilus_common::{ - cache::Cache, - live::{runner::replace_exec_event_sender, sender::EventSender}, -}; -use nautilus_live::{ExecutionClientCore, execution::failure::CommandFailure}; -use nautilus_model::{ - enums::{AccountType, AssetClass, LiquiditySide, OmsType, OrderSide, OrderType}, - events::OrderInitialized, - identifiers::{ - AccountId, ClientOrderId, InstrumentId, OrderListId, StrategyId, Symbol, TradeId, TraderId, - Venue, VenueOrderId, - }, - instruments::{InstrumentAny, OptionSpread, stubs::equity_aapl}, - orders::{OrderList, builder::OrderTestBuilder, stubs::TestOrderEventStubs}, - types::{Currency, Money, Price, Quantity}, -}; -use rstest::rstest; -use rust_decimal::Decimal; -use ustr::Ustr; - -use super::*; -use crate::common::consts::{IB_CLIENT_ID, IB_VENUE}; - -fn create_test_instrument_provider() -> Arc { - let config = crate::config::InteractiveBrokersInstrumentProviderConfig::default(); - Arc::new(InteractiveBrokersInstrumentProvider::new(config)) -} - -fn create_test_execution_client() -> ( - InteractiveBrokersExecutionClient, - tokio::sync::mpsc::UnboundedReceiver, - Rc>, -) { - let trader_id = TraderId::from("TESTER-001"); - let account_id = AccountId::from("IB-001"); - let cache = Rc::new(RefCell::new(Cache::default())); - let core = ExecutionClientCore::new( - trader_id, - *IB_CLIENT_ID, - *IB_VENUE, - OmsType::Netting, - account_id, - AccountType::Margin, - None, - cache.clone(), - ); - let instrument_provider = create_test_instrument_provider(); - let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); - replace_exec_event_sender(tx); - let client = InteractiveBrokersExecutionClient::new( - core, - InteractiveBrokersExecutionClientConfig::default(), - instrument_provider, - ) - .unwrap(); - - (client, rx, cache) -} - -fn create_test_spread_instrument() -> InstrumentId { - InstrumentId::new( - Symbol::from("(1)SPY C400_((1))SPY C410"), - Venue::from("SMART"), - ) -} - -fn create_test_leg_instrument() -> InstrumentId { - InstrumentId::new(Symbol::from("SPY C400"), Venue::from("SMART")) -} - -fn create_test_stock_instrument() -> InstrumentId { - InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")) -} - -fn create_test_limit_order(client_order_id: ClientOrderId) -> OrderAny { - OrderTestBuilder::new(OrderType::Limit) - .instrument_id(create_test_stock_instrument()) - .client_order_id(client_order_id) - .side(OrderSide::Buy) - .price(Price::from("100.00")) - .quantity(Quantity::from(1)) - .submit(true) - .build() -} - -fn create_test_accepted_limit_order( - trader_id: TraderId, - strategy_id: StrategyId, - instrument_id: InstrumentId, - client_order_id: ClientOrderId, - venue_order_id: VenueOrderId, - account_id: AccountId, -) -> OrderAny { - let mut order = OrderTestBuilder::new(OrderType::Limit) - .trader_id(trader_id) - .strategy_id(strategy_id) - .instrument_id(instrument_id) - .client_order_id(client_order_id) - .side(OrderSide::Buy) - .price(Price::from("100.00")) - .quantity(Quantity::from(1)) - .submit(true) - .build(); - let accepted = TestOrderEventStubs::accepted(&order, account_id, venue_order_id); - order.apply(accepted).unwrap(); - order -} - -fn create_tracked_order_context( - client_order_id: ClientOrderId, - instrument_id: InstrumentId, -) -> TrackedOrderContext { - TrackedOrderContext { - client_order_id, - trader_id: TraderId::from("TRADER-001"), - strategy_id: StrategyId::from("STRATEGY-001"), - instrument_id, - order_side: OrderSide::Buy, - order_type: OrderType::Limit, - accepted: false, - avg_px: None, - } -} - -struct SubmitTrackingState { - order_id_map: Arc>>, - venue_order_id_map: Arc>>, - instrument_id_map: Arc>>, - trader_id_map: Arc>>, - strategy_id_map: Arc>>, - active_order_contexts: Arc>>, - terminal_order_contexts: Arc>>, -} - -impl SubmitTrackingState { - fn new() -> Self { - Self { - order_id_map: Arc::new(Mutex::new(AHashMap::new())), - venue_order_id_map: Arc::new(Mutex::new(AHashMap::new())), - instrument_id_map: Arc::new(Mutex::new(AHashMap::new())), - trader_id_map: Arc::new(Mutex::new(AHashMap::new())), - strategy_id_map: Arc::new(Mutex::new(AHashMap::new())), - active_order_contexts: Arc::new(Mutex::new(AHashMap::new())), - terminal_order_contexts: Arc::new(Mutex::new(FifoCacheMap::new())), - } - } - - fn cache( - &self, - order_id: i32, - client_order_id: ClientOrderId, - instrument_id: InstrumentId, - trader_id: TraderId, - strategy_id: StrategyId, - ) { - InteractiveBrokersExecutionClient::cache_order_tracking( - order_id, - client_order_id, - instrument_id, - trader_id, - strategy_id, - OrderSide::Buy, - OrderType::Limit, - &self.order_id_map, - &self.venue_order_id_map, - &self.instrument_id_map, - &self.trader_id_map, - &self.strategy_id_map, - &self.active_order_contexts, - &self.terminal_order_contexts, - ); - } - - fn assert_active( - &self, - order_id: i32, - client_order_id: ClientOrderId, - instrument_id: InstrumentId, - trader_id: TraderId, - strategy_id: StrategyId, - accepted: bool, - ) { - assert_eq!( - self.order_id_map.lock().get(&client_order_id), - Some(&order_id) - ); - assert_eq!( - self.venue_order_id_map.lock().get(&order_id), - Some(&client_order_id) - ); - assert_eq!( - self.instrument_id_map.lock().get(&order_id), - Some(&instrument_id) - ); - assert_eq!(self.trader_id_map.lock().get(&order_id), Some(&trader_id)); - assert_eq!( - self.strategy_id_map.lock().get(&order_id), - Some(&strategy_id) - ); - - let contexts = self.active_order_contexts.lock(); - let context = contexts.get(&order_id).unwrap(); - assert_eq!(context.client_order_id, client_order_id); - assert_eq!(context.instrument_id, instrument_id); - assert_eq!(context.trader_id, trader_id); - assert_eq!(context.strategy_id, strategy_id); - assert_eq!(context.order_side, OrderSide::Buy); - assert_eq!(context.order_type, OrderType::Limit); - assert_eq!(context.accepted, accepted); - assert_eq!(context.avg_px, None); - assert!(self.terminal_order_contexts.lock().get(&order_id).is_none()); - } - - fn emit_accepted( - &self, - order_id: i32, - account_id: AccountId, - exec_sender: &EventSender, - ) -> bool { - InteractiveBrokersExecutionClient::emit_order_accepted_if_needed( - order_id, - VenueOrderId::from(order_id.to_string()), - account_id, - UnixNanos::new(29), - &self.active_order_contexts, - exec_sender, - ) - .unwrap() - } - - fn assert_absent(&self, order_id: i32, client_order_id: ClientOrderId) { - assert_eq!(self.order_id_map.lock().get(&client_order_id), None); - assert_eq!(self.venue_order_id_map.lock().get(&order_id), None); - assert_eq!(self.instrument_id_map.lock().get(&order_id), None); - assert_eq!(self.trader_id_map.lock().get(&order_id), None); - assert_eq!(self.strategy_id_map.lock().get(&order_id), None); - assert!(self.active_order_contexts.lock().get(&order_id).is_none()); - assert!(self.terminal_order_contexts.lock().get(&order_id).is_none()); - } -} - -async fn process_submitted_status( - order_id: i32, - state: &SubmitTrackingState, - exec_sender: &EventSender, -) { - InteractiveBrokersExecutionClient::handle_order_status( - &create_test_order_status(order_id, "Submitted"), - &state.order_id_map, - &state.venue_order_id_map, - &create_test_instrument_provider(), - exec_sender, - UnixNanos::new(27), - AccountId::from("IB-001"), - &state.instrument_id_map, - &state.trader_id_map, - &state.strategy_id_map, - &state.active_order_contexts, - &state.terminal_order_contexts, - &Arc::new(Mutex::new(AHashMap::new())), - &Arc::new(Mutex::new(AHashMap::new())), - &Arc::new(Mutex::new(AHashMap::new())), - &Arc::new(Mutex::new(AHashMap::new())), - &Arc::new(Mutex::new(ahash::AHashSet::new())), - &Arc::new(Mutex::new(AHashMap::new())), - ) - .await - .unwrap(); -} - -fn next_order_event( - rx: &mut tokio::sync::mpsc::UnboundedReceiver, -) -> OrderEventAny { - match rx.try_recv().unwrap() { - ExecutionEvent::Order(event) => event, - event => panic!("Expected order event, was {event:?}"), - } -} - -#[rstest] -#[case(1, 0, 1)] -#[case(1, 310, 310_000_001)] -#[case(42, 1_402, 402_000_042)] -#[case(450_000_123, 402, 450_000_123)] -#[case(1, -12, 12_000_001)] -fn apply_client_order_id_floor( - #[case] next_id: i32, - #[case] client_id: i32, - #[case] expected: i32, -) { - assert_eq!( - InteractiveBrokersExecutionClient::apply_client_order_id_floor(next_id, client_id), - expected - ); -} - -#[rstest] -fn ib_order_selector_parses_numeric_venue_order_id() { - let selector = IbOrderSelector::from_venue_order_id(&VenueOrderId::from("123")).unwrap(); - - assert_eq!(selector, IbOrderSelector::OrderId(123)); - assert!(selector.matches(123, 456)); - assert!(!selector.matches(124, 456)); - assert_eq!(selector.venue_order_id(), VenueOrderId::from("123")); -} - -#[rstest] -fn ib_order_selector_parses_perm_venue_order_id() { - let selector = IbOrderSelector::from_venue_order_id(&VenueOrderId::from("PERM-456")).unwrap(); - - assert_eq!(selector, IbOrderSelector::PermId(456)); - assert!(selector.matches(0, 456)); - assert!(selector.matches(123, 456)); - assert!(!selector.matches(123, 457)); - assert_eq!(selector.venue_order_id(), VenueOrderId::from("PERM-456")); -} - -#[rstest] -#[case( - "PERM-invalid", - "Failed to parse venue_order_id \"PERM-invalid\" as IB perm_id" -)] -#[case("invalid", "Failed to parse venue_order_id \"invalid\" as IB order_id")] -fn ib_order_selector_rejects_invalid_venue_order_id( - #[case] venue_order_id: &str, - #[case] expected: &str, -) { - let result = IbOrderSelector::from_venue_order_id(&VenueOrderId::from(venue_order_id)); - - assert_eq!(result.unwrap_err().to_string(), expected); -} - -#[rstest] -#[case(false, true)] -#[case(true, false)] -fn active_open_order_excludes_deactivated_records( - #[case] deactivate: bool, - #[case] expected: bool, -) { - let order = IBOrder { - deactivate, - ..Default::default() - }; - - assert_eq!( - InteractiveBrokersExecutionClient::is_active_open_order(&order), - expected - ); -} - -#[rstest] -fn order_submit_error_classifies_by_delivery_evidence() { - let invalid = ibapi::Error::InvalidArgument("invalid quantity".to_string()); - let unsupported = ibapi::Error::ServerVersion(100, 99, "feature".to_string()); - let rejection = ibapi::Error::Notice(ibapi::Notice { - code: 201, - message: "Order rejected".to_string(), - error_time: None, - advanced_order_reject_json: String::new(), - }); - let cancellation = ibapi::Error::Notice(ibapi::Notice { - code: 202, - message: "Order cancelled".to_string(), - error_time: None, - advanced_order_reject_json: String::new(), - }); - - assert_eq!( - InteractiveBrokersExecutionClient::classify_order_submit_error(&invalid), - CommandFailure::not_sent(invalid.to_string()) - ); - assert_eq!( - InteractiveBrokersExecutionClient::classify_order_submit_error(&unsupported), - CommandFailure::not_sent(unsupported.to_string()) - ); - assert_eq!( - InteractiveBrokersExecutionClient::classify_order_submit_error(&rejection), - CommandFailure::venue_rejected(rejection.to_string()) - ); - assert_eq!( - InteractiveBrokersExecutionClient::classify_order_submit_error(&cancellation), - CommandFailure::ambiguous(cancellation.to_string()) - ); - - for ambiguous in [ - ibapi::Error::ConnectionReset, - ibapi::Error::EndOfStream, - ibapi::Error::Io(std::io::Error::new( - std::io::ErrorKind::BrokenPipe, - "partial write", - )), - ibapi::Error::Io(std::io::Error::new( - std::io::ErrorKind::TimedOut, - "write timed out", - )), - ] { - assert_eq!( - InteractiveBrokersExecutionClient::classify_order_submit_error(&ambiguous), - CommandFailure::ambiguous(ambiguous.to_string()) - ); - } - - assert!(InteractiveBrokersExecutionClient::is_definitive_order_submit_error(&invalid)); - assert!(InteractiveBrokersExecutionClient::is_definitive_order_submit_error(&unsupported)); - assert!( - !InteractiveBrokersExecutionClient::is_definitive_order_submit_error( - &ibapi::Error::ConnectionReset - ) - ); -} - -#[rstest] -fn single_submit_definitive_failure_rejects_and_removes_tracking() { - let state = SubmitTrackingState::new(); - let order_id = 7101; - let client_order_id = ClientOrderId::from("O-SINGLE-NOT-SENT"); - let instrument_id = InstrumentId::new(Symbol::from("MSFT"), Venue::from("SMART")); - let trader_id = TraderId::from("TRADER-SINGLE-001"); - let strategy_id = StrategyId::from("STRATEGY-SINGLE-001"); - let account_id = AccountId::from("IB-SINGLE-001"); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - state.cache( - order_id, - client_order_id, - instrument_id, - trader_id, - strategy_id, - ); - - let result = InteractiveBrokersExecutionClient::handle_order_submit_failure( - &ibapi::Error::InvalidArgument("quantity must be positive".to_string()), - "Failed to submit order", - order_id, - account_id, - UnixNanos::new(19), - &state.order_id_map, - &state.venue_order_id_map, - &state.instrument_id_map, - &state.trader_id_map, - &state.strategy_id_map, - &state.active_order_contexts, - &state.terminal_order_contexts, - &exec_sender.clone().into(), - nautilus_core::time::get_atomic_clock_realtime(), - ); - - assert_eq!( - result.unwrap_err().to_string(), - "Failed to submit order: InvalidArgument: quantity must be positive" - ); - state.assert_absent(order_id, client_order_id); - - match exec_receiver.try_recv().unwrap() { - ExecutionEvent::Order(OrderEventAny::Rejected(event)) => { - assert_eq!(event.trader_id, trader_id); - assert_eq!(event.strategy_id, strategy_id); - assert_eq!(event.instrument_id, instrument_id); - assert_eq!(event.client_order_id, client_order_id); - assert_eq!(event.account_id, account_id); - assert_eq!( - event.reason, - "Failed to submit order: InvalidArgument: quantity must be positive" - ); - assert_eq!(event.ts_event, UnixNanos::new(19)); - assert!(!event.reconciliation); - assert!(!event.due_post_only); - } - event => panic!("Expected rejected order event, was {event:?}"), - } - assert!(exec_receiver.try_recv().is_err()); -} - -#[tokio::test] -async fn single_submit_ambiguous_failure_retains_tracking_for_status_resolution() { - let state = SubmitTrackingState::new(); - let order_id = 7102; - let client_order_id = ClientOrderId::from("O-SINGLE-AMBIGUOUS"); - let instrument_id = InstrumentId::new(Symbol::from("NVDA"), Venue::from("SMART")); - let trader_id = TraderId::from("TRADER-SINGLE-002"); - let strategy_id = StrategyId::from("STRATEGY-SINGLE-002"); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - state.cache( - order_id, - client_order_id, - instrument_id, - trader_id, - strategy_id, - ); - - let result = InteractiveBrokersExecutionClient::handle_order_submit_failure( - &ibapi::Error::Io(std::io::Error::new( - std::io::ErrorKind::TimedOut, - "socket write timed out", - )), - "Failed to submit order", - order_id, - AccountId::from("IB-001"), - UnixNanos::new(23), - &state.order_id_map, - &state.venue_order_id_map, - &state.instrument_id_map, - &state.trader_id_map, - &state.strategy_id_map, - &state.active_order_contexts, - &state.terminal_order_contexts, - &exec_sender.clone().into(), - nautilus_core::time::get_atomic_clock_realtime(), - ); - - assert_eq!( - result.unwrap_err().to_string(), - "Failed to submit order; outcome is unknown after possible transmission: socket write timed out" - ); - assert!(exec_receiver.try_recv().is_err()); - state.assert_active( - order_id, - client_order_id, - instrument_id, - trader_id, - strategy_id, - false, - ); - - process_submitted_status(order_id, &state, &exec_sender.clone().into()).await; - - match exec_receiver.try_recv().unwrap() { - ExecutionEvent::Order(OrderEventAny::Accepted(event)) => { - assert_eq!(event.trader_id, trader_id); - assert_eq!(event.strategy_id, strategy_id); - assert_eq!(event.instrument_id, instrument_id); - assert_eq!(event.client_order_id, client_order_id); - assert_eq!( - event.venue_order_id, - VenueOrderId::from(order_id.to_string()) - ); - assert_eq!(event.account_id, AccountId::from("IB-001")); - assert_eq!(event.ts_event, UnixNanos::new(27)); - } - event => panic!("Expected accepted order event, was {event:?}"), - } - assert!(exec_receiver.try_recv().is_err()); - state.assert_active( - order_id, - client_order_id, - instrument_id, - trader_id, - strategy_id, - true, - ); -} - -#[rstest] -fn list_submit_definitive_partial_failure_preserves_prefix_and_omits_tail() { - let state = SubmitTrackingState::new(); - let prior_id = 7201; - let current_id = 7202; - let tail_id = 7203; - let prior_client_id = ClientOrderId::from("O-LIST-PRIOR"); - let current_client_id = ClientOrderId::from("O-LIST-NOT-SENT"); - let tail_client_id = ClientOrderId::from("O-LIST-TAIL"); - let prior_instrument_id = InstrumentId::new(Symbol::from("AMD"), Venue::from("SMART")); - let current_instrument_id = InstrumentId::new(Symbol::from("INTC"), Venue::from("SMART")); - let trader_id = TraderId::from("TRADER-LIST-001"); - let strategy_id = StrategyId::from("STRATEGY-LIST-001"); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - state.cache( - prior_id, - prior_client_id, - prior_instrument_id, - trader_id, - strategy_id, - ); - state.cache( - current_id, - current_client_id, - current_instrument_id, - trader_id, - strategy_id, - ); - assert!(state.emit_accepted( - prior_id, - AccountId::from("IB-LIST-001"), - &exec_sender.clone().into() - )); - assert!(matches!( - exec_receiver.try_recv().unwrap(), - ExecutionEvent::Order(OrderEventAny::Accepted(event)) - if event.client_order_id == prior_client_id - && event.venue_order_id == VenueOrderId::from(prior_id.to_string()) - )); - - let error = ibapi::Error::ServerVersion(170, 169, "order feature".to_string()); - let result = InteractiveBrokersExecutionClient::handle_order_submit_failure( - &error, - "Failed to submit order from list", - current_id, - AccountId::from("IB-LIST-001"), - UnixNanos::new(31), - &state.order_id_map, - &state.venue_order_id_map, - &state.instrument_id_map, - &state.trader_id_map, - &state.strategy_id_map, - &state.active_order_contexts, - &state.terminal_order_contexts, - &exec_sender.clone().into(), - nautilus_core::time::get_atomic_clock_realtime(), - ); - - assert_eq!( - result.unwrap_err().to_string(), - format!("Failed to submit order from list: {error}") - ); - state.assert_active( - prior_id, - prior_client_id, - prior_instrument_id, - trader_id, - strategy_id, - true, - ); - state.assert_absent(current_id, current_client_id); - state.assert_absent(tail_id, tail_client_id); - - match exec_receiver.try_recv().unwrap() { - ExecutionEvent::Order(OrderEventAny::Rejected(event)) => { - assert_eq!(event.client_order_id, current_client_id); - assert_eq!(event.instrument_id, current_instrument_id); - assert_eq!(event.ts_event, UnixNanos::new(31)); - } - event => panic!("Expected rejected order event, was {event:?}"), - } - assert!(exec_receiver.try_recv().is_err()); -} - -#[tokio::test] -async fn list_submit_ambiguous_partial_failure_retains_attempted_children_only() { - let state = SubmitTrackingState::new(); - let prior_id = 7301; - let current_id = 7302; - let tail_id = 7303; - let prior_client_id = ClientOrderId::from("O-LIST-AMBIGUOUS-PRIOR"); - let current_client_id = ClientOrderId::from("O-LIST-AMBIGUOUS-CURRENT"); - let tail_client_id = ClientOrderId::from("O-LIST-AMBIGUOUS-TAIL"); - let prior_instrument_id = InstrumentId::new(Symbol::from("META"), Venue::from("SMART")); - let current_instrument_id = InstrumentId::new(Symbol::from("GOOG"), Venue::from("SMART")); - let trader_id = TraderId::from("TRADER-LIST-002"); - let strategy_id = StrategyId::from("STRATEGY-LIST-002"); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - state.cache( - prior_id, - prior_client_id, - prior_instrument_id, - trader_id, - strategy_id, - ); - state.cache( - current_id, - current_client_id, - current_instrument_id, - trader_id, - strategy_id, - ); - assert!(state.emit_accepted( - prior_id, - AccountId::from("IB-001"), - &exec_sender.clone().into() - )); - assert!(matches!( - exec_receiver.try_recv().unwrap(), - ExecutionEvent::Order(OrderEventAny::Accepted(event)) - if event.client_order_id == prior_client_id - && event.venue_order_id == VenueOrderId::from(prior_id.to_string()) - )); - - let result = InteractiveBrokersExecutionClient::handle_order_submit_failure( - &ibapi::Error::Io(std::io::Error::new( - std::io::ErrorKind::BrokenPipe, - "socket closed after partial write", - )), - "Failed to submit order from list", - current_id, - AccountId::from("IB-001"), - UnixNanos::new(37), - &state.order_id_map, - &state.venue_order_id_map, - &state.instrument_id_map, - &state.trader_id_map, - &state.strategy_id_map, - &state.active_order_contexts, - &state.terminal_order_contexts, - &exec_sender.clone().into(), - nautilus_core::time::get_atomic_clock_realtime(), - ); - - assert_eq!( - result.unwrap_err().to_string(), - "Failed to submit order from list; outcome is unknown after possible transmission: socket closed after partial write" - ); - assert!(exec_receiver.try_recv().is_err()); - state.assert_active( - prior_id, - prior_client_id, - prior_instrument_id, - trader_id, - strategy_id, - true, - ); - state.assert_active( - current_id, - current_client_id, - current_instrument_id, - trader_id, - strategy_id, - false, - ); - state.assert_absent(tail_id, tail_client_id); - - process_submitted_status(current_id, &state, &exec_sender.clone().into()).await; - - assert!(matches!( - exec_receiver.try_recv().unwrap(), - ExecutionEvent::Order(OrderEventAny::Accepted(event)) - if event.client_order_id == current_client_id - && event.venue_order_id == VenueOrderId::from(current_id.to_string()) - )); - assert!(exec_receiver.try_recv().is_err()); - state.assert_active( - prior_id, - prior_client_id, - prior_instrument_id, - trader_id, - strategy_id, - true, - ); - state.assert_active( - current_id, - current_client_id, - current_instrument_id, - trader_id, - strategy_id, - true, - ); - state.assert_absent(tail_id, tail_client_id); -} - -#[rstest] -fn submit_order_denies_reduce_only() { - let (client, mut rx, cache) = create_test_execution_client(); - let order = OrderTestBuilder::new(OrderType::Limit) - .instrument_id(create_test_stock_instrument()) - .client_order_id(ClientOrderId::from("O-IB-REDUCE-ONLY")) - .side(OrderSide::Sell) - .price(Price::from("100.00")) - .quantity(Quantity::from(1)) - .reduce_only(true) - .submit(true) - .build(); - cache - .borrow_mut() - .add_order(order.clone(), None, Some(*IB_CLIENT_ID), false) - .unwrap(); - let cmd = SubmitOrder::from_order( - &order, - client.core.trader_id, - Some(client.core.client_id), - None, - UUID4::new(), - UnixNanos::default(), - ); - - client.submit_order(cmd).unwrap(); - - match next_order_event(&mut rx) { - OrderEventAny::Denied(event) => { - assert_eq!(event.client_order_id, order.client_order_id()); - assert_eq!(event.reason, "UNSUPPORTED_REDUCE_ONLY"); - } - event => panic!("Expected OrderDenied, was {event:?}"), - } - assert!(rx.try_recv().is_err()); -} - -#[rstest] -fn submit_order_denies_when_client_not_ready() { - let (client, mut rx, cache) = create_test_execution_client(); - let order = create_test_limit_order(ClientOrderId::from("O-IB-001")); - cache - .borrow_mut() - .add_order(order.clone(), None, Some(*IB_CLIENT_ID), false) - .unwrap(); - let cmd = SubmitOrder::from_order( - &order, - client.core.trader_id, - Some(client.core.client_id), - None, - UUID4::new(), - UnixNanos::default(), - ); - - client.submit_order(cmd).unwrap(); - - match next_order_event(&mut rx) { - OrderEventAny::Denied(event) => { - assert_eq!(event.client_order_id, order.client_order_id()); - assert_eq!( - event.reason.to_string(), - "Interactive Brokers client is not ready; refusing to submit order" - ); - } - event => panic!("Expected OrderDenied, was {event:?}"), - } -} - -#[rstest] -fn submit_order_list_denies_all_orders_when_reduce_only_is_present() { - let (client, mut rx, cache) = create_test_execution_client(); - let reduce_only = OrderTestBuilder::new(OrderType::Limit) - .instrument_id(create_test_stock_instrument()) - .client_order_id(ClientOrderId::from("O-IB-REDUCE-ONLY")) - .side(OrderSide::Sell) - .price(Price::from("100.00")) - .quantity(Quantity::from(1)) - .reduce_only(true) - .submit(true) - .build(); - let regular = create_test_limit_order(ClientOrderId::from("O-IB-REGULAR")); - for order in [&reduce_only, ®ular] { - cache - .borrow_mut() - .add_order(order.clone(), None, Some(*IB_CLIENT_ID), false) - .unwrap(); - } - let order_list = OrderList::new( - OrderListId::from("OL-IB-REDUCE-ONLY"), - reduce_only.instrument_id(), - reduce_only.strategy_id(), - vec![reduce_only.client_order_id(), regular.client_order_id()], - UnixNanos::default(), - ); - let cmd = SubmitOrderList::new( - client.core.trader_id, - Some(client.core.client_id), - reduce_only.strategy_id(), - order_list, - vec![ - OrderInitialized::from(&reduce_only), - OrderInitialized::from(®ular), - ], - None, - None, - None, - UUID4::new(), - UnixNanos::default(), - None, - ); - - client.submit_order_list(cmd).unwrap(); - - for client_order_id in [reduce_only.client_order_id(), regular.client_order_id()] { - match next_order_event(&mut rx) { - OrderEventAny::Denied(event) => { - assert_eq!(event.client_order_id, client_order_id); - assert_eq!(event.reason, "UNSUPPORTED_REDUCE_ONLY"); - } - event => panic!("Expected OrderDenied, was {event:?}"), - } - } - assert!(rx.try_recv().is_err()); -} - -#[rstest] -fn submit_order_list_denies_all_orders_when_client_not_ready() { - let (client, mut rx, cache) = create_test_execution_client(); - let order1 = create_test_limit_order(ClientOrderId::from("O-IB-001")); - let order2 = create_test_limit_order(ClientOrderId::from("O-IB-002")); - for order in [&order1, &order2] { - cache - .borrow_mut() - .add_order(order.clone(), None, Some(*IB_CLIENT_ID), false) - .unwrap(); - } - let order_list = OrderList::new( - OrderListId::from("OL-IB-001"), - order1.instrument_id(), - order1.strategy_id(), - vec![order1.client_order_id(), order2.client_order_id()], - UnixNanos::default(), - ); - let cmd = SubmitOrderList::new( - client.core.trader_id, - Some(client.core.client_id), - order1.strategy_id(), - order_list, - vec![ - OrderInitialized::from(&order1), - OrderInitialized::from(&order2), - ], - None, - None, - None, - UUID4::new(), - UnixNanos::default(), - None, - ); - - client.submit_order_list(cmd).unwrap(); - - for expected_client_order_id in [order1.client_order_id(), order2.client_order_id()] { - match next_order_event(&mut rx) { - OrderEventAny::Denied(event) => { - assert_eq!(event.client_order_id, expected_client_order_id); - assert_eq!( - event.reason.to_string(), - "Interactive Brokers client is not ready; refusing to submit order list" - ); - } - event => panic!("Expected OrderDenied, was {event:?}"), - } - } -} - -#[rstest] -fn modify_order_rejects_when_client_not_ready() { - let (client, mut rx, _) = create_test_execution_client(); - let order = create_test_limit_order(ClientOrderId::from("O-IB-001")); - let cmd = ModifyOrder::new( - client.core.trader_id, - Some(client.core.client_id), - order.strategy_id(), - order.instrument_id(), - order.client_order_id(), - Some(VenueOrderId::from("1001")), - Some(Quantity::from(2)), - Some(Price::from("101.00")), - None, - UUID4::new(), - UnixNanos::default(), - None, - None, - ); - - client.modify_order(cmd).unwrap(); - - match next_order_event(&mut rx) { - OrderEventAny::ModifyRejected(event) => { - assert_eq!(event.trader_id, client.core.trader_id); - assert_eq!(event.client_order_id, order.client_order_id()); - assert_eq!(event.instrument_id, order.instrument_id()); - assert_eq!(event.strategy_id, order.strategy_id()); - assert_eq!(event.venue_order_id, Some(VenueOrderId::from("1001"))); - assert_eq!(event.account_id, Some(client.core.account_id)); - assert_eq!(event.ts_init, event.ts_event); - assert!(!event.reconciliation); - assert_eq!(event.causation_id, None); - assert_eq!( - event.reason.to_string(), - "Interactive Brokers client is not ready; refusing to modify order" - ); - } - event => panic!("Expected OrderModifyRejected, was {event:?}"), - } - assert!(rx.try_recv().is_err()); -} - -#[rstest] -fn cancel_order_rejects_when_client_not_ready() { - let (client, mut rx, cache) = create_test_execution_client(); - let target_trader_id = TraderId::from("TRADER-TARGET"); - let target_strategy_id = StrategyId::from("STRATEGY-TARGET"); - let instrument_id = create_test_stock_instrument(); - let client_order_id = ClientOrderId::from("O-IB-001"); - let venue_order_id = VenueOrderId::from("1001"); - let order = create_test_accepted_limit_order( - target_trader_id, - target_strategy_id, - instrument_id, - client_order_id, - venue_order_id, - client.core.account_id, - ); - cache - .borrow_mut() - .add_order(order, None, Some(*IB_CLIENT_ID), false) - .unwrap(); - let cmd = CancelOrder::new( - TraderId::from("TRADER-REQUESTER"), - Some(client.core.client_id), - StrategyId::from("STRATEGY-REQUESTER"), - instrument_id, - client_order_id, - Some(venue_order_id), - UUID4::new(), - UnixNanos::default(), - None, - None, - ); - - client.cancel_order(cmd).unwrap(); - - match next_order_event(&mut rx) { - OrderEventAny::CancelRejected(event) => { - assert_eq!(event.trader_id, target_trader_id); - assert_eq!(event.client_order_id, client_order_id); - assert_eq!(event.instrument_id, instrument_id); - assert_eq!(event.strategy_id, target_strategy_id); - assert_eq!(event.venue_order_id, Some(venue_order_id)); - assert_eq!(event.account_id, Some(client.core.account_id)); - assert_eq!(event.ts_init, event.ts_event); - assert!(!event.reconciliation); - assert_eq!(event.causation_id, None); - assert_eq!( - event.reason.to_string(), - "Interactive Brokers client is not ready; refusing to cancel order" - ); - } - event => panic!("Expected OrderCancelRejected, was {event:?}"), - } - assert!(rx.try_recv().is_err()); -} - -#[rstest] -fn cancel_all_orders_emits_no_events_when_client_not_ready() { - let (client, mut rx, cache) = create_test_execution_client(); - let order = create_test_limit_order(ClientOrderId::from("O-IB-001")); - let accepted = OrderEventAny::Accepted(OrderAccepted::new( - order.trader_id(), - order.strategy_id(), - order.instrument_id(), - order.client_order_id(), - VenueOrderId::from("1001"), - client.core.account_id, - UUID4::new(), - UnixNanos::default(), - UnixNanos::default(), - false, - )); - { - let mut cache = cache.borrow_mut(); - cache - .add_order(order.clone(), None, Some(client.core.client_id), false) - .unwrap(); - cache.update_order(&accepted).unwrap(); - } - let cmd = CancelAllOrders::new( - client.core.trader_id, - Some(client.core.client_id), - order.strategy_id(), - order.instrument_id(), - None, - UUID4::new(), - UnixNanos::default(), - None, - None, - ); - - client.cancel_all_orders(cmd).unwrap(); - - // A whole-request local failure must not become one rejection per order - assert!(rx.try_recv().is_err(), "expected no events"); -} - -fn create_test_execution_data( - order_id: i32, - execution_id: &str, - shares: f64, - price: f64, - side: &str, -) -> ExecutionData { - let contract = Contract { - contract_id: 12345, - symbol: IBSymbol::from("SPY"), - security_type: SecurityType::Option, - last_trade_date_or_contract_month: String::from("20250101"), - strike: 400.0, - right: Some(OptionRight::Call), - multiplier: String::from("100"), - exchange: Exchange::from("SMART"), - currency: IBCurrency::from("USD"), - local_symbol: String::from("SPY C400"), - trading_class: String::new(), - combo_legs: vec![], - ..Default::default() - }; - - let execution = Execution { - execution_id: execution_id.to_string(), - order_id, - time: String::from("20250101 08:00:00"), - side: if side == "BOT" { - ExecutionSide::Bought - } else { - ExecutionSide::Sold - }, - shares, - price, - perm_id: 0, - client_id: 0, - liquidation: 0, - account_number: String::new(), - exchange: String::new(), - cumulative_quantity: shares, - average_price: price, - order_reference: String::new(), - ev_rule: String::new(), - ev_multiplier: None, - model_code: String::new(), - last_liquidity: Liquidity::None, - pending_price_revision: false, - submitter: String::new(), - }; - - ExecutionData { - request_id: 0, - contract, - execution, - } -} - -fn create_test_stock_execution_data( - contract_id: i32, - order_id: i32, - execution_id: &str, -) -> ExecutionData { - let contract = Contract { - contract_id, - symbol: IBSymbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("SMART"), - currency: IBCurrency::from("USD"), - ..Default::default() - }; - - let execution = Execution { - execution_id: execution_id.to_string(), - order_id, - time: String::from("20250101 08:00:00"), - side: ExecutionSide::Bought, - shares: 10.0, - price: 150.25, - perm_id: 0, - client_id: 0, - liquidation: 0, - account_number: String::new(), - exchange: String::new(), - cumulative_quantity: 10.0, - average_price: 150.25, - order_reference: String::from("O-IB-001"), - ev_rule: String::new(), - ev_multiplier: None, - model_code: String::new(), - last_liquidity: Liquidity::None, - pending_price_revision: false, - submitter: String::new(), - }; - - ExecutionData { - request_id: 0, - contract, - execution, - } -} - -fn create_test_bag_execution_data(order_id: i32, execution_id: &str) -> ExecutionData { - let contract = Contract { - contract_id: 0, - symbol: IBSymbol::from("SPY"), - security_type: SecurityType::Spread, - exchange: Exchange::from("SMART"), - currency: IBCurrency::from("USD"), - combo_legs: vec![ - ibapi::contracts::ComboLeg { - contract_id: 12345, - ratio: 1, - action: LegAction::Buy, - exchange: String::from("SMART"), - open_close: ibapi::contracts::ComboLegOpenClose::Same, - short_sale_slot: 0, - designated_location: String::new(), - exempt_code: 0, - }, - ibapi::contracts::ComboLeg { - contract_id: 67890, - ratio: 1, - action: LegAction::Sell, - exchange: String::from("SMART"), - open_close: ibapi::contracts::ComboLegOpenClose::Same, - short_sale_slot: 0, - designated_location: String::new(), - exempt_code: 0, - }, - ], - ..Default::default() - }; - - let execution = Execution { - execution_id: execution_id.to_string(), - order_id, - time: String::from("20250101 08:00:00"), - side: ExecutionSide::Bought, - shares: 1.0, - price: 1.25, - perm_id: 0, - client_id: 0, - liquidation: 0, - account_number: String::new(), - exchange: String::new(), - cumulative_quantity: 1.0, - average_price: 1.25, - order_reference: String::from("O-IB-SPREAD"), - ev_rule: String::new(), - ev_multiplier: None, - model_code: String::new(), - last_liquidity: Liquidity::None, - pending_price_revision: false, - submitter: String::new(), - }; - - ExecutionData { - request_id: 0, - contract, - execution, - } -} - -fn create_pending_combo_fill( - client_order_id: ClientOrderId, - quantity: Quantity, -) -> PendingComboFill { - PendingComboFill { - trader_id: TraderId::from("TRADER-001"), - strategy_id: StrategyId::from("STRATEGY-001"), - account_id: AccountId::from("IB-001"), - instrument_id: create_test_spread_instrument(), - venue_order_id: VenueOrderId::from("7001"), - trade_id: TradeId::from("T-001"), - order_side: OrderSide::Buy, - order_type: OrderType::Limit, - last_qty: quantity, - commission: Money::new(1.0, Currency::USD()), - liquidity_side: LiquiditySide::NoLiquiditySide, - quote_currency: Currency::USD(), - client_order_id, - ts_event: UnixNanos::new(1), - ts_init: UnixNanos::new(1), - } -} - -fn create_test_option_spread() -> OptionSpread { - OptionSpread::builder() - .instrument_id(create_test_spread_instrument()) - .raw_symbol(Symbol::from("(1)SPY C400_((1))SPY C410")) - .asset_class(AssetClass::Equity) - .exchange(Ustr::from("SMART")) - .underlying(Ustr::from("SPY")) - .strategy_type(Ustr::from("VERTICAL")) - .activation_ns(UnixNanos::new(0)) - .expiration_ns(UnixNanos::new(0)) - .currency(Currency::USD()) - .price_precision(2) - .price_increment(Price::from("0.01")) - .multiplier(Quantity::from(100)) - .lot_size(Quantity::from(1)) - .ts_event(UnixNanos::new(0)) - .ts_init(UnixNanos::new(0)) - .build() - .unwrap() -} - -fn create_test_order_status(order_id: i32, status: &str) -> IBOrderStatus { - IBOrderStatus { - order_id, - status: OrderStatusKind::from_str(status).unwrap(), - filled: 0.0, - remaining: 0.0, - average_fill_price: Some(0.0), - perm_id: 0, - parent_id: 0, - last_fill_price: Some(0.0), - client_id: 0, - why_held: String::new(), - market_cap_price: Some(0.0), - } -} - -fn create_test_open_order(order_id: i32, status: &str, order_ref: &str) -> IBOrderData { - IBOrderData { - order_id, - contract: Contract { - contract_id: 12345, - symbol: IBSymbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("SMART"), - currency: IBCurrency::from("USD"), - ..Default::default() - }, - order: IBOrder { - order_ref: order_ref.to_string(), - ..Default::default() - }, - order_state: OrderState { - status: OrderStatusKind::from_str(status).unwrap(), - ..Default::default() - }, - } -} - -#[rstest] -#[case(false, "Submitted")] -#[case(true, "PreSubmitted")] -#[tokio::test] -async fn handle_order_update_ignores_deactivated_open_order( - #[case] what_if: bool, - #[case] status: &str, -) { - let order_id = 7009; - let client_order_id = ClientOrderId::from("O-DEACTIVATED"); - let equity = equity_aapl(); - let instrument_id = equity.id(); - let order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let venue_order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let instrument_provider = create_test_instrument_provider(); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); - let instrument_id_map = Arc::new(Mutex::new(AHashMap::new())); - let trader_id_map = Arc::new(Mutex::new(AHashMap::new())); - let strategy_id_map = Arc::new(Mutex::new(AHashMap::new())); - let active_order_contexts = Arc::new(Mutex::new(AHashMap::new())); - let terminal_order_contexts = Arc::new(Mutex::new(FifoCacheMap::new())); - let spread_fill_tracking = Arc::new(Mutex::new(AHashMap::new())); - let order_avg_prices = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fills = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fill_avgs = Arc::new(Mutex::new(AHashMap::new())); - let order_fill_progress = Arc::new(Mutex::new(AHashMap::new())); - let pending_cancel_orders = Arc::new(Mutex::new(ahash::AHashSet::new())); - let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); - let mut open_order = create_test_open_order(order_id, status, client_order_id.as_str()); - open_order.order.what_if = what_if; - open_order.order.deactivate = true; - open_order.order.total_quantity = 1.0; - - instrument_provider.insert_test_instrument(InstrumentAny::from(equity), 12345, 1); - order_id_map.lock().insert(client_order_id, order_id); - venue_order_id_map.lock().insert(order_id, client_order_id); - instrument_id_map.lock().insert(order_id, instrument_id); - trader_id_map - .lock() - .insert(order_id, TraderId::from("TRADER-001")); - strategy_id_map - .lock() - .insert(order_id, StrategyId::from("STRATEGY-001")); - active_order_contexts.lock().insert( - order_id, - create_tracked_order_context(client_order_id, instrument_id), - ); - - let result = InteractiveBrokersExecutionClient::handle_order_update( - &OrderUpdate::OpenOrder(open_order), - &order_id_map, - &venue_order_id_map, - &instrument_provider, - &exec_sender.clone().into(), - nautilus_core::time::get_atomic_clock_realtime(), - AccountId::from("IB-001"), - &commission_cache, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &active_order_contexts, - &terminal_order_contexts, - &spread_fill_tracking, - &order_avg_prices, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - &pending_cancel_orders, - &pending_execution_cache, - ) - .await; - - result.unwrap(); - assert!(exec_receiver.try_recv().is_err()); - assert_eq!(order_id_map.lock().get(&client_order_id), Some(&order_id)); - assert_eq!( - venue_order_id_map.lock().get(&order_id), - Some(&client_order_id) - ); - assert_eq!( - instrument_id_map.lock().get(&order_id), - Some(&instrument_id) - ); - assert!( - !active_order_contexts - .lock() - .get(&order_id) - .unwrap() - .accepted - ); -} - -#[rstest] -fn test_remove_order_tracking_clears_submit_identity() { - let order_id = 7008; - let client_order_id = ClientOrderId::from("O-SUBMIT-FAIL"); - let instrument_id = create_test_stock_instrument(); - let order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let venue_order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let instrument_id_map = Arc::new(Mutex::new(AHashMap::new())); - let trader_id_map = Arc::new(Mutex::new(AHashMap::new())); - let strategy_id_map = Arc::new(Mutex::new(AHashMap::new())); - let active_order_contexts = Arc::new(Mutex::new(AHashMap::new())); - let terminal_order_contexts = Arc::new(Mutex::new(FifoCacheMap::new())); - - InteractiveBrokersExecutionClient::cache_order_tracking( - order_id, - client_order_id, - instrument_id, - TraderId::from("TRADER-001"), - StrategyId::from("STRATEGY-001"), - OrderSide::Buy, - OrderType::Limit, - &order_id_map, - &venue_order_id_map, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &active_order_contexts, - &terminal_order_contexts, - ); - - InteractiveBrokersExecutionClient::remove_order_tracking( - order_id, - client_order_id, - &order_id_map, - &venue_order_id_map, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &active_order_contexts, - &terminal_order_contexts, - ); - - assert!(order_id_map.lock().is_empty()); - assert!(venue_order_id_map.lock().is_empty()); - assert!(instrument_id_map.lock().is_empty()); - assert!(trader_id_map.lock().is_empty()); - assert!(strategy_id_map.lock().is_empty()); - assert!(active_order_contexts.lock().is_empty()); - assert!(terminal_order_contexts.lock().is_empty()); -} - -#[tokio::test] -async fn test_get_leg_position_standard_spread() { - let spread_id = InstrumentId::new( - Symbol::from("(1)SPY C400_((1))SPY C410"), - Venue::from("SMART"), - ); - let leg_id = InstrumentId::new(Symbol::from("SPY C400"), Venue::from("SMART")); - - let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id); - assert_eq!(result, 0); // First leg is at position 0 -} - -#[tokio::test] -async fn test_get_leg_position_second_leg() { - let spread_id = InstrumentId::new( - Symbol::from("(1)SPY C400_((1))SPY C410"), - Venue::from("SMART"), - ); - let leg_id = InstrumentId::new(Symbol::from("SPY C410"), Venue::from("SMART")); - - let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id); - assert_eq!(result, 1); // Second leg is at position 1 -} - -#[tokio::test] -async fn test_get_leg_position_ratio_spread() { - let spread_id = InstrumentId::new( - Symbol::from("(1)E4DN5 P6350_((2))E4DN5 P6355"), - Venue::from("XCME"), - ); - let leg_id = InstrumentId::new(Symbol::from("E4DN5 P6350"), Venue::from("XCME")); - - let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id); - assert_eq!(result, 0); -} - -#[tokio::test] -async fn test_get_leg_position_not_found() { - let spread_id = InstrumentId::new( - Symbol::from("(1)SPY C400_((1))SPY C410"), - Venue::from("SMART"), - ); - let leg_id = InstrumentId::new(Symbol::from("SPY C420"), Venue::from("SMART")); - - let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id); - // Should fallback to position 0 - assert_eq!(result, 0); -} - -#[tokio::test] -async fn test_get_leg_instrument_id_and_ratio() { - let instrument_provider = create_test_instrument_provider(); - let leg_id = create_test_leg_instrument(); - - // Create a contract with combo legs - let contract = Contract { - contract_id: 12345, - symbol: IBSymbol::from("SPY"), - security_type: SecurityType::Option, - last_trade_date_or_contract_month: String::from("20250101"), - strike: 400.0, - right: Some(OptionRight::Call), - multiplier: String::from("100"), - exchange: Exchange::from("SMART"), - currency: IBCurrency::from("USD"), - local_symbol: String::from("SPY C400"), - trading_class: String::new(), - combo_legs: vec![ibapi::contracts::ComboLeg { - contract_id: 12345, - ratio: 1, - action: LegAction::Buy, - exchange: String::from("SMART"), - open_close: ibapi::contracts::ComboLegOpenClose::Same, - short_sale_slot: 0, - designated_location: String::new(), - exempt_code: 0, - }], - ..Default::default() - }; - - let result = InteractiveBrokersExecutionClient::get_leg_instrument_id_and_ratio( - &contract, - &leg_id, - &instrument_provider, - ); - let (returned_leg_id, ratio) = result; - // Since we can't easily mock the contract ID mapping, it should fallback to the provided leg_id - assert_eq!(returned_leg_id, leg_id); - // Fallback ratio is 1 - assert_eq!(ratio, 1); -} - -#[tokio::test] -async fn test_get_leg_instrument_id_and_ratio_with_sell_action() { - let instrument_provider = create_test_instrument_provider(); - let leg_id = create_test_leg_instrument(); - - let contract = Contract { - contract_id: 12345, - symbol: IBSymbol::from("SPY"), - security_type: SecurityType::Option, - last_trade_date_or_contract_month: String::from("20250101"), - strike: 400.0, - right: Some(OptionRight::Call), - multiplier: String::from("100"), - exchange: Exchange::from("SMART"), - currency: IBCurrency::from("USD"), - local_symbol: String::from("SPY C400"), - trading_class: String::new(), - combo_legs: vec![ibapi::contracts::ComboLeg { - contract_id: 12345, - ratio: 2, - action: LegAction::Sell, - exchange: String::from("SMART"), - open_close: ibapi::contracts::ComboLegOpenClose::Same, - short_sale_slot: 0, - designated_location: String::new(), - exempt_code: 0, - }], - ..Default::default() - }; - - let result = InteractiveBrokersExecutionClient::get_leg_instrument_id_and_ratio( - &contract, - &leg_id, - &instrument_provider, - ); - let (_, ratio) = result; - // Should fallback to ratio 1 - assert_eq!(ratio, 1); -} - -#[rstest] -fn test_cached_spread_instrument_ids_for_preload_deduplicates_spread_orders() { - let instrument_provider = create_test_instrument_provider(); - let mut cache = Cache::default(); - let spread_instrument_id = create_test_spread_instrument(); - - let order_one = OrderTestBuilder::new(OrderType::Limit) - .instrument_id(spread_instrument_id) - .side(OrderSide::Buy) - .price(Price::from("1.00")) - .quantity(Quantity::from(1)) - .build(); - let order_two = OrderTestBuilder::new(OrderType::Limit) - .instrument_id(spread_instrument_id) - .client_order_id(ClientOrderId::from("O-SPREAD-002")) - .side(OrderSide::Buy) - .price(Price::from("1.00")) - .quantity(Quantity::from(2)) - .build(); - - cache.add_order(order_one, None, None, false).unwrap(); - cache.add_order(order_two, None, None, false).unwrap(); - - let spread_ids = InteractiveBrokersExecutionClient::cached_spread_instrument_ids_for_preload( - &cache, - &instrument_provider, - ); - - assert_eq!(spread_ids, vec![spread_instrument_id]); -} - -#[rstest] -fn test_cached_spread_instrument_ids_for_preload_ignores_non_spread_orders() { - let instrument_provider = create_test_instrument_provider(); - let mut cache = Cache::default(); - let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("SMART")); - - let order = OrderTestBuilder::new(OrderType::Limit) - .instrument_id(instrument_id) - .side(OrderSide::Buy) - .price(Price::from("1.00")) - .quantity(Quantity::from(1)) - .build(); - - cache.add_order(order, None, None, false).unwrap(); - - let spread_ids = InteractiveBrokersExecutionClient::cached_spread_instrument_ids_for_preload( - &cache, - &instrument_provider, - ); - - assert!(spread_ids.is_empty()); -} - -#[rstest] -fn test_parse_historical_fill_report_uses_provider_resolved_stock_venue() { - let (client, _, _) = create_test_execution_client(); - let equity = equity_aapl(); - let instrument_id = equity.id(); - client - .instrument_provider - .insert_test_instrument(InstrumentAny::from(equity), 265598, 1); - let exec_data = create_test_stock_execution_data(0, 123, "exec-aapl-001"); - let cmd = GenerateFillReportsBuilder::default() - .ts_init(UnixNanos::default()) - .build() - .unwrap(); - - let report = client - .parse_historical_fill_report(&cmd, &exec_data, 1.25, "USD", UnixNanos::default()) - .unwrap(); - - assert_eq!(report.instrument_id, instrument_id); - assert_eq!( - report.client_order_id, - Some(ClientOrderId::from("O-IB-001")) - ); - assert_eq!(report.trade_id, TradeId::from("exec-aapl-001")); - assert_eq!(report.venue_order_id, VenueOrderId::from("123")); - assert_eq!(report.last_qty, Quantity::from(10)); - assert_eq!(report.last_px, Price::from("150.25")); -} - -#[rstest] -fn test_report_contract_resolution_preserves_canonical_opra_id() { - let (client, _, _) = create_test_execution_client(); - let instrument_id = InstrumentId::from("SPY 250101C00400000.OPRA"); - client - .instrument_provider - .insert_test_contract_id_mapping(12_345, instrument_id); - let exec_data = create_test_execution_data(123, "exec-opra-001", 1.0, 1.25, "BOT"); - - let resolved = client - .resolve_report_contract_instrument_id(&exec_data.contract) - .unwrap(); - - assert_eq!(resolved, instrument_id); -} - -#[rstest] -fn test_parse_historical_fill_report_uses_cached_bag_spread_id() { - let (client, _, _) = create_test_execution_client(); - let spread = create_test_option_spread(); - let instrument_id = spread.id; - client - .instrument_provider - .insert_test_instrument(InstrumentAny::from(spread), 54321, 1); - client - .instrument_provider - .insert_test_contract_id_mapping(12345, create_test_leg_instrument()); - client.instrument_provider.insert_test_contract_id_mapping( - 67890, - InstrumentId::new(Symbol::from("SPY C410"), Venue::from("SMART")), - ); - let exec_data = create_test_bag_execution_data(7001, "exec-spread-001"); - let cmd = GenerateFillReportsBuilder::default() - .ts_init(UnixNanos::default()) - .build() - .unwrap(); - - let report = client - .parse_historical_fill_report(&cmd, &exec_data, 2.00, "USD", UnixNanos::default()) - .unwrap(); - - assert_eq!(report.instrument_id, instrument_id); - assert_eq!( - report.client_order_id, - Some(ClientOrderId::from("O-IB-SPREAD")) - ); - assert_eq!(report.trade_id, TradeId::from("exec-spread-001")); - assert_eq!(report.venue_order_id, VenueOrderId::from("7001")); - assert_eq!(report.last_qty, Quantity::from(1)); - assert_eq!(report.last_px, Price::from("1.25")); -} - -#[tokio::test] -async fn test_handle_spread_execution_first_fill() { - let instrument_provider = create_test_instrument_provider(); - let equity = equity_aapl(); - let spread = create_test_option_spread(); - let spread_instrument_id = spread.id; - let instrument_id = equity.id(); - instrument_provider.insert_test_instrument(InstrumentAny::from(equity), 12345, 1); - instrument_provider.insert_test_instrument(InstrumentAny::from(spread), 54321, 1); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - let spread_fill_tracking = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fills = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fill_avgs = Arc::new(Mutex::new(AHashMap::new())); - let order_fill_progress = Arc::new(Mutex::new(AHashMap::new())); - - let exec_data = create_test_execution_data(213, "exec-001", 3.0, 5.25, "BOT"); - let client_order_id = ClientOrderId::from("O-001"); - let context = create_tracked_order_context(client_order_id, spread_instrument_id); - let account_id = AccountId::from("IB-001"); - let ts_init = UnixNanos::new(0); - - pending_combo_fill_avgs.lock().insert( - client_order_id, - std::collections::VecDeque::from([(Decimal::from(3), Price::from("2.25"))]), - ); - order_fill_progress.lock().insert( - client_order_id, - (Decimal::from(3), Decimal::from_str("6.75").unwrap()), - ); - - InteractiveBrokersExecutionClient::handle_spread_execution( - &exec_data, - client_order_id, - spread_instrument_id, - &instrument_id, - 1.0, - "USD", - &instrument_provider, - &exec_sender.clone().into(), - ts_init, - account_id, - &spread_fill_tracking, - &context, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - None, // avg_px - ) - .await - .unwrap(); - - let combo_event = exec_receiver.try_recv().unwrap(); - match combo_event { - ExecutionEvent::Order(OrderEventAny::Filled(fill)) => { - assert_eq!(fill.instrument_id, spread_instrument_id); - assert_eq!(fill.last_qty, Quantity::from(3)); - assert_eq!(fill.last_px, Price::from("2.25")); - assert_eq!(fill.client_order_id, client_order_id); - } - other => panic!("unexpected combo event: {other:?}"), - } - - let leg_event = exec_receiver.try_recv().unwrap(); - match leg_event { - ExecutionEvent::Report(ExecutionReport::Fill(fill)) => { - assert_eq!(fill.instrument_id, instrument_id); - assert_eq!(fill.last_qty, Quantity::from(3)); - assert_eq!(fill.last_px, Price::from("5.25")); - } - other => panic!("unexpected leg event: {other:?}"), - } - assert!(pending_combo_fills.lock().is_empty()); - assert!(spread_fill_tracking.lock().contains_key(&client_order_id)); -} - -#[tokio::test] -async fn test_handle_spread_execution_duplicate_detection() { - let instrument_provider = create_test_instrument_provider(); - let (exec_sender, _exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - let spread_fill_tracking = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fills = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fill_avgs = Arc::new(Mutex::new(AHashMap::new())); - let order_fill_progress = Arc::new(Mutex::new(AHashMap::new())); - - let exec_data = create_test_execution_data(213, "exec-001", 3.0, 5.25, "BOT"); - let client_order_id = ClientOrderId::from("O-001"); - let spread_instrument_id = create_test_spread_instrument(); - let context = create_tracked_order_context(client_order_id, spread_instrument_id); - let leg_instrument_id = create_test_leg_instrument(); - let account_id = AccountId::from("IB-001"); - let ts_init = UnixNanos::new(0); - - // Pre-populate tracking with the fill ID to simulate duplicate - { - let mut tracking = spread_fill_tracking.lock(); - let fill_set = tracking - .entry(client_order_id) - .or_insert_with(ahash::AHashSet::new); - fill_set.insert("exec-001".to_string()); - } - - let result = InteractiveBrokersExecutionClient::handle_spread_execution( - &exec_data, - client_order_id, - spread_instrument_id, - &leg_instrument_id, - 1.0, - "USD", - &instrument_provider, - &exec_sender.clone().into(), - ts_init, - account_id, - &spread_fill_tracking, - &context, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - None, // avg_px - ) - .await; - - // Should return Ok(()) immediately without processing duplicate - assert!(result.is_ok()); -} - -#[rstest] -fn test_flush_pending_combo_fills_emits_tracked_order_fill() { - let client_order_id = ClientOrderId::from("O-COMBO-001"); - let pending_combo_fills = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fill_avgs = Arc::new(Mutex::new(AHashMap::new())); - let order_fill_progress = Arc::new(Mutex::new(AHashMap::new())); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - - pending_combo_fills.lock().insert( - client_order_id, - std::collections::VecDeque::from([create_pending_combo_fill( - client_order_id, - Quantity::from(2), - )]), - ); - pending_combo_fill_avgs.lock().insert( - client_order_id, - std::collections::VecDeque::from([(Decimal::from(2), Price::from("2.75"))]), - ); - order_fill_progress.lock().insert( - client_order_id, - (Decimal::from(2), Decimal::from_str("5.50").unwrap()), - ); - - InteractiveBrokersExecutionClient::flush_pending_combo_fills( - client_order_id, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - &exec_sender.clone().into(), - ) - .unwrap(); - - let event = exec_receiver.try_recv().unwrap(); - match event { - ExecutionEvent::Order(OrderEventAny::Filled(fill)) => { - assert_eq!(fill.client_order_id, client_order_id); - assert_eq!(fill.last_qty, Quantity::from(2)); - assert_eq!(fill.last_px, Price::from("2.75")); - assert_eq!(fill.order_type, OrderType::Limit); - } - other => panic!("unexpected event: {other:?}"), - } - assert!(pending_combo_fills.lock().is_empty()); - assert!(pending_combo_fill_avgs.lock().is_empty()); - assert!(order_fill_progress.lock().is_empty()); -} - -#[rstest] -fn test_update_order_avg_price_allows_negative_spread_avg_fill_price() { - let instrument_provider = create_test_instrument_provider(); - let spread = create_test_option_spread(); - let spread_instrument_id = spread.id; - let client_order_id = ClientOrderId::from("O-COMBO-NEG-001"); - let order_avg_prices = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fill_avgs = Arc::new(Mutex::new(AHashMap::new())); - let order_fill_progress = Arc::new(Mutex::new(AHashMap::new())); - - instrument_provider.insert_test_instrument(InstrumentAny::from(spread), 54321, 1); - - InteractiveBrokersExecutionClient::update_order_avg_price( - client_order_id, - &spread_instrument_id, - -2.25, - 3.0, - &instrument_provider, - &order_avg_prices, - &pending_combo_fill_avgs, - &order_fill_progress, - ) - .unwrap(); - - let avg_px = order_avg_prices - .lock() - .get(&client_order_id) - .copied() - .unwrap(); - assert_eq!(avg_px, Price::from("-2.25")); - - let avg_chunks = pending_combo_fill_avgs.lock(); - let (fill_delta, partial_avg_px) = avg_chunks - .get(&client_order_id) - .unwrap() - .front() - .copied() - .unwrap(); - assert_eq!(fill_delta, Decimal::from(3)); - assert_eq!(partial_avg_px, Price::from("-2.25")); - - let fill_progress = order_fill_progress.lock(); - let (filled, total_notional) = fill_progress.get(&client_order_id).copied().unwrap(); - assert_eq!(filled, Decimal::from(3)); - assert_eq!(total_notional, Decimal::from_str("-6.75").unwrap()); -} - -#[rstest] -fn test_flush_pending_combo_fills_retains_partial_avg_chunk_remainder() { - let client_order_id = ClientOrderId::from("O-COMBO-002"); - let pending_combo_fills = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fill_avgs = Arc::new(Mutex::new(AHashMap::new())); - let order_fill_progress = Arc::new(Mutex::new(AHashMap::new())); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - - pending_combo_fills.lock().insert( - client_order_id, - std::collections::VecDeque::from([create_pending_combo_fill( - client_order_id, - Quantity::from(1), - )]), - ); - pending_combo_fill_avgs.lock().insert( - client_order_id, - std::collections::VecDeque::from([(Decimal::from(3), Price::from("2.10"))]), - ); - order_fill_progress.lock().insert( - client_order_id, - (Decimal::from(3), Decimal::from_str("6.30").unwrap()), - ); - - InteractiveBrokersExecutionClient::flush_pending_combo_fills( - client_order_id, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - &exec_sender.clone().into(), - ) - .unwrap(); - - let event = exec_receiver.try_recv().unwrap(); - match event { - ExecutionEvent::Order(OrderEventAny::Filled(fill)) => { - assert_eq!(fill.client_order_id, client_order_id); - assert_eq!(fill.last_qty, Quantity::from(1)); - assert_eq!(fill.last_px, Price::from("2.10")); - } - other => panic!("unexpected event: {other:?}"), - } - - let avg_chunks = pending_combo_fill_avgs.lock(); - let remainder = avg_chunks.get(&client_order_id).unwrap().front().unwrap(); - assert_eq!(remainder.0, Decimal::from(2)); - assert_eq!(remainder.1, Price::from("2.10")); - assert!(pending_combo_fills.lock().is_empty()); - assert!(order_fill_progress.lock().is_empty()); -} - -#[rstest] -fn test_emit_order_pending_cancel_is_idempotent() { - let order_id = 7001; - let client_order_id = ClientOrderId::from("O-CANCEL-001"); - let instrument_id_map = Arc::new(Mutex::new(AHashMap::new())); - let trader_id_map = Arc::new(Mutex::new(AHashMap::new())); - let strategy_id_map = Arc::new(Mutex::new(AHashMap::new())); - let pending_cancel_orders = Arc::new(Mutex::new(ahash::AHashSet::new())); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - - instrument_id_map - .lock() - .insert(order_id, create_test_spread_instrument()); - trader_id_map - .lock() - .insert(order_id, TraderId::from("TRADER-001")); - strategy_id_map - .lock() - .insert(order_id, StrategyId::from("STRATEGY-001")); - - InteractiveBrokersExecutionClient::emit_order_pending_cancel( - order_id, - client_order_id, - VenueOrderId::from("PERM-456"), - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &pending_cancel_orders, - &exec_sender.clone().into(), - UnixNanos::new(1), - AccountId::from("IB-001"), - ) - .unwrap(); - InteractiveBrokersExecutionClient::emit_order_pending_cancel( - order_id, - client_order_id, - VenueOrderId::from("PERM-456"), - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &pending_cancel_orders, - &exec_sender.clone().into(), - UnixNanos::new(1), - AccountId::from("IB-001"), - ) - .unwrap(); - - match exec_receiver.try_recv().unwrap() { - ExecutionEvent::Order(OrderEventAny::PendingCancel(event)) => { - assert_eq!(event.venue_order_id, Some(VenueOrderId::from("PERM-456"))); - } - event => panic!("Expected OrderPendingCancel, was {event:?}"), - } - assert!(exec_receiver.try_recv().is_err()); - assert!(pending_cancel_orders.lock().contains(&client_order_id)); -} - -#[rstest] -fn cancel_order_target_validation_rejects_identity_conflicts() { - let trader_id = TraderId::from("TRADER-TARGET"); - let target_strategy_id = StrategyId::from("STRATEGY-TARGET"); - let client_order_id = ClientOrderId::from("O-TARGET-001"); - let instrument_id = create_test_stock_instrument(); - let venue_order_id = VenueOrderId::from("PERM-456"); - let target_order = create_test_accepted_limit_order( - trader_id, - target_strategy_id, - instrument_id, - client_order_id, - venue_order_id, - AccountId::from("IB-001"), - ); - - let command = |instrument_id, venue_order_id| { - CancelOrder::new( - trader_id, - Some(*IB_CLIENT_ID), - StrategyId::from("OPERATOR-001"), - instrument_id, - client_order_id, - Some(venue_order_id), - UUID4::new(), - UnixNanos::new(1), - None, - None, - ) - }; - - let error = InteractiveBrokersExecutionClient::validate_cancel_order_target( - &command(create_test_leg_instrument(), venue_order_id), - &target_order, - ) - .unwrap_err(); - assert!(error.to_string().contains("command instrument ID")); - - let error = InteractiveBrokersExecutionClient::validate_cancel_order_target( - &command(instrument_id, VenueOrderId::from("PERM-999")), - &target_order, - ) - .unwrap_err(); - assert!(error.to_string().contains("command venue order ID")); -} - -#[rstest] -fn cancel_tracking_blocks_preexisting_route_until_identity_complete() { - let order_id = 7001; - let trader_id = TraderId::from("TRADER-TARGET"); - let strategy_id = StrategyId::from("STRATEGY-TARGET"); - let instrument_id = create_test_stock_instrument(); - let client_order_id = ClientOrderId::from("O-TARGET-001"); - let venue_order_id = VenueOrderId::from("PERM-456"); - let target_order = create_test_accepted_limit_order( - trader_id, - strategy_id, - instrument_id, - client_order_id, - venue_order_id, - AccountId::from("IB-001"), - ); - let cmd = CancelOrder::new( - trader_id, - Some(*IB_CLIENT_ID), - strategy_id, - instrument_id, - client_order_id, - Some(venue_order_id), - UUID4::new(), - UnixNanos::new(1), - None, - None, - ); - let order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let venue_order_id_map = Arc::new(Mutex::new(AHashMap::from_iter([( - order_id, - client_order_id, - )]))); - let instrument_id_map = Arc::new(Mutex::new(AHashMap::new())); - let trader_id_map = Arc::new(Mutex::new(AHashMap::new())); - let strategy_id_map = Arc::new(Mutex::new(AHashMap::new())); - - let writer_order_id_map = Arc::clone(&order_id_map); - let writer_venue_order_id_map = Arc::clone(&venue_order_id_map); - let writer_instrument_id_map = Arc::clone(&instrument_id_map); - let writer_trader_id_map = Arc::clone(&trader_id_map); - let writer_strategy_id_map = Arc::clone(&strategy_id_map); - let strategy_guard = strategy_id_map.lock(); - - let writer = std::thread::spawn(move || { - InteractiveBrokersExecutionClient::cache_cancel_order_tracking( - order_id, - &cmd, - &target_order, - &writer_order_id_map, - &writer_venue_order_id_map, - &writer_instrument_id_map, - &writer_trader_id_map, - &writer_strategy_id_map, - ) - }); - - let deadline = std::time::Instant::now() + Duration::from_secs(5); - - while !trader_id_map.lock().contains_key(&order_id) { - assert!( - std::time::Instant::now() < deadline, - "tracking writer stalled" - ); - std::thread::yield_now(); - } - assert!(venue_order_id_map.try_lock().is_none()); - - drop(strategy_guard); - writer.join().unwrap().unwrap(); - assert_eq!( - venue_order_id_map.lock().get(&order_id), - Some(&client_order_id) - ); -} - -#[rstest] -#[case("TRADER-RESTORED", "STRATEGY-RESTORED")] -#[case("TRADER-OPERATOR", "OPERATOR-001")] -#[tokio::test] -async fn test_cancel_order_recovery_tracks_resolved_order_identity( - #[case] requesting_trader_id: &str, - #[case] requesting_strategy_id: &str, -) { - let (client, mut exec_receiver, cache) = create_test_execution_client(); - let spread = create_test_option_spread(); - let instrument_id = spread.id; - client - .instrument_provider - .insert_test_instrument(InstrumentAny::from(spread), 54321, 1); - - assert!(client.order_id_map.lock().is_empty()); - assert!(client.venue_order_id_map.lock().is_empty()); - assert!(client.instrument_id_map.lock().is_empty()); - assert!(client.trader_id_map.lock().is_empty()); - assert!(client.strategy_id_map.lock().is_empty()); - - let trader_id = TraderId::from("TRADER-RESTORED"); - let target_strategy_id = StrategyId::from("STRATEGY-RESTORED"); - let client_order_id = ClientOrderId::from("O-RESTORED-001"); - let venue_order_id = VenueOrderId::from("PERM-456"); - let target_order = create_test_accepted_limit_order( - trader_id, - target_strategy_id, - instrument_id, - client_order_id, - venue_order_id, - client.core.account_id, - ); - cache - .borrow_mut() - .add_order(target_order, None, Some(*IB_CLIENT_ID), false) - .unwrap(); - let target_order = client.core.get_order(&client_order_id).unwrap(); - - let cmd = CancelOrder::new( - TraderId::from(requesting_trader_id), - Some(*IB_CLIENT_ID), - StrategyId::from(requesting_strategy_id), - instrument_id, - client_order_id, - Some(venue_order_id), - UUID4::new(), - UnixNanos::new(1), - None, - None, - ); - InteractiveBrokersExecutionClient::validate_cancel_order_target(&cmd, &target_order).unwrap(); - // Raw order ID returned after resolving PERM-456 - let resolved_order_id = 7001; - - InteractiveBrokersExecutionClient::cache_cancel_order_tracking( - resolved_order_id, - &cmd, - &target_order, - &client.order_id_map, - &client.venue_order_id_map, - &client.instrument_id_map, - &client.trader_id_map, - &client.strategy_id_map, - ) - .unwrap(); - - let mut pending_status = create_test_order_status(resolved_order_id, "PendingCancel"); - pending_status.perm_id = 456; - let exec_sender = get_exec_event_sender(); - InteractiveBrokersExecutionClient::handle_order_status( - &pending_status, - &client.order_id_map, - &client.venue_order_id_map, - &client.instrument_provider, - &exec_sender, - UnixNanos::new(2), - client.core.account_id, - &client.instrument_id_map, - &client.trader_id_map, - &client.strategy_id_map, - &client.active_order_contexts, - &client.terminal_order_contexts, - &client.order_avg_prices, - &client.pending_combo_fills, - &client.pending_combo_fill_avgs, - &client.order_fill_progress, - &client.pending_cancel_orders, - &client.spread_fill_tracking, - ) - .await - .unwrap(); - - match exec_receiver.try_recv().unwrap() { - ExecutionEvent::Order(OrderEventAny::PendingCancel(event)) => { - assert_eq!(event.trader_id, trader_id); - assert_eq!(event.strategy_id, target_strategy_id); - assert_eq!(event.instrument_id, instrument_id); - assert_eq!(event.client_order_id, client_order_id); - assert_eq!(event.venue_order_id, Some(venue_order_id)); - } - other => panic!("unexpected event: {other:?}"), - } - assert!( - client - .pending_cancel_orders - .lock() - .contains(&client_order_id) - ); - - let mut canceled_status = create_test_order_status(resolved_order_id, "Cancelled"); - canceled_status.perm_id = 456; - InteractiveBrokersExecutionClient::handle_order_status( - &canceled_status, - &client.order_id_map, - &client.venue_order_id_map, - &client.instrument_provider, - &exec_sender, - UnixNanos::new(3), - client.core.account_id, - &client.instrument_id_map, - &client.trader_id_map, - &client.strategy_id_map, - &client.active_order_contexts, - &client.terminal_order_contexts, - &client.order_avg_prices, - &client.pending_combo_fills, - &client.pending_combo_fill_avgs, - &client.order_fill_progress, - &client.pending_cancel_orders, - &client.spread_fill_tracking, - ) - .await - .unwrap(); - - match exec_receiver.try_recv().unwrap() { - ExecutionEvent::Order(OrderEventAny::Canceled(event)) => { - assert_eq!(event.trader_id, trader_id); - assert_eq!(event.strategy_id, target_strategy_id); - assert_eq!(event.instrument_id, instrument_id); - assert_eq!(event.client_order_id, client_order_id); - assert_eq!(event.venue_order_id, Some(VenueOrderId::from("PERM-456"))); - } - other => panic!("unexpected event: {other:?}"), - } - - assert!(exec_receiver.try_recv().is_err()); - assert!(client.order_id_map.lock().is_empty()); - assert!(client.venue_order_id_map.lock().is_empty()); - assert!(client.instrument_id_map.lock().is_empty()); - assert!(client.trader_id_map.lock().is_empty()); - assert!(client.strategy_id_map.lock().is_empty()); - assert!(client.pending_cancel_orders.lock().is_empty()); - assert!(client.active_order_contexts.lock().is_empty()); - assert!(client.terminal_order_contexts.lock().is_empty()); -} - -#[tokio::test] -async fn test_handle_order_status_canceled_emits_canceled_event() { - let instrument_provider = create_test_instrument_provider(); - let spread = create_test_option_spread(); - let venue_order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let instrument_id_map = Arc::new(Mutex::new(AHashMap::new())); - let trader_id_map = Arc::new(Mutex::new(AHashMap::new())); - let strategy_id_map = Arc::new(Mutex::new(AHashMap::new())); - let active_order_contexts = Arc::new(Mutex::new(AHashMap::new())); - let terminal_order_contexts = Arc::new(Mutex::new(FifoCacheMap::new())); - let order_avg_prices = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fills = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fill_avgs = Arc::new(Mutex::new(AHashMap::new())); - let order_fill_progress = Arc::new(Mutex::new(AHashMap::new())); - let pending_cancel_orders = Arc::new(Mutex::new(ahash::AHashSet::new())); - let order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let spread_fill_tracking = Arc::new(Mutex::new(AHashMap::new())); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - let order_id = 7001; - let client_order_id = ClientOrderId::from("O-CANCEL-002"); - let instrument_id = spread.id; - - instrument_provider.insert_test_instrument(InstrumentAny::from(spread), 54321, 1); - - venue_order_id_map.lock().insert(order_id, client_order_id); - order_id_map.lock().insert(client_order_id, order_id); - instrument_id_map.lock().insert(order_id, instrument_id); - trader_id_map - .lock() - .insert(order_id, TraderId::from("TRADER-001")); - strategy_id_map - .lock() - .insert(order_id, StrategyId::from("STRATEGY-001")); - active_order_contexts.lock().insert( - order_id, - create_tracked_order_context(client_order_id, instrument_id), - ); - pending_cancel_orders.lock().insert(client_order_id); - pending_combo_fills.lock().insert( - client_order_id, - VecDeque::from([create_pending_combo_fill( - client_order_id, - Quantity::from(1), - )]), - ); - let mut status = create_test_order_status(order_id, "Cancelled"); - status.filled = 1.0; - status.average_fill_price = Some(2.25); - - InteractiveBrokersExecutionClient::handle_order_status( - &status, - &order_id_map, - &venue_order_id_map, - &instrument_provider, - &exec_sender.clone().into(), - UnixNanos::new(1), - AccountId::from("IB-001"), - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &active_order_contexts, - &terminal_order_contexts, - &order_avg_prices, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - &pending_cancel_orders, - &spread_fill_tracking, - ) - .await - .unwrap(); - - assert!(matches!( - exec_receiver.try_recv().unwrap(), - ExecutionEvent::Order(OrderEventAny::Accepted(event)) - if event.client_order_id == client_order_id - )); - assert!(matches!( - exec_receiver.try_recv().unwrap(), - ExecutionEvent::Order(OrderEventAny::Filled(event)) - if event.client_order_id == client_order_id - && event.last_px == Price::from("2.25") - )); - - match exec_receiver.try_recv().unwrap() { - ExecutionEvent::Order(OrderEventAny::Canceled(event)) => { - assert_eq!(event.client_order_id, client_order_id); - assert_eq!(event.instrument_id, instrument_id); - } - other => panic!("unexpected event: {other:?}"), - } - assert!(!pending_cancel_orders.lock().contains(&client_order_id)); - assert!(order_id_map.lock().is_empty()); - assert!(venue_order_id_map.lock().is_empty()); - assert!(instrument_id_map.lock().is_empty()); - assert!(trader_id_map.lock().is_empty()); - assert!(strategy_id_map.lock().is_empty()); - assert!(active_order_contexts.lock().is_empty()); - assert!(terminal_order_contexts.lock().contains_key(&order_id)); -} - -#[tokio::test] -async fn test_opra_cancel_status_preserves_canonical_instrument_identity() { - let state = SubmitTrackingState::new(); - let order_id = 7_002; - let client_order_id = ClientOrderId::from("O-OPRA-CANCEL-001"); - let instrument_id = InstrumentId::from("SPY 250101C00400000.OPRA"); - state.cache( - order_id, - client_order_id, - instrument_id, - TraderId::from("TRADER-001"), - StrategyId::from("STRATEGY-001"), - ); - - let instrument_provider = create_test_instrument_provider(); - let order_avg_prices = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fills = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fill_avgs = Arc::new(Mutex::new(AHashMap::new())); - let order_fill_progress = Arc::new(Mutex::new(AHashMap::new())); - let pending_cancel_orders = Arc::new(Mutex::new(ahash::AHashSet::new())); - let spread_fill_tracking = Arc::new(Mutex::new(AHashMap::new())); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - - InteractiveBrokersExecutionClient::handle_order_status( - &create_test_order_status(order_id, "Cancelled"), - &state.order_id_map, - &state.venue_order_id_map, - &instrument_provider, - &exec_sender.clone().into(), - UnixNanos::new(1), - AccountId::from("IB-001"), - &state.instrument_id_map, - &state.trader_id_map, - &state.strategy_id_map, - &state.active_order_contexts, - &state.terminal_order_contexts, - &order_avg_prices, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - &pending_cancel_orders, - &spread_fill_tracking, - ) - .await - .unwrap(); - - assert!(matches!( - exec_receiver.try_recv().unwrap(), - ExecutionEvent::Order(OrderEventAny::Accepted(event)) - if event.instrument_id == instrument_id - )); - assert!(matches!( - exec_receiver.try_recv().unwrap(), - ExecutionEvent::Order(OrderEventAny::Canceled(event)) - if event.instrument_id == instrument_id - && event.client_order_id == client_order_id - )); - assert!(exec_receiver.try_recv().is_err()); - assert_eq!( - state - .terminal_order_contexts - .lock() - .get(&order_id) - .unwrap() - .instrument_id, - instrument_id - ); -} - -#[tokio::test] -async fn test_process_order_update_stream_emits_accepted_then_canceled() { - let instrument_provider = create_test_instrument_provider(); - let venue_order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let instrument_id_map = Arc::new(Mutex::new(AHashMap::new())); - let trader_id_map = Arc::new(Mutex::new(AHashMap::new())); - let strategy_id_map = Arc::new(Mutex::new(AHashMap::new())); - let active_order_contexts = Arc::new(Mutex::new(AHashMap::new())); - let terminal_order_contexts = Arc::new(Mutex::new(FifoCacheMap::new())); - let order_avg_prices = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fills = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fill_avgs = Arc::new(Mutex::new(AHashMap::new())); - let order_fill_progress = Arc::new(Mutex::new(AHashMap::new())); - let pending_cancel_orders = Arc::new(Mutex::new(ahash::AHashSet::new())); - let spread_fill_tracking = Arc::new(Mutex::new(AHashMap::new())); - let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); - let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); - let order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - let (update_sender, update_receiver) = tokio::sync::mpsc::unbounded_channel(); - let mut subscription = Subscription::new(update_receiver); - let order_id = 7002; - let client_order_id = ClientOrderId::from("O-STREAM-001"); - let instrument_id = create_test_spread_instrument(); - - venue_order_id_map.lock().insert(order_id, client_order_id); - instrument_id_map.lock().insert(order_id, instrument_id); - trader_id_map - .lock() - .insert(order_id, TraderId::from("TRADER-001")); - strategy_id_map - .lock() - .insert(order_id, StrategyId::from("STRATEGY-001")); - active_order_contexts.lock().insert( - order_id, - create_tracked_order_context(client_order_id, instrument_id), - ); - - update_sender - .send(Ok(OrderUpdate::OpenOrder(create_test_open_order( - order_id, - "Submitted", - "", - )))) - .unwrap(); - update_sender - .send(Ok(OrderUpdate::OrderStatus(create_test_order_status( - order_id, - "Cancelled", - )))) - .unwrap(); - drop(update_sender); - - InteractiveBrokersExecutionClient::process_order_update_stream( - &mut subscription, - &order_id_map, - &venue_order_id_map, - &instrument_provider, - &exec_sender.clone().into(), - nautilus_core::time::get_atomic_clock_realtime(), - AccountId::from("IB-001"), - &commission_cache, - &pending_execution_cache, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &active_order_contexts, - &terminal_order_contexts, - &spread_fill_tracking, - &order_avg_prices, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - &pending_cancel_orders, - ) - .await; - - let accepted_event = exec_receiver.try_recv().unwrap(); - assert!(matches!( - accepted_event, - ExecutionEvent::Order(OrderEventAny::Accepted(_)) - )); - - let canceled_event = exec_receiver.try_recv().unwrap(); - assert!(matches!( - canceled_event, - ExecutionEvent::Order(OrderEventAny::Canceled(_)) - )); -} - -#[tokio::test] -async fn test_process_order_update_stream_clears_market_order_update_prices() { - let instrument_provider = create_test_instrument_provider(); - let equity = equity_aapl(); - let order_id = 7005; - let contract_id = 12347; - let client_order_id = ClientOrderId::from("O-STREAM-MKT-UPDATE"); - let venue_order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let instrument_id_map = Arc::new(Mutex::new(AHashMap::new())); - let trader_id_map = Arc::new(Mutex::new(AHashMap::new())); - let strategy_id_map = Arc::new(Mutex::new(AHashMap::new())); - let active_order_contexts = Arc::new(Mutex::new(AHashMap::new())); - let terminal_order_contexts = Arc::new(Mutex::new(FifoCacheMap::new())); - let order_avg_prices = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fills = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fill_avgs = Arc::new(Mutex::new(AHashMap::new())); - let order_fill_progress = Arc::new(Mutex::new(AHashMap::new())); - let pending_cancel_orders = Arc::new(Mutex::new(ahash::AHashSet::new())); - let spread_fill_tracking = Arc::new(Mutex::new(AHashMap::new())); - let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); - let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); - let order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - let (update_sender, update_receiver) = tokio::sync::mpsc::unbounded_channel(); - let mut subscription = Subscription::new(update_receiver); - - let instrument_id = equity.id(); - instrument_provider.insert_test_instrument(InstrumentAny::from(equity), contract_id, 1); - venue_order_id_map.lock().insert(order_id, client_order_id); - instrument_id_map.lock().insert(order_id, instrument_id); - trader_id_map - .lock() - .insert(order_id, TraderId::from("TRADER-001")); - strategy_id_map - .lock() - .insert(order_id, StrategyId::from("STRATEGY-001")); - active_order_contexts.lock().insert( - order_id, - create_tracked_order_context(client_order_id, instrument_id), - ); - - let mut open_order = create_test_open_order(order_id, "Submitted", ""); - open_order.contract.contract_id = contract_id; - open_order.order.total_quantity = 10.0; - open_order.order.order_type = "MKT".to_string(); - open_order.order.limit_price = Some(150.25); - open_order.order.aux_price = Some(149.75); - - update_sender - .send(Ok(OrderUpdate::OpenOrder(open_order))) - .unwrap(); - drop(update_sender); - - InteractiveBrokersExecutionClient::process_order_update_stream( - &mut subscription, - &order_id_map, - &venue_order_id_map, - &instrument_provider, - &exec_sender.clone().into(), - nautilus_core::time::get_atomic_clock_realtime(), - AccountId::from("IB-001"), - &commission_cache, - &pending_execution_cache, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &active_order_contexts, - &terminal_order_contexts, - &spread_fill_tracking, - &order_avg_prices, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - &pending_cancel_orders, - ) - .await; - - let accepted_event = exec_receiver.try_recv().unwrap(); - assert!(matches!( - accepted_event, - ExecutionEvent::Order(OrderEventAny::Accepted(_)) - )); - - let updated_event = exec_receiver.try_recv().unwrap(); - match updated_event { - ExecutionEvent::Order(OrderEventAny::Updated(event)) => { - assert_eq!(event.client_order_id, client_order_id); - assert_eq!(event.quantity, Quantity::from(10)); - assert_eq!(event.price, None); - assert_eq!(event.trigger_price, None); - } - other => panic!("unexpected event: {other:?}"), - } -} - -#[rstest] -#[case(false)] -#[case(true)] -#[tokio::test] -async fn test_process_order_update_stream_emits_fill_after_commission_report( - #[case] already_accepted: bool, -) { - let instrument_provider = create_test_instrument_provider(); - let equity = equity_aapl(); - let order_id = 7003; - let contract_id = 12345; - let client_order_id = ClientOrderId::from("O-STREAM-002"); - let venue_order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let instrument_id_map = Arc::new(Mutex::new(AHashMap::new())); - let trader_id_map = Arc::new(Mutex::new(AHashMap::new())); - let strategy_id_map = Arc::new(Mutex::new(AHashMap::new())); - let active_order_contexts = Arc::new(Mutex::new(AHashMap::new())); - let terminal_order_contexts = Arc::new(Mutex::new(FifoCacheMap::new())); - let order_avg_prices = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fills = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fill_avgs = Arc::new(Mutex::new(AHashMap::new())); - let order_fill_progress = Arc::new(Mutex::new(AHashMap::new())); - let pending_cancel_orders = Arc::new(Mutex::new(ahash::AHashSet::new())); - let spread_fill_tracking = Arc::new(Mutex::new(AHashMap::new())); - let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); - let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); - let order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - let (update_sender, update_receiver) = tokio::sync::mpsc::unbounded_channel(); - let mut subscription = Subscription::new(update_receiver); - - let instrument_id = equity.id(); - instrument_provider.insert_test_instrument(InstrumentAny::from(equity), contract_id, 1); - venue_order_id_map.lock().insert(order_id, client_order_id); - instrument_id_map.lock().insert(order_id, instrument_id); - trader_id_map - .lock() - .insert(order_id, TraderId::from("TRADER-001")); - strategy_id_map - .lock() - .insert(order_id, StrategyId::from("STRATEGY-001")); - let mut context = create_tracked_order_context(client_order_id, instrument_id); - context.accepted = already_accepted; - active_order_contexts.lock().insert(order_id, context); - - let mut exec_data = create_test_execution_data(order_id, "exec-stream-001", 100.0, 50.0, "BOT"); - exec_data.contract.contract_id = contract_id; - exec_data.contract.security_type = SecurityType::Stock; - exec_data.contract.symbol = IBSymbol::from("AAPL"); - exec_data.contract.exchange = Exchange::from("SMART"); - exec_data.contract.currency = IBCurrency::from("USD"); - exec_data.execution.order_reference.clear(); - - update_sender - .send(Ok(OrderUpdate::ExecutionData(exec_data))) - .unwrap(); - update_sender - .send(Ok(OrderUpdate::CommissionReport(CommissionReport { - execution_id: String::from("exec-stream-001"), - commission: 1.25, - currency: String::from("USD"), - realized_pnl: None, - yields: None, - yield_redemption_date: String::new(), - }))) - .unwrap(); - drop(update_sender); - - InteractiveBrokersExecutionClient::process_order_update_stream( - &mut subscription, - &order_id_map, - &venue_order_id_map, - &instrument_provider, - &exec_sender.clone().into(), - nautilus_core::time::get_atomic_clock_realtime(), - AccountId::from("IB-001"), - &commission_cache, - &pending_execution_cache, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &active_order_contexts, - &terminal_order_contexts, - &spread_fill_tracking, - &order_avg_prices, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - &pending_cancel_orders, - ) - .await; - - if !already_accepted { - let accepted_event = exec_receiver.try_recv().unwrap(); - assert!(matches!( - accepted_event, - ExecutionEvent::Order(OrderEventAny::Accepted(event)) - if event.client_order_id == client_order_id - )); - } - - let fill_event = exec_receiver.try_recv().unwrap(); - match fill_event { - ExecutionEvent::Order(OrderEventAny::Filled(fill)) => { - assert_eq!(fill.trader_id, TraderId::from("TRADER-001")); - assert_eq!(fill.strategy_id, StrategyId::from("STRATEGY-001")); - assert_eq!(fill.client_order_id, client_order_id); - assert_eq!(fill.instrument_id, instrument_id); - assert_eq!(fill.trade_id, TradeId::from("exec-stream-001")); - assert_eq!(fill.order_side, OrderSide::Buy); - assert_eq!(fill.order_type, OrderType::Limit); - assert_eq!(fill.last_qty, Quantity::from(100)); - assert_eq!(fill.last_px, Price::from("50")); - assert_eq!(fill.currency, Currency::USD()); - assert_eq!(fill.commission, Some(Money::new(1.25, Currency::USD()))); - } - other => panic!("unexpected event: {other:?}"), - } - assert!(commission_cache.lock().is_empty()); - assert!(exec_receiver.try_recv().is_err()); -} - -#[tokio::test] -async fn test_process_order_update_stream_retains_terminal_identity_for_late_fill() { - let instrument_provider = create_test_instrument_provider(); - let equity = equity_aapl(); - let order_id = 7006; - let contract_id = 12348; - let client_order_id = ClientOrderId::from("O-STREAM-LATE-FILL"); - let instrument_id = equity.id(); - let order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let venue_order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let instrument_id_map = Arc::new(Mutex::new(AHashMap::new())); - let trader_id_map = Arc::new(Mutex::new(AHashMap::new())); - let strategy_id_map = Arc::new(Mutex::new(AHashMap::new())); - let active_order_contexts = Arc::new(Mutex::new(AHashMap::new())); - let terminal_order_contexts = Arc::new(Mutex::new(FifoCacheMap::new())); - let order_avg_prices = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fills = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fill_avgs = Arc::new(Mutex::new(AHashMap::new())); - let order_fill_progress = Arc::new(Mutex::new(AHashMap::new())); - let pending_cancel_orders = Arc::new(Mutex::new(ahash::AHashSet::new())); - let spread_fill_tracking = Arc::new(Mutex::new(AHashMap::new())); - let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); - let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - let (update_sender, update_receiver) = tokio::sync::mpsc::unbounded_channel(); - let mut subscription = Subscription::new(update_receiver); - - instrument_provider.insert_test_instrument(InstrumentAny::from(equity), contract_id, 1); - order_id_map.lock().insert(client_order_id, order_id); - venue_order_id_map.lock().insert(order_id, client_order_id); - instrument_id_map.lock().insert(order_id, instrument_id); - trader_id_map - .lock() - .insert(order_id, TraderId::from("TRADER-001")); - strategy_id_map - .lock() - .insert(order_id, StrategyId::from("STRATEGY-001")); - active_order_contexts.lock().insert( - order_id, - create_tracked_order_context(client_order_id, instrument_id), - ); - - let mut exec_data = - create_test_execution_data(order_id, "exec-stream-late", 100.0, 50.0, "BOT"); - exec_data.contract.contract_id = contract_id; - exec_data.contract.security_type = SecurityType::Stock; - exec_data.contract.symbol = IBSymbol::from("AAPL"); - exec_data.contract.exchange = Exchange::from("SMART"); - exec_data.contract.currency = IBCurrency::from("USD"); - exec_data.execution.order_reference.clear(); - - update_sender - .send(Ok(OrderUpdate::OrderStatus(create_test_order_status( - order_id, "Filled", - )))) - .unwrap(); - update_sender - .send(Ok(OrderUpdate::ExecutionData(exec_data))) - .unwrap(); - drop(update_sender); - - InteractiveBrokersExecutionClient::process_order_update_stream( - &mut subscription, - &order_id_map, - &venue_order_id_map, - &instrument_provider, - &exec_sender.clone().into(), - nautilus_core::time::get_atomic_clock_realtime(), - AccountId::from("IB-001"), - &commission_cache, - &pending_execution_cache, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &active_order_contexts, - &terminal_order_contexts, - &spread_fill_tracking, - &order_avg_prices, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - &pending_cancel_orders, - ) - .await; - - assert!(matches!( - exec_receiver.try_recv().unwrap(), - ExecutionEvent::Order(OrderEventAny::Accepted(event)) - if event.client_order_id == client_order_id - )); - assert!( - pending_execution_cache - .lock() - .contains_key(&String::from("exec-stream-late")) - ); - assert!(exec_receiver.try_recv().is_err()); - - let (update_sender, update_receiver) = tokio::sync::mpsc::unbounded_channel(); - let mut subscription = Subscription::new(update_receiver); - update_sender - .send(Ok(OrderUpdate::CommissionReport(CommissionReport { - execution_id: String::from("exec-stream-late"), - commission: 1.25, - currency: String::from("USD"), - realized_pnl: None, - yields: None, - yield_redemption_date: String::new(), - }))) - .unwrap(); - drop(update_sender); - - InteractiveBrokersExecutionClient::process_order_update_stream( - &mut subscription, - &order_id_map, - &venue_order_id_map, - &instrument_provider, - &exec_sender.clone().into(), - nautilus_core::time::get_atomic_clock_realtime(), - AccountId::from("IB-001"), - &commission_cache, - &pending_execution_cache, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &active_order_contexts, - &terminal_order_contexts, - &spread_fill_tracking, - &order_avg_prices, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - &pending_cancel_orders, - ) - .await; - - assert!(matches!( - exec_receiver.try_recv().unwrap(), - ExecutionEvent::Order(OrderEventAny::Filled(event)) - if event.client_order_id == client_order_id - && event.trade_id == TradeId::from("exec-stream-late") - )); - assert!(exec_receiver.try_recv().is_err()); - assert!(pending_execution_cache.lock().is_empty()); - assert!(active_order_contexts.lock().is_empty()); - let terminal_contexts = terminal_order_contexts.lock(); - let terminal_context = terminal_contexts.get(&order_id).unwrap(); - assert!(terminal_context.accepted); - assert_eq!(terminal_context.client_order_id, client_order_id); -} - -#[tokio::test] -async fn test_process_order_update_stream_retains_terminal_combo_routing() { - let instrument_provider = create_test_instrument_provider(); - let equity = equity_aapl(); - let spread = create_test_option_spread(); - let order_id = 7007; - let contract_id = 12345; - let client_order_id = ClientOrderId::from("O-STREAM-LATE-COMBO"); - let instrument_id = spread.id; - let leg_instrument_id = equity.id(); - let order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let venue_order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let instrument_id_map = Arc::new(Mutex::new(AHashMap::new())); - let trader_id_map = Arc::new(Mutex::new(AHashMap::new())); - let strategy_id_map = Arc::new(Mutex::new(AHashMap::new())); - let active_order_contexts = Arc::new(Mutex::new(AHashMap::new())); - let terminal_order_contexts = Arc::new(Mutex::new(FifoCacheMap::new())); - let order_avg_prices = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fills = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fill_avgs = Arc::new(Mutex::new(AHashMap::new())); - let order_fill_progress = Arc::new(Mutex::new(AHashMap::new())); - let pending_cancel_orders = Arc::new(Mutex::new(ahash::AHashSet::new())); - let spread_fill_tracking = Arc::new(Mutex::new(AHashMap::new())); - let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); - let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - let (update_sender, update_receiver) = tokio::sync::mpsc::unbounded_channel(); - let mut subscription = Subscription::new(update_receiver); - - instrument_provider.insert_test_instrument(InstrumentAny::from(equity), contract_id, 1); - instrument_provider.insert_test_instrument(InstrumentAny::from(spread), 54321, 1); - order_id_map.lock().insert(client_order_id, order_id); - venue_order_id_map.lock().insert(order_id, client_order_id); - instrument_id_map.lock().insert(order_id, instrument_id); - trader_id_map - .lock() - .insert(order_id, TraderId::from("TRADER-001")); - strategy_id_map - .lock() - .insert(order_id, StrategyId::from("STRATEGY-001")); - active_order_contexts.lock().insert( - order_id, - create_tracked_order_context(client_order_id, instrument_id), - ); - - let mut status = create_test_order_status(order_id, "Filled"); - status.filled = 1.0; - status.average_fill_price = Some(2.25); - let mut exec_data = - create_test_execution_data(order_id, "exec-stream-late-combo", 1.0, 5.25, "BOT"); - exec_data.contract.combo_legs = create_test_bag_execution_data(order_id, "unused") - .contract - .combo_legs; - exec_data.execution.order_reference.clear(); - - update_sender - .send(Ok(OrderUpdate::OrderStatus(status))) - .unwrap(); - update_sender - .send(Ok(OrderUpdate::ExecutionData(exec_data))) - .unwrap(); - update_sender - .send(Ok(OrderUpdate::CommissionReport(CommissionReport { - execution_id: String::from("exec-stream-late-combo"), - commission: 1.25, - currency: String::from("USD"), - realized_pnl: None, - yields: None, - yield_redemption_date: String::new(), - }))) - .unwrap(); - drop(update_sender); - - InteractiveBrokersExecutionClient::process_order_update_stream( - &mut subscription, - &order_id_map, - &venue_order_id_map, - &instrument_provider, - &exec_sender.clone().into(), - nautilus_core::time::get_atomic_clock_realtime(), - AccountId::from("IB-001"), - &commission_cache, - &pending_execution_cache, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &active_order_contexts, - &terminal_order_contexts, - &spread_fill_tracking, - &order_avg_prices, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - &pending_cancel_orders, - ) - .await; - - assert!(matches!( - exec_receiver.try_recv().unwrap(), - ExecutionEvent::Order(OrderEventAny::Accepted(event)) - if event.client_order_id == client_order_id - )); - assert!(matches!( - exec_receiver.try_recv().unwrap(), - ExecutionEvent::Order(OrderEventAny::Filled(event)) - if event.client_order_id == client_order_id - && event.instrument_id == instrument_id - && event.last_px == Price::from("2.25") - )); - assert!(matches!( - exec_receiver.try_recv().unwrap(), - ExecutionEvent::Report(ExecutionReport::Fill(event)) - if event.instrument_id == leg_instrument_id - )); - assert!(exec_receiver.try_recv().is_err()); - assert!(active_order_contexts.lock().is_empty()); - assert!(terminal_order_contexts.lock().contains_key(&order_id)); -} - -#[tokio::test] -async fn test_process_order_update_stream_learns_order_ref_from_execution() { - let instrument_provider = create_test_instrument_provider(); - let equity = equity_aapl(); - let order_id = 7004; - let contract_id = 12346; - let client_order_id = ClientOrderId::from("O-STREAM-EXEC-REF"); - let venue_order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let instrument_id_map = Arc::new(Mutex::new(AHashMap::new())); - let trader_id_map = Arc::new(Mutex::new(AHashMap::new())); - let strategy_id_map = Arc::new(Mutex::new(AHashMap::new())); - let active_order_contexts = Arc::new(Mutex::new(AHashMap::new())); - let terminal_order_contexts = Arc::new(Mutex::new(FifoCacheMap::new())); - let order_avg_prices = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fills = Arc::new(Mutex::new(AHashMap::new())); - let pending_combo_fill_avgs = Arc::new(Mutex::new(AHashMap::new())); - let order_fill_progress = Arc::new(Mutex::new(AHashMap::new())); - let pending_cancel_orders = Arc::new(Mutex::new(ahash::AHashSet::new())); - let spread_fill_tracking = Arc::new(Mutex::new(AHashMap::new())); - let commission_cache = Arc::new(Mutex::new(CommissionCache::new())); - let pending_execution_cache = Arc::new(Mutex::new(PendingExecutionCache::new())); - let order_id_map = Arc::new(Mutex::new(AHashMap::new())); - let (exec_sender, mut exec_receiver) = tokio::sync::mpsc::unbounded_channel(); - let (update_sender, update_receiver) = tokio::sync::mpsc::unbounded_channel(); - let mut subscription = Subscription::new(update_receiver); - - let instrument_id = equity.id(); - instrument_provider.insert_test_instrument(InstrumentAny::from(equity), contract_id, 1); - - let mut exec_data = create_test_execution_data(order_id, "exec-stream-002", 100.0, 50.0, "BOT"); - exec_data.contract.contract_id = contract_id; - exec_data.contract.security_type = SecurityType::Stock; - exec_data.contract.symbol = IBSymbol::from("AAPL"); - exec_data.contract.exchange = Exchange::from("SMART"); - exec_data.contract.currency = IBCurrency::from("USD"); - exec_data.execution.order_reference = client_order_id.to_string(); - - update_sender - .send(Ok(OrderUpdate::ExecutionData(exec_data))) - .unwrap(); - update_sender - .send(Ok(OrderUpdate::CommissionReport(CommissionReport { - execution_id: String::from("exec-stream-002"), - commission: 1.25, - currency: String::from("USD"), - realized_pnl: None, - yields: None, - yield_redemption_date: String::new(), - }))) - .unwrap(); - drop(update_sender); - - InteractiveBrokersExecutionClient::process_order_update_stream( - &mut subscription, - &order_id_map, - &venue_order_id_map, - &instrument_provider, - &exec_sender.clone().into(), - nautilus_core::time::get_atomic_clock_realtime(), - AccountId::from("IB-001"), - &commission_cache, - &pending_execution_cache, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &active_order_contexts, - &terminal_order_contexts, - &spread_fill_tracking, - &order_avg_prices, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - &pending_cancel_orders, - ) - .await; - - let fill_event = exec_receiver.try_recv().unwrap(); - match fill_event { - ExecutionEvent::Report(ExecutionReport::Fill(fill)) => { - assert_eq!(fill.client_order_id, Some(client_order_id)); - assert_eq!(fill.instrument_id, instrument_id); - } - other => panic!("unexpected event: {other:?}"), - } - assert_eq!( - venue_order_id_map.lock().get(&order_id), - Some(&client_order_id) - ); - assert_eq!(order_id_map.lock().get(&client_order_id), Some(&order_id)); -} - -#[rstest] -fn test_get_leg_position_edge_cases() { - // Test with single component (no spread) - let spread_id = InstrumentId::new(Symbol::from("SPY C400"), Venue::from("SMART")); - let leg_id = InstrumentId::new(Symbol::from("SPY C400"), Venue::from("SMART")); - let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id); - assert_eq!(result, 0); // Should fallback to 0 - - // Test with invalid format - let spread_id = InstrumentId::new(Symbol::from("INVALID_FORMAT"), Venue::from("SMART")); - let leg_id = InstrumentId::new(Symbol::from("SPY C400"), Venue::from("SMART")); - let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id); - assert_eq!(result, 0); // Should fallback to 0 -} - -#[rstest] -fn test_get_leg_position_three_leg_spread() { - let spread_id = InstrumentId::new( - Symbol::from("(1)LEG1_((1))LEG2_((2))LEG3"), - Venue::from("SMART"), - ); - - // Test first leg - let leg_id1 = InstrumentId::new(Symbol::from("LEG1"), Venue::from("SMART")); - let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id1); - assert_eq!(result, 0); - - // Test second leg - let leg_id2 = InstrumentId::new(Symbol::from("LEG2"), Venue::from("SMART")); - let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id2); - assert_eq!(result, 1); - - // Test third leg - let leg_id3 = InstrumentId::new(Symbol::from("LEG3"), Venue::from("SMART")); - let result = InteractiveBrokersExecutionClient::get_leg_position(&spread_id, &leg_id3); - assert_eq!(result, 2); -} diff --git a/crates/adapters/interactive_brokers/src/execution/core_tracking.rs b/crates/adapters/interactive_brokers/src/execution/core_tracking.rs deleted file mode 100644 index 3ef94a49b17b..000000000000 --- a/crates/adapters/interactive_brokers/src/execution/core_tracking.rs +++ /dev/null @@ -1,427 +0,0 @@ -// ------------------------------------------------------------------------------------------------- -// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -// https://nautechsystems.io -// -// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html -// ------------------------------------------------------------------------------------------------- - -use nautilus_common::live::sender::EventSender; - -use super::*; - -impl InteractiveBrokersExecutionClient { - pub(super) fn cached_spread_instrument_ids_for_preload( - cache: &Cache, - instrument_provider: &InteractiveBrokersInstrumentProvider, - ) -> Vec { - let mut spread_ids = ahash::AHashSet::new(); - - for client_order_id in cache.iter_client_order_ids(None, None, None, None) { - if let Some(order) = cache.order(&client_order_id) { - let instrument_id = order.instrument_id(); - if is_spread_instrument_id(&instrument_id) - && instrument_provider.find(&instrument_id).is_none() - { - spread_ids.insert(instrument_id); - } - } - } - - let mut spread_ids: Vec = spread_ids.into_iter().collect(); - spread_ids.sort_by_key(|a| a.to_string()); - spread_ids - } - - pub(super) async fn preload_cached_spread_instruments( - &self, - client: &Client, - ) -> anyhow::Result<()> { - let spread_ids = { - let cache = self.core.cache(); - Self::cached_spread_instrument_ids_for_preload(&cache, &self.instrument_provider) - }; - - if spread_ids.is_empty() { - return Ok(()); - } - - tracing::debug!( - "Preloading {} cached Interactive Brokers spread instrument(s) before reconciliation", - spread_ids.len() - ); - - for instrument_id in spread_ids { - match self - .instrument_provider - .fetch_spread_instrument(client, instrument_id, false, None) - .await - { - Ok(true) => { - tracing::debug!("Preloaded cached spread instrument {}", instrument_id); - } - Ok(false) => { - tracing::warn!( - "Failed to preload cached spread instrument {}", - instrument_id - ); - } - Err(e) => { - tracing::warn!( - "Failed to preload cached spread instrument {}: {}", - instrument_id, - e - ); - } - } - } - - Ok(()) - } - - pub(super) fn get_mapped_instrument_id( - order_id: i32, - instrument_id_map: &Arc>>, - ) -> Option { - let map = instrument_id_map.lock(); - map.get(&order_id).copied() - } - - pub(super) fn get_required_order_actor_ids( - order_id: i32, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - ) -> anyhow::Result<(TraderId, StrategyId)> { - let trader_id = { - let map = trader_id_map.lock(); - map.get(&order_id).copied() - } - .with_context(|| format!("Trader ID not found for Interactive Brokers order {order_id}"))?; - - let strategy_id = { - let map = strategy_id_map.lock(); - map.get(&order_id).copied() - } - .with_context(|| { - format!("Strategy ID not found for Interactive Brokers order {order_id}") - })?; - - Ok((trader_id, strategy_id)) - } - - pub(super) fn resolve_contract_for_instrument( - instrument_id: InstrumentId, - instrument_provider: &Arc, - ) -> anyhow::Result { - instrument_provider - .resolve_contract_for_instrument(instrument_id) - .context("Failed to convert instrument ID to IB contract") - } - - pub(super) fn contract_with_order_exchange_param( - mut contract: ibapi::contracts::Contract, - params: Option<&nautilus_core::Params>, - ) -> anyhow::Result { - let Some(params) = params else { - return Ok(contract); - }; - - let Some(exchange_value) = params.get("exchange") else { - return Ok(contract); - }; - - let Some(exchange) = exchange_value.as_str() else { - anyhow::bail!("`exchange` order param must be a string"); - }; - - if exchange.is_empty() { - return Ok(contract); - } - - contract.exchange = ibapi::contracts::Exchange::from(exchange); - Ok(contract) - } - - #[allow(clippy::too_many_arguments)] - pub(super) fn cache_order_tracking( - ib_order_id: i32, - client_order_id: ClientOrderId, - instrument_id: InstrumentId, - trader_id: TraderId, - strategy_id: StrategyId, - order_side: OrderSide, - order_type: OrderType, - order_id_map: &Arc>>, - venue_order_id_map: &Arc>>, - instrument_id_map: &Arc>>, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - active_order_contexts: &Arc>>, - terminal_order_contexts: &Arc>>, - ) { - { - let mut order_map = order_id_map.lock(); - order_map.insert(client_order_id, ib_order_id); - } - - { - let mut venue_map = venue_order_id_map.lock(); - venue_map.insert(ib_order_id, client_order_id); - } - - { - let mut instrument_map = instrument_id_map.lock(); - instrument_map.insert(ib_order_id, instrument_id); - } - - { - let mut trader_map = trader_id_map.lock(); - trader_map.insert(ib_order_id, trader_id); - } - - { - let mut strategy_map = strategy_id_map.lock(); - strategy_map.insert(ib_order_id, strategy_id); - } - - terminal_order_contexts.lock().remove(&ib_order_id); - active_order_contexts.lock().insert( - ib_order_id, - TrackedOrderContext { - client_order_id, - trader_id, - strategy_id, - instrument_id, - order_side, - order_type, - accepted: false, - avg_px: None, - }, - ); - } - - #[allow(clippy::too_many_arguments)] - pub(super) fn cache_cancel_order_tracking( - ib_order_id: i32, - cmd: &CancelOrder, - target_order: &OrderAny, - order_id_map: &Arc>>, - venue_order_id_map: &Arc>>, - instrument_id_map: &Arc>>, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - ) -> anyhow::Result<()> { - // Order-status callbacks first map the IB order ID to a client order ID, then read - // its instrument, trader, and strategy IDs. Hold this lock while updating all maps - // so a callback sees either the complete identity or no route at all. - let mut venue_map = venue_order_id_map.lock(); - if let Some(existing_client_order_id) = venue_map.get(&ib_order_id) { - anyhow::ensure!( - *existing_client_order_id == cmd.client_order_id, - "IB order ID {ib_order_id} is already mapped to client order {existing_client_order_id}" - ); - } - venue_map.remove(&ib_order_id); - - order_id_map.lock().insert(cmd.client_order_id, ib_order_id); - instrument_id_map - .lock() - .insert(ib_order_id, target_order.instrument_id()); - trader_id_map - .lock() - .insert(ib_order_id, target_order.trader_id()); - strategy_id_map - .lock() - .insert(ib_order_id, target_order.strategy_id()); - venue_map.insert(ib_order_id, cmd.client_order_id); - - Ok(()) - } - - pub(super) fn get_tracked_order_context( - ib_order_id: i32, - active_order_contexts: &Arc>>, - terminal_order_contexts: &Arc>>, - ) -> Option { - if let Some(context) = active_order_contexts.lock().get(&ib_order_id).cloned() { - return Some(context); - } - - terminal_order_contexts.lock().get(&ib_order_id).cloned() - } - - pub(super) fn emit_order_accepted_if_needed( - ib_order_id: i32, - venue_order_id: VenueOrderId, - account_id: AccountId, - ts_event: UnixNanos, - active_order_contexts: &Arc>>, - exec_sender: &EventSender, - ) -> anyhow::Result { - let mut contexts = active_order_contexts.lock(); - let Some(context) = contexts.get_mut(&ib_order_id) else { - return Ok(false); - }; - - Self::emit_order_accepted(context, venue_order_id, account_id, ts_event, exec_sender) - } - - #[allow(clippy::too_many_arguments)] - pub(super) fn emit_order_accepted_for_fill_if_needed( - ib_order_id: i32, - venue_order_id: VenueOrderId, - account_id: AccountId, - ts_event: UnixNanos, - active_order_contexts: &Arc>>, - terminal_order_contexts: &Arc>>, - exec_sender: &EventSender, - ) -> anyhow::Result { - if Self::emit_order_accepted_if_needed( - ib_order_id, - venue_order_id, - account_id, - ts_event, - active_order_contexts, - exec_sender, - )? { - return Ok(true); - } - - let mut contexts = terminal_order_contexts.lock(); - let Some(context) = contexts.get_mut(&ib_order_id) else { - return Ok(false); - }; - - Self::emit_order_accepted(context, venue_order_id, account_id, ts_event, exec_sender) - } - - fn emit_order_accepted( - context: &mut TrackedOrderContext, - venue_order_id: VenueOrderId, - account_id: AccountId, - ts_event: UnixNanos, - exec_sender: &EventSender, - ) -> anyhow::Result { - if context.accepted { - return Ok(false); - } - - let event = OrderAccepted::new( - context.trader_id, - context.strategy_id, - context.instrument_id, - context.client_order_id, - venue_order_id, - account_id, - UUID4::new(), - ts_event, - ts_event, - false, - ); - exec_sender - .send(ExecutionEvent::Order(OrderEventAny::Accepted(event))) - .map_err(|e| anyhow::anyhow!("Failed to send order accepted event: {e}"))?; - context.accepted = true; - - Ok(true) - } - - #[allow(clippy::too_many_arguments)] - pub(super) fn remove_order_tracking( - ib_order_id: i32, - client_order_id: ClientOrderId, - order_id_map: &Arc>>, - venue_order_id_map: &Arc>>, - instrument_id_map: &Arc>>, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - active_order_contexts: &Arc>>, - terminal_order_contexts: &Arc>>, - ) { - order_id_map.lock().remove(&client_order_id); - venue_order_id_map.lock().remove(&ib_order_id); - instrument_id_map.lock().remove(&ib_order_id); - trader_id_map.lock().remove(&ib_order_id); - strategy_id_map.lock().remove(&ib_order_id); - active_order_contexts.lock().remove(&ib_order_id); - terminal_order_contexts.lock().remove(&ib_order_id); - } -} - -#[cfg(test)] -mod tests { - use ibapi::contracts::{Contract, Exchange}; - use nautilus_core::Params; - use rstest::rstest; - use serde_json::Value; - - use super::*; - - fn contract_with_exchange(exchange: &str) -> Contract { - Contract { - exchange: Exchange::from(exchange), - ..Default::default() - } - } - - #[rstest] - fn test_contract_with_order_exchange_param_overrides_exchange() { - let contract = contract_with_exchange("SMART"); - let mut params = Params::new(); - params.insert("exchange".to_string(), Value::String("IEX".to_string())); - - let updated = InteractiveBrokersExecutionClient::contract_with_order_exchange_param( - contract.clone(), - Some(¶ms), - ) - .unwrap(); - - assert_eq!(updated.exchange.as_str(), "IEX"); - assert_eq!(contract.exchange.as_str(), "SMART"); - } - - #[rstest] - fn test_contract_with_order_exchange_param_keeps_contract_without_exchange() { - let contract = contract_with_exchange("SMART"); - let params = Params::new(); - - let updated = InteractiveBrokersExecutionClient::contract_with_order_exchange_param( - contract, - Some(¶ms), - ) - .unwrap(); - - assert_eq!(updated.exchange.as_str(), "SMART"); - } - - #[rstest] - fn test_contract_with_order_exchange_param_keeps_contract_with_empty_exchange() { - let contract = contract_with_exchange("SMART"); - let mut params = Params::new(); - params.insert("exchange".to_string(), Value::String(String::new())); - - let updated = InteractiveBrokersExecutionClient::contract_with_order_exchange_param( - contract, - Some(¶ms), - ) - .unwrap(); - - assert_eq!(updated.exchange.as_str(), "SMART"); - } - - #[rstest] - fn test_contract_with_order_exchange_param_rejects_non_string_exchange() { - let contract = contract_with_exchange("SMART"); - let mut params = Params::new(); - params.insert("exchange".to_string(), Value::Bool(true)); - - let err = InteractiveBrokersExecutionClient::contract_with_order_exchange_param( - contract, - Some(¶ms), - ) - .unwrap_err(); - - assert!(err.to_string().contains("must be a string")); - } -} diff --git a/crates/adapters/interactive_brokers/src/execution/core_updates.rs b/crates/adapters/interactive_brokers/src/execution/core_updates.rs deleted file mode 100644 index 431d34dfe562..000000000000 --- a/crates/adapters/interactive_brokers/src/execution/core_updates.rs +++ /dev/null @@ -1,1434 +0,0 @@ -// ------------------------------------------------------------------------------------------------- -// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -// https://nautechsystems.io -// -// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html -// ------------------------------------------------------------------------------------------------- - -use ibapi::subscriptions::SubscriptionItem; -use nautilus_common::live::sender::EventSender; - -use super::*; -use crate::{ - common::enums::{IbAction, IbOrderStatus, IbOrderType}, - execution::parse, -}; - -impl InteractiveBrokersExecutionClient { - /// Starts the order update subscription stream. - /// - /// # Errors - /// - /// Returns an error if starting the subscription fails. - pub(super) async fn start_order_updates(&self) -> anyhow::Result<()> { - let client = self.ib_client.as_ref().context("IB client not connected")?; - - let timeout_dur = Duration::from_secs(self.config.request_timeout); - log::debug!( - "Starting IB order update stream subscription (timeout={:?}, client_id={}, account_id={})", - timeout_dur, - self.client_id(), - self.account_id() - ); - let mut subscription = tokio::time::timeout(timeout_dur, client.order_update_stream()) - .await - .context("Timeout starting order update stream")??; - - let order_id_map = Arc::clone(&self.order_id_map); - let venue_order_id_map = Arc::clone(&self.venue_order_id_map); - let instrument_provider = Arc::clone(&self.instrument_provider); - let exec_sender = get_exec_event_sender(); - let clock = get_atomic_clock_realtime(); - let account_id = self.core.account_id; - let commission_cache = Arc::clone(&self.commission_cache); - let pending_execution_cache = Arc::clone(&self.pending_execution_cache); - let instrument_id_map = Arc::clone(&self.instrument_id_map); - let trader_id_map = Arc::clone(&self.trader_id_map); - let strategy_id_map = Arc::clone(&self.strategy_id_map); - let active_order_contexts = Arc::clone(&self.active_order_contexts); - let terminal_order_contexts = Arc::clone(&self.terminal_order_contexts); - let spread_fill_tracking = Arc::clone(&self.spread_fill_tracking); - let order_avg_prices = Arc::clone(&self.order_avg_prices); - let pending_combo_fills = Arc::clone(&self.pending_combo_fills); - let pending_combo_fill_avgs = Arc::clone(&self.pending_combo_fill_avgs); - let order_fill_progress = Arc::clone(&self.order_fill_progress); - let pending_cancel_orders = Arc::clone(&self.pending_cancel_orders); - - let future = async move { - Self::process_order_update_stream( - &mut subscription, - &order_id_map, - &venue_order_id_map, - &instrument_provider, - &exec_sender, - clock, - account_id, - &commission_cache, - &pending_execution_cache, - &instrument_id_map, - &trader_id_map, - &strategy_id_map, - &active_order_contexts, - &terminal_order_contexts, - &spread_fill_tracking, - &order_avg_prices, - &pending_combo_fills, - &pending_combo_fill_avgs, - &order_fill_progress, - &pending_cancel_orders, - ) - .await; - }; - - self.session_tasks - .spawn(future) - .context("failed to register IB order update stream task")?; - - log::debug!("IB order update stream subscription started"); - - Ok(()) - } - - #[allow(clippy::too_many_arguments)] - pub(super) async fn process_order_update_stream( - subscription: &mut ibapi::subscriptions::Subscription, - order_id_map: &Arc>>, - venue_order_id_map: &Arc>>, - instrument_provider: &Arc, - exec_sender: &EventSender, - clock: &'static AtomicTime, - account_id: AccountId, - commission_cache: &Arc>, - pending_execution_cache: &Arc>, - instrument_id_map: &Arc>>, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - active_order_contexts: &Arc>>, - terminal_order_contexts: &Arc>>, - spread_fill_tracking: &Arc>>>, - order_avg_prices: &Arc>>, - pending_combo_fills: &Arc>>>, - pending_combo_fill_avgs: &Arc>>>, - order_fill_progress: &Arc>>, - pending_cancel_orders: &Arc>>, - ) { - while let Some(update_result) = subscription.next().await { - match update_result { - Ok(SubscriptionItem::Data(update)) => { - if let Err(e) = Self::handle_order_update( - &update, - order_id_map, - venue_order_id_map, - instrument_provider, - exec_sender, - clock, - account_id, - commission_cache, - instrument_id_map, - trader_id_map, - strategy_id_map, - active_order_contexts, - terminal_order_contexts, - spread_fill_tracking, - order_avg_prices, - pending_combo_fills, - pending_combo_fill_avgs, - order_fill_progress, - pending_cancel_orders, - pending_execution_cache, - ) - .await - { - tracing::error!("Error handling order update: {e}"); - } - } - Ok(SubscriptionItem::Notice(notice)) => { - tracing::debug!("Received IB order update notice: {notice:?}"); - } - Err(e) => { - tracing::error!("Error receiving order update: {e}"); - } - } - } - } - - #[allow(clippy::too_many_arguments)] - pub(super) async fn handle_order_update( - update: &OrderUpdate, - order_id_map: &Arc>>, - venue_order_id_map: &Arc>>, - instrument_provider: &Arc, - exec_sender: &EventSender, - clock: &'static AtomicTime, - account_id: AccountId, - commission_cache: &Arc>, - instrument_id_map: &Arc>>, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - active_order_contexts: &Arc>>, - terminal_order_contexts: &Arc>>, - spread_fill_tracking: &Arc>>>, - order_avg_prices: &Arc>>, - pending_combo_fills: &Arc>>>, - pending_combo_fill_avgs: &Arc>>>, - order_fill_progress: &Arc>>, - pending_cancel_orders: &Arc>>, - pending_live_exec_data: &Arc>, - ) -> anyhow::Result<()> { - let ts_init = clock.get_time_ns(); - - match update { - OrderUpdate::OrderStatus(status) => { - Self::handle_order_status( - status, - order_id_map, - venue_order_id_map, - instrument_provider, - exec_sender, - ts_init, - account_id, - instrument_id_map, - trader_id_map, - strategy_id_map, - active_order_contexts, - terminal_order_contexts, - order_avg_prices, - pending_combo_fills, - pending_combo_fill_avgs, - order_fill_progress, - pending_cancel_orders, - spread_fill_tracking, - ) - .await?; - } - OrderUpdate::ExecutionData(exec_data) => { - let execution_id = exec_data.execution.execution_id.clone(); - let has_commission = commission_cache.lock().contains_key(&execution_id); - - if !has_commission { - tracing::debug!( - "Buffering execution data {} until commission report arrives", - execution_id - ); - pending_live_exec_data - .lock() - .insert(execution_id, exec_data.clone()); - return Ok(()); - } - - Self::handle_execution_data( - exec_data, - order_id_map, - venue_order_id_map, - instrument_provider, - exec_sender, - ts_init, - account_id, - commission_cache, - spread_fill_tracking, - instrument_id_map, - active_order_contexts, - terminal_order_contexts, - order_avg_prices, - pending_combo_fills, - pending_combo_fill_avgs, - order_fill_progress, - ) - .await?; - } - OrderUpdate::CommissionReport(commission) => { - let pending_exec_data = pending_live_exec_data - .lock() - .remove(&commission.execution_id); - - { - let mut cache = commission_cache.lock(); - // IB uses -1.0 as a pending-sentinel before the real commission arrives; - // clamp only that sentinel to zero (legitimate rebates can be negative). - let commission_value = if commission.commission == -1.0_f64 { - 0.0_f64 - } else { - commission.commission - }; - cache.insert( - commission.execution_id.clone(), - (commission_value, commission.currency.clone()), - ); - } - - if let Some(exec_data) = pending_exec_data { - Self::handle_execution_data( - &exec_data, - order_id_map, - venue_order_id_map, - instrument_provider, - exec_sender, - ts_init, - account_id, - commission_cache, - spread_fill_tracking, - instrument_id_map, - active_order_contexts, - terminal_order_contexts, - order_avg_prices, - pending_combo_fills, - pending_combo_fill_avgs, - order_fill_progress, - ) - .await?; - } - } - OrderUpdate::OpenOrder(order_data) => { - if !Self::is_active_open_order(&order_data.order) { - tracing::debug!( - "Ignoring deactivated open order: order_id={}, order_ref={}", - order_data.order_id, - order_data.order.order_ref - ); - return Ok(()); - } - - if order_data.order.what_if - && IbOrderStatus::from_str(order_data.order_state.status.as_str()) - .is_ok_and(|status| status == IbOrderStatus::PreSubmitted) - { - Self::handle_whatif_order( - order_data, - venue_order_id_map, - instrument_id_map, - trader_id_map, - strategy_id_map, - instrument_provider, - exec_sender, - clock.get_time_ns(), - account_id, - ) - .await?; - } else { - let status_str = order_data.order_state.status.as_str(); - tracing::debug!( - "Received open order: order_id={}, status={}, order_ref={}", - order_data.order_id, - status_str, - order_data.order.order_ref - ); - - let client_order_id = if let Some(order_ref) = - parse::normalized_order_ref(&order_data.order.order_ref) - { - Some(ClientOrderId::from(order_ref)) - } else { - let map = venue_order_id_map.lock(); - map.get(&order_data.order_id).copied() - }; - - if let Some(client_order_id) = client_order_id - && IbOrderStatus::from_str(status_str).is_ok_and(IbOrderStatus::is_accepted) - { - let instrument_id = { - Self::get_mapped_instrument_id(order_data.order_id, instrument_id_map) - .map(Ok) - .unwrap_or_else(|| { - Self::resolve_contract_instrument_id( - instrument_provider, - &order_data.contract, - ) - })? - }; - let venue_order_id = - parse::ib_venue_order_id(order_data.order_id, order_data.order.perm_id); - if Self::emit_order_accepted_if_needed( - order_data.order_id, - venue_order_id, - account_id, - ts_init, - active_order_contexts, - exec_sender, - )? { - tracing::debug!( - "Order {} accepted (IB openOrder status: {})", - client_order_id, - status_str - ); - } - - Self::emit_order_updated_from_open_order( - order_data, - client_order_id, - instrument_id, - trader_id_map, - strategy_id_map, - instrument_provider, - exec_sender, - ts_init, - account_id, - )?; - } - } - } - } - - Ok(()) - } - - #[allow(clippy::too_many_arguments)] - fn emit_order_updated_from_open_order( - order_data: &ibapi::orders::OrderData, - client_order_id: ClientOrderId, - instrument_id: InstrumentId, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - instrument_provider: &Arc, - exec_sender: &EventSender, - ts_init: UnixNanos, - account_id: AccountId, - ) -> anyhow::Result<()> { - let Some(instrument) = instrument_provider.find(&instrument_id) else { - return Ok(()); - }; - - if order_data.order.total_quantity <= 0.0 { - return Ok(()); - } - - let (trader_id, strategy_id) = Self::get_required_order_actor_ids( - order_data.order_id, - trader_id_map, - strategy_id_map, - )?; - let price_magnifier = instrument_provider.get_price_magnifier(&instrument_id) as f64; - let (price, trigger_price) = Self::open_order_price_fields( - order_data, - price_magnifier, - instrument.price_precision(), - ); - let quantity = Quantity::new(order_data.order.total_quantity, instrument.size_precision()); - let venue_order_id = - parse::ib_venue_order_id(order_data.order_id, order_data.order.perm_id); - let event = OrderUpdated::new( - trader_id, - strategy_id, - instrument_id, - client_order_id, - quantity, - UUID4::new(), - ts_init, - ts_init, - false, - Some(venue_order_id), - Some(account_id), - price, - trigger_price, - None, - false, - ); - - exec_sender - .send(ExecutionEvent::Order(OrderEventAny::Updated(event))) - .map_err(|e| anyhow::anyhow!("Failed to send order updated event: {e}")) - } - - fn open_order_price_fields( - order_data: &ibapi::orders::OrderData, - price_magnifier: f64, - price_precision: u8, - ) -> (Option, Option) { - let order_type = IbOrderType::from_str(order_data.order.order_type.as_str()) - .map_or(OrderType::Market, IbOrderType::nautilus_order_type); - let price = order_data - .order - .limit_price - .map(|price| Price::new(price * price_magnifier, price_precision)); - let trigger_price = order_data - .order - .aux_price - .map(|price| Price::new(price * price_magnifier, price_precision)); - - match order_type { - OrderType::Market | OrderType::MarketToLimit | OrderType::TrailingStopMarket => { - (None, None) - } - OrderType::Limit | OrderType::TrailingStopLimit => (price, None), - OrderType::StopMarket | OrderType::MarketIfTouched => (None, trigger_price), - OrderType::StopLimit | OrderType::LimitIfTouched => (price, trigger_price), - } - } - - #[allow(clippy::too_many_arguments)] - pub(super) async fn handle_order_status( - status: &IBOrderStatus, - order_id_map: &Arc>>, - venue_order_id_map: &Arc>>, - instrument_provider: &Arc, - exec_sender: &EventSender, - ts_init: UnixNanos, - account_id: AccountId, - instrument_id_map: &Arc>>, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - active_order_contexts: &Arc>>, - terminal_order_contexts: &Arc>>, - order_avg_prices: &Arc>>, - pending_combo_fills: &Arc>>>, - pending_combo_fill_avgs: &Arc>>>, - order_fill_progress: &Arc>>, - pending_cancel_orders: &Arc>>, - spread_fill_tracking: &Arc>>>, - ) -> anyhow::Result<()> { - let client_order_id = { - let map = venue_order_id_map.lock(); - map.get(&status.order_id).copied() - }; - - let Some(client_order_id) = client_order_id else { - tracing::debug!("Order status for unknown order ID: {}", status.order_id); - return Ok(()); - }; - - let instrument_id = Self::get_mapped_instrument_id(status.order_id, instrument_id_map); - - let Some(instrument_id) = instrument_id else { - tracing::debug!("Instrument ID not found for order ID: {}", status.order_id); - return Ok(()); - }; - - Self::update_order_avg_price( - client_order_id, - &instrument_id, - status.average_fill_price.unwrap_or(0.0), - status.filled, - instrument_provider, - order_avg_prices, - pending_combo_fill_avgs, - order_fill_progress, - )?; - - let ib_order_status = IbOrderStatus::from_str(status.status.as_str()).ok(); - - if ib_order_status == Some(IbOrderStatus::Inactive) && status.why_held == "locate" { - tracing::warn!( - "Order {} held for short-sell locate, order remains active", - client_order_id - ); - return Ok(()); - } - - let venue_order_id = parse::ib_venue_order_id(status.order_id, status.perm_id); - let is_terminal = ib_order_status.is_some_and(IbOrderStatus::is_terminal); - - if matches!( - ib_order_status, - Some(IbOrderStatus::Filled | IbOrderStatus::Cancelled | IbOrderStatus::ApiCancelled) - ) { - Self::emit_order_accepted_if_needed( - status.order_id, - venue_order_id, - account_id, - ts_init, - active_order_contexts, - exec_sender, - )?; - } - - if is_terminal { - Self::flush_pending_combo_fills( - client_order_id, - pending_combo_fills, - pending_combo_fill_avgs, - order_fill_progress, - exec_sender, - )?; - pending_combo_fills.lock().remove(&client_order_id); - pending_combo_fill_avgs.lock().remove(&client_order_id); - order_fill_progress.lock().remove(&client_order_id); - } - - let status_str = status.status.as_str(); - - match ib_order_status { - Some(IbOrderStatus::Submitted | IbOrderStatus::PreSubmitted) => { - if Self::emit_order_accepted_if_needed( - status.order_id, - venue_order_id, - account_id, - ts_init, - active_order_contexts, - exec_sender, - )? { - tracing::debug!( - "Order {} accepted (IB status: {})", - client_order_id, - status_str - ); - } else { - tracing::debug!( - "Order {} already accepted (IB status: {})", - client_order_id, - status_str - ); - } - } - Some(IbOrderStatus::Filled) => { - tracing::debug!( - "Order {} filled (IB status: {})", - client_order_id, - status_str - ); - } - Some(IbOrderStatus::Cancelled | IbOrderStatus::ApiCancelled) => { - pending_cancel_orders.lock().remove(&client_order_id); - - let (trader_id, strategy_id) = Self::get_required_order_actor_ids( - status.order_id, - trader_id_map, - strategy_id_map, - )?; - - let event = OrderCanceled::new( - trader_id, - strategy_id, - instrument_id, - client_order_id, - UUID4::new(), - ts_init, - ts_init, - false, - Some(venue_order_id), - Some(account_id), - None, - ); - exec_sender - .send(ExecutionEvent::Order(OrderEventAny::Canceled(event))) - .map_err(|e| anyhow::anyhow!("Failed to send order canceled event: {e}"))?; - tracing::debug!("Order {} canceled", client_order_id); - } - Some(IbOrderStatus::PendingCancel) => { - Self::emit_order_pending_cancel( - status.order_id, - client_order_id, - venue_order_id, - instrument_id_map, - trader_id_map, - strategy_id_map, - pending_cancel_orders, - exec_sender, - ts_init, - account_id, - )?; - tracing::debug!("Order {} pending cancel", client_order_id); - } - _ => { - tracing::debug!( - "Order status update for order {}: {} (status: {})", - client_order_id, - status_str, - status_str - ); - } - } - - if is_terminal { - Self::evict_terminal_order_state( - client_order_id, - status.order_id, - order_id_map, - venue_order_id_map, - instrument_id_map, - trader_id_map, - strategy_id_map, - active_order_contexts, - terminal_order_contexts, - order_avg_prices, - pending_combo_fills, - pending_combo_fill_avgs, - order_fill_progress, - pending_cancel_orders, - spread_fill_tracking, - ); - } - - Ok(()) - } - - #[allow(clippy::too_many_arguments)] - fn evict_terminal_order_state( - client_order_id: ClientOrderId, - order_id: i32, - order_id_map: &Arc>>, - venue_order_id_map: &Arc>>, - instrument_id_map: &Arc>>, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - active_order_contexts: &Arc>>, - terminal_order_contexts: &Arc>>, - order_avg_prices: &Arc>>, - pending_combo_fills: &Arc>>>, - pending_combo_fill_avgs: &Arc>>>, - order_fill_progress: &Arc>>, - pending_cancel_orders: &Arc>>, - spread_fill_tracking: &Arc>>>, - ) { - let avg_px = order_avg_prices.lock().get(&client_order_id).copied(); - - if let Some(mut context) = active_order_contexts.lock().remove(&order_id) { - context.avg_px = avg_px; - terminal_order_contexts.lock().insert(order_id, context); - } - - order_id_map.lock().remove(&client_order_id); - venue_order_id_map.lock().remove(&order_id); - instrument_id_map.lock().remove(&order_id); - trader_id_map.lock().remove(&order_id); - strategy_id_map.lock().remove(&order_id); - order_avg_prices.lock().remove(&client_order_id); - pending_combo_fills.lock().remove(&client_order_id); - pending_combo_fill_avgs.lock().remove(&client_order_id); - order_fill_progress.lock().remove(&client_order_id); - pending_cancel_orders.lock().remove(&client_order_id); - spread_fill_tracking.lock().remove(&client_order_id); - } - - #[allow(clippy::too_many_arguments)] - pub(super) async fn handle_execution_data( - exec_data: &ExecutionData, - order_id_map: &Arc>>, - venue_order_id_map: &Arc>>, - instrument_provider: &Arc, - exec_sender: &EventSender, - ts_init: UnixNanos, - account_id: AccountId, - commission_cache: &Arc>, - spread_fill_tracking: &Arc>>>, - instrument_id_map: &Arc>>, - active_order_contexts: &Arc>>, - terminal_order_contexts: &Arc>>, - order_avg_prices: &Arc>>, - pending_combo_fills: &Arc>>>, - pending_combo_fill_avgs: &Arc>>>, - order_fill_progress: &Arc>>, - ) -> anyhow::Result<()> { - let tracked_context = Self::get_tracked_order_context( - exec_data.execution.order_id, - active_order_contexts, - terminal_order_contexts, - ); - let mapped_client_order_id = { - let map = venue_order_id_map.lock(); - map.get(&exec_data.execution.order_id).copied() - }; - - let client_order_id = if let Some(client_order_id) = mapped_client_order_id { - client_order_id - } else if let Some(context) = tracked_context.as_ref() { - context.client_order_id - } else if let Some(order_ref) = - parse::normalized_order_ref(&exec_data.execution.order_reference) - { - let client_order_id = ClientOrderId::from(order_ref); - order_id_map - .lock() - .insert(client_order_id, exec_data.execution.order_id); - venue_order_id_map - .lock() - .insert(exec_data.execution.order_id, client_order_id); - client_order_id - } else { - tracing::debug!( - "Execution data for unknown order ID: {}", - exec_data.execution.order_id - ); - return Ok(()); - }; - - let instrument_id = if let Some(mapped_id) = - Self::get_mapped_instrument_id(exec_data.execution.order_id, instrument_id_map) - { - mapped_id - } else if let Some(cached_id) = - instrument_provider.get_instrument_id_by_contract_id(exec_data.contract.contract_id) - { - cached_id - } else { - Self::resolve_contract_instrument_id(instrument_provider, &exec_data.contract)? - }; - - let (commission, commission_currency) = { - let mut cache = commission_cache.lock(); - let Some((commission, commission_currency)) = - cache.remove(&exec_data.execution.execution_id) - else { - tracing::debug!( - "Execution data {} is waiting for commission report", - exec_data.execution.execution_id - ); - return Ok(()); - }; - (commission, commission_currency) - }; - - let is_bag = matches!( - exec_data.contract.security_type, - ibapi::contracts::SecurityType::Spread - ) || !exec_data.contract.combo_legs.is_empty(); - - let spread_instrument_id = { - let map = instrument_id_map.lock(); - map.get(&exec_data.execution.order_id).copied() - } - .or_else(|| { - tracked_context - .as_ref() - .map(|context| context.instrument_id) - }); - - let is_spread = if let Some(spread_id) = spread_instrument_id { - if let Some(instrument) = instrument_provider.find(&spread_id) { - instrument.is_spread() - } else { - false - } - } else { - false - }; - - let is_spread_id = instrument_id.symbol.as_str().contains("_(") - || instrument_id.symbol.as_str().contains(")_"); - - let avg_px = { - let avg_prices = order_avg_prices.lock(); - avg_prices.get(&client_order_id).copied() - } - .or_else(|| tracked_context.as_ref().and_then(|context| context.avg_px)); - - let venue_order_id = - parse::ib_venue_order_id(exec_data.execution.order_id, exec_data.execution.perm_id); - - if tracked_context.is_some() { - Self::emit_order_accepted_for_fill_if_needed( - exec_data.execution.order_id, - venue_order_id, - account_id, - parse_execution_time(&exec_data.execution.time)?, - active_order_contexts, - terminal_order_contexts, - exec_sender, - )?; - } - - if (is_bag || is_spread_id) - && is_spread - && let Some(spread_id) = spread_instrument_id - && let Some(context) = tracked_context.as_ref() - { - if let Err(e) = Self::handle_spread_execution( - exec_data, - client_order_id, - spread_id, - &instrument_id, - commission, - &commission_currency, - instrument_provider, - exec_sender, - ts_init, - account_id, - spread_fill_tracking, - context, - pending_combo_fills, - pending_combo_fill_avgs, - order_fill_progress, - avg_px, - ) - .await - { - tracing::warn!( - "Error handling spread execution, falling back to regular fill: {e}" - ); - } else { - return Ok(()); - } - } - - let fill_report = parse_execution_to_fill_report( - &exec_data.execution, - &exec_data.contract, - commission, - &commission_currency, - instrument_id, - account_id, - instrument_provider, - ts_init, - avg_px, - )?; - - if let Some(context) = tracked_context { - let quote_currency = instrument_provider - .find(&context.instrument_id) - .with_context(|| { - format!( - "Instrument {} not found for tracked fill", - context.instrument_id - ) - })? - .quote_currency(); - let event = OrderFilled::new( - context.trader_id, - context.strategy_id, - context.instrument_id, - context.client_order_id, - fill_report.venue_order_id, - fill_report.account_id, - fill_report.trade_id, - context.order_side, - context.order_type, - fill_report.last_qty, - fill_report.last_px, - quote_currency, - fill_report.liquidity_side, - UUID4::new(), - fill_report.ts_event, - fill_report.ts_init, - false, - fill_report.venue_position_id, - Some(fill_report.commission), - None, - ); - exec_sender.send(ExecutionEvent::Order(OrderEventAny::Filled(event)))?; - } else { - exec_sender.send(ExecutionEvent::Report(ExecutionReport::Fill(Box::new( - fill_report, - ))))?; - } - - Ok(()) - } - - #[allow(clippy::too_many_arguments)] - pub(super) fn update_order_avg_price( - client_order_id: ClientOrderId, - instrument_id: &InstrumentId, - avg_fill_price: f64, - filled: f64, - instrument_provider: &Arc, - order_avg_prices: &Arc>>, - pending_combo_fill_avgs: &Arc>>>, - order_fill_progress: &Arc>>, - ) -> anyhow::Result<()> { - let is_spread_order = is_spread_instrument_id(instrument_id); - if filled <= 0.0 || !parse::should_use_avg_fill_price(avg_fill_price, instrument_id) { - return Ok(()); - } - - let Some(instrument) = instrument_provider.find(instrument_id) else { - return Ok(()); - }; - - let price_magnifier = instrument_provider.get_price_magnifier(instrument_id) as f64; - let converted_avg_price = avg_fill_price * price_magnifier; - let avg_px = Price::new(converted_avg_price, instrument.price_precision()); - - order_avg_prices.lock().insert(client_order_id, avg_px); - - let filled_decimal = Decimal::from_f64_retain(filled) - .ok_or_else(|| anyhow::anyhow!("Failed to convert filled qty to Decimal: {filled}"))?; - let avg_decimal = Decimal::from_f64_retain(converted_avg_price).ok_or_else(|| { - anyhow::anyhow!("Failed to convert avg fill price to Decimal: {converted_avg_price}") - })?; - - let mut progress = order_fill_progress.lock(); - let (previous_filled, previous_notional) = progress - .get(&client_order_id) - .copied() - .unwrap_or((Decimal::ZERO, Decimal::ZERO)); - let total_notional = filled_decimal * avg_decimal; - progress.insert(client_order_id, (filled_decimal, total_notional)); - drop(progress); - - let fill_delta = filled_decimal - previous_filled; - if fill_delta <= Decimal::ZERO || !is_spread_order { - return Ok(()); - } - - let notional_delta = total_notional - previous_notional; - let partial_avg_decimal = notional_delta / fill_delta; - let partial_avg_px = - Price::from_decimal_dp(partial_avg_decimal, instrument.price_precision()) - .map_err(|e| anyhow::anyhow!("Failed to create avg_px price: {e}"))?; - - pending_combo_fill_avgs - .lock() - .entry(client_order_id) - .or_default() - .push_back((fill_delta, partial_avg_px)); - - Ok(()) - } - - pub(super) fn flush_pending_combo_fills( - client_order_id: ClientOrderId, - pending_combo_fills: &Arc>>>, - pending_combo_fill_avgs: &Arc>>>, - order_fill_progress: &Arc>>, - exec_sender: &EventSender, - ) -> anyhow::Result<()> { - let mut combo_fills = pending_combo_fills.lock(); - let mut avg_chunks = pending_combo_fill_avgs.lock(); - - loop { - let maybe_fill = combo_fills - .get(&client_order_id) - .and_then(|fills| fills.front().cloned()); - let maybe_avg = avg_chunks - .get(&client_order_id) - .and_then(|chunks| chunks.front().cloned()); - - let (fill, (avg_qty, avg_px)) = match (maybe_fill, maybe_avg) { - (Some(fill), Some(avg)) => (fill, avg), - _ => break, - }; - - let fill_qty_decimal = fill.last_qty.as_decimal(); - if fill_qty_decimal > avg_qty { - break; - } - - let event = OrderFilled::new( - fill.trader_id, - fill.strategy_id, - fill.instrument_id, - fill.client_order_id, - fill.venue_order_id, - fill.account_id, - fill.trade_id, - fill.order_side, - fill.order_type, - fill.last_qty, - avg_px, - fill.quote_currency, - fill.liquidity_side, - UUID4::new(), - fill.ts_event, - fill.ts_init, - false, - None, - Some(fill.commission), - None, - ); - exec_sender.send(ExecutionEvent::Order(OrderEventAny::Filled(event)))?; - - if let Some(fills) = combo_fills.get_mut(&client_order_id) { - fills.pop_front(); - if fills.is_empty() { - combo_fills.remove(&client_order_id); - } - } - - if let Some(chunks) = avg_chunks.get_mut(&client_order_id) { - if fill_qty_decimal == avg_qty { - chunks.pop_front(); - } else { - chunks[0] = (avg_qty - fill_qty_decimal, avg_px); - } - - if chunks.is_empty() { - avg_chunks.remove(&client_order_id); - } - } - } - - if !combo_fills.contains_key(&client_order_id) { - order_fill_progress.lock().remove(&client_order_id); - } - - Ok(()) - } - - #[allow(clippy::too_many_arguments)] - pub(super) async fn handle_whatif_order( - order_data: &ibapi::orders::OrderData, - venue_order_id_map: &Arc>>, - instrument_id_map: &Arc>>, - trader_id_map: &Arc>>, - strategy_id_map: &Arc>>, - instrument_provider: &Arc, - exec_sender: &EventSender, - ts_init: UnixNanos, - account_id: AccountId, - ) -> anyhow::Result<()> { - let client_order_id = { - let map = venue_order_id_map.lock(); - map.get(&order_data.order_id).copied() - }; - - if client_order_id.is_none() { - tracing::debug!( - "What-if order for unknown order ID: {}", - order_data.order_id - ); - return Ok(()); - } - let client_order_id = client_order_id.expect("checked above"); - - let instrument_id = Self::get_mapped_instrument_id(order_data.order_id, instrument_id_map) - .map(Ok) - .unwrap_or_else(|| { - Self::resolve_contract_instrument_id(instrument_provider, &order_data.contract) - })?; - - let (trader_id, strategy_id) = Self::get_required_order_actor_ids( - order_data.order_id, - trader_id_map, - strategy_id_map, - )?; - - let reason_json = serde_json::to_string(&order_data.order_state) - .unwrap_or_else(|_| format!("whatIf analysis for order {}", order_data.order_id)); - - let event = OrderRejected::new( - trader_id, - strategy_id, - instrument_id, - client_order_id, - account_id, - Ustr::from(&reason_json), - UUID4::new(), - ts_init, - ts_init, - false, - false, - ); - - exec_sender - .send(ExecutionEvent::Order(OrderEventAny::Rejected(event))) - .map_err(|e| anyhow::anyhow!("Failed to send order rejected event: {e}"))?; - - tracing::debug!( - "What-if analysis completed for order {}: margin change={:?}, commission={:?}", - client_order_id, - order_data - .order_state - .initial_margin_after - .and_then(|after| order_data - .order_state - .initial_margin_before - .map(|before| after - before)), - order_data.order_state.commission - ); - - Ok(()) - } - - #[allow(clippy::too_many_arguments)] - pub(super) async fn handle_spread_execution( - exec_data: &ExecutionData, - client_order_id: ClientOrderId, - spread_instrument_id: InstrumentId, - leg_instrument_id: &InstrumentId, - commission: f64, - commission_currency: &str, - instrument_provider: &Arc, - exec_sender: &EventSender, - ts_init: UnixNanos, - account_id: AccountId, - spread_fill_tracking: &Arc>>>, - context: &TrackedOrderContext, - pending_combo_fills: &Arc>>>, - pending_combo_fill_avgs: &Arc>>>, - order_fill_progress: &Arc>>, - avg_px: Option, - ) -> anyhow::Result<()> { - let trade_id = TradeId::new(&exec_data.execution.execution_id); - let fill_id = trade_id.to_string(); - - let fill_count = { - let mut tracking = spread_fill_tracking.lock(); - - let fill_set = tracking.entry(client_order_id).or_default(); - - if fill_set.contains(&fill_id) { - tracing::debug!( - "Fill {} already processed for spread order {}, skipping", - fill_id, - client_order_id - ); - return Ok(()); - } - - fill_set.insert(fill_id); - fill_set.len() - }; - - let (leg_id, ratio) = Self::get_leg_instrument_id_and_ratio( - &exec_data.contract, - leg_instrument_id, - instrument_provider, - ); - - let spread_n_legs = - crate::common::parse::parse_spread_instrument_id_to_legs(&spread_instrument_id)?.len(); - - if (fill_count - 1) % spread_n_legs == 0 { - let pending_combo_fill = Self::build_pending_combo_fill( - exec_data, - client_order_id, - spread_instrument_id, - leg_id, - ratio, - commission, - commission_currency, - instrument_provider, - ts_init, - account_id, - context, - )?; - let combo_qty = pending_combo_fill.last_qty.as_decimal(); - pending_combo_fills - .lock() - .entry(client_order_id) - .or_default() - .push_back(pending_combo_fill); - let mut avg_chunks = pending_combo_fill_avgs.lock(); - - if !avg_chunks.contains_key(&client_order_id) - && let Some(avg_px) = avg_px - { - avg_chunks.insert(client_order_id, VecDeque::from([(combo_qty, avg_px)])); - } - drop(avg_chunks); - Self::flush_pending_combo_fills( - client_order_id, - pending_combo_fills, - pending_combo_fill_avgs, - order_fill_progress, - exec_sender, - )?; - } - - Self::generate_leg_fill( - exec_data, - client_order_id, - spread_instrument_id, - leg_id, - ratio, - commission, - commission_currency, - instrument_provider, - exec_sender, - ts_init, - account_id, - avg_px, - )?; - - Ok(()) - } - - pub(super) fn get_leg_instrument_id_and_ratio( - contract: &ibapi::contracts::Contract, - leg_instrument_id: &InstrumentId, - instrument_provider: &Arc, - ) -> (InstrumentId, i32) { - if let Some(leg_id) = - instrument_provider.get_instrument_id_by_contract_id(contract.contract_id) - { - if let Some(combo_leg) = contract.combo_legs.iter().find(|leg| { - if let Some(matched_id) = - instrument_provider.get_instrument_id_by_contract_id(leg.contract_id) - { - matched_id == leg_id - } else { - false - } - }) { - let ratio = IbAction::from_str(combo_leg.action.as_str()) - .map_or(-combo_leg.ratio, |action| { - action.signed_multiplier() * combo_leg.ratio - }); - return (leg_id, ratio); - } - } - - (*leg_instrument_id, 1) - } - - #[allow(clippy::too_many_arguments)] - pub(super) fn build_pending_combo_fill( - exec_data: &ExecutionData, - client_order_id: ClientOrderId, - spread_instrument_id: InstrumentId, - _leg_instrument_id: InstrumentId, - ratio: i32, - commission: f64, - commission_currency: &str, - instrument_provider: &Arc, - ts_init: UnixNanos, - account_id: AccountId, - context: &TrackedOrderContext, - ) -> anyhow::Result { - let spread_instrument = instrument_provider - .find(&spread_instrument_id) - .context("Spread instrument not found")?; - - let combo_quantity_value = exec_data.execution.shares / (ratio.abs() as f64); - let combo_quantity = - Quantity::new(combo_quantity_value, spread_instrument.size_precision()); - - let n_legs = spread_instrument_id.symbol.as_str().matches('_').count() + 1; - let combo_commission_value = commission * (n_legs as f64) / (ratio.abs() as f64); - let commission_money = - Money::new(combo_commission_value, Currency::from(commission_currency)); - - let ts_event = parse_execution_time(&exec_data.execution.time)?; - - Ok(PendingComboFill { - trader_id: context.trader_id, - strategy_id: context.strategy_id, - account_id, - instrument_id: spread_instrument_id, - venue_order_id: parse::ib_venue_order_id( - exec_data.execution.order_id, - exec_data.execution.perm_id, - ), - trade_id: TradeId::new(&exec_data.execution.execution_id), - order_side: context.order_side, - order_type: context.order_type, - last_qty: combo_quantity, - commission: commission_money, - liquidity_side: LiquiditySide::NoLiquiditySide, - quote_currency: spread_instrument.quote_currency(), - client_order_id, - ts_event, - ts_init, - }) - } - - #[allow(clippy::too_many_arguments)] - pub(super) fn generate_leg_fill( - exec_data: &ExecutionData, - client_order_id: ClientOrderId, - spread_instrument_id: InstrumentId, - leg_instrument_id: InstrumentId, - _ratio: i32, - commission: f64, - commission_currency: &str, - instrument_provider: &Arc, - exec_sender: &EventSender, - ts_init: UnixNanos, - account_id: AccountId, - avg_px: Option, - ) -> anyhow::Result<()> { - let leg_instrument = instrument_provider - .find(&leg_instrument_id) - .context("Leg instrument not found")?; - - let price_magnifier = instrument_provider.get_price_magnifier(&leg_instrument_id) as f64; - let execution_price = exec_data.execution.price * price_magnifier; - let leg_price = Price::new(execution_price, leg_instrument.price_precision()); - - let leg_quantity = - Quantity::new(exec_data.execution.shares, leg_instrument.size_precision()); - - let order_side = IbAction::from_str(exec_data.execution.side.as_str())?.order_side(); - - let commission_money = Money::new(commission, Currency::from(commission_currency)); - - let leg_position = Self::get_leg_position(&spread_instrument_id, &leg_instrument_id); - let leg_client_order_id = ClientOrderId::new(format!( - "{}-LEG-{}", - client_order_id, leg_instrument_id.symbol - )); - let leg_trade_id = TradeId::new(format!( - "{}-{}", - exec_data.execution.execution_id, leg_position - )); - let venue_order_id = - parse::ib_venue_order_id(exec_data.execution.order_id, exec_data.execution.perm_id); - let leg_venue_order_id = - VenueOrderId::new(format!("{}-LEG-{}", venue_order_id.as_str(), leg_position)); - - let ts_event = parse_execution_time(&exec_data.execution.time)?; - - let mut fill_report = FillReport::new( - account_id, - leg_instrument_id, - leg_venue_order_id, - leg_trade_id, - order_side, - leg_quantity, - leg_price, - commission_money, - LiquiditySide::NoLiquiditySide, - Some(leg_client_order_id), - None, - ts_event, - ts_init, - None, - ); - - if let Some(price) = avg_px { - fill_report.avg_px = Some(price.as_decimal()); - } - - exec_sender.send(ExecutionEvent::Report(ExecutionReport::Fill(Box::new( - fill_report, - ))))?; - - tracing::debug!( - "Generated leg fill: instrument_id={}, client_order_id={}, quantity={}, price={}", - leg_instrument_id, - leg_client_order_id, - leg_quantity, - leg_price - ); - - Ok(()) - } - - pub(super) fn get_leg_position( - spread_instrument_id: &InstrumentId, - leg_instrument_id: &InstrumentId, - ) -> usize { - let legs = - match crate::common::parse::parse_spread_instrument_id_to_legs(spread_instrument_id) { - Ok(legs) => legs, - Err(e) => { - log::warn!( - "Failed to parse spread instrument ID {} for leg position: {e}", - spread_instrument_id - ); - return 0; - } - }; - - for (idx, (parsed_leg_id, _)) in legs.iter().enumerate() { - if *parsed_leg_id == *leg_instrument_id { - return idx; - } - } - - log::warn!( - "Leg instrument ID {} not found in spread instrument ID {}", - leg_instrument_id, - spread_instrument_id - ); - 0 - } - - fn resolve_contract_instrument_id( - instrument_provider: &Arc, - contract: &Contract, - ) -> anyhow::Result { - match instrument_provider.resolve_instrument_id_for_contract(contract) { - Ok(instrument_id) => Ok(instrument_id), - Err(provider_error) if contract.security_type != SecurityType::Spread => { - ib_contract_to_instrument_id_simple(contract).with_context(|| { - format!( - "Failed to resolve IBKR contract to instrument ID using provider ({provider_error}) or simple conversion", - ) - }) - } - Err(provider_error) => Err(provider_error) - .context("Failed to resolve BAG contract to spread instrument ID"), - } - } -} diff --git a/crates/adapters/interactive_brokers/src/execution/incarnations.rs b/crates/adapters/interactive_brokers/src/execution/incarnations.rs new file mode 100644 index 000000000000..3f7c137c2fe9 --- /dev/null +++ b/crates/adapters/interactive_brokers/src/execution/incarnations.rs @@ -0,0 +1,606 @@ +// ------------------------------------------------------------------------------------------------- +// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. +// https://nautechsystems.io +// +// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html +// ------------------------------------------------------------------------------------------------- + +//! Tracks distinct broker orders sharing a Nautilus order reference. + +use super::{core::*, parse::parse_order_data_to_report}; +use crate::common::enums::IbAction; + +/// Returns the client order ID reporting a broker order that shares another order's reference. +/// +/// The engine models one physical order per client order ID, so each additional permanent ID +/// reaches it as an external order under this ID. +pub(super) fn duplicate_client_order_id( + account_id: AccountId, + perm_id: i64, +) -> anyhow::Result { + Ok(ClientOrderId::new_checked(format!( + "DUP-{}:{}:{}", + account_id.as_str().len(), + account_id, + parse::ib_venue_order_id(0, perm_id) + ))?) +} + +#[derive(Clone, Debug)] +pub(super) struct BrokerIncarnation { + pub(super) data: Option, + pub(super) route: Option<(i32, i32)>, + pub(super) terminal: bool, + pub(super) seen: bool, + pub(super) route_conflict: bool, + /// Filled quantity already delivered to the engine together with its executions. + pub(super) filled_resolved: f64, +} + +#[derive(Clone, Debug)] +pub(super) struct OrderIncarnationGroup { + pub(super) parent: TrackedOrder, + pub(super) account_id: AccountId, + pub(super) primary_order_id: i32, + pub(super) members: AHashMap, +} + +impl OrderTrackerState { + pub(super) fn observe_order_binding(&mut self, binding: &ibapi::orders::OrderBound) { + if let Some(parent) = self.incarnation_parents.get(&binding.perm_id).copied() { + if let Some(member) = self + .groups + .get_mut(&parent) + .and_then(|group| group.members.get_mut(&binding.perm_id)) + { + member.route = Some((binding.client_id, binding.order_id)); + member.route_conflict = false; + member.seen = !member.terminal; + } + } + } + + pub(super) fn group_for_client(&self, client_order_id: ClientOrderId) -> Option { + if self.groups.contains_key(&client_order_id) { + return Some(client_order_id); + } + self.groups.iter().find_map(|(parent, group)| { + group + .members + .keys() + .any(|perm_id| { + duplicate_client_order_id(group.account_id, *perm_id).ok() + == Some(client_order_id) + }) + .then_some(*parent) + }) + } + + pub(super) fn incarnation_data(&self, perm_id: i64) -> Option<&ibapi::orders::OrderData> { + self.incarnation_member(perm_id)?.data.as_ref() + } + + // A status showing more filled than the engine received executions for must resolve them + // first; otherwise the engine infers a fill that the real execution later repeats + pub(super) fn duplicate_fills_unresolved(&self, perm_id: i64, filled: f64) -> bool { + self.incarnation_member(perm_id) + .is_some_and(|member| filled > member.filled_resolved) + } + + pub(super) fn observe_duplicate_fills_resolved(&mut self, perm_id: i64, filled: f64) { + let Some(parent) = self.incarnation_parents.get(&perm_id).copied() else { + return; + }; + let group = if self.groups.contains_key(&parent) { + self.groups.get_mut(&parent) + } else { + self.group_history.get_mut(&parent) + }; + + if let Some(member) = group.and_then(|group| group.members.get_mut(&perm_id)) { + member.filled_resolved = member.filled_resolved.max(filled); + } + } + + fn incarnation_member(&self, perm_id: i64) -> Option<&BrokerIncarnation> { + let parent = self.incarnation_parents.get(&perm_id)?; + self.groups + .get(parent) + .or_else(|| self.group_history.get(parent))? + .members + .get(&perm_id) + } + + pub(super) fn group_cancel_candidates( + &self, + instrument_id: InstrumentId, + account_id: AccountId, + order_side: Option, + ) -> impl Iterator + '_ { + self.groups + .iter() + .filter(move |(_, group)| { + group.parent.instrument_id == instrument_id + && group.account_id == account_id + && order_side.is_none_or(|side| side == group.parent.order_side) + && group.members.values().any(|member| !member.terminal) + }) + .map(|(parent, _)| *parent) + } + + pub(super) fn group_context(&self, perm_id: i64) -> Option<(i32, TrackedOrder, AccountId)> { + let parent = self.incarnation_parents.get(&perm_id)?; + let group = self + .groups + .get(parent) + .or_else(|| self.group_history.get(parent))?; + Some(( + group.primary_order_id, + group.parent.clone(), + group.account_id, + )) + } + + pub(super) fn record_incarnation( + &mut self, + primary_order_id: i32, + parent: &TrackedOrder, + account_id: AccountId, + perm_id: i64, + data: Option<&ibapi::orders::OrderData>, + completed: bool, + ) { + let parent_id = parent.client_order_id; + if !self.groups.contains_key(&parent_id) { + let group = self.group_history.remove(&parent_id).unwrap_or_else(|| { + let mut members = AHashMap::new(); + let snapshot = self.order_snapshots.get(&parent.perm_id).cloned(); + members.insert( + parent.perm_id, + BrokerIncarnation { + route: Some((self.client_id, primary_order_id)), + terminal: self.terminal_orders.contains_key(&primary_order_id), + seen: false, + route_conflict: false, + filled_resolved: 0.0, + data: snapshot, + }, + ); + OrderIncarnationGroup { + parent: parent.clone(), + account_id, + primary_order_id, + members, + } + }); + self.groups.insert(parent_id, group); + } + let group = self.groups.get_mut(&parent_id).expect("group was inserted"); + self.incarnation_parents.insert(parent.perm_id, parent_id); + self.incarnation_parents.insert(perm_id, parent_id); + let new_member = !group.members.contains_key(&perm_id); + let member = group.members.entry(perm_id).or_insert(BrokerIncarnation { + data: None, + route: None, + terminal: false, + seen: false, + route_conflict: false, + filled_resolved: 0.0, + }); + + if let Some(data) = data { + let terminal = completed + || matches!( + data.order_state.status, + OrderStatusKind::Filled + | OrderStatusKind::Cancelled + | OrderStatusKind::ApiCancelled + ); + let route = (data.order.client_id, data.order_id); + member.route_conflict |= member.seen && member.route != Some(route); + member.route = Some(route); + member.terminal |= terminal; + member.seen = !member.terminal; + let mut snapshot = data.clone(); + if let Some(previous) = &member.data { + if previous.order_state.status == OrderStatusKind::Filled + || (member.terminal && !terminal) + { + snapshot.order_state.status = previous.order_state.status.clone(); + } + snapshot.order.filled_quantity = snapshot + .order + .filled_quantity + .max(previous.order.filled_quantity); + } + member.data = Some(snapshot); + } + + if new_member { + tracing::warn!( + "IB order {parent_id} has multiple permanent IDs: {} and {perm_id}; retaining separate broker orders", + parent.perm_id + ); + } + } + + pub(super) fn observe_order_data( + &mut self, + data: &ibapi::orders::OrderData, + account_id: AccountId, + completed: bool, + ) -> anyhow::Result> { + let correlation = self.correlate( + data.order.client_id, + data.order_id, + data.order.perm_id, + &data.order.order_ref, + )?; + + match correlation { + OrderCorrelation::Tracked { order_id, context } => { + if data.order.perm_id > 0 { + self.order_snapshots + .insert(data.order.perm_id, data.clone()); + if let Some(order) = self.order_mut(order_id) { + if order.perm_id == 0 { + order.perm_id = data.order.perm_id; + } + } + } + + if self.groups.contains_key(&context.client_order_id) { + self.record_incarnation( + order_id, + &context, + account_id, + data.order.perm_id, + Some(data), + completed, + ); + } + Ok(None) + } + OrderCorrelation::Duplicate { order_id, context } => { + self.record_incarnation( + order_id, + &context, + account_id, + data.order.perm_id, + Some(data), + completed, + ); + Ok(Some(context)) + } + OrderCorrelation::Untracked { .. } => Ok(None), + } + } + + pub(super) fn observe_incarnation_status( + &mut self, + status: &IBOrderStatus, + ) -> Option<(TrackedOrder, ibapi::orders::OrderData)> { + let parent_id = *self.incarnation_parents.get(&status.perm_id)?; + let group = self.groups.get_mut(&parent_id)?; + let member = group.members.get_mut(&status.perm_id)?; + let was_terminal = member.terminal; + let terminal = matches!( + status.status, + OrderStatusKind::Filled | OrderStatusKind::Cancelled | OrderStatusKind::ApiCancelled + ); + member.terminal |= terminal; + if let Some(data) = member.data.as_mut() { + if !was_terminal + || status.status == OrderStatusKind::Filled + || (terminal && data.order_state.status != OrderStatusKind::Filled) + { + data.order_state.status = status.status.clone(); + } + + if status.filled.is_finite() && status.filled >= 0.0 { + data.order.filled_quantity = data.order.filled_quantity.max(status.filled); + } + } + + if status.perm_id == group.parent.perm_id { + return None; + } + member.data.clone().map(|data| (group.parent.clone(), data)) + } + + pub(super) fn group_cancel_routes( + &self, + parent_id: ClientOrderId, + order_side: Option, + ) -> (Vec<(i64, i32)>, Vec) { + let Some(group) = self.groups.get(&parent_id) else { + return (Vec::new(), Vec::new()); + }; + let mut route_counts = AHashMap::new(); + + for member in group + .members + .values() + .filter(|member| !member.terminal && member.seen) + { + if let Some(route) = member.route { + *route_counts.entry(route).or_insert(0_usize) += 1; + } + } + let mut routes = Vec::new(); + let mut unresolved = Vec::new(); + + for (perm_id, member) in &group.members { + if member.terminal { + continue; + } + + // A sided request skips members whose broker side is unknown + let member_side = member + .data + .as_ref() + .map(|data| IbAction::from(data.order.action).order_side()); + if order_side.is_some_and(|side| member_side != Some(side)) { + continue; + } + + match member.route { + Some((client_id, order_id)) + if member.seen + && !member.route_conflict + && client_id == self.client_id + && order_id != 0 + && route_counts.get(&(client_id, order_id)) == Some(&1) => + { + routes.push((*perm_id, order_id)); + } + _ => unresolved.push(*perm_id), + } + } + routes.sort_unstable(); + unresolved.sort_unstable(); + (routes, unresolved) + } + + pub(super) fn archive_finished_groups(&mut self) { + let closed: Vec<_> = self + .groups + .iter() + .filter_map(|(id, group)| { + group + .members + .values() + .all(|member| member.terminal) + .then_some(*id) + }) + .collect(); + + for id in closed { + if let Some(group) = self.groups.remove(&id) { + for perm_id in group.members.keys() { + if let Ok(child) = duplicate_client_order_id(group.account_id, *perm_id) { + self.auxiliary_orders.remove(&child); + if let Some(order_id) = self.order_id_map.remove(&child) { + if self + .active_orders + .get(&order_id) + .is_some_and(|order| order.client_order_id == child) + { + let order = self + .active_orders + .remove(&order_id) + .expect("owned child was checked"); + self.terminal_orders.insert(order_id, order); + } + + if self.venue_order_id_map.get(&order_id) == Some(&child) { + self.venue_order_id_map.remove(&order_id); + } + } + } + } + self.group_history.insert(id, group); + } + } + self.incarnation_parents + .retain(|_, id| self.groups.contains_key(id) || self.group_history.contains_key(id)); + } +} + +impl InteractiveBrokersExecutionClient { + pub(super) async fn cancel_incarnation_group( + parent_id: ClientOrderId, + client: &Arc, + orders: &OrderTracker, + provider: &Arc, + exec_sender: &EventSender, + ts_init: UnixNanos, + account_id: AccountId, + ib_account: Ustr, + request_timeout_secs: u64, + order_side: Option, + parent_pending_cancel: bool, + ) -> anyhow::Result<()> { + { + let mut state = orders.lock()?; + if let Some(group) = state.groups.get_mut(&parent_id) { + for member in group.members.values_mut() { + member.seen = false; + member.route_conflict = false; + } + } else { + return Ok(()); + } + } + let mut unresolved_fills = Vec::new(); + let refresh = async { + for completed in [false, true] { + let subscription = if completed { + client.completed_orders(false).await? + } else { + client.all_open_orders().await? + }; + let mut subscription = subscription.filter_data(); + while let Some(item) = subscription.next().await { + let Orders::OrderData(data) = item? else { + continue; + }; + + if data.order.account != ib_account { + continue; + } + let parent = { + let state = orders.lock()?; + let Some(group) = state.groups.get(&parent_id) else { + continue; + }; + + if !group.members.contains_key(&data.order.perm_id) + && parse::normalized_order_ref(&data.order.order_ref) + != Some(parent_id.as_str()) + { + continue; + } + group.parent.clone() + }; + let resolved = provider.resolve_instrument_id_for_contract(&data.contract)?; + anyhow::ensure!( + resolved == parent.instrument_id, + "IB shared order reference {parent_id} has conflicting instruments" + ); + let primary_id = orders + .lock()? + .groups + .get(&parent_id) + .expect("group exists") + .primary_order_id; + orders.lock()?.record_incarnation( + primary_id, + &parent, + account_id, + data.order.perm_id, + Some(&data), + completed, + ); + + if data.order.perm_id != parent.perm_id { + let perm_id = data.order.perm_id; + let mut report = parse_order_data_to_report( + &data, + parent.instrument_id, + account_id, + provider, + ts_init, + )?; + report.client_order_id = + Some(duplicate_client_order_id(account_id, perm_id)?); + + // Resolved after this open-orders stream ends + if orders + .lock()? + .duplicate_fills_unresolved(perm_id, report.filled_qty.as_f64()) + { + unresolved_fills.push(perm_id); + } else { + exec_sender.send(ExecutionEvent::Report(ExecutionReport::Order( + Box::new(report), + )))?; + } + } + } + } + Ok::<_, anyhow::Error>(()) + }; + tokio::time::timeout(Duration::from_secs(request_timeout_secs), refresh) + .await + .context("timed out refreshing IB order incarnations")??; + + let (routes, unresolved, parent) = { + let state = orders.lock()?; + let (routes, unresolved) = state.group_cancel_routes(parent_id, order_side); + let parent = state + .groups + .get(&parent_id) + .expect("group exists") + .parent + .clone(); + (routes, unresolved, parent) + }; + + for perm_id in unresolved_fills { + let target = IbOrderSelector::PermId(perm_id); + + if orders.lock()?.hold_fill(target, None) { + Self::resolve_held_fills( + client, + target, + parent.instrument_id, + Some(duplicate_client_order_id(account_id, perm_id)?), + Duration::from_secs(request_timeout_secs), + orders, + provider, + exec_sender, + account_id, + ib_account, + ) + .await?; + } + } + + if !unresolved.is_empty() { + tracing::warn!( + "IB cancellation for {parent_id} has unresolved or unbound permanent IDs {unresolved:?}; no group completion is inferred" + ); + } + + for (perm_id, order_id) in routes { + let client_order_id = if perm_id == parent.perm_id { + parent_id + } else { + duplicate_client_order_id(account_id, perm_id)? + }; + let send = tokio::time::timeout( + Duration::from_secs(request_timeout_secs), + client.cancel_order(order_id, ""), + ) + .await; + + match send { + // A strategy cancel already published the parent's PENDING_CANCEL + Ok(Ok(_)) if client_order_id == parent_id && parent_pending_cancel => {} + Ok(Ok(_)) => { + let event = OrderPendingCancel::new( + parent.trader_id, + parent.strategy_id, + parent.instrument_id, + client_order_id, + Some(account_id), + UUID4::new(), + ts_init, + ts_init, + false, + Some(parse::ib_venue_order_id(order_id, perm_id)), + ); + + if let Err(e) = + exec_sender.send(ExecutionEvent::Order(OrderEventAny::PendingCancel(event))) + { + tracing::error!( + "IB cancel was sent but pending state could not be delivered: {e}" + ); + } + } + Ok(Err(e)) => tracing::warn!( + "IB cancel outcome for permanent ID {perm_id} remains unresolved: {e}" + ), + Err(e) => { + tracing::warn!("IB cancel outcome for permanent ID {perm_id} timed out: {e}"); + } + } + } + orders.lock()?.archive_finished_groups(); + Ok(()) + } +} diff --git a/crates/adapters/interactive_brokers/src/execution/mod.rs b/crates/adapters/interactive_brokers/src/execution/mod.rs index fbd1e84a1a30..5a9e54b2250b 100644 --- a/crates/adapters/interactive_brokers/src/execution/mod.rs +++ b/crates/adapters/interactive_brokers/src/execution/mod.rs @@ -21,4 +21,11 @@ pub mod core; pub mod parse; pub mod transform; +mod commands; +mod incarnations; +mod order_state; +mod reports; +mod spreads; +mod updates; + pub use core::InteractiveBrokersExecutionClient; diff --git a/crates/adapters/interactive_brokers/src/execution/order_state.rs b/crates/adapters/interactive_brokers/src/execution/order_state.rs new file mode 100644 index 000000000000..b48419b7fdee --- /dev/null +++ b/crates/adapters/interactive_brokers/src/execution/order_state.rs @@ -0,0 +1,678 @@ +// ------------------------------------------------------------------------------------------------- +// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. +// https://nautechsystems.io +// +// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html +// ------------------------------------------------------------------------------------------------- + +//! Interactive Brokers order tracking and cached instrument state. + +use nautilus_common::live::sender::EventSender; + +use super::{core::*, incarnations::duplicate_client_order_id}; +use crate::common::spreads::parse_spread_instrument_id_to_legs; + +impl OrderTrackerState { + // Holds `fill` for `target` and returns whether the caller starts the resolution + pub(super) fn hold_fill(&mut self, target: IbOrderSelector, fill: Option) -> bool { + let starts = !self.fills_held.contains_key(&target); + self.fills_held.entry(target).or_default().extend(fill); + starts + } + + pub(super) fn correlate( + &self, + client_id: i32, + order_id: i32, + perm_id: i64, + order_ref: &str, + ) -> anyhow::Result { + let reference = parse::normalized_order_ref(order_ref).map(ClientOrderId::from); + + if let Some((primary_order_id, context, account_id)) = self.group_context(perm_id) { + let child_id = duplicate_client_order_id(account_id, perm_id)?; + anyhow::ensure!( + reference.is_none_or(|id| id == context.client_order_id || id == child_id), + "IB permanent ID {perm_id} has a conflicting order reference {order_ref}" + ); + return if perm_id == context.perm_id { + Ok(OrderCorrelation::Tracked { + order_id: primary_order_id, + context, + }) + } else { + Ok(OrderCorrelation::Duplicate { + order_id: primary_order_id, + context, + }) + }; + } + + let raw = (client_id == self.client_id) + .then(|| self.order(order_id).map(|order| (order_id, order.clone()))) + .flatten(); + let by_perm = if perm_id > 0 { + self.active_orders + .iter() + .chain(self.terminal_orders.iter()) + .find(|(_, order)| order.perm_id == perm_id) + .map(|(id, order)| (*id, order.clone())) + } else { + None + }; + let by_ref = reference.and_then(|reference| { + self.order_id_map + .get(&reference) + .and_then(|id| self.order(*id).map(|order| (*id, order.clone()))) + .or_else(|| { + self.active_orders + .iter() + .chain(self.terminal_orders.iter()) + .find(|(_, order)| order.client_order_id == reference) + .map(|(id, order)| (*id, order.clone())) + }) + }); + let identities: Vec<_> = [raw.as_ref(), by_perm.as_ref(), by_ref.as_ref()] + .into_iter() + .flatten() + .collect(); + + if let Some((_, first)) = identities.first() { + anyhow::ensure!( + identities + .iter() + .all(|(_, order)| order.client_order_id == first.client_order_id), + "Conflicting IB order identities for client {client_id}, order {order_id}, permanent ID {perm_id}" + ); + anyhow::ensure!( + reference.is_none_or(|id| id == first.client_order_id), + "IB order reference {order_ref} conflicts with tracked order {}", + first.client_order_id + ); + } + + if client_id != self.client_id && by_ref.is_some() && by_perm.is_none() { + return Ok(OrderCorrelation::Untracked { + client_order_id: None, + }); + } + let Some((tracking_id, mut context)) = by_ref.or(by_perm).or(raw) else { + return Ok(OrderCorrelation::Untracked { + client_order_id: reference, + }); + }; + + if context.perm_id > 0 && perm_id > 0 && context.perm_id != perm_id { + return Ok(OrderCorrelation::Duplicate { + order_id: tracking_id, + context, + }); + } + + if context.perm_id == 0 && perm_id > 0 { + context.perm_id = perm_id; + } + Ok(OrderCorrelation::Tracked { + order_id: tracking_id, + context, + }) + } +} + +impl InteractiveBrokersExecutionClient { + pub(super) fn cached_instrument_ids_for_preload( + cache: &Cache, + instrument_provider: &InteractiveBrokersInstrumentProvider, + client_id: ClientId, + account_id: AccountId, + ) -> Vec { + let mut instrument_ids = ahash::AHashSet::new(); + + for client_order_id in cache.iter_client_order_ids(None, None, None, None) { + if cache + .client_id(&client_order_id) + .is_some_and(|id| *id != client_id) + { + continue; + } + + if let Some(order) = cache.order(&client_order_id) { + if order.account_id().is_some_and(|id| id != account_id) { + continue; + } + let instrument_id = order.instrument_id(); + if instrument_provider.find(&instrument_id).is_none() { + instrument_ids.insert(instrument_id); + } + } + } + let mut instrument_ids: Vec<_> = instrument_ids.into_iter().collect(); + instrument_ids.sort_unstable(); + instrument_ids + } + + pub(super) async fn preload_cached_instruments(&self, client: &Client) { + let instrument_ids = { + let cache = self.core.cache(); + self.instrument_provider.seed_from_cache(&cache); + Self::cached_instrument_ids_for_preload( + &cache, + &self.instrument_provider, + self.core.client_id, + self.core.account_id, + ) + }; + + for instrument_id in instrument_ids { + match self + .instrument_provider + .load_with_return_async(client, instrument_id, None) + .await + { + Ok(Some(loaded)) if loaded == instrument_id => { + tracing::debug!("Preloaded cached IB instrument {instrument_id}"); + } + Ok(_) => tracing::warn!( + "Cached IB instrument {instrument_id} is unavailable, its cached orders cannot be reconciled" + ), + Err(e) => tracing::warn!( + "Failed to preload cached IB instrument {instrument_id}, its cached orders cannot be reconciled: {e:#}" + ), + } + } + } + + // A combo order's report resolves only once its spread is cached, and startup + // reconciliation needs the spread and its legs in the cache, which the data client fills + // from its own load set alone. Published here at connect, they reach the cache before + // reconciliation runs. + pub(super) async fn publish_combo_order_instruments(&self, client: &Client) { + let mut contracts = Vec::new(); + let timeout_dur = Duration::from_secs(self.config.request_timeout); + let queries = tokio::time::timeout(timeout_dur, async { + let mut open = client.all_open_orders().await?; + while let Some(item) = open.next().await { + if let SubscriptionItem::Data(Orders::OrderData(data)) = item? { + contracts.push(data.contract); + } + } + let mut completed = client.completed_orders(false).await?; + while let Some(item) = completed.next().await { + if let SubscriptionItem::Data(Orders::OrderData(data)) = item? { + contracts.push(data.contract); + } + } + Ok::<_, anyhow::Error>(()) + }) + .await; + + match queries { + Ok(Ok(())) => {} + Ok(Err(e)) => tracing::warn!("Failed to read IB orders for combo instruments: {e:#}"), + Err(_) => tracing::warn!("Timed out reading IB orders for combo instruments"), + } + + let mut loaded = AHashSet::new(); + + for contract in contracts + .iter() + .filter(|contract| contract.security_type == SecurityType::Spread) + { + if !loaded.insert(contract.contract_id) { + continue; + } + + if let Err(e) = self.publish_combo_instruments(client, contract).await { + tracing::warn!( + "Failed to load IB combo contract ID {}, its orders cannot be reconciled: {e:#}", + contract.contract_id + ); + } + } + } + + async fn publish_combo_instruments( + &self, + client: &Client, + contract: &Contract, + ) -> anyhow::Result<()> { + // IB serves contract details for the legs but not for a BAG itself + for combo_leg in &contract.combo_legs { + if self + .instrument_provider + .get_instrument_id_by_contract_id(combo_leg.contract_id) + .is_none() + { + let leg_contract = Contract { + contract_id: combo_leg.contract_id, + exchange: Exchange::from(combo_leg.exchange.as_str()), + ..Default::default() + }; + self.instrument_provider + .get_instrument(client, &leg_contract) + .await? + .with_context(|| { + format!( + "IB returned no instrument for BAG leg {}", + combo_leg.contract_id + ) + })?; + } + } + + let spread_id = self + .instrument_provider + .spread_instrument_id_for_contract(contract)?; + self.instrument_provider + .fetch_spread_instrument(client, spread_id, false, None) + .await?; + let spread = self + .instrument_provider + .find(&spread_id) + .with_context(|| format!("Spread instrument {spread_id} was not loaded"))?; + let legs = parse_spread_instrument_id_to_legs(&spread_id)?; + let data_sender = get_data_event_sender(); + + for instrument in legs + .iter() + .filter_map(|(leg_id, _)| self.instrument_provider.find(leg_id)) + .chain(std::iter::once(spread)) + { + if self.core.cache().instrument(&instrument.id()).is_none() + && let Err(e) = data_sender.send(DataEvent::Instrument(instrument)) + { + tracing::warn!("Failed to publish IB spread instrument: {e}"); + } + } + + Ok(()) + } + + // Startup reconciliation recovers a venue position only when its instrument is in the + // cache, which the data client fills from its own load set alone. + pub(super) async fn publish_position_instruments( + &self, + client: &Client, + contracts: Vec, + ) { + let data_sender = get_data_event_sender(); + + for contract in contracts { + let instrument = match self + .instrument_provider + .get_instrument(client, &contract) + .await + { + Ok(Some(instrument)) => instrument, + Ok(None) => { + tracing::warn!( + "Cannot resolve instrument for IB position contract ID {}, its position cannot be reconciled", + contract.contract_id + ); + continue; + } + Err(e) => { + tracing::warn!( + "Failed to resolve instrument for IB position contract ID {}, its position cannot be reconciled: {e:#}", + contract.contract_id + ); + continue; + } + }; + + if self.core.cache().instrument(&instrument.id()).is_some() { + continue; + } + + if let Err(e) = data_sender.send(DataEvent::Instrument(instrument)) { + tracing::warn!("Failed to publish IB position instrument: {e}"); + } + } + } + + pub(super) fn get_mapped_instrument_id( + order_id: i32, + orders: &OrderTracker, + ) -> anyhow::Result> { + Ok(orders + .lock()? + .active_orders + .get(&order_id) + .map(|order| order.instrument_id)) + } + + pub(super) fn get_required_order_actor_ids( + order_id: i32, + orders: &OrderTracker, + ) -> anyhow::Result<(TraderId, StrategyId)> { + let state = orders.lock()?; + let order = state.active_orders.get(&order_id).with_context(|| { + format!("Tracked state not found for Interactive Brokers order {order_id}") + })?; + + Ok((order.trader_id, order.strategy_id)) + } + + pub(super) fn resolve_contract_for_instrument( + instrument_id: InstrumentId, + instrument_provider: &Arc, + ) -> anyhow::Result { + instrument_provider + .resolve_contract_for_instrument(instrument_id) + .context("Failed to convert instrument ID to IB contract") + } + + pub(super) fn contract_with_order_exchange_param( + mut contract: ibapi::contracts::Contract, + params: Option<&nautilus_core::Params>, + ) -> anyhow::Result { + let Some(params) = params else { + return Ok(contract); + }; + + let Some(exchange_value) = params.get("exchange") else { + return Ok(contract); + }; + + let Some(exchange) = exchange_value.as_str() else { + anyhow::bail!("`exchange` order param must be a string"); + }; + + if exchange.is_empty() { + return Ok(contract); + } + + contract.exchange = ibapi::contracts::Exchange::from(exchange); + Ok(contract) + } + + #[allow(clippy::too_many_arguments)] // The fields form one tracked order record. + pub(super) fn cache_order_tracking( + ib_order_id: i32, + client_order_id: ClientOrderId, + instrument_id: InstrumentId, + trader_id: TraderId, + strategy_id: StrategyId, + order_side: OrderSide, + order_type: OrderType, + orders: &OrderTracker, + ) -> anyhow::Result<()> { + let mut state = orders.lock()?; + state.order_id_map.insert(client_order_id, ib_order_id); + state + .venue_order_id_map + .insert(ib_order_id, client_order_id); + state.terminal_orders.remove(&ib_order_id); + state.active_orders.insert( + ib_order_id, + TrackedOrder { + client_order_id, + trader_id, + strategy_id, + instrument_id, + order_side, + order_type, + accepted: false, + avg_px: None, + pending_cancel: false, + pending_modify: None, + perm_id: 0, + spread_fill_ids: ahash::AHashSet::new(), + spread_fills_held: HeldSpreadFills::default(), + last_update: None, + }, + ); + + Ok(()) + } + + pub(super) fn cache_recovered_order_tracking( + ib_order_id: i32, + target_order: &OrderAny, + orders: &OrderTracker, + ) -> anyhow::Result<()> { + let client_order_id = target_order.client_order_id(); + // Publish the raw IB route only after the recovered identity is complete. + let mut state = orders.lock()?; + let perm_id = target_order + .venue_order_id() + .and_then(|id| IbOrderSelector::from_venue_order_id(&id).ok()) + .and_then(|selector| match selector { + IbOrderSelector::PermId(id) => Some(id), + _ => None, + }) + .unwrap_or(0); + + if let Some(existing) = state.venue_order_id_map.get(&ib_order_id).copied() { + if existing != client_order_id { + let parent = state.group_for_client(client_order_id); + anyhow::ensure!( + parent.is_some() && parent == state.group_for_client(existing), + "IB order ID {ib_order_id} is already mapped to client order {existing}" + ); + state + .auxiliary_orders + .entry(client_order_id) + .or_insert_with(|| TrackedOrder { + client_order_id: target_order.client_order_id(), + trader_id: target_order.trader_id(), + strategy_id: target_order.strategy_id(), + instrument_id: target_order.instrument_id(), + order_side: target_order.order_side(), + order_type: target_order.order_type(), + accepted: true, + avg_px: None, + pending_cancel: false, + pending_modify: None, + perm_id, + spread_fill_ids: ahash::AHashSet::new(), + spread_fills_held: HeldSpreadFills::default(), + last_update: None, + }); + return Ok(()); + } + } + state.order_id_map.insert(client_order_id, ib_order_id); + state.terminal_orders.remove(&ib_order_id); + state + .active_orders + .entry(ib_order_id) + .or_insert_with(|| TrackedOrder { + client_order_id: target_order.client_order_id(), + trader_id: target_order.trader_id(), + strategy_id: target_order.strategy_id(), + instrument_id: target_order.instrument_id(), + order_side: target_order.order_side(), + order_type: target_order.order_type(), + accepted: true, + avg_px: None, + pending_cancel: false, + pending_modify: None, + perm_id, + spread_fill_ids: ahash::AHashSet::new(), + spread_fills_held: HeldSpreadFills::default(), + last_update: Some(( + target_order.quantity(), + target_order.price(), + target_order.trigger_price(), + )), + }); + state + .venue_order_id_map + .insert(ib_order_id, client_order_id); + + Ok(()) + } + + pub(super) fn get_tracked_order_context( + ib_order_id: i32, + orders: &OrderTracker, + ) -> anyhow::Result> { + let state = orders.lock()?; + if let Some(context) = state.active_orders.get(&ib_order_id).cloned() { + return Ok(Some(context)); + } + + Ok(state.terminal_orders.get(&ib_order_id).cloned()) + } + + pub(super) fn emit_order_accepted_if_needed( + ib_order_id: i32, + venue_order_id: VenueOrderId, + account_id: AccountId, + ts_event: UnixNanos, + orders: &OrderTracker, + exec_sender: &EventSender, + ) -> anyhow::Result { + let mut state = orders.lock()?; + let Some(context) = state.active_orders.get_mut(&ib_order_id) else { + return Ok(false); + }; + + Self::emit_order_accepted(context, venue_order_id, account_id, ts_event, exec_sender) + } + + pub(super) fn emit_order_accepted_for_fill_if_needed( + ib_order_id: i32, + venue_order_id: VenueOrderId, + account_id: AccountId, + ts_event: UnixNanos, + orders: &OrderTracker, + exec_sender: &EventSender, + ) -> anyhow::Result { + let mut state = orders.lock()?; + let context = if let Some(context) = state.active_orders.get_mut(&ib_order_id) { + context + } else if let Some(context) = state.terminal_orders.get_mut(&ib_order_id) { + context + } else { + return Ok(false); + }; + + Self::emit_order_accepted(context, venue_order_id, account_id, ts_event, exec_sender) + } + + fn emit_order_accepted( + context: &mut TrackedOrder, + venue_order_id: VenueOrderId, + account_id: AccountId, + ts_event: UnixNanos, + exec_sender: &EventSender, + ) -> anyhow::Result { + if context.accepted { + return Ok(false); + } + + let event = OrderAccepted::new( + context.trader_id, + context.strategy_id, + context.instrument_id, + context.client_order_id, + venue_order_id, + account_id, + UUID4::new(), + ts_event, + ts_event, + false, + ); + exec_sender + .send(ExecutionEvent::Order(OrderEventAny::Accepted(event))) + .map_err(|e| anyhow::anyhow!("Failed to send order accepted event: {e}"))?; + context.accepted = true; + + Ok(true) + } + + pub(super) fn remove_order_tracking( + ib_order_id: i32, + client_order_id: ClientOrderId, + orders: &OrderTracker, + ) -> anyhow::Result<()> { + let mut state = orders.lock()?; + state.order_id_map.remove(&client_order_id); + state.venue_order_id_map.remove(&ib_order_id); + state.active_orders.remove(&ib_order_id); + state.terminal_orders.remove(&ib_order_id); + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use ibapi::contracts::{Contract, Exchange}; + use nautilus_core::Params; + use rstest::rstest; + use serde_json::Value; + + use super::*; + + fn contract_with_exchange(exchange: &str) -> Contract { + Contract { + exchange: Exchange::from(exchange), + ..Default::default() + } + } + + #[rstest] + fn test_contract_with_order_exchange_param_overrides_exchange() { + let contract = contract_with_exchange("SMART"); + let mut params = Params::new(); + params.insert("exchange".to_string(), Value::String("IEX".to_string())); + + let updated = InteractiveBrokersExecutionClient::contract_with_order_exchange_param( + contract.clone(), + Some(¶ms), + ) + .unwrap(); + + assert_eq!(updated.exchange.as_str(), "IEX"); + assert_eq!(contract.exchange.as_str(), "SMART"); + } + + #[rstest] + fn test_contract_with_order_exchange_param_keeps_contract_without_exchange() { + let contract = contract_with_exchange("SMART"); + let params = Params::new(); + + let updated = InteractiveBrokersExecutionClient::contract_with_order_exchange_param( + contract, + Some(¶ms), + ) + .unwrap(); + + assert_eq!(updated.exchange.as_str(), "SMART"); + } + + #[rstest] + fn test_contract_with_order_exchange_param_keeps_contract_with_empty_exchange() { + let contract = contract_with_exchange("SMART"); + let mut params = Params::new(); + params.insert("exchange".to_string(), Value::String(String::new())); + + let updated = InteractiveBrokersExecutionClient::contract_with_order_exchange_param( + contract, + Some(¶ms), + ) + .unwrap(); + + assert_eq!(updated.exchange.as_str(), "SMART"); + } + + #[rstest] + fn test_contract_with_order_exchange_param_rejects_non_string_exchange() { + let contract = contract_with_exchange("SMART"); + let mut params = Params::new(); + params.insert("exchange".to_string(), Value::Bool(true)); + + let err = InteractiveBrokersExecutionClient::contract_with_order_exchange_param( + contract, + Some(¶ms), + ) + .unwrap_err(); + + assert!(err.to_string().contains("must be a string")); + } +} diff --git a/crates/adapters/interactive_brokers/src/execution/parse.rs b/crates/adapters/interactive_brokers/src/execution/parse.rs index 2804e06ce880..f42880e8c2f2 100644 --- a/crates/adapters/interactive_brokers/src/execution/parse.rs +++ b/crates/adapters/interactive_brokers/src/execution/parse.rs @@ -17,8 +17,9 @@ use std::str::FromStr; +use ahash::AHashMap; use anyhow::Context; -use ibapi::orders::{Execution, OrderStatus}; +use ibapi::orders::{Execution, Liquidity, OrderData, OrderStatus}; use jiff::{ Timestamp, civil::DateTime, @@ -27,12 +28,12 @@ use jiff::{ use nautilus_core::{UnixNanos, datetime::get_timezone}; use nautilus_model::{ enums::{ - LiquiditySide, OrderSide, OrderStatus as NautilusOrderStatus, OrderType, TimeInForce, - TrailingOffsetType, + ContingencyType, LiquiditySide, OrderSide, OrderStatus as NautilusOrderStatus, OrderType, + TimeInForce, TrailingOffsetType, }, identifiers::{AccountId, ClientOrderId, InstrumentId, TradeId, VenueOrderId}, instruments::Instrument, - reports::{FillReport, OrderStatusReport}, + reports::{FillReport, OrderStatusReport, PositionStatusReport}, types::{Currency, Money, Price, Quantity}, }; use rust_decimal::Decimal; @@ -40,11 +41,244 @@ use rust_decimal::Decimal; use crate::{ common::{ enums::{IbAction, IbOrderStatus, IbOrderType, IbTimeInForce}, - parse::is_spread_instrument_id, + spreads::is_spread_instrument_id, }, + execution::transform::ib_trigger_method_to_trigger_type, providers::instruments::InteractiveBrokersInstrumentProvider, }; +pub(super) fn parse_order_data_to_report( + data: &OrderData, + instrument_id: InstrumentId, + account_id: AccountId, + instrument_provider: &InteractiveBrokersInstrumentProvider, + ts_init: UnixNanos, +) -> anyhow::Result { + anyhow::ensure!( + data.order.total_quantity.is_finite() + && data.order.total_quantity >= 0.0 + && data.order.filled_quantity.is_finite() + && data.order.filled_quantity >= 0.0, + "IB order {} has invalid quantities", + data.order_id + ); + let mut report = parse_order_status_to_report( + &OrderStatus { + order_id: data.order_id, + status: data.order_state.status.clone(), + filled: data.order.filled_quantity, + remaining: (data.order.total_quantity - data.order.filled_quantity).max(0.0), + average_fill_price: None, + perm_id: data.order.perm_id, + parent_id: 0, + last_fill_price: None, + client_id: data.order.client_id, + why_held: String::new(), + market_cap_price: None, + }, + Some(&data.order), + instrument_id, + account_id, + instrument_provider, + ts_init, + )?; + + // Open orders carry no venue time; a completed order reports when it finished + if !data.order_state.completed_time.is_empty() { + match parse_execution_time(&data.order_state.completed_time) { + Ok(completed) => report.ts_last = completed, + Err(e) => tracing::debug!( + "Cannot parse completed time of IB order {}: {e}", + data.order_id + ), + } + } + + Ok(report) +} + +/// Links the contingencies IB reports across one batch of order status reports. +/// +/// IB keeps bracket parents (`parent_id`) and OCA groups on its side; a status report parsed on +/// its own cannot name the related orders. Each report is matched to its IB order by index. +/// Reports whose OCA type IB does not model keep no contingency, and a contingency is only set +/// when at least one related order is present in the batch. +/// Sets the average price of filled order reports that lack one from their fill reports. +/// +/// IB completed orders carry no average fill price, which reconciliation needs to rebuild a +/// filled order before applying its fills. +pub(crate) fn fill_missing_avg_px( + order_reports: &mut [OrderStatusReport], + fill_reports: &[FillReport], +) { + for report in order_reports + .iter_mut() + .filter(|report| report.avg_px.is_none() && !report.filled_qty.is_zero()) + { + let (notional, quantity) = fill_reports + .iter() + .filter(|fill| fill.venue_order_id == report.venue_order_id) + .fold( + (Decimal::ZERO, Decimal::ZERO), + |(notional, quantity), fill| { + ( + notional + fill.last_px.as_decimal() * fill.last_qty.as_decimal(), + quantity + fill.last_qty.as_decimal(), + ) + }, + ); + + if !quantity.is_zero() { + report.avg_px = Some(notional / quantity); + } + } +} + +/// Replaces IB's position average cost with the entry price of the fills that built the position. +/// +/// IB's `avgCost` includes commissions, while a position rebuilt from fills has the average of +/// their prices. When the fills of an instrument net to exactly the reported quantity, the +/// position was flat before them, so they hold its whole history and give its entry price. +/// Other positions keep IB's average cost, which position recovery prices from. +pub(crate) fn fill_position_avg_px_from_fills( + position_reports: &mut [PositionStatusReport], + fill_reports: &[FillReport], +) { + for report in position_reports + .iter_mut() + .filter(|report| !report.signed_decimal_qty.is_zero()) + { + let mut fills: Vec<_> = fill_reports + .iter() + .filter(|fill| { + fill.account_id == report.account_id && fill.instrument_id == report.instrument_id + }) + .collect(); + fills.sort_by_key(|fill| fill.ts_event); + + if let Some((quantity, avg_px)) = netting_entry(&fills) + && quantity == report.signed_decimal_qty + { + report.avg_px_open = Some(avg_px); + } + } +} + +// Replays fills with netting semantics: fills on the open side average in, reducing fills +// keep the entry price, and a flip opens at the flipping fill's price +fn netting_entry(fills: &[&FillReport]) -> Option<(Decimal, Decimal)> { + let mut quantity = Decimal::ZERO; + let mut avg_px = Decimal::ZERO; + + for fill in fills { + let last_qty = fill.last_qty.as_decimal(); + let last_px = fill.last_px.as_decimal(); + let signed_qty = match fill.order_side { + OrderSide::Buy => last_qty, + OrderSide::Sell => -last_qty, + }; + + if quantity.is_zero() || quantity.is_sign_negative() == signed_qty.is_sign_negative() { + let open_qty = quantity.abs(); + avg_px = (avg_px * open_qty + last_px * last_qty) / (open_qty + last_qty); + } else if last_qty > quantity.abs() { + avg_px = last_px; + } + quantity += signed_qty; + } + + (!quantity.is_zero()).then_some((quantity, avg_px)) +} + +/// Returns whether an execution is the combo-level (BAG) execution of a spread order. +/// +/// IB sends commission reports only for the leg executions of a combo; the combo-level +/// execution carries none, so its fill has zero commission. +pub(crate) fn is_combo_execution(exec_data: &ibapi::orders::ExecutionData) -> bool { + matches!( + exec_data.contract.security_type, + ibapi::contracts::SecurityType::Spread + ) +} + +pub(crate) fn link_order_contingencies( + reports: &mut [OrderStatusReport], + orders: &[ibapi::orders::Order], +) { + debug_assert_eq!(reports.len(), orders.len()); + let order_ids: Vec = reports + .iter() + .map(|report| { + report + .client_order_id + .unwrap_or_else(|| ClientOrderId::from(report.venue_order_id.as_str())) + }) + .collect(); + + let mut oca_groups: AHashMap<(&str, &str), Vec> = AHashMap::new(); + + for (index, order) in orders.iter().enumerate() { + if !order.oca_group.is_empty() { + oca_groups + .entry((order.account.as_str(), order.oca_group.as_str())) + .or_default() + .push(index); + } + } + + let mut children: AHashMap> = AHashMap::new(); + + for (index, order) in orders.iter().enumerate() { + let parent = orders.iter().position(|candidate| { + order + .parent_perm_id + .is_some_and(|perm_id| perm_id != 0 && candidate.perm_id == perm_id) + || (order.parent_id != 0 + && candidate.order_id == order.parent_id + && candidate.client_id == order.client_id) + }); + + if let Some(parent) = parent.filter(|parent| *parent != index) { + reports[index].parent_order_id = Some(order_ids[parent]); + children.entry(parent).or_default().push(index); + } + } + + for (index, order) in orders.iter().enumerate() { + if let Some(members) = oca_groups.get(&(order.account.as_str(), order.oca_group.as_str())) { + let contingency_type = match order.oca_type { + ibapi::orders::OcaType::CancelWithBlock => Some(ContingencyType::Oco), + ibapi::orders::OcaType::ReduceWithBlock + | ibapi::orders::OcaType::ReduceWithoutBlock => Some(ContingencyType::Ouo), + _ => None, + }; + let linked: Vec = members + .iter() + .filter(|member| **member != index) + .map(|member| order_ids[*member]) + .collect(); + + if let Some(contingency_type) = contingency_type + && !linked.is_empty() + { + reports[index].contingency_type = Some(contingency_type); + reports[index].linked_order_ids = Some(linked); + continue; + } + } + + if let Some(child_indices) = children.get(&index) { + reports[index].contingency_type = Some(ContingencyType::Oto); + reports[index].linked_order_ids = Some( + child_indices + .iter() + .map(|child| order_ids[*child]) + .collect(), + ); + } + } +} + pub(crate) fn should_use_avg_fill_price(avg_fill_price: f64, instrument_id: &InstrumentId) -> bool { avg_fill_price.is_finite() && avg_fill_price != f64::MAX @@ -125,6 +359,7 @@ pub fn parse_execution_to_fill_report( let venue_order_id = ib_venue_order_id(execution.order_id, execution.perm_id); let client_order_id = normalized_order_ref(&execution.order_reference).map(ClientOrderId::new); + let liquidity_side = execution_liquidity_side(&execution.last_liquidity); let mut report = FillReport::new( account_id, @@ -135,7 +370,7 @@ pub fn parse_execution_to_fill_report( last_qty, last_px, commission_money, - LiquiditySide::NoLiquiditySide, + liquidity_side, client_order_id, None, // venue_position_id ts_event, @@ -147,6 +382,24 @@ pub fn parse_execution_to_fill_report( Ok(report) } +fn execution_liquidity_side(liquidity: &Liquidity) -> LiquiditySide { + match liquidity { + Liquidity::AddedLiquidity => LiquiditySide::Maker, + Liquidity::RemovedLiquidity => LiquiditySide::Taker, + Liquidity::None => LiquiditySide::NoLiquiditySide, + Liquidity::LiquidityRoutedOut => { + tracing::warn!( + "IB execution liquidity was routed out and has no maker/taker representation" + ); + LiquiditySide::NoLiquiditySide + } + Liquidity::Unknown(code) => { + tracing::warn!("IB execution used unknown liquidity code {code}"); + LiquiditySide::NoLiquiditySide + } + } +} + /// Parse an IB order status to a Nautilus OrderStatusReport. /// /// # Errors @@ -193,11 +446,11 @@ pub fn parse_order_status_to_report( .map_or(0, |instr| instr.price_precision()); // Get quantity - let quantity = if let Some(order) = order { - Quantity::new(order.total_quantity, size_precision) - } else { - Quantity::zero(size_precision) - }; + let total_quantity = order.map_or(0.0, |order| order.total_quantity); + let quantity = Quantity::new( + total_quantity.max(order_status.filled + order_status.remaining), + size_precision, + ); // Get filled quantity let filled_qty = Quantity::new(order_status.filled, size_precision); @@ -225,15 +478,33 @@ pub fn parse_order_status_to_report( .map(ClientOrderId::new); // Map order type from IB order if available - let order_type = order - .map(|order| map_ib_order_type(&order.order_type, order.limit_price)) - .unwrap_or(OrderType::Market); + let order_type = order.map_or(OrderType::Market, |order| { + map_ib_order_type(&order.order_type, order.limit_price) + }); + + // A GTD order needs its expiry to be rebuilt from the report; IB still enforces the date, + // so an unparsable one degrades to GTC rather than failing the whole report set + let expire_time = order + .filter(|order| !order.good_till_date.is_empty()) + .and_then(|order| match parse_execution_time(&order.good_till_date) { + Ok(expire_time) => Some(expire_time), + Err(e) => { + tracing::warn!( + "Reporting IB order {} as GTC: cannot parse goodTillDate: {e}", + order_status.order_id + ); + None + } + }); // Map time in force from IB order if available let time_in_force = if let Some(order) = order { let ib_time_in_force = IbTimeInForce::from(order.tif.clone()); - if ib_time_in_force == IbTimeInForce::GoodTilDate || !order.good_till_date.is_empty() { + + if expire_time.is_some() { TimeInForce::Gtd + } else if ib_time_in_force == IbTimeInForce::GoodTilDate { + TimeInForce::Gtc } else { ib_time_in_force.nautilus_time_in_force() } @@ -259,9 +530,24 @@ pub fn parse_order_status_to_report( Some(nautilus_core::UUID4::new()), // report_id ); + if let Some(expire_time) = expire_time { + report = report.with_expire_time(expire_time); + } + // Set optional fields if let Some(order) = order { - if let Some(limit_price) = order.limit_price { + // IB reports a zero limit price on market-type orders, which reconciliation would + // otherwise take as the fill price of an order it has no fills for + if let Some(limit_price) = order.limit_price + && matches!( + order_type, + OrderType::Limit + | OrderType::StopLimit + | OrderType::LimitIfTouched + | OrderType::TrailingStopLimit + | OrderType::MarketToLimit + ) + { let converted = limit_price * price_magnifier; report = report.with_price(Price::new(converted, price_precision)); } @@ -273,6 +559,19 @@ pub fn parse_order_status_to_report( report = report.with_trigger_price(trigger_price); } + if matches!( + order_type, + OrderType::LimitIfTouched + | OrderType::MarketIfTouched + | OrderType::StopLimit + | OrderType::StopMarket + | OrderType::TrailingStopLimit + | OrderType::TrailingStopMarket + ) { + report = + report.with_trigger_type(ib_trigger_method_to_trigger_type(order.trigger_method)); + } + if let Some(limit_offset) = limit_offset { report = report.with_limit_offset(limit_offset); } @@ -290,6 +589,13 @@ pub fn parse_order_status_to_report( report = report.with_avg_px(decimal_from_f64(avg_px_value)?); } + if let Some(display_size) = order + .and_then(|order| order.display_size) + .filter(|size| *size > 0 && f64::from(*size) < quantity.as_f64()) + { + report = report.with_display_qty(Quantity::new(f64::from(display_size), size_precision)); + } + Ok(report) } @@ -302,6 +608,7 @@ fn map_ib_order_type(order_type: &str, limit_price: Option) -> OrderType { } } +#[allow(clippy::type_complexity)] // The tuple mirrors the four optional IB pricing fields. fn parse_ib_order_pricing_fields( order: &ibapi::orders::Order, order_type: OrderType, @@ -468,7 +775,7 @@ mod tests { orders::{Action, ExecutionSide, Liquidity, Order, OrderStatusKind}, }; use nautilus_model::{ - enums::TrailingOffsetType, + enums::{PositionSide, TrailingOffsetType, TriggerType}, identifiers::{Symbol, Venue}, instruments::{InstrumentAny, stubs::equity_aapl}, }; @@ -491,6 +798,170 @@ mod tests { use rstest::rstest; + #[rstest] + #[case::fills_open_position(PositionSide::Long, vec![(OrderSide::Buy, 7, "229.67")], "229.67")] + #[case::fills_average_in( + PositionSide::Long, + vec![(OrderSide::Buy, 5, "229.00"), (OrderSide::Buy, 2, "232.50")], + "230.00" + )] + #[case::reduce_keeps_entry( + PositionSide::Long, + vec![(OrderSide::Buy, 4, "229.00"), (OrderSide::Buy, 4, "231.00"), (OrderSide::Sell, 1, "235.00")], + "230.00" + )] + #[case::flip_opens_at_fill( + PositionSide::Short, + vec![(OrderSide::Buy, 2, "230.00"), (OrderSide::Sell, 9, "228.00")], + "228.00" + )] + #[case::fills_partial_history(PositionSide::Long, vec![(OrderSide::Buy, 2, "230.00")], "229.81")] + #[case::no_fills(PositionSide::Long, vec![], "229.81")] + fn test_fill_position_avg_px_from_fills( + #[case] position_side: PositionSide, + #[case] fills: Vec<(OrderSide, u64, &str)>, + #[case] expected: &str, + ) { + let instrument_id = InstrumentId::from("NVDA.NASDAQ"); + let account_id = AccountId::from("IB-DU001"); + let mut reports = vec![PositionStatusReport::new( + account_id, + instrument_id, + position_side, + Quantity::from(7), + UnixNanos::default(), + UnixNanos::default(), + None, + None, + Some(Decimal::from_str("229.81").unwrap()), + )]; + let other_fill = FillReport::new( + account_id, + InstrumentId::from("AAPL.NASDAQ"), + VenueOrderId::new("PERM-9"), + TradeId::new("T-OTHER"), + OrderSide::Sell, + Quantity::from(7), + Price::from("150.00"), + Money::from("1.00 USD"), + LiquiditySide::Taker, + None, + None, + UnixNanos::from(1), + UnixNanos::default(), + None, + ); + let mut fill_reports: Vec<_> = fills + .into_iter() + .enumerate() + .rev() + .map(|(index, (side, qty, px))| { + FillReport::new( + account_id, + instrument_id, + VenueOrderId::new(format!("PERM-{index}")), + TradeId::new(format!("T-{index}")), + side, + Quantity::from(qty), + Price::from(px), + Money::from("1.00 USD"), + LiquiditySide::Taker, + None, + None, + UnixNanos::from(index as u64 + 10), + UnixNanos::default(), + None, + ) + }) + .collect(); + fill_reports.push(other_fill); + + fill_position_avg_px_from_fills(&mut reports, &fill_reports); + + assert_eq!( + reports[0].avg_px_open, + Some(Decimal::from_str(expected).unwrap()) + ); + } + + #[rstest] + fn test_fill_missing_avg_px_uses_fill_reports() { + let instrument_id = InstrumentId::from("ESZ6.XCME"); + let account_id = AccountId::from("IB-DU001"); + let order_report = |venue_order_id: &str, avg_px: Option| { + let mut report = OrderStatusReport::new( + account_id, + instrument_id, + None, + VenueOrderId::new(venue_order_id), + Some(OrderSide::Buy), + OrderType::Market, + TimeInForce::Day, + NautilusOrderStatus::Filled, + Quantity::from(3), + Quantity::from(3), + UnixNanos::default(), + UnixNanos::default(), + UnixNanos::default(), + None, + ); + report.avg_px = avg_px; + report + }; + let fill = |venue_order_id: &str, trade_id: &str, qty: u64, px: &str| { + FillReport::new( + account_id, + instrument_id, + VenueOrderId::new(venue_order_id), + TradeId::new(trade_id), + OrderSide::Buy, + Quantity::from(qty), + Price::from(px), + Money::from("0.00 USD"), + LiquiditySide::Taker, + None, + None, + UnixNanos::default(), + UnixNanos::default(), + None, + ) + }; + let mut reports = vec![ + order_report("PERM-1", None), + order_report("PERM-2", Some(Decimal::from(7))), + order_report("PERM-3", None), + ]; + let fills = vec![ + fill("PERM-1", "T-1", 1, "2.55"), + fill("PERM-1", "T-2", 2, "2.40"), + fill("PERM-2", "T-3", 3, "9.00"), + ]; + + fill_missing_avg_px(&mut reports, &fills); + + assert_eq!(reports[0].avg_px, Some(Decimal::from_str("2.45").unwrap())); + assert_eq!(reports[1].avg_px, Some(Decimal::from(7))); + assert_eq!(reports[2].avg_px, None); + } + + #[rstest] + #[case::combo(ibapi::contracts::SecurityType::Spread, true)] + #[case::leg(ibapi::contracts::SecurityType::FuturesOption, false)] + fn test_is_combo_execution( + #[case] security_type: ibapi::contracts::SecurityType, + #[case] expected: bool, + ) { + let exec_data = ibapi::orders::ExecutionData { + contract: ibapi::contracts::Contract { + security_type, + ..Default::default() + }, + ..Default::default() + }; + + assert_eq!(is_combo_execution(&exec_data), expected); + } + #[rstest] fn test_ibalgo_with_zero_limit_price_maps_to_market() { assert_eq!(map_ib_order_type("IBALGO", Some(0.0)), OrderType::Market); @@ -640,8 +1111,7 @@ mod tests { let error_msg = e.to_string(); assert!( error_msg.contains("not found") || error_msg.contains("instrument"), - "Unexpected error: {}", - error_msg + "Unexpected error: {error_msg}" ); } } @@ -680,8 +1150,7 @@ mod tests { let error_msg = e.to_string(); assert!( error_msg.contains("not found") || error_msg.contains("instrument"), - "Unexpected error: {}", - error_msg + "Unexpected error: {error_msg}" ); } } @@ -690,7 +1159,7 @@ mod tests { fn test_parse_order_status_to_report_spread_allows_negative_avg_fill_price() { let instrument_provider = create_test_instrument_provider(); let instrument_id = InstrumentId::new( - Symbol::from("(1)SPY C400_((1))SPY C410"), + Symbol::from("(1)SPY C400___((1))SPY C410"), Venue::from("SMART"), ); let account_id = AccountId::from("IB-001"); @@ -801,6 +1270,161 @@ mod tests { ); } + #[rstest] + fn test_parse_order_status_to_report_recovers_completed_order_quantity() { + let instrument_provider = create_test_instrument_provider(); + let instrument_id = create_test_instrument_id(); + let order_status = OrderStatus { + status: OrderStatusKind::Filled, + filled: 3.0, + remaining: 0.0, + ..Default::default() + }; + let order = Order { + total_quantity: 0.0, + filled_quantity: 3.0, + ..Default::default() + }; + + let report = parse_order_status_to_report( + &order_status, + Some(&order), + instrument_id, + AccountId::from("IB-001"), + &instrument_provider, + UnixNanos::new(0), + ) + .unwrap(); + + assert_eq!(report.quantity, Quantity::from(3)); + assert_eq!(report.filled_qty, Quantity::from(3)); + } + + #[rstest] + fn test_parse_order_status_to_report_sets_stop_trigger_type() { + let instrument_provider = create_test_instrument_provider(); + let instrument_id = create_test_instrument_id(); + let order_status = OrderStatus { + status: OrderStatusKind::Cancelled, + remaining: 1.0, + ..Default::default() + }; + let order = Order { + total_quantity: 1.0, + order_type: "STP".to_string(), + aux_price: Some(100.0), + trigger_method: ibapi::orders::conditions::TriggerMethod::Last, + ..Default::default() + }; + + let report = parse_order_status_to_report( + &order_status, + Some(&order), + instrument_id, + AccountId::from("IB-001"), + &instrument_provider, + UnixNanos::new(0), + ) + .unwrap(); + + assert_eq!(report.order_type, OrderType::StopMarket); + assert_eq!(report.trigger_price, Some(Price::new(100.0, 0))); + assert_eq!(report.trigger_type, Some(TriggerType::LastPrice)); + } + + #[rstest] + #[case( + "GTD", + "20260925 20:00:00 UTC", + TimeInForce::Gtd, + Some(1_790_366_400_000_000_000) + )] + #[case( + "GTC", + "20260925-20:00:00", + TimeInForce::Gtd, + Some(1_790_366_400_000_000_000) + )] + #[case("GTD", "not a date", TimeInForce::Gtc, None)] + #[case("DAY", "", TimeInForce::Day, None)] + fn test_parse_order_status_to_report_sets_gtd_expire_time( + #[case] tif: &str, + #[case] good_till_date: &str, + #[case] expected_time_in_force: TimeInForce, + #[case] expected_expire_time: Option, + ) { + let instrument_provider = create_test_instrument_provider(); + let instrument_id = create_test_instrument_id(); + let order_status = OrderStatus { + status: OrderStatusKind::Filled, + filled: 1.0, + ..Default::default() + }; + let order = Order { + total_quantity: 1.0, + order_type: "LMT".to_string(), + limit_price: Some(101.0), + tif: ibapi::orders::TimeInForce::from(tif), + good_till_date: good_till_date.to_string(), + ..Default::default() + }; + + let report = parse_order_status_to_report( + &order_status, + Some(&order), + instrument_id, + AccountId::from("IB-001"), + &instrument_provider, + UnixNanos::new(0), + ) + .unwrap(); + + assert_eq!(report.time_in_force, expected_time_in_force); + assert_eq!( + report.expire_time, + expected_expire_time.map(UnixNanos::from) + ); + } + + #[rstest] + #[case("MKT", Some(0.0), OrderType::Market, None)] + #[case("STP", Some(0.0), OrderType::StopMarket, None)] + #[case("LMT", Some(101.0), OrderType::Limit, Some(Price::new(101.0, 0)))] + fn test_parse_order_status_to_report_sets_price_only_for_limit_types( + #[case] ib_order_type: &str, + #[case] limit_price: Option, + #[case] expected_order_type: OrderType, + #[case] expected_price: Option, + ) { + let instrument_provider = create_test_instrument_provider(); + let instrument_id = create_test_instrument_id(); + let order_status = OrderStatus { + status: OrderStatusKind::Filled, + filled: 1.0, + ..Default::default() + }; + let order = Order { + total_quantity: 1.0, + order_type: ib_order_type.to_string(), + limit_price, + aux_price: Some(100.0), + ..Default::default() + }; + + let report = parse_order_status_to_report( + &order_status, + Some(&order), + instrument_id, + AccountId::from("IB-001"), + &instrument_provider, + UnixNanos::new(0), + ) + .unwrap(); + + assert_eq!(report.order_type, expected_order_type); + assert_eq!(report.price, expected_price); + } + #[rstest] fn test_ib_venue_order_id_prefers_perm_id_and_falls_back_to_order_id() { assert_eq!(ib_venue_order_id(123, 456).to_string(), "PERM-456"); @@ -814,6 +1438,28 @@ mod tests { assert_eq!(normalized_order_ref(""), None); } + #[rstest] + #[case(Liquidity::AddedLiquidity, LiquiditySide::Maker)] + #[case(Liquidity::RemovedLiquidity, LiquiditySide::Taker)] + #[case(Liquidity::None, LiquiditySide::NoLiquiditySide)] + #[case(Liquidity::LiquidityRoutedOut, LiquiditySide::NoLiquiditySide)] + #[case(Liquidity::Unknown(4), LiquiditySide::NoLiquiditySide)] + fn test_execution_liquidity_side( + #[case] liquidity: Liquidity, + #[case] expected: LiquiditySide, + ) { + assert_eq!(execution_liquidity_side(&liquidity), expected); + } + + struct PricingExpectation { + order_type: OrderType, + price: Option, + trigger_price: Option, + limit_offset: Option, + trailing_offset: Option, + trailing_offset_type: Option, + } + #[rstest] #[case( "MKT", @@ -821,12 +1467,14 @@ mod tests { None, None, None, - OrderType::Market, - None, - None, - None, - None, - None + PricingExpectation { + order_type: OrderType::Market, + price: None, + trigger_price: None, + limit_offset: None, + trailing_offset: None, + trailing_offset_type: None, + } )] #[case( "LMT", @@ -834,12 +1482,14 @@ mod tests { None, None, None, - OrderType::Limit, - Some(Price::new(185.0, 0)), - None, - None, - None, - None + PricingExpectation { + order_type: OrderType::Limit, + price: Some(Price::new(185.0, 0)), + trigger_price: None, + limit_offset: None, + trailing_offset: None, + trailing_offset_type: None, + } )] #[case( "IBALGO", @@ -847,12 +1497,14 @@ mod tests { None, None, None, - OrderType::Limit, - Some(Price::new(185.0, 0)), - None, - None, - None, - None + PricingExpectation { + order_type: OrderType::Limit, + price: Some(Price::new(185.0, 0)), + trigger_price: None, + limit_offset: None, + trailing_offset: None, + trailing_offset_type: None, + } )] #[case( "IBALGO", @@ -860,12 +1512,14 @@ mod tests { None, None, None, - OrderType::Market, - None, - None, - None, - None, - None + PricingExpectation { + order_type: OrderType::Market, + price: None, + trigger_price: None, + limit_offset: None, + trailing_offset: None, + trailing_offset_type: None, + } )] #[case( "MIT", @@ -873,12 +1527,14 @@ mod tests { Some(180.0), None, None, - OrderType::MarketIfTouched, - None, - Some(Price::new(180.0, 0)), - None, - None, - None + PricingExpectation { + order_type: OrderType::MarketIfTouched, + price: None, + trigger_price: Some(Price::new(180.0, 0)), + limit_offset: None, + trailing_offset: None, + trailing_offset_type: None, + } )] #[case( "LIT", @@ -886,12 +1542,14 @@ mod tests { Some(180.0), None, None, - OrderType::LimitIfTouched, - Some(Price::new(179.0, 0)), - Some(Price::new(180.0, 0)), - None, - None, - None + PricingExpectation { + order_type: OrderType::LimitIfTouched, + price: Some(Price::new(179.0, 0)), + trigger_price: Some(Price::new(180.0, 0)), + limit_offset: None, + trailing_offset: None, + trailing_offset_type: None, + } )] #[case( "STP", @@ -899,12 +1557,14 @@ mod tests { Some(180.0), None, None, - OrderType::StopMarket, - None, - Some(Price::new(180.0, 0)), - None, - None, - None + PricingExpectation { + order_type: OrderType::StopMarket, + price: None, + trigger_price: Some(Price::new(180.0, 0)), + limit_offset: None, + trailing_offset: None, + trailing_offset_type: None, + } )] #[case( "STP LMT", @@ -912,12 +1572,14 @@ mod tests { Some(180.0), None, None, - OrderType::StopLimit, - Some(Price::new(179.0, 0)), - Some(Price::new(180.0, 0)), - None, - None, - None + PricingExpectation { + order_type: OrderType::StopLimit, + price: Some(Price::new(179.0, 0)), + trigger_price: Some(Price::new(180.0, 0)), + limit_offset: None, + trailing_offset: None, + trailing_offset_type: None, + } )] #[case( "TRAIL LIMIT", @@ -925,12 +1587,14 @@ mod tests { Some(2.5), Some(185.0), Some(0.25), - OrderType::TrailingStopLimit, - None, - Some(Price::new(185.0, 0)), - Some(Decimal::from_str("0.25").unwrap()), - Some(Decimal::from_str("2.5").unwrap()), - Some(TrailingOffsetType::Price), + PricingExpectation { + order_type: OrderType::TrailingStopLimit, + price: None, + trigger_price: Some(Price::new(185.0, 0)), + limit_offset: Some(Decimal::from_str("0.25").unwrap()), + trailing_offset: Some(Decimal::from_str("2.5").unwrap()), + trailing_offset_type: Some(TrailingOffsetType::Price), + }, )] fn test_parse_order_status_to_report_maps_pricing_fields_by_order_type( #[case] ib_order_type: &str, @@ -938,12 +1602,7 @@ mod tests { #[case] aux_price: Option, #[case] trail_stop_price: Option, #[case] limit_price_offset: Option, - #[case] expected_order_type: OrderType, - #[case] expected_price: Option, - #[case] expected_trigger_price: Option, - #[case] expected_limit_offset: Option, - #[case] expected_trailing_offset: Option, - #[case] expected_trailing_offset_type: Option, + #[case] expected: PricingExpectation, ) { let instrument_provider = create_test_instrument_provider(); let instrument_id = create_test_instrument_id(); @@ -971,7 +1630,7 @@ mod tests { aux_price, trail_stop_price, limit_price_offset, - tif: ibapi::orders::TimeInForce::GoodTilCanceled, + tif: ibapi::orders::TimeInForce::GoodTillCanceled, ..Default::default() }; @@ -985,12 +1644,12 @@ mod tests { ) .unwrap(); - assert_eq!(report.order_type, expected_order_type); - assert_eq!(report.price, expected_price); - assert_eq!(report.trigger_price, expected_trigger_price); - assert_eq!(report.limit_offset, expected_limit_offset); - assert_eq!(report.trailing_offset, expected_trailing_offset); - assert_eq!(report.trailing_offset_type, expected_trailing_offset_type); + assert_eq!(report.order_type, expected.order_type); + assert_eq!(report.price, expected.price); + assert_eq!(report.trigger_price, expected.trigger_price); + assert_eq!(report.limit_offset, expected.limit_offset); + assert_eq!(report.trailing_offset, expected.trailing_offset); + assert_eq!(report.trailing_offset_type, expected.trailing_offset_type); } #[rstest] @@ -1019,7 +1678,7 @@ mod tests { order_type: "TRAIL".to_string(), trail_stop_price: Some(185.0), trailing_percent: Some(2.5), - tif: ibapi::orders::TimeInForce::GoodTilCanceled, + tif: ibapi::orders::TimeInForce::GoodTillCanceled, ..Default::default() }; @@ -1094,8 +1753,7 @@ mod tests { let error_msg = e.to_string(); assert!( error_msg.contains("not found") || error_msg.contains("instrument"), - "Unexpected error: {}", - error_msg + "Unexpected error: {error_msg}" ); } Ok(fill) => { @@ -1203,8 +1861,7 @@ mod tests { let error_msg = e.to_string(); assert!( error_msg.contains("not found") || error_msg.contains("instrument"), - "Unexpected error: {}", - error_msg + "Unexpected error: {error_msg}" ); } Ok(fill) => { @@ -1212,4 +1869,196 @@ mod tests { } } } + + fn contingency_report( + client_order_id: Option<&str>, + venue_order_id: &str, + ) -> OrderStatusReport { + OrderStatusReport::new( + AccountId::from("IB-001"), + create_test_instrument_id(), + client_order_id.map(ClientOrderId::from), + VenueOrderId::from(venue_order_id), + OrderSide::Sell.into(), + OrderType::StopMarket, + TimeInForce::Gtc, + NautilusOrderStatus::Accepted, + Quantity::from(1), + Quantity::from(0), + UnixNanos::default(), + UnixNanos::default(), + UnixNanos::default(), + None, + ) + } + + fn contingency_order(order_id: i32, perm_id: i64, parent_id: i32, oca: &str) -> Order { + Order { + order_id, + perm_id, + client_id: 7, + parent_id, + account: "DU123".to_string(), + oca_group: oca.to_string(), + oca_type: ibapi::orders::OcaType::ReduceWithBlock, + ..Default::default() + } + } + + #[rstest] + fn test_link_order_contingencies_restores_bracket_and_oca_group() { + let mut reports = vec![ + contingency_report(Some("O-ENTRY"), "PERM-10"), + contingency_report(Some("O-STOP"), "PERM-11"), + contingency_report(None, "PERM-12"), + contingency_report(Some("O-ALONE"), "PERM-13"), + ]; + let orders = vec![ + contingency_order(1, 10, 0, ""), + contingency_order(2, 11, 1, "EXIT-1"), + contingency_order(3, 12, 1, "EXIT-1"), + contingency_order(4, 13, 99, ""), + ]; + + link_order_contingencies(&mut reports, &orders); + + let links = reports + .iter() + .map(|report| { + ( + report.contingency_type, + report.linked_order_ids.clone(), + report.parent_order_id, + ) + }) + .collect::>(); + assert_eq!( + links, + vec![ + ( + Some(ContingencyType::Oto), + Some(vec![ + ClientOrderId::from("O-STOP"), + ClientOrderId::from("PERM-12") + ]), + None, + ), + ( + Some(ContingencyType::Ouo), + Some(vec![ClientOrderId::from("PERM-12")]), + Some(ClientOrderId::from("O-ENTRY")), + ), + ( + Some(ContingencyType::Ouo), + Some(vec![ClientOrderId::from("O-STOP")]), + Some(ClientOrderId::from("O-ENTRY")), + ), + (None, None, None), + ] + ); + } + + #[rstest] + #[case::cancel(ibapi::orders::OcaType::CancelWithBlock, Some(ContingencyType::Oco))] + #[case::reduce_without_block( + ibapi::orders::OcaType::ReduceWithoutBlock, + Some(ContingencyType::Ouo) + )] + #[case::unset(ibapi::orders::OcaType::None, None)] + fn test_link_order_contingencies_maps_oca_type( + #[case] oca_type: ibapi::orders::OcaType, + #[case] expected: Option, + ) { + let mut reports = vec![ + contingency_report(Some("O-A"), "PERM-20"), + contingency_report(Some("O-B"), "PERM-21"), + ]; + let mut orders = vec![ + contingency_order(1, 20, 0, "GROUP"), + contingency_order(2, 21, 0, "GROUP"), + ]; + + for order in &mut orders { + order.oca_type = oca_type; + } + + link_order_contingencies(&mut reports, &orders); + + assert_eq!( + reports + .iter() + .map(|report| (report.contingency_type, report.linked_order_ids.clone())) + .collect::>(), + vec![ + (expected, expected.map(|_| vec![ClientOrderId::from("O-B")])), + (expected, expected.map(|_| vec![ClientOrderId::from("O-A")])), + ] + ); + } + + #[rstest] + #[case::iceberg(Some(2), Some(Quantity::from(2)))] + #[case::full_size(Some(5), None)] + #[case::unset(Some(0), None)] + fn test_parse_order_status_to_report_sets_display_qty( + #[case] display_size: Option, + #[case] expected: Option, + ) { + let instrument_provider = create_test_instrument_provider(); + let order = Order { + total_quantity: 5.0, + order_type: "LMT".to_string(), + limit_price: Some(101.0), + display_size, + ..Default::default() + }; + let order_status = OrderStatus { + status: OrderStatusKind::Submitted, + remaining: 5.0, + ..Default::default() + }; + + let report = parse_order_status_to_report( + &order_status, + Some(&order), + create_test_instrument_id(), + AccountId::from("IB-001"), + &instrument_provider, + UnixNanos::new(0), + ) + .unwrap(); + + assert_eq!(report.display_qty, expected); + } + + #[rstest] + #[case::completed("20260924 12:02:04 UTC", 1_790_251_324_000_000_000)] + #[case::open("", 7)] + fn test_parse_order_data_to_report_uses_completed_time( + #[case] completed_time: &str, + #[case] expected_ts_last: u64, + ) { + let instrument_provider = create_test_instrument_provider(); + let mut data = OrderData { + order_id: 1, + order: Order { + total_quantity: 1.0, + order_type: "MKT".to_string(), + ..Default::default() + }, + ..Default::default() + }; + data.order_state.completed_time = completed_time.to_string(); + + let report = parse_order_data_to_report( + &data, + create_test_instrument_id(), + AccountId::from("IB-001"), + &instrument_provider, + UnixNanos::new(7), + ) + .unwrap(); + + assert_eq!(report.ts_last, UnixNanos::from(expected_ts_last)); + } } diff --git a/crates/adapters/interactive_brokers/src/execution/reports.rs b/crates/adapters/interactive_brokers/src/execution/reports.rs new file mode 100644 index 000000000000..f0b18eb02cd7 --- /dev/null +++ b/crates/adapters/interactive_brokers/src/execution/reports.rs @@ -0,0 +1,646 @@ +// ------------------------------------------------------------------------------------------------- +// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. +// https://nautechsystems.io +// +// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------------------------------- + +//! Report collection for the Interactive Brokers execution client. +//! +//! Collection owns everything it uses and never touches the live cache, so it can run on a +//! runtime worker as an `ExecutionReportTask` and inline through the same code. + +use ibapi::client::Client; + +use super::{core::*, parse::parse_order_data_to_report}; + +/// Inputs for turning IB report data into reports, with no venue access. +#[derive(Clone)] +pub(super) struct IbReportContext { + instrument_provider: Arc, + account_id: AccountId, +} + +impl IbReportContext { + pub(super) fn new( + instrument_provider: Arc, + account_id: AccountId, + ) -> Self { + Self { + instrument_provider, + account_id, + } + } + + pub(super) fn parse_historical_fill_report( + &self, + cmd: &GenerateFillReports, + exec_data: &ExecutionData, + commission: f64, + commission_currency: &str, + ts_init: UnixNanos, + ) -> Option { + let instrument_id = match self.resolve_historical_execution_instrument_id(exec_data) { + Ok(instrument_id) => instrument_id, + Err(e) => { + Self::warn_historical_fill_report_parse_error(exec_data, &e); + return None; + } + }; + + self.build_historical_fill_report( + cmd, + exec_data, + instrument_id, + commission, + commission_currency, + ts_init, + ) + } + + fn build_historical_fill_report( + &self, + cmd: &GenerateFillReports, + exec_data: &ExecutionData, + instrument_id: InstrumentId, + commission: f64, + commission_currency: &str, + ts_init: UnixNanos, + ) -> Option { + if let Some(filter_id) = cmd.instrument_id + && instrument_id != filter_id + { + return None; + } + + if let Some(filter_venue_order_id) = cmd.venue_order_id + && ib_venue_order_id(exec_data.execution.order_id, exec_data.execution.perm_id) + != filter_venue_order_id + { + return None; + } + + if let Some(end) = cmd.end { + match parse_execution_time(&exec_data.execution.time) { + Ok(ts_event) if ts_event > end => return None, + Ok(_) => {} + Err(e) => { + Self::warn_historical_fill_report_parse_error(exec_data, &e); + return None; + } + } + } + + match parse_execution_to_fill_report( + &exec_data.execution, + &exec_data.contract, + commission, + commission_currency, + instrument_id, + self.account_id, + &self.instrument_provider, + ts_init, + None, // avg_px (not available in historical fills) + ) { + Ok(report) => Some(report), + Err(e) => { + Self::warn_historical_fill_report_parse_error(exec_data, &e); + None + } + } + } + + fn resolve_historical_execution_instrument_id( + &self, + exec_data: &ExecutionData, + ) -> anyhow::Result { + self.resolve_report_contract_instrument_id(&exec_data.contract) + } + + pub(super) fn resolve_report_contract_instrument_id( + &self, + contract: &Contract, + ) -> anyhow::Result { + self.instrument_provider + .resolve_instrument_id_for_contract(contract) + .context("Failed to resolve IBKR report contract to instrument ID") + } + + pub(super) fn position_avg_px_open( + &self, + instrument_id: &InstrumentId, + instrument: &InstrumentAny, + average_cost: f64, + ) -> Option { + if average_cost <= 0.0 { + return None; + } + + let price_magnifier = self.instrument_provider.get_price_magnifier(instrument_id) as f64; + let multiplier = instrument.multiplier().as_f64(); + let converted_avg_cost = average_cost / (multiplier * price_magnifier); + Decimal::from_f64_retain(converted_avg_cost) + .map(|price| price.round_dp(instrument.price_precision() as u32)) + } + + pub(super) fn warn_historical_fill_report_parse_error( + exec_data: &ExecutionData, + error: &anyhow::Error, + ) { + tracing::warn!( + symbol = exec_data.contract.symbol.as_str(), + sec_type = ?exec_data.contract.security_type, + exchange = exec_data.contract.exchange.as_str(), + primary_exchange = exec_data.contract.primary_exchange.as_str(), + local_symbol = exec_data.contract.local_symbol.as_str(), + con_id = exec_data.contract.contract_id, + order_id = exec_data.execution.order_id, + order_ref = exec_data.execution.order_reference.as_str(), + execution_id = exec_data.execution.execution_id.as_str(), + error = %error, + "Failed to parse IBKR historical fill report", + ); + } +} + +/// Owned Interactive Brokers report collection. +pub(super) struct IbReportClient { + client: Arc, + context: IbReportContext, + ib_account: Ustr, + request_timeout: Duration, +} + +impl IbReportClient { + pub(super) fn new( + client: Arc, + context: IbReportContext, + ib_account: Ustr, + request_timeout: Duration, + ) -> Self { + Self { + client, + context, + ib_account, + request_timeout, + } + } + + pub(super) async fn generate_order_status_report( + &self, + cmd: &GenerateOrderStatusReport, + ) -> anyhow::Result> { + let plural_cmd = GenerateOrderStatusReports { + command_id: cmd.command_id, + ts_init: cmd.ts_init, + open_only: false, + instrument_id: cmd.instrument_id, + start: None, + end: None, + params: cmd.params.clone(), + log_receipt_level: LogLevel::Info, + correlation_id: cmd.correlation_id, + causation_id: cmd.causation_id, + }; + + let reports = self.generate_order_status_reports(&plural_cmd).await?; + + // Filter by client_order_id and venue_order_id + let report = reports.into_iter().find(|r| { + let matches_client = if let Some(filter_client_id) = cmd.client_order_id { + r.client_order_id == Some(filter_client_id) + } else { + true + }; + let matches_venue = if let Some(filter_venue_id) = cmd.venue_order_id { + r.venue_order_id == filter_venue_id + } else { + true + }; + matches_client && matches_venue + }); + + Ok(report) + } + + pub(super) async fn generate_order_status_reports( + &self, + cmd: &GenerateOrderStatusReports, + ) -> anyhow::Result> { + let client = self.client.as_ref(); + + let timeout_dur = self.request_timeout; + let subscription = tokio::time::timeout(timeout_dur, client.all_open_orders()) + .await + .context("Timeout requesting open orders")??; + let mut subscription = subscription.filter_data(); + let mut reports = Vec::new(); + let mut ib_orders = Vec::new(); + let ts_init = get_atomic_clock_realtime().get_time_ns(); + let ib_account = self.ib_account; + + while let Some(order_result) = subscription.next().await { + match order_result { + Ok(Orders::OrderData(data)) => { + if !data.order.account.is_empty() && data.order.account != ib_account { + continue; + } + + // Convert IB contract to instrument ID + let instrument_id = self + .context + .resolve_report_contract_instrument_id(&data.contract) + .with_context(|| { + format!( + "Failed to resolve IB order {} contract ID {} ({:?})", + data.order_id, + data.contract.contract_id, + data.contract.security_type + ) + })?; + + // Filter by instrument_id if specified + if let Some(filter_id) = cmd.instrument_id + && instrument_id != filter_id + { + continue; + } + + // Parse to order status report using minimal OrderStatus + // Note: OrderState doesn't have filled/average_fill_price, so we use defaults + let report = parse_order_data_to_report( + &data, + instrument_id, + self.context.account_id, + &self.context.instrument_provider, + ts_init, + ) + .with_context(|| format!("Failed to parse IB order {}", data.order_id))?; + reports.push(report); + ib_orders.push(data.order); + } + Ok(_) => { + // Ignore other order types + } + Err(e) => return Err(e.into()), + } + } + + if !cmd.open_only { + let completed = tokio::time::timeout(timeout_dur, client.completed_orders(false)) + .await + .context("Timeout requesting completed orders")??; + let mut completed = completed.filter_data(); + + while let Some(order_result) = completed.next().await { + let Orders::OrderData(data) = order_result? else { + continue; + }; + + if !data.order.account.is_empty() && data.order.account != ib_account { + continue; + } + + // An OCA group that reduces its members on a fill cancels an unfilled member + // by reducing its quantity to zero, leaving nothing to reconcile + if data.order.total_quantity == 0.0 && data.order.filled_quantity == 0.0 { + tracing::debug!( + "Skipping completed IB order {} with zero quantity", + data.order.perm_id + ); + continue; + } + + let instrument_id = self + .context + .resolve_report_contract_instrument_id(&data.contract) + .with_context(|| { + format!( + "Failed to resolve completed IB order contract ID {} ({:?})", + data.contract.contract_id, data.contract.security_type + ) + })?; + + if cmd + .instrument_id + .is_some_and(|filter_id| instrument_id != filter_id) + { + continue; + } + + let report = parse_order_data_to_report( + &data, + instrument_id, + self.context.account_id, + &self.context.instrument_provider, + ts_init, + ) + .context("Failed to parse completed IB order")?; + + if !reports + .iter() + .any(|existing| existing.venue_order_id == report.venue_order_id) + { + reports.push(report); + ib_orders.push(data.order); + } + } + } + + parse::link_order_contingencies(&mut reports, &ib_orders); + + Ok(reports) + } + + pub(super) async fn generate_fill_reports( + &self, + cmd: GenerateFillReports, + ) -> anyhow::Result> { + let client = self.client.as_ref(); + + let filter = + InteractiveBrokersExecutionClient::execution_filter(self.ib_account, cmd.start); + + let timeout_dur = self.request_timeout; + let subscription = tokio::time::timeout(timeout_dur, client.executions(filter)) + .await + .context("Timeout requesting executions")??; + let mut subscription = subscription.filter_data(); + let mut reports = Vec::new(); + let ts_init = get_atomic_clock_realtime().get_time_ns(); + let mut pending_exec_data: AHashMap = AHashMap::new(); + let mut pending_commissions: AHashMap = AHashMap::new(); + let mut combo_executions = Vec::new(); + + while let Some(exec_result) = subscription.next().await { + match exec_result { + Ok(Executions::ExecutionData(exec_data)) => { + let execution_id = exec_data.execution.execution_id.clone(); + if parse::is_combo_execution(&exec_data) { + combo_executions.push(exec_data); + } else if let Some((commission, commission_currency)) = + pending_commissions.remove(&execution_id) + { + if let Some(report) = self.context.parse_historical_fill_report( + &cmd, + &exec_data, + commission, + &commission_currency, + ts_init, + ) { + reports.push(report); + } + } else { + pending_exec_data.insert(execution_id, exec_data); + } + } + Ok(Executions::CommissionReport(commission)) => { + if let Some(exec_data) = pending_exec_data.remove(&commission.execution_id) { + if let Some(report) = self.context.parse_historical_fill_report( + &cmd, + &exec_data, + commission.commission, + &commission.currency, + ts_init, + ) { + reports.push(report); + } + } else { + pending_commissions.insert( + commission.execution_id, + (commission.commission, commission.currency), + ); + } + } + Err(e) => { + tracing::warn!("Error receiving execution data: {e}"); + } + } + } + + anyhow::ensure!( + pending_exec_data.is_empty(), + "IB did not provide commission reports for execution IDs: {}", + pending_exec_data + .keys() + .map(String::as_str) + .collect::>() + .join(", ") + ); + + if !combo_executions.is_empty() { + self.resolve_combo_fill_reports(&cmd, client, combo_executions, &mut reports, ts_init) + .await?; + } + + Ok(reports) + } + + pub(super) async fn generate_position_status_reports( + &self, + cmd: &GeneratePositionStatusReports, + ) -> anyhow::Result> { + let client = self.client.as_ref(); + + let timeout_dur = self.request_timeout; + let subscription = tokio::time::timeout(timeout_dur, client.positions()) + .await + .context("Timeout requesting positions")??; + let mut subscription = subscription.filter_data(); + let mut reports = Vec::new(); + let ts_init = get_atomic_clock_realtime().get_time_ns(); + let ib_account = self.ib_account; + + // Process positions until PositionEnd; return empty list when none (reconciliation parity: + // never return None/missing for "no positions"). + while let Some(position_result) = subscription.next().await { + match position_result { + Ok(PositionUpdate::Position(position)) => { + // Filter for the specific account + if position.account != ib_account { + continue; + } + + let instrument = match self + .context + .instrument_provider + .get_instrument(client, &position.contract) + .await + { + Ok(Some(instrument)) => instrument, + Ok(None) => anyhow::bail!( + "Cannot resolve position instrument for IB contract ID {} ({:?})", + position.contract.contract_id, + position.contract.security_type + ), + Err(e) => return Err(e).context(format!( + "Failed to resolve position instrument for IB contract ID {} ({:?})", + position.contract.contract_id, position.contract.security_type + )), + }; + let instrument_id = instrument.id(); + + // Filter by instrument_id if specified + if let Some(filter_id) = cmd.instrument_id + && instrument_id != filter_id + { + continue; + } + + // Determine position side + let position_side = if position.position == 0.0 { + PositionSide::Flat + } else if position.position > 0.0 { + PositionSide::Long + } else { + PositionSide::Short + }; + + let quantity = + Quantity::new(position.position.abs(), instrument.size_precision()); + + // Convert IB avg_cost to Nautilus Price, accounting for price magnifier and multiplier + // Python: converted_avg_cost = avg_cost / (multiplier * price_magnifier) + let avg_px_open = self.context.position_avg_px_open( + &instrument_id, + &instrument, + position.average_cost, + ); + + let report = PositionStatusReport::new( + self.context.account_id, + instrument_id, + position_side, + quantity, + ts_init, // ts_last + ts_init, // ts_init + None, // report_id: auto-generated + None, // venue_position_id + avg_px_open, + ); + + reports.push(report); + } + Ok(PositionUpdate::PositionEnd) => { + // End of position list + break; + } + Err(e) => return Err(e.into()), + } + } + + if reports.is_empty() + && let Some(instrument_id) = cmd.instrument_id + { + let precision = self + .context + .instrument_provider + .find(&instrument_id) + .map_or(0, |instrument| instrument.size_precision()); + reports.push(PositionStatusReport::new( + self.context.account_id, + instrument_id, + PositionSide::Flat, + Quantity::zero(precision), + ts_init, + ts_init, + None, + None, + None, + )); + } + + Ok(reports) + } + + // A combo-level execution carries a generic BAG contract without its legs, so its spread + // resolves through the combo order's contract by permanent ID. It becomes the spread + // order's fill with zero commission, as on the live path, and the combo's leg executions + // are dropped: they share its order IDs, so they would fill the spread order at leg prices, + // and startup reconciles leg positions from IB's position reports. + async fn resolve_combo_fill_reports( + &self, + cmd: &GenerateFillReports, + client: &Client, + combo_executions: Vec, + reports: &mut Vec, + ts_init: UnixNanos, + ) -> anyhow::Result<()> { + let combo_perm_ids: AHashSet = combo_executions + .iter() + .map(|exec_data| exec_data.execution.perm_id) + .collect(); + let combo_contracts = self.combo_order_contracts(client, &combo_perm_ids).await?; + let combo_venue_order_ids: AHashSet = combo_perm_ids + .iter() + .map(|perm_id| ib_venue_order_id(0, *perm_id)) + .collect(); + reports.retain(|report| !combo_venue_order_ids.contains(&report.venue_order_id)); + + for exec_data in combo_executions { + let perm_id = exec_data.execution.perm_id; + let instrument_id = match combo_contracts + .get(&perm_id) + .context("IB returned no combo order for the execution") + .and_then(|contract| self.context.resolve_report_contract_instrument_id(contract)) + { + Ok(instrument_id) => instrument_id, + Err(e) => { + IbReportContext::warn_historical_fill_report_parse_error(&exec_data, &e); + continue; + } + }; + + if let Some(report) = self.context.build_historical_fill_report( + cmd, + &exec_data, + instrument_id, + 0.0, + exec_data.contract.currency.as_str(), + ts_init, + ) { + reports.push(report); + } + } + + Ok(()) + } + + async fn combo_order_contracts( + &self, + client: &Client, + perm_ids: &AHashSet, + ) -> anyhow::Result> { + let timeout_dur = self.request_timeout; + tokio::time::timeout(timeout_dur, async { + let mut contracts = AHashMap::new(); + let mut open = client.all_open_orders().await?; + while let Some(item) = open.next().await { + if let SubscriptionItem::Data(Orders::OrderData(data)) = item? + && perm_ids.contains(&data.order.perm_id) + { + contracts.insert(data.order.perm_id, data.contract); + } + } + let mut completed = client.completed_orders(false).await?; + while let Some(item) = completed.next().await { + if let SubscriptionItem::Data(Orders::OrderData(data)) = item? + && perm_ids.contains(&data.order.perm_id) + { + contracts.insert(data.order.perm_id, data.contract); + } + } + Ok::<_, anyhow::Error>(contracts) + }) + .await + .context("timed out reading IB combo orders")? + } +} diff --git a/crates/adapters/interactive_brokers/src/execution/spreads.rs b/crates/adapters/interactive_brokers/src/execution/spreads.rs new file mode 100644 index 000000000000..d46efdb70587 --- /dev/null +++ b/crates/adapters/interactive_brokers/src/execution/spreads.rs @@ -0,0 +1,316 @@ +// ------------------------------------------------------------------------------------------------- +// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. +// https://nautechsystems.io +// +// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html +// ------------------------------------------------------------------------------------------------- + +//! Interactive Brokers spread execution handling. + +use nautilus_common::live::sender::EventSender; + +use super::core::*; +use crate::{ + common::{enums::IbAction, spreads::parse_spread_instrument_id_to_legs}, + execution::parse, +}; + +const SPREAD_LEG_FILLS_TIMEOUT: Duration = Duration::from_secs(5); + +impl InteractiveBrokersExecutionClient { + // Returns the leg instrument of a new leg execution, or `None` for a duplicate + pub(super) async fn handle_spread_execution( + exec_data: &ExecutionData, + fill: &SpreadFillContext<'_>, + instrument_provider: &Arc, + exec_sender: &EventSender, + orders: &OrderTracker, + context: &TrackedOrder, + ) -> anyhow::Result> { + let trade_id = TradeId::new(&exec_data.execution.execution_id); + let fill_id = trade_id.to_string(); + + { + let mut state = orders.lock()?; + let Some(order) = state.order_mut(exec_data.execution.order_id) else { + anyhow::bail!( + "Tracked state not found for Interactive Brokers order {}", + exec_data.execution.order_id, + ); + }; + + if order.spread_fill_ids.contains(&fill_id) { + tracing::debug!( + "Fill {} already processed for spread order {}, skipping", + fill_id, + fill.client_order_id, + ); + return Ok(None); + } + + order.spread_fill_ids.insert(fill_id); + } + + // A leg execution carries the leg contract itself, so its instrument resolves as for a + // plain fill and must be one of the spread's legs + let leg_id = match instrument_provider + .get_instrument_id_by_contract_id(exec_data.contract.contract_id) + { + Some(leg_id) => leg_id, + None => Self::resolve_contract_instrument_id(instrument_provider, &exec_data.contract)?, + }; + let spread_legs = parse_spread_instrument_id_to_legs(&fill.spread_instrument_id)?; + anyhow::ensure!( + spread_legs.iter().any(|(id, _)| *id == leg_id), + "Execution instrument {leg_id} is not a leg of spread {}", + fill.spread_instrument_id + ); + + Self::generate_leg_fill( + exec_data, + fill, + context, + leg_id, + instrument_provider, + exec_sender, + )?; + + Ok(Some(leg_id)) + } + + pub(super) fn hold_spread_fill( + orders: &OrderTracker, + order_id: i32, + fill: OrderFilled, + exec_sender: &EventSender, + ) -> anyhow::Result<()> { + let released = { + let mut state = orders.lock()?; + let order = state.order_mut(order_id).with_context(|| { + format!("Tracked state not found for Interactive Brokers order {order_id}") + })?; + order + .spread_fills_held + .fills + .push_back((tokio::time::Instant::now(), fill)); + Self::release_spread_fills(&mut order.spread_fills_held)? + }; + Self::send_spread_fills(released, exec_sender) + } + + pub(super) fn record_spread_leg_fill( + orders: &OrderTracker, + order_id: i32, + leg_instrument_id: InstrumentId, + quantity: f64, + exec_sender: &EventSender, + ) -> anyhow::Result<()> { + let released = { + let mut state = orders.lock()?; + let order = state.order_mut(order_id).with_context(|| { + format!("Tracked state not found for Interactive Brokers order {order_id}") + })?; + *order + .spread_fills_held + .leg_quantities + .entry(leg_instrument_id) + .or_default() += quantity; + Self::release_spread_fills(&mut order.spread_fills_held)? + }; + Self::send_spread_fills(released, exec_sender) + } + + // Sends spread fills held longer than the timeout without their legs, so a lost leg + // execution cannot leave the spread order unfilled + pub(super) fn flush_spread_fills_without_legs( + orders: &OrderTracker, + exec_sender: &EventSender, + ) -> anyhow::Result<()> { + let released = { + let mut state = orders.lock()?; + let order_ids: Vec = state + .active_orders + .iter() + .chain(state.terminal_orders.iter()) + .filter(|(_, order)| !order.spread_fills_held.fills.is_empty()) + .map(|(order_id, _)| *order_id) + .collect(); + let mut released = Vec::new(); + + for order_id in order_ids { + let Some(order) = state.order_mut(order_id) else { + continue; + }; + let held = &mut order.spread_fills_held.fills; + + while held + .front() + .is_some_and(|(held_at, _)| held_at.elapsed() >= SPREAD_LEG_FILLS_TIMEOUT) + { + let (_, fill) = held.pop_front().expect("front checked above"); + tracing::warn!( + trade_id = %fill.trade_id, + "IB leg executions for spread fill did not arrive within 5 seconds; \ + emitting the spread fill", + ); + released.push(fill); + } + } + released + }; + Self::send_spread_fills(released, exec_sender) + } + + // Releases held spread fills in arrival order while the sent leg quantities cover them + fn release_spread_fills(held: &mut HeldSpreadFills) -> anyhow::Result> { + const QUANTITY_TOLERANCE: f64 = 1e-9; + let mut released = Vec::new(); + + while let Some((_, fill)) = held.fills.front() { + let required: Vec<(InstrumentId, f64)> = + parse_spread_instrument_id_to_legs(&fill.instrument_id)? + .into_iter() + .map(|(leg_id, ratio)| { + ( + leg_id, + fill.last_qty.as_f64() * f64::from(ratio.unsigned_abs()), + ) + }) + .collect(); + let covered = required.iter().all(|(leg_id, quantity)| { + held.leg_quantities.get(leg_id).copied().unwrap_or_default() + >= quantity - QUANTITY_TOLERANCE + }); + + if !covered { + break; + } + + for (leg_id, quantity) in required { + if let Some(sent) = held.leg_quantities.get_mut(&leg_id) { + *sent -= quantity; + } + } + held.leg_quantities + .retain(|_, quantity| *quantity > QUANTITY_TOLERANCE); + let (_, fill) = held.fills.pop_front().expect("front checked above"); + released.push(fill); + } + Ok(released) + } + + fn send_spread_fills( + fills: Vec, + exec_sender: &EventSender, + ) -> anyhow::Result<()> { + for fill in fills { + exec_sender.send(ExecutionEvent::Order(OrderEventAny::Filled(fill)))?; + } + Ok(()) + } + + // Leg fills are `OrderFilled` events so the engine applies them to leg positions without an + // order of their own; a report would be held for an order status no leg can have. + pub(super) fn generate_leg_fill( + exec_data: &ExecutionData, + fill: &SpreadFillContext<'_>, + context: &TrackedOrder, + leg_instrument_id: InstrumentId, + instrument_provider: &Arc, + exec_sender: &EventSender, + ) -> anyhow::Result<()> { + let leg_instrument = instrument_provider + .find(&leg_instrument_id) + .context("Leg instrument not found")?; + + let price_magnifier = instrument_provider.get_price_magnifier(&leg_instrument_id) as f64; + let execution_price = exec_data.execution.price * price_magnifier; + let leg_price = Price::new(execution_price, leg_instrument.price_precision()); + + let leg_quantity = + Quantity::new(exec_data.execution.shares, leg_instrument.size_precision()); + + let order_side = IbAction::from_str(exec_data.execution.side.as_str())?.order_side(); + + let commission_money = + Money::new(fill.commission, Currency::from(fill.commission_currency)); + + let leg_position = Self::get_leg_position(&fill.spread_instrument_id, &leg_instrument_id); + let leg_client_order_id = ClientOrderId::new(format!( + "{}-LEG-{}", + fill.client_order_id, leg_instrument_id.symbol + )); + let leg_trade_id = TradeId::new(format!( + "{}-{}", + exec_data.execution.execution_id, leg_position + )); + let venue_order_id = + parse::ib_venue_order_id(exec_data.execution.order_id, exec_data.execution.perm_id); + let leg_venue_order_id = + VenueOrderId::new(format!("{}-LEG-{}", venue_order_id.as_str(), leg_position)); + + let ts_event = parse_execution_time(&exec_data.execution.time)?; + + let event = OrderFilled::new( + context.trader_id, + context.strategy_id, + leg_instrument_id, + leg_client_order_id, + leg_venue_order_id, + fill.account_id, + leg_trade_id, + order_side, + context.order_type, + leg_quantity, + leg_price, + leg_instrument.quote_currency(), + LiquiditySide::NoLiquiditySide, + UUID4::new(), + ts_event, + fill.ts_init, + false, + None, + Some(commission_money), + None, + ); + exec_sender.send(ExecutionEvent::Order(OrderEventAny::Filled(event)))?; + + tracing::debug!( + "Generated leg fill: instrument_id={}, client_order_id={}, quantity={}, price={}", + leg_instrument_id, + leg_client_order_id, + leg_quantity, + leg_price + ); + + Ok(()) + } + + pub(super) fn get_leg_position( + spread_instrument_id: &InstrumentId, + leg_instrument_id: &InstrumentId, + ) -> usize { + let legs = match parse_spread_instrument_id_to_legs(spread_instrument_id) { + Ok(legs) => legs, + Err(e) => { + log::warn!( + "Failed to parse spread instrument ID {spread_instrument_id} for leg position: {e}" + ); + return 0; + } + }; + + for (idx, (parsed_leg_id, _)) in legs.iter().enumerate() { + if *parsed_leg_id == *leg_instrument_id { + return idx; + } + } + + log::warn!( + "Leg instrument ID {leg_instrument_id} not found in spread instrument ID {spread_instrument_id}" + ); + 0 + } +} diff --git a/crates/adapters/interactive_brokers/src/execution/transform.rs b/crates/adapters/interactive_brokers/src/execution/transform.rs index 4e3f9422cec8..985c126862c4 100644 --- a/crates/adapters/interactive_brokers/src/execution/transform.rs +++ b/crates/adapters/interactive_brokers/src/execution/transform.rs @@ -15,10 +15,7 @@ //! Order transformation utilities for converting Nautilus orders to IB orders. -use ibapi::{ - contracts::Contract, - orders::{Action, Order as IBOrder, TimeInForce}, -}; +use ibapi::orders::{Action, Order as IBOrder, TimeInForce}; use nautilus_core::UnixNanos; use nautilus_model::{ enums::{ @@ -38,8 +35,8 @@ mod tags; use self::{ policy::{ - apply_account_policy, apply_display_quantity_policy, apply_expire_time_policy, - apply_order_list_policy, apply_quantity_policy, apply_trailing_order_policy, + apply_display_quantity_policy, apply_expire_time_policy, apply_quantity_policy, + apply_trailing_order_policy, }, tags::apply_ib_order_tags, }; @@ -51,7 +48,6 @@ use self::{ /// Returns an error if the transformation fails. pub fn nautilus_order_to_ib_order( order: &OrderAny, - _contract: &Contract, instrument_provider: &InteractiveBrokersInstrumentProvider, order_id: i32, order_ref: &str, @@ -71,7 +67,7 @@ pub fn nautilus_order_to_ib_order( order.trigger_price(), price_magnifier, ); - let tif = transform_time_in_force(order.time_in_force(), order.expire_time()); + let tif = transform_time_in_force(order.time_in_force()); let mut ib_order = IBOrder { order_id, @@ -87,18 +83,11 @@ pub fn nautilus_order_to_ib_order( }; apply_expire_time_policy(&mut ib_order, order); - apply_account_policy(&mut ib_order, order); apply_quantity_policy(&mut ib_order, order, instrument_provider)?; apply_trailing_order_policy(&mut ib_order, order, price_magnifier)?; apply_display_quantity_policy(&mut ib_order, order); - // Note: Parent ID in Nautilus is ClientOrderId, but IB expects order_id. - // Parent order ID mapping requires client_order_id -> IB order_id tracking, - // which is handled at the execution client layer. - let _parent_order_id = order.parent_order_id(); - apply_ib_order_tags(&mut ib_order, order.tags())?; - apply_order_list_policy(&mut ib_order, order); Ok(ib_order) } @@ -130,10 +119,7 @@ fn transform_order_type( } /// Transform Nautilus time in force to IB time in force. -fn transform_time_in_force( - tif: NautilusTimeInForce, - _expire_time: Option, -) -> TimeInForce { +fn transform_time_in_force(tif: NautilusTimeInForce) -> TimeInForce { IbTimeInForce::from_nautilus(tif).ibapi_time_in_force() } @@ -169,12 +155,24 @@ pub(super) fn trigger_type_to_ib_trigger_method( value.ibapi_trigger_method() } +pub(super) const fn ib_trigger_method_to_trigger_type( + trigger_method: ibapi::orders::conditions::TriggerMethod, +) -> TriggerType { + match trigger_method { + ibapi::orders::conditions::TriggerMethod::Default => TriggerType::Default, + ibapi::orders::conditions::TriggerMethod::DoubleBidAsk => TriggerType::DoubleBidAsk, + ibapi::orders::conditions::TriggerMethod::Last => TriggerType::LastPrice, + ibapi::orders::conditions::TriggerMethod::DoubleLast => TriggerType::DoubleLast, + ibapi::orders::conditions::TriggerMethod::BidAsk => TriggerType::BidAsk, + ibapi::orders::conditions::TriggerMethod::LastOrBidAsk => TriggerType::LastOrBidAsk, + ibapi::orders::conditions::TriggerMethod::Midpoint => TriggerType::MidPoint, + ibapi::orders::conditions::TriggerMethod::Unknown(_) => TriggerType::Default, + } +} + #[cfg(test)] mod tests { - use ibapi::{ - contracts::{Contract, Currency, Exchange, SecurityType, Symbol}, - orders::OrderCondition, - }; + use ibapi::orders::OrderCondition; use nautilus_model::{ enums::{OrderSide, OrderType, TimeInForce as NautilusTimeInForce, TrailingOffsetType}, identifiers::{InstrumentId, OrderListId, Symbol as NautilusSymbol, Venue}, @@ -191,7 +189,7 @@ mod tests { fn create_test_order_with_tags(tags_json: &str) -> OrderAny { let instrument_id = InstrumentId::new(NautilusSymbol::from("AAPL"), Venue::from("NASDAQ")); - let tag = Ustr::from(&format!("IBOrderTags:{}", tags_json)); + let tag = Ustr::from(&format!("IBOrderTags:{tags_json}")); OrderTestBuilder::new(OrderType::Limit) .instrument_id(instrument_id) .side(OrderSide::Buy) @@ -205,18 +203,10 @@ mod tests { fn test_active_start_time_encoding() { let tags_json = r#"{"activeStartTime": "20250101 09:30:00 UTC"}"#; let order = create_test_order_with_tags(tags_json); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let config = InteractiveBrokersInstrumentProviderConfig::default(); let provider = InteractiveBrokersInstrumentProvider::new(config); - let result = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001"); + let result = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001"); assert!(result.is_ok()); let ib_order = result.unwrap(); @@ -227,18 +217,10 @@ mod tests { fn test_active_stop_time_encoding() { let tags_json = r#"{"activeStopTime": "20250101 16:00:00 UTC"}"#; let order = create_test_order_with_tags(tags_json); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let config = InteractiveBrokersInstrumentProviderConfig::default(); let provider = InteractiveBrokersInstrumentProvider::new(config); - let result = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001"); + let result = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001"); assert!(result.is_ok()); let ib_order = result.unwrap(); @@ -249,18 +231,10 @@ mod tests { fn test_both_active_times_encoding() { let tags_json = r#"{"activeStartTime": "20250101 09:30:00 UTC", "activeStopTime": "20250101 16:00:00 UTC"}"#; let order = create_test_order_with_tags(tags_json); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let config = InteractiveBrokersInstrumentProviderConfig::default(); let provider = InteractiveBrokersInstrumentProvider::new(config); - let result = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001"); + let result = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001"); assert!(result.is_ok()); let ib_order = result.unwrap(); @@ -279,19 +253,11 @@ mod tests { .quantity(Quantity::from(100)) .time_in_force(NautilusTimeInForce::AtTheOpen) .build(); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let ib_order = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001") + let ib_order = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001") .expect("order transform should succeed"); assert_eq!(ib_order.tif, TimeInForce::OnOpen); @@ -301,42 +267,42 @@ mod tests { fn test_tags_apply_market_on_open_alias() { let tags_json = r#"{"orderType":"MarketOnOpen"}"#; let order = create_test_order_with_tags(tags_json); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let ib_order = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001") + let ib_order = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001") .expect("order transform should succeed"); assert_eq!(ib_order.order_type, "MKT"); assert_eq!(ib_order.tif, TimeInForce::OnOpen); } + #[rstest] + #[case::gtd(r#"{"timeInForce":"GTD"}"#, TimeInForce::GoodTillDate)] + #[case::lowercase(r#"{"tif":"ioc"}"#, TimeInForce::ImmediateOrCancel)] + #[case::snake_key(r#"{"time_in_force":"DTC"}"#, TimeInForce::DayTillCanceled)] + fn test_tags_apply_time_in_force(#[case] tags_json: &str, #[case] expected: TimeInForce) { + let order = create_test_order_with_tags(tags_json); + let provider = InteractiveBrokersInstrumentProvider::new( + InteractiveBrokersInstrumentProviderConfig::default(), + ); + + let ib_order = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001") + .expect("order transform should succeed"); + + assert_eq!(ib_order.tif, expected); + } + #[rstest] fn test_tags_apply_at_auction_alias() { let tags_json = r#"{"orderType":"AtAuction","limitPrice":150.0}"#; let order = create_test_order_with_tags(tags_json); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let ib_order = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001") + let ib_order = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001") .expect("order transform should succeed"); assert_eq!(ib_order.order_type, "MTL"); @@ -348,7 +314,6 @@ mod tests { fn test_tags_apply_auction_limit_fields() { let tags_json = r#"{ "orderType": "AuctionLimit", - "auctionStrategy": "Improvement", "startingPrice": 1.25, "stockRefPrice": 150.25, "delta": 0.5, @@ -356,26 +321,14 @@ mod tests { "stockRangeUpper": 155.0 }"#; let order = create_test_order_with_tags(tags_json); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let ib_order = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001") + let ib_order = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001") .expect("order transform should succeed"); assert_eq!(ib_order.order_type, "LMT"); - assert_eq!( - ib_order.auction_strategy, - Some(ibapi::orders::AuctionStrategy::Improvement) - ); assert_eq!(ib_order.starting_price, Some(1.25)); assert_eq!(ib_order.stock_ref_price, Some(150.25)); assert_eq!(ib_order.delta, Some(0.5)); @@ -387,19 +340,11 @@ mod tests { fn test_tags_apply_auction_relative_fields() { let tags_json = r#"{"orderType":"AuctionRelative","auxPrice":0.01}"#; let order = create_test_order_with_tags(tags_json); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let ib_order = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001") + let ib_order = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001") .expect("order transform should succeed"); assert_eq!(ib_order.order_type, "REL"); @@ -454,19 +399,11 @@ mod tests { "softDollarTier": {"name": "tier", "value": "val", "display_name": "display"} }"#; let order = create_test_order_with_tags(tags_json); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let ib_order = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001") + let ib_order = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001") .expect("order transform should succeed"); assert_eq!(ib_order.display_size, Some(25)); @@ -541,19 +478,11 @@ mod tests { fn test_invalid_tag_set_rejects_order_transform() { let tags_json = r#"{"whatIf": true, "displaySize": "invalid"}"#; let order = create_test_order_with_tags(tags_json); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let result = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001"); + let result = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001"); assert!(result.is_err()); assert!( @@ -568,19 +497,11 @@ mod tests { fn test_non_utc_datetime_tag_rejects_order_transform() { let tags_json = r#"{"activeStartTime": "20250101 09:30:00 EST"}"#; let order = create_test_order_with_tags(tags_json); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let result = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001"); + let result = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001"); assert!(result.is_err()); assert!( @@ -609,22 +530,14 @@ mod tests { .time_in_force(NautilusTimeInForce::Gtd) .expire_time(expire_time) .build(); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let ib_order = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001") + let ib_order = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001") .expect("order transform should succeed"); - assert_eq!(ib_order.tif, TimeInForce::GoodTilDate); + assert_eq!(ib_order.tif, TimeInForce::GoodTillDate); assert_eq!(ib_order.good_till_date, "20250115 14:30:00 UTC"); } @@ -641,19 +554,11 @@ mod tests { .trailing_offset(dec!(0.5)) .trailing_offset_type(TrailingOffsetType::Price) .build(); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let ib_order = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001") + let ib_order = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001") .expect("order transform should succeed"); assert_eq!(ib_order.aux_price, Some(0.5)); @@ -674,19 +579,11 @@ mod tests { .trailing_offset(dec!(25)) .trailing_offset_type(TrailingOffsetType::BasisPoints) .build(); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let ib_order = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001") + let ib_order = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001") .expect("order transform should succeed"); assert_eq!(ib_order.aux_price, None); @@ -707,19 +604,11 @@ mod tests { .trailing_offset(dec!(5)) .trailing_offset_type(TrailingOffsetType::Ticks) .build(); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let err = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001") + let err = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001") .expect_err("unsupported trailing offset type should fail"); assert_eq!( @@ -753,19 +642,11 @@ mod tests { ] }"#; let order = create_test_order_with_tags(tags_json); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let ib_order = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001") + let ib_order = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001") .expect("order transform should succeed"); assert!(ib_order.outside_rth); @@ -805,19 +686,11 @@ mod tests { .price(Price::from("150.00")) .order_list_id(OrderListId::from("OL-001")) .build(); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let ib_order = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001") + let ib_order = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001") .expect("order transform should succeed"); assert!(ib_order.oca_group.is_empty()); @@ -838,19 +711,11 @@ mod tests { r#"IBOrderTags:{"ocaGroup":"CUSTOM-GROUP","ocaType":1}"#, )]) .build(); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; let provider = InteractiveBrokersInstrumentProvider::new( InteractiveBrokersInstrumentProviderConfig::default(), ); - let ib_order = nautilus_order_to_ib_order(&order, &contract, &provider, 1, "TEST-001") + let ib_order = nautilus_order_to_ib_order(&order, &provider, 1, "TEST-001") .expect("order transform should succeed"); assert_eq!(ib_order.oca_group, "CUSTOM-GROUP"); diff --git a/crates/adapters/interactive_brokers/src/execution/transform/policy.rs b/crates/adapters/interactive_brokers/src/execution/transform/policy.rs index 66131d0e458b..bd9760784cc9 100644 --- a/crates/adapters/interactive_brokers/src/execution/transform/policy.rs +++ b/crates/adapters/interactive_brokers/src/execution/transform/policy.rs @@ -13,6 +13,8 @@ // limitations under the License. // ------------------------------------------------------------------------------------------------- +//! Interactive Brokers order transformation policies. + use ibapi::orders::{Order as IBOrder, TimeInForce}; use nautilus_model::{ enums::{OrderSide, OrderType as NautilusOrderType, TrailingOffsetType}, @@ -24,19 +26,13 @@ use super::{convert_price, format_ib_datetime, trigger_type_to_ib_trigger_method use crate::providers::instruments::InteractiveBrokersInstrumentProvider; pub(super) fn apply_expire_time_policy(ib_order: &mut IBOrder, order: &OrderAny) { - if matches!(ib_order.tif, TimeInForce::GoodTilDate) + if matches!(ib_order.tif, TimeInForce::GoodTillDate) && let Some(expire) = order.expire_time() { ib_order.good_till_date = format_ib_datetime(expire); } } -pub(super) fn apply_account_policy(ib_order: &mut IBOrder, order: &OrderAny) { - if let Some(account_id) = order.account_id() { - ib_order.account = account_id.to_string(); - } -} - pub(super) fn apply_quantity_policy( ib_order: &mut IBOrder, order: &OrderAny, @@ -84,7 +80,7 @@ pub(super) fn apply_trailing_order_policy( Some(TrailingOffsetType::Price) | None => { ib_order.aux_price = Some(trailing_offset_f64); } - Some(other) => anyhow::bail!("`TrailingOffsetType` {:?} is not supported", other), + Some(other) => anyhow::bail!("`TrailingOffsetType` {other:?} is not supported"), } } @@ -105,8 +101,3 @@ pub(super) fn apply_display_quantity_policy(ib_order: &mut IBOrder, order: &Orde ib_order.display_size = Some(display_qty.as_f64() as i32); } } - -pub(super) fn apply_order_list_policy(_ib_order: &mut IBOrder, _order: &OrderAny) { - // Order lists only control parent/transmit behavior at the execution client layer. - // IB OCA groups must be requested explicitly through IB order tags. -} diff --git a/crates/adapters/interactive_brokers/src/execution/transform/tags.rs b/crates/adapters/interactive_brokers/src/execution/transform/tags.rs index f6706cb5b1a3..f70141acfacf 100644 --- a/crates/adapters/interactive_brokers/src/execution/transform/tags.rs +++ b/crates/adapters/interactive_brokers/src/execution/transform/tags.rs @@ -13,6 +13,8 @@ // limitations under the License. // ------------------------------------------------------------------------------------------------- +//! Interactive Brokers order tag parsing and application. + use std::str::FromStr; use anyhow::Context; @@ -195,11 +197,6 @@ fn normalize_order_tag_update(field: &str, value: &Value) -> Option normalize_i32_enum_value( - "auction_strategy", - value, - &[(1, "Match"), (2, "Improvement"), (3, "Transparent")], - ), "volatility_type" => { normalize_i32_enum_value("volatility_type", value, &[(1, "Daily"), (2, "Annual")]) } @@ -294,18 +291,7 @@ fn normalize_tif_value(value: &Value) -> Option { } fn ibapi_tif_serde_value(tif: IbTimeInForce) -> Value { - let value = match tif { - IbTimeInForce::Day => "Day", - IbTimeInForce::GoodTilCanceled => "GoodTilCanceled", - IbTimeInForce::ImmediateOrCancel => "ImmediateOrCancel", - IbTimeInForce::GoodTilDate => "GoodTilDate", - IbTimeInForce::OnOpen => "OnOpen", - IbTimeInForce::FillOrKill => "FillOrKill", - IbTimeInForce::DayTilCanceled => "DayTilCanceled", - IbTimeInForce::Auction => "Auction", - }; - - Value::String(value.to_string()) + Value::String(tif.ibapi_time_in_force().as_str().to_string()) } fn normalize_i32_enum_value( diff --git a/crates/adapters/interactive_brokers/src/execution/updates.rs b/crates/adapters/interactive_brokers/src/execution/updates.rs new file mode 100644 index 000000000000..39771cc4ca30 --- /dev/null +++ b/crates/adapters/interactive_brokers/src/execution/updates.rs @@ -0,0 +1,2279 @@ +// ------------------------------------------------------------------------------------------------- +// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. +// https://nautechsystems.io +// +// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html +// ------------------------------------------------------------------------------------------------- + +//! Interactive Brokers order and execution update handling. + +use nautilus_common::live::sender::EventSender; + +use super::{core::*, incarnations::duplicate_client_order_id}; +use crate::{ + common::enums::{IbOrderStatus, IbOrderType}, + execution::parse, +}; + +const COMMISSION_REPORT_TIMEOUT: Duration = Duration::from_secs(5); + +/// Longest a fill for an untracked order waits for the order details. It stays below the live +/// node's default `position_check_threshold_ms` of 5 seconds, so the continuous position check +/// cannot dispatch the held fill as an orderless fill first. +const FILL_HOLD_TIMEOUT: Duration = Duration::from_secs(3); + +/// Venue access for notice handling spawned from the update and notice streams. +pub(super) struct NoticeVenueContext { + pub(super) client: Arc, + pub(super) request_timeout_secs: u64, + pub(super) cancellation: CancellationToken, + pub(super) tasks: TaskSpawner, +} + +impl InteractiveBrokersExecutionClient { + fn execution_in_account(exec_data: &ExecutionData, ib_account: Ustr) -> anyhow::Result { + anyhow::ensure!( + !exec_data.execution.account_number.is_empty(), + "IB execution {} has no account identity", + exec_data.execution.execution_id + ); + Ok(exec_data.execution.account_number == ib_account) + } + + fn execution_context( + exec_data: &ExecutionData, + orders: &OrderTracker, + provider: &Arc, + account_id: AccountId, + ) -> anyhow::Result<(i32, Option, Option)> { + anyhow::ensure!( + exec_data.execution.shares.is_finite() + && exec_data.execution.shares > 0.0 + && exec_data.execution.price.is_finite(), + "IB execution {} has invalid price or quantity", + exec_data.execution.execution_id + ); + let correlation = orders.lock()?.correlate( + exec_data.execution.client_id, + exec_data.execution.order_id, + exec_data.execution.perm_id, + &exec_data.execution.order_reference, + )?; + let context = match &correlation { + OrderCorrelation::Tracked { context, .. } + | OrderCorrelation::Duplicate { context, .. } => Some(context), + OrderCorrelation::Untracked { .. } => None, + }; + + if let Some(context) = context { + let instrument = provider.find(&context.instrument_id).with_context(|| { + format!( + "IB execution instrument {} is unavailable", + context.instrument_id + ) + })?; + + if !instrument.is_spread() { + let contract = provider.resolve_contract_for_instrument(context.instrument_id)?; + anyhow::ensure!( + contract.contract_id == 0 + || exec_data.contract.contract_id == 0 + || contract.contract_id == exec_data.contract.contract_id, + "IB execution {} conflicts with its tracked contract", + exec_data.execution.execution_id + ); + let expected_side = if context.order_side == OrderSide::Buy { + "BOT" + } else { + "SLD" + }; + anyhow::ensure!( + matches!(&correlation, OrderCorrelation::Duplicate { .. }) + || exec_data.execution.side.as_str() == expected_side, + "IB execution {} conflicts with its tracked side", + exec_data.execution.execution_id + ); + } + } + + match correlation { + OrderCorrelation::Tracked { order_id, context } => { + if exec_data.execution.perm_id > 0 { + if let Some(order) = orders.lock()?.order_mut(order_id) { + if order.perm_id == 0 { + order.perm_id = exec_data.execution.perm_id; + } + } + } + let client_order_id = context.client_order_id; + Ok((order_id, Some(context), Some(client_order_id))) + } + OrderCorrelation::Untracked { client_order_id } => { + Ok((exec_data.execution.order_id, None, client_order_id)) + } + OrderCorrelation::Duplicate { order_id, context } => { + orders.lock()?.record_incarnation( + order_id, + &context, + account_id, + exec_data.execution.perm_id, + None, + false, + ); + let client_order_id = + duplicate_client_order_id(account_id, exec_data.execution.perm_id)?; + Ok((order_id, None, Some(client_order_id))) + } + } + } + + /// Starts the order update subscription stream. + /// + /// # Errors + /// + /// Returns an error if starting the subscription fails. + pub(super) async fn start_order_updates(&self) -> anyhow::Result<()> { + let client = self.ib_client.as_ref().context("IB client not connected")?; + + let timeout_dur = Duration::from_secs(self.config.request_timeout); + log::debug!( + "Starting IB order update stream subscription (timeout={:?}, client_id={}, account_id={})", + timeout_dur, + self.client_id(), + self.account_id() + ); + let mut subscription = tokio::time::timeout(timeout_dur, client.order_update_stream()) + .await + .context("Timeout starting order update stream")??; + + let orders = self.orders.clone(); + let instrument_provider = Arc::clone(&self.instrument_provider); + let exec_sender = get_exec_event_sender(); + let clock = get_atomic_clock_realtime(); + let account_id = self.core.account_id; + let ib_account = self.ib_account; + let commission_cache = Arc::clone(&self.commission_cache); + let pending_execution_cache = Arc::clone(&self.pending_execution_cache); + let position_tracker = Arc::clone(&self.position_tracker); + let is_connected = Arc::clone(&self.is_connected); + let tasks = self + .session_tasks + .spawner() + .context("failed to acquire IB execution session task spawner")?; + let cancellation_token = tasks.cancellation_token(); + let venue_ctx = NoticeVenueContext { + client: client.as_arc().clone(), + request_timeout_secs: self.config.request_timeout, + cancellation: tasks.cancellation_token(), + tasks: tasks.clone(), + }; + + let future = async move { + tokio::select! { + () = Self::process_order_update_stream( + &mut subscription, + &orders, + &instrument_provider, + &exec_sender, + clock, + account_id, + ib_account, + &commission_cache, + &pending_execution_cache, + &position_tracker, + Some(&venue_ctx), + ) => { + tracing::error!( + "IB order update stream ended; execution client is disconnected" + ); + is_connected.store(false, Ordering::Relaxed); + } + () = cancellation_token.cancelled() => {} + } + }; + + tasks + .spawn(future) + .context("failed to register IB order update task")?; + + self.start_global_notices()?; + + log::debug!("IB order update stream subscription started"); + + Ok(()) + } + + fn start_global_notices(&self) -> anyhow::Result<()> { + let client = self.ib_client.as_ref().context("IB client not connected")?; + let mut notices = client + .notice_stream() + .context("Failed to subscribe to IB global notice stream")?; + let exec_sender = get_exec_event_sender(); + let clock = get_atomic_clock_realtime(); + let account_id = self.core.account_id; + let orders = self.orders.clone(); + let is_connected = Arc::clone(&self.is_connected); + let tasks = self + .session_tasks + .spawner() + .context("failed to acquire IB execution session task spawner")?; + let cancellation_token = tasks.cancellation_token(); + let venue_ctx = NoticeVenueContext { + client: client.as_arc().clone(), + request_timeout_secs: self.config.request_timeout, + cancellation: tasks.cancellation_token(), + tasks: tasks.clone(), + }; + + let future = async move { + loop { + tokio::select! { + () = cancellation_token.cancelled() => return, + notice = notices.next() => { + let Some(notice) = notice else { + tracing::warn!( + "IB global notice stream ended; execution client is disconnected" + ); + is_connected.store(false, Ordering::Relaxed); + return; + }; + + if let Err(e) = Self::handle_order_notice( + ¬ice, + &orders, + &exec_sender, + clock.get_time_ns(), + account_id, + Some(&venue_ctx), + ) { + tracing::error!("Error handling global IB notice: {e}"); + } + } + } + } + }; + + tasks + .spawn(future) + .context("failed to register IB global notice task")?; + Ok(()) + } + + #[allow(clippy::too_many_arguments)] // The stream task owns explicit shared client state. + pub(super) async fn process_order_update_stream( + subscription: &mut S, + orders: &OrderTracker, + instrument_provider: &Arc, + exec_sender: &EventSender, + clock: &'static AtomicTime, + account_id: AccountId, + ib_account: Ustr, + commission_cache: &Arc>, + pending_execution_cache: &Arc>, + position_tracker: &PositionTracker, + venue_ctx: Option<&NoticeVenueContext>, + ) where + S: futures_util::Stream, ibapi::Error>> + Unpin, + { + let mut commission_timeout = tokio::time::interval_at( + tokio::time::Instant::now() + Duration::from_secs(1), + Duration::from_secs(1), + ); + + loop { + tokio::select! { + update_result = subscription.next() => { + let Some(update_result) = update_result else { + break; + }; + + match update_result { + Ok(SubscriptionItem::Data(update)) => { + if let Err(e) = Self::handle_order_update( + &update, + orders, + instrument_provider, + exec_sender, + clock, + account_id, + ib_account, + commission_cache, + pending_execution_cache, + position_tracker, + venue_ctx, + ) + .await + { + tracing::error!("Error handling order update: {e}"); + } + } + Ok(SubscriptionItem::Notice(notice)) => { + if let Err(e) = Self::handle_order_notice( + ¬ice, + orders, + exec_sender, + clock.get_time_ns(), + account_id, + venue_ctx, + ) { + tracing::error!("Error handling IB order update notice: {e}"); + } + } + Err(e) => { + tracing::error!("Error receiving order update: {e}"); + } + } + } + _ = commission_timeout.tick() => { + if let Err(e) = Self::flush_executions_without_commission( + orders, + instrument_provider, + exec_sender, + clock, + account_id, + ib_account, + commission_cache, + pending_execution_cache, + position_tracker, + venue_ctx, + ) + .await + { + tracing::error!("Error flushing IB executions without commission: {e}"); + } + + if let Err(e) = Self::flush_spread_fills_without_legs(orders, exec_sender) { + tracing::error!("Error flushing IB spread fills without legs: {e}"); + } + } + } + } + } + + #[allow(clippy::too_many_arguments)] // Timeout processing shares explicit client state. + pub(super) async fn flush_executions_without_commission( + orders: &OrderTracker, + instrument_provider: &Arc, + exec_sender: &EventSender, + clock: &'static AtomicTime, + account_id: AccountId, + ib_account: Ustr, + commission_cache: &Arc>, + pending_execution_cache: &Arc>, + position_tracker: &PositionTracker, + venue_ctx: Option<&NoticeVenueContext>, + ) -> anyhow::Result<()> { + let pending = pending_execution_cache.lock().drain(); + + for (execution_id, (received_at, exec_data)) in pending { + if received_at.elapsed() < COMMISSION_REPORT_TIMEOUT { + pending_execution_cache + .lock() + .insert(execution_id, (received_at, exec_data)); + continue; + } + + let commission_currency = instrument_provider + .get_instrument_id_by_contract_id(exec_data.contract.contract_id) + .and_then(|instrument_id| instrument_provider.find(&instrument_id)) + .map_or_else( + || exec_data.contract.currency.to_string(), + |instrument| instrument.quote_currency().code.to_string(), + ); + + if commission_currency.is_empty() { + // Keep the execution pending so a late commissionReport or instrument load can + // still resolve it; aborting here would drop the other drained entries. + tracing::error!( + execution_id, + "Commission currency unavailable for IB execution; keeping the fill pending", + ); + pending_execution_cache + .lock() + .insert(execution_id, (received_at, exec_data)); + continue; + } + + tracing::warn!( + execution_id, + "IB commissionReport did not arrive within 5 seconds; emitting with zero commission", + ); + commission_cache + .lock() + .insert(execution_id, (0.0, commission_currency)); + + if let Err(e) = Self::handle_execution_data( + &exec_data, + orders, + instrument_provider, + exec_sender, + clock.get_time_ns(), + account_id, + ib_account, + commission_cache, + position_tracker, + venue_ctx, + ) + .await + { + tracing::error!( + execution_id = exec_data.execution.execution_id, + "Failed to emit IB execution after commission timeout: {e}", + ); + } + } + + Ok(()) + } + + pub(super) fn handle_order_notice( + notice: &Notice, + orders: &OrderTracker, + exec_sender: &EventSender, + ts_init: UnixNanos, + account_id: AccountId, + venue_ctx: Option<&NoticeVenueContext>, + ) -> anyhow::Result<()> { + match notice.category() { + NoticeCategory::Cancellation => { + tracing::debug!("Received IB order cancellation notice: {notice}"); + return Ok(()); + } + NoticeCategory::Warning + | NoticeCategory::SystemMessage + | NoticeCategory::DataAdvisory => { + tracing::warn!("Received IB order notice: {notice}"); + return Ok(()); + } + NoticeCategory::OrderRejection | NoticeCategory::Error => {} + _ => { + tracing::warn!("Received unclassified IB order notice: {notice}"); + return Ok(()); + } + } + + let Some(order_id) = notice.request_id else { + if notice.category() == NoticeCategory::OrderRejection { + tracing::error!("Received request-less IB order rejection: {notice}"); + } else { + tracing::error!("Received IB error notice: {notice}"); + } + return Ok(()); + }; + let order = { + let state = orders.lock()?; + state.active_orders.get(&order_id).cloned() + }; + let Some(order) = order else { + // The transport forwards every request-scoped error to the order update + // stream; ids that do not resolve to a tracked order belong to data or + // historical requests. + if notice.category() == NoticeCategory::OrderRejection { + tracing::warn!( + "Received IB rejection for unknown order ID {}: {}", + order_id, + notice + ); + } else { + tracing::debug!( + "Ignoring IB error notice for untracked request ID {}: {}", + order_id, + notice + ); + } + return Ok(()); + }; + let client_order_id = order.client_order_id; + + if order.accepted { + return Self::handle_accepted_order_notice( + notice, + &order, + order_id, + orders, + exec_sender, + ts_init, + account_id, + venue_ctx, + ); + } + + // ibapi classifies every code outside its informational ranges as `Error`, including + // notices for orders IB keeps working (10349 sets the TIF from an order preset), so + // only an order IB lists nowhere is rejected + if notice.category() == NoticeCategory::Error + && let Some(venue_ctx) = venue_ctx + { + tracing::warn!( + "IB notice for submitted order {client_order_id}; checking whether IB lists it: {notice}" + ); + Self::spawn_order_presence_check( + venue_ctx, + order_id, + client_order_id, + Some(Ustr::from(notice.message.as_str())), + orders.clone(), + exec_sender.clone(), + account_id, + ); + return Ok(()); + } + + Self::emit_order_rejected( + &order, + account_id, + notice.message.as_str(), + ts_init, + exec_sender, + )?; + Self::evict_terminal_order_state(client_order_id, order_id, orders)?; + + tracing::warn!("Order {} rejected: {}", client_order_id, notice.message); + Ok(()) + } + + #[allow(clippy::too_many_arguments)] // Notice resolution preserves the stream context. + fn handle_accepted_order_notice( + notice: &Notice, + order: &TrackedOrder, + order_id: i32, + orders: &OrderTracker, + exec_sender: &EventSender, + ts_init: UnixNanos, + account_id: AccountId, + venue_ctx: Option<&NoticeVenueContext>, + ) -> anyhow::Result<()> { + let client_order_id = order.client_order_id; + + // A venue-working order stays working: IB delivers 201-class codes for + // rejected modifications and error codes that do not prove the order died. + if Self::emit_modify_rejected_if_pending( + order, + order_id, + notice.message.as_str(), + orders, + exec_sender, + ts_init, + account_id, + )? { + return Ok(()); + } + + // IB refuses a cancel of an order it no longer considers cancellable (161, 10148) + // with an order notice; the presence check below still resolves an order that is gone + let cancel_perm_id = { + let mut state = orders.lock()?; + state.active_orders.get_mut(&order_id).and_then(|order| { + std::mem::replace(&mut order.pending_cancel, false).then_some(order.perm_id) + }) + }; + + if let Some(perm_id) = cancel_perm_id { + let event = OrderCancelRejected::new( + order.trader_id, + order.strategy_id, + order.instrument_id, + client_order_id, + Ustr::from(notice.message.as_str()), + UUID4::new(), + ts_init, + ts_init, + false, + Some(ib_venue_order_id(order_id, perm_id)), + Some(account_id), + ); + exec_sender + .send(ExecutionEvent::Order(OrderEventAny::CancelRejected(event))) + .map_err(|e| anyhow::anyhow!("Failed to send order cancel rejected event: {e}"))?; + tracing::warn!( + "Cancel of order {client_order_id} rejected: {}", + notice.message + ); + } else { + tracing::warn!( + "IB notice for accepted order {} left non-terminal (venue state uncertain): {}", + client_order_id, + notice + ); + } + + if let Some(venue_ctx) = venue_ctx { + Self::spawn_order_presence_check( + venue_ctx, + order_id, + client_order_id, + None, + orders.clone(), + exec_sender.clone(), + account_id, + ); + } + Ok(()) + } + + // Rejects the pending modify of an accepted order, returning whether one was pending. + fn emit_modify_rejected_if_pending( + order: &TrackedOrder, + order_id: i32, + reason: &str, + orders: &OrderTracker, + exec_sender: &EventSender, + ts_init: UnixNanos, + account_id: AccountId, + ) -> anyhow::Result { + let (was_pending_modify, perm_id) = { + let mut state = orders.lock()?; + state + .active_orders + .get_mut(&order_id) + .map_or((false, 0), |order| { + (order.pending_modify.take().is_some(), order.perm_id) + }) + }; + + if !was_pending_modify { + return Ok(false); + } + + let event = OrderModifyRejected::new( + order.trader_id, + order.strategy_id, + order.instrument_id, + order.client_order_id, + Ustr::from(reason), + UUID4::new(), + ts_init, + ts_init, + false, + Some(ib_venue_order_id(order_id, perm_id)), + Some(account_id), + ); + exec_sender + .send(ExecutionEvent::Order(OrderEventAny::ModifyRejected(event))) + .map_err(|e| anyhow::anyhow!("Failed to send order modify rejected event: {e}"))?; + tracing::warn!( + "Modify of order {} rejected: {}", + order.client_order_id, + reason + ); + Ok(true) + } + + fn spawn_order_presence_check( + venue_ctx: &NoticeVenueContext, + ib_order_id: i32, + client_order_id: ClientOrderId, + rejection_reason: Option, + orders: OrderTracker, + exec_sender: EventSender, + account_id: AccountId, + ) { + let client = Arc::clone(&venue_ctx.client); + let timeout_dur = Duration::from_secs(venue_ctx.request_timeout_secs); + let cancellation = venue_ctx.cancellation.child_token(); + + let future = async move { + tokio::select! { + () = Self::check_order_presence( + &client, + ib_order_id, + client_order_id, + rejection_reason, + timeout_dur, + &orders, + &exec_sender, + account_id, + ) => {} + () = cancellation.cancelled() => {} + } + }; + + if let Err(e) = venue_ctx.tasks.spawn(future) { + tracing::warn!("Skipping IB order presence check after shutdown began: {e}"); + } + } + + #[allow(clippy::too_many_arguments)] // Venue resolution shares the notice context. + async fn check_order_presence( + client: &Arc, + ib_order_id: i32, + client_order_id: ClientOrderId, + rejection_reason: Option, + timeout_dur: Duration, + orders: &OrderTracker, + exec_sender: &EventSender, + account_id: AccountId, + ) { + // Only a complete answer proves absence: ibapi ends a stream after an error, and a + // stream cut short must leave the order working rather than reject it + match Self::find_open_order(client, ib_order_id, timeout_dur).await { + Ok(true) => { + tracing::debug!("Order {client_order_id} remains open at the venue after notice"); + return; + } + Ok(false) => {} + Err(e) => { + tracing::warn!( + "Presence check for {client_order_id} could not read open orders, leaving it working: {e:#}" + ); + return; + } + } + + Self::resolve_absent_order( + client, + ib_order_id, + client_order_id, + rejection_reason, + timeout_dur, + orders, + exec_sender, + account_id, + ) + .await; + } + + // Queries completed orders for the terminal status of an order that is absent + // from open orders after a venue notice, and emits the inferred terminal event + // so the engine does not hold a dead order as working. + #[allow(clippy::too_many_arguments)] // Venue resolution shares the notice context. + async fn resolve_absent_order( + client: &Arc, + ib_order_id: i32, + client_order_id: ClientOrderId, + rejection_reason: Option, + timeout_dur: Duration, + orders: &OrderTracker, + exec_sender: &EventSender, + account_id: AccountId, + ) { + let tracked_perm_id = orders.lock().ok().and_then(|state| { + state + .active_orders + .get(&ib_order_id) + .map(|order| order.perm_id) + }); + + let completed = match Self::find_completed_order( + client, + ib_order_id, + tracked_perm_id, + timeout_dur, + ) + .await + { + Ok(completed) => completed, + Err(e) => { + tracing::error!( + "Order {client_order_id} is absent from open orders and completed orders could not be read: {e:#}; venue state needs reconciliation" + ); + return; + } + }; + + let event = { + let Ok(state) = orders.lock() else { + tracing::error!( + "Order {client_order_id} absent-order resolution could not lock tracking" + ); + return; + }; + // A terminal order was already closed by the update stream during the query + let Some(tracked) = state.active_orders.get(&ib_order_id) else { + tracing::debug!( + "Order {client_order_id} is no longer active; absent-order resolution done" + ); + return; + }; + Self::absent_order_terminal_event( + completed, + rejection_reason, + tracked, + ib_order_id, + account_id, + get_atomic_clock_realtime().get_time_ns(), + ) + }; + + match event { + Some(event) => { + if exec_sender.send(ExecutionEvent::Order(event)).is_err() { + tracing::error!( + "Failed to send inferred terminal event for absent order {client_order_id}" + ); + return; + } + + if let Err(e) = + Self::evict_terminal_order_state(client_order_id, ib_order_id, orders) + { + tracing::error!( + "Failed to evict tracking for resolved absent order {client_order_id}: {e}" + ); + } + } + None => { + tracing::error!( + "Order {client_order_id} (IB order ID {ib_order_id}) is absent from open orders after a venue notice and completed orders gave no terminal status; venue state needs reconciliation" + ); + } + } + } + + // Returns whether IB lists the order as open. ibapi ends the stream cleanly only after + // the end message and yields an error first on a reset, so `?` fails a partial list. + async fn find_open_order( + client: &Arc, + ib_order_id: i32, + timeout_dur: Duration, + ) -> anyhow::Result { + tokio::time::timeout(timeout_dur, async { + let mut subscription = client.all_open_orders().await?; + while let Some(item) = subscription.next().await { + match item? { + SubscriptionItem::Data(Orders::OrderData(data)) + if data.order_id == ib_order_id => + { + return Ok(true); + } + _ => {} + } + } + Ok(false) + }) + .await + .context("timed out reading open orders")? + } + + // Returns the completed status and filled quantity of the order, failing on a partial list. + async fn find_completed_order( + client: &Arc, + ib_order_id: i32, + tracked_perm_id: Option, + timeout_dur: Duration, + ) -> anyhow::Result> { + tokio::time::timeout(timeout_dur, async { + let mut subscription = client.completed_orders(false).await?; + while let Some(item) = subscription.next().await { + match item? { + SubscriptionItem::Data(Orders::OrderData(data)) + if data.order_id == ib_order_id + || (data.order.perm_id != 0 + && Some(data.order.perm_id) == tracked_perm_id) => + { + return Ok(Some(( + data.order_state.status, + data.order.perm_id, + data.order.filled_quantity, + ))); + } + _ => {} + } + } + Ok(None) + }) + .await + .context("timed out reading completed orders")? + } + + // Maps a completed-orders status for an absent order to the inferred terminal + // event, or None when no event can be honestly inferred. An order absent from + // completed orders too is rejected only when a notice before acceptance gives + // the rejection reason. + pub(super) fn absent_order_terminal_event( + completed: Option<(OrderStatusKind, i64, f64)>, + rejection_reason: Option, + tracked: &TrackedOrder, + ib_order_id: i32, + account_id: AccountId, + ts_init: UnixNanos, + ) -> Option { + let Some((status, perm_id, filled)) = completed else { + return rejection_reason.map(|reason| { + OrderEventAny::Rejected(OrderRejected::new( + tracked.trader_id, + tracked.strategy_id, + tracked.instrument_id, + tracked.client_order_id, + account_id, + reason, + UUID4::new(), + ts_init, + ts_init, + false, + false, + )) + }); + }; + let venue_order_id = ib_venue_order_id(ib_order_id, perm_id); + + // A partially filled order cannot be rejected, so an Inactive remainder is canceled + let partially_filled = filled > 0.0; + + match status { + OrderStatusKind::Cancelled | OrderStatusKind::ApiCancelled => { + Some(OrderEventAny::Canceled(OrderCanceled::new( + tracked.trader_id, + tracked.strategy_id, + tracked.instrument_id, + tracked.client_order_id, + UUID4::new(), + ts_init, + ts_init, + false, + Some(venue_order_id), + Some(account_id), + None, + ))) + } + OrderStatusKind::Inactive if partially_filled => { + Some(OrderEventAny::Canceled(OrderCanceled::new( + tracked.trader_id, + tracked.strategy_id, + tracked.instrument_id, + tracked.client_order_id, + UUID4::new(), + ts_init, + ts_init, + false, + Some(venue_order_id), + Some(account_id), + None, + ))) + } + OrderStatusKind::Inactive => Some(OrderEventAny::Rejected(OrderRejected::new( + tracked.trader_id, + tracked.strategy_id, + tracked.instrument_id, + tracked.client_order_id, + account_id, + Ustr::from("IB reports Inactive after a venue notice"), + UUID4::new(), + ts_init, + ts_init, + false, + false, + ))), + OrderStatusKind::Filled => { + tracing::info!( + "Order {} completed as Filled after a venue notice; fills arrive through the update stream", + tracked.client_order_id + ); + None + } + _ => { + tracing::warn!( + "Order {} completed with status {} after a venue notice; no terminal event inferred", + tracked.client_order_id, + status.as_str() + ); + None + } + } + } + + #[allow(clippy::too_many_arguments)] // Update dispatch preserves the stream context. + pub(super) async fn handle_order_update( + update: &OrderUpdate, + orders: &OrderTracker, + instrument_provider: &Arc, + exec_sender: &EventSender, + clock: &'static AtomicTime, + account_id: AccountId, + ib_account: Ustr, + commission_cache: &Arc>, + pending_live_exec_data: &Arc>, + position_tracker: &PositionTracker, + venue_ctx: Option<&NoticeVenueContext>, + ) -> anyhow::Result<()> { + let ts_init = clock.get_time_ns(); + + match update { + OrderUpdate::OrderBound(binding) => { + orders.lock()?.observe_order_binding(binding); + } + + OrderUpdate::OrderStatus(status) => { + Self::handle_order_status( + status, + orders, + instrument_provider, + exec_sender, + ts_init, + account_id, + ib_account, + venue_ctx, + ) + .await?; + } + OrderUpdate::ExecutionData(exec_data) => { + // ibapi also broadcasts `reqExecutions` replies here. Only unsolicited + // execDetails are live fills; they decode to request ID -1 or 0, while + // ibapi assigns positive request IDs + if exec_data.request_id > 0 { + tracing::debug!( + "Ignoring IB execution {} replayed for request {}", + exec_data.execution.execution_id, + exec_data.request_id + ); + return Ok(()); + } + + if !Self::execution_in_account(exec_data, ib_account)? { + return Ok(()); + } + Self::execution_context(exec_data, orders, instrument_provider, account_id)?; + // Record the own-fill delta immediately so the position stream cannot + // observe the post-fill quantity before the fill is known; the id + // cache makes the later commission-gated call a no-op. + record_own_fill( + position_tracker, + &exec_data.execution.execution_id, + exec_data.contract.contract_id, + exec_data.execution.side, + exec_data.execution.shares, + ) + .await?; + + let execution_id = exec_data.execution.execution_id.clone(); + tracing::debug!( + execution_id, + order_id = exec_data.execution.order_id, + contract_id = exec_data.contract.contract_id, + local_symbol = exec_data.contract.local_symbol, + security_type = ?exec_data.contract.security_type, + side = ?exec_data.execution.side, + shares = exec_data.execution.shares, + price = exec_data.execution.price, + cumulative_quantity = exec_data.execution.cumulative_quantity, + average_price = exec_data.execution.average_price, + "Received IB execDetails", + ); + // IB sends no commissionReport for the combo execution of a spread, its + // commission arrives on the leg executions + let has_commission = parse::is_combo_execution(exec_data) + || commission_cache.lock().contains_key(&execution_id); + + if !has_commission { + tracing::debug!( + "Buffering execution data {} until commission report arrives", + execution_id + ); + pending_live_exec_data.lock().insert( + execution_id, + (tokio::time::Instant::now(), exec_data.clone()), + ); + return Ok(()); + } + + Self::handle_execution_data( + exec_data, + orders, + instrument_provider, + exec_sender, + ts_init, + account_id, + ib_account, + commission_cache, + position_tracker, + venue_ctx, + ) + .await?; + } + OrderUpdate::CommissionReport(commission) => { + tracing::debug!( + execution_id = commission.execution_id, + commission = commission.commission, + currency = commission.currency, + "Received IB commissionReport", + ); + let pending_exec_data = pending_live_exec_data + .lock() + .remove(&commission.execution_id); + + { + let mut cache = commission_cache.lock(); + // IB uses -1.0 as a pending-sentinel before the real commission arrives; + // clamp only that sentinel to zero (legitimate rebates can be negative). + let commission_value = if commission.commission == -1.0_f64 { + 0.0_f64 + } else { + commission.commission + }; + cache.insert( + commission.execution_id.clone(), + (commission_value, commission.currency.clone()), + ); + } + + if let Some((_, exec_data)) = pending_exec_data { + Self::handle_execution_data( + &exec_data, + orders, + instrument_provider, + exec_sender, + ts_init, + account_id, + ib_account, + commission_cache, + position_tracker, + venue_ctx, + ) + .await?; + } + } + OrderUpdate::OpenOrder(order_data) => { + if !Self::is_active_open_order(&order_data.order) { + tracing::debug!( + "Ignoring deactivated open order: order_id={}, order_ref={}", + order_data.order_id, + order_data.order.order_ref + ); + return Ok(()); + } + + if order_data.order.what_if + && IbOrderStatus::from_str(order_data.order_state.status.as_str()) + .is_ok_and(|status| status == IbOrderStatus::PreSubmitted) + { + Self::handle_whatif_order( + order_data, + orders, + instrument_provider, + exec_sender, + clock.get_time_ns(), + account_id, + ) + .await?; + } else { + anyhow::ensure!( + !order_data.order.account.is_empty(), + "IB open order {} has no account identity", + order_data.order_id + ); + + if order_data.order.account != ib_account { + return Ok(()); + } + let mut normalized_order = order_data.clone(); + let correlation = orders.lock()?.correlate( + order_data.order.client_id, + order_data.order_id, + order_data.order.perm_id, + &order_data.order.order_ref, + )?; + + if let OrderCorrelation::Tracked { context, .. } + | OrderCorrelation::Duplicate { context, .. } = &correlation + { + let resolved = instrument_provider + .resolve_instrument_id_for_contract(&order_data.contract)?; + anyhow::ensure!( + resolved == context.instrument_id, + "IB order reference has conflicting instrument identity" + ); + } + let sibling = orders + .lock()? + .observe_order_data(order_data, account_id, false)?; + + if let Some(parent) = sibling { + let snapshot = orders + .lock()? + .incarnation_data(order_data.order.perm_id) + .cloned() + .unwrap_or_else(|| order_data.clone()); + let order_data = &snapshot; + let report = parse::parse_order_data_to_report( + order_data, + parent.instrument_id, + account_id, + instrument_provider, + ts_init, + )?; + Self::report_duplicate_order( + report, + order_data.order.perm_id, + orders, + instrument_provider, + exec_sender, + account_id, + ib_account, + venue_ctx, + )?; + orders.lock()?.archive_finished_groups(); + return Ok(()); + } + + if let OrderCorrelation::Tracked { order_id, .. } = correlation { + normalized_order.order_id = order_id; + } + let order_data = &normalized_order; + let status_str = order_data.order_state.status.as_str(); + tracing::debug!( + "Received open order: order_id={}, status={}, order_ref={}", + order_data.order_id, + status_str, + order_data.order.order_ref + ); + + let client_order_id = if let Some(order_ref) = + parse::normalized_order_ref(&order_data.order.order_ref) + { + Some(ClientOrderId::from(order_ref)) + } else { + orders + .lock()? + .venue_order_id_map + .get(&order_data.order_id) + .copied() + }; + + if let Some(client_order_id) = client_order_id + && IbOrderStatus::from_str(status_str).is_ok_and(IbOrderStatus::is_accepted) + { + let instrument_id = { + Self::get_mapped_instrument_id(order_data.order_id, orders)? + .map_or_else( + || { + Self::resolve_contract_instrument_id( + instrument_provider, + &order_data.contract, + ) + }, + Ok, + )? + }; + let venue_order_id = + parse::ib_venue_order_id(order_data.order_id, order_data.order.perm_id); + if Self::emit_order_accepted_if_needed( + order_data.order_id, + venue_order_id, + account_id, + ts_init, + orders, + exec_sender, + )? { + tracing::debug!( + "Order {} accepted (IB openOrder status: {})", + client_order_id, + status_str + ); + } + + Self::emit_order_updated_from_open_order( + order_data, + client_order_id, + instrument_id, + orders, + instrument_provider, + exec_sender, + ts_init, + account_id, + )?; + } + } + } + } + + Ok(()) + } + + #[allow(clippy::too_many_arguments)] // Event construction requires the full venue update. + fn emit_order_updated_from_open_order( + order_data: &ibapi::orders::OrderData, + client_order_id: ClientOrderId, + instrument_id: InstrumentId, + orders: &OrderTracker, + instrument_provider: &Arc, + exec_sender: &EventSender, + ts_init: UnixNanos, + account_id: AccountId, + ) -> anyhow::Result<()> { + let Some(instrument) = instrument_provider.find(&instrument_id) else { + anyhow::bail!( + "Cannot update IB order {client_order_id}: instrument {instrument_id} is unavailable" + ); + }; + + if order_data.order.total_quantity <= 0.0 { + return Ok(()); + } + + let actor_ids = { + let state = orders.lock()?; + state + .active_orders + .get(&order_data.order_id) + .map(|order| (order.trader_id, order.strategy_id)) + }; + let Some((trader_id, strategy_id)) = actor_ids else { + // External orders whose `order_ref` parses as a client order id refresh + // through here without tracked state; nothing to update. + tracing::debug!( + "Skipping order update for untracked IB order {}", + order_data.order_id + ); + return Ok(()); + }; + + { + let mut state = orders.lock()?; + if let Some(order) = state.active_orders.get_mut(&order_data.order_id) { + if order.perm_id == 0 && order_data.order.perm_id != 0 { + order.perm_id = order_data.order.perm_id; + } + // Clear the pending modify only when this openOrder reflects the + // requested values; an unrelated refresh (e.g. induced by an + // all-open-orders query) must not resolve it. + if order + .pending_modify + .as_ref() + .is_some_and(|pending| pending.matches(&order_data.order)) + { + order.pending_modify = None; + } + } + } + + let price_magnifier = instrument_provider.get_price_magnifier(&instrument_id) as f64; + let (price, trigger_price) = Self::open_order_price_fields( + order_data, + price_magnifier, + instrument.price_precision(), + ); + let quantity = Quantity::new(order_data.order.total_quantity, instrument.size_precision()); + + // IB repeats openOrder for every open-orders query, including periodic reconciliation + let update = Some((quantity, price, trigger_price)); + if let Some(order) = orders.lock()?.active_orders.get_mut(&order_data.order_id) { + if order.last_update == update { + return Ok(()); + } + order.last_update = update; + } + + let venue_order_id = + parse::ib_venue_order_id(order_data.order_id, order_data.order.perm_id); + let event = OrderUpdated::new( + trader_id, + strategy_id, + instrument_id, + client_order_id, + quantity, + UUID4::new(), + ts_init, + ts_init, + false, + Some(venue_order_id), + Some(account_id), + price, + trigger_price, + None, + false, + ); + + exec_sender + .send(ExecutionEvent::Order(OrderEventAny::Updated(event))) + .map_err(|e| anyhow::anyhow!("Failed to send order updated event: {e}")) + } + + fn open_order_price_fields( + order_data: &ibapi::orders::OrderData, + price_magnifier: f64, + price_precision: u8, + ) -> (Option, Option) { + let order_type = IbOrderType::from_str(order_data.order.order_type.as_str()) + .map_or(OrderType::Market, IbOrderType::nautilus_order_type); + let price = order_data + .order + .limit_price + .map(|price| Price::new(price * price_magnifier, price_precision)); + let trigger_price = order_data + .order + .aux_price + .map(|price| Price::new(price * price_magnifier, price_precision)); + // A trailing order's aux price is its offset; IB reports its trigger separately + let trail_stop_price = order_data + .order + .trail_stop_price + .map(|price| Price::new(price * price_magnifier, price_precision)); + + match order_type { + OrderType::Market | OrderType::MarketToLimit => (None, None), + OrderType::Limit => (price, None), + OrderType::TrailingStopMarket => (None, trail_stop_price), + OrderType::TrailingStopLimit => (price, trail_stop_price), + OrderType::StopMarket | OrderType::MarketIfTouched => (None, trigger_price), + OrderType::StopLimit | OrderType::LimitIfTouched => (price, trigger_price), + } + } + + #[allow(clippy::too_many_arguments)] // Status handling preserves the stream context. + pub(super) async fn handle_order_status( + status: &IBOrderStatus, + orders: &OrderTracker, + instrument_provider: &Arc, + exec_sender: &EventSender, + ts_init: UnixNanos, + account_id: AccountId, + ib_account: Ustr, + venue_ctx: Option<&NoticeVenueContext>, + ) -> anyhow::Result<()> { + let sibling = { + let mut state = orders.lock()?; + let sibling = state.observe_incarnation_status(status); + state.archive_finished_groups(); + sibling + }; + + if let Some((parent, data)) = sibling { + let mut normalized = status.clone(); + normalized.status = data.order_state.status.clone(); + normalized.filled = data.order.filled_quantity; + let status = &normalized; + let report = parse_order_status_to_report( + status, + Some(&data.order), + parent.instrument_id, + account_id, + instrument_provider, + ts_init, + )?; + Self::report_duplicate_order( + report, + status.perm_id, + orders, + instrument_provider, + exec_sender, + account_id, + ib_account, + venue_ctx, + )?; + orders.lock()?.archive_finished_groups(); + return Ok(()); + } + let correlation = + orders + .lock()? + .correlate(status.client_id, status.order_id, status.perm_id, "")?; + let mut normalized = status.clone(); + let order = match correlation { + OrderCorrelation::Tracked { order_id, context } => { + let state = orders.lock()?; + let Some(active) = state.active_orders.get(&order_id) else { + return Ok(()); + }; + anyhow::ensure!( + active.client_order_id == context.client_order_id, + "IB status refers to an order ID reused by another order" + ); + normalized.order_id = order_id; + context + } + OrderCorrelation::Duplicate { context, .. } => { + tracing::warn!( + "IB sibling permanent ID {} for {} awaits authoritative order details", + status.perm_id, + context.client_order_id + ); + return Ok(()); + } + OrderCorrelation::Untracked { .. } => return Ok(()), + }; + let status = &normalized; + let client_order_id = order.client_order_id; + let instrument_id = order.instrument_id; + + Self::update_order_avg_price( + status.order_id, + &instrument_id, + status.average_fill_price.unwrap_or(0.0), + status.filled, + instrument_provider, + orders, + )?; + + let ib_order_status = IbOrderStatus::from_str(status.status.as_str()).ok(); + + if ib_order_status == Some(IbOrderStatus::Inactive) && status.why_held == "locate" { + tracing::warn!( + "Order {} held for short-sell locate, order remains active", + client_order_id + ); + return Ok(()); + } + + let venue_order_id = parse::ib_venue_order_id(status.order_id, status.perm_id); + let is_terminal = ib_order_status.is_some_and(IbOrderStatus::is_terminal); + + if matches!( + ib_order_status, + Some(IbOrderStatus::Filled | IbOrderStatus::Cancelled | IbOrderStatus::ApiCancelled) + ) { + Self::emit_order_accepted_if_needed( + status.order_id, + venue_order_id, + account_id, + ts_init, + orders, + exec_sender, + )?; + } + + if status.perm_id != 0 { + let mut state = orders.lock()?; + if let Some(order) = state.active_orders.get_mut(&status.order_id) + && order.perm_id == 0 + { + order.perm_id = status.perm_id; + } + } + + let status_str = status.status.as_str(); + + match ib_order_status { + Some(IbOrderStatus::Submitted | IbOrderStatus::PreSubmitted) => { + if Self::emit_order_accepted_if_needed( + status.order_id, + venue_order_id, + account_id, + ts_init, + orders, + exec_sender, + )? { + tracing::debug!( + "Order {} accepted (IB status: {})", + client_order_id, + status_str + ); + } else { + tracing::debug!( + "Order {} already accepted (IB status: {})", + client_order_id, + status_str + ); + } + } + Some(IbOrderStatus::Inactive) => { + let reason = if status.why_held.is_empty() { + "IB reported order as inactive" + } else { + status.why_held.as_str() + }; + + // IB can report a working order as Inactive after it refuses a modify, even + // after a partial fill, so a pending modify is rejected and the order kept + if order.accepted + && Self::emit_modify_rejected_if_pending( + &order, + status.order_id, + reason, + orders, + exec_sender, + ts_init, + account_id, + )? + { + return Ok(()); + } + + if status.filled > 0.0 { + Self::emit_order_canceled( + &order, + venue_order_id, + account_id, + ts_init, + exec_sender, + )?; + tracing::warn!( + "Order {} canceled after partial fill when IB reported Inactive: {}", + client_order_id, + reason, + ); + } else if order.accepted { + tracing::warn!( + "IB reported accepted order {} as Inactive, left working (venue state uncertain): {}", + client_order_id, + reason, + ); + return Ok(()); + } else { + Self::emit_order_rejected(&order, account_id, reason, ts_init, exec_sender)?; + tracing::warn!("Order {} rejected: {}", client_order_id, reason); + } + } + Some(IbOrderStatus::Filled) => { + tracing::debug!( + "Order {} filled (IB status: {})", + client_order_id, + status_str + ); + } + Some(IbOrderStatus::Cancelled | IbOrderStatus::ApiCancelled) => { + if let Some(order) = orders.lock()?.active_orders.get_mut(&status.order_id) { + order.pending_cancel = false; + } + + Self::emit_order_canceled( + &order, + venue_order_id, + account_id, + ts_init, + exec_sender, + )?; + tracing::debug!("Order {} canceled", client_order_id); + } + Some(IbOrderStatus::PendingCancel) => { + Self::emit_order_pending_cancel( + status.order_id, + client_order_id, + venue_order_id, + orders, + exec_sender, + ts_init, + account_id, + )?; + tracing::debug!("Order {} pending cancel", client_order_id); + } + _ => { + tracing::debug!( + "Order status update for order {}: {}", + client_order_id, + status_str + ); + } + } + + if is_terminal { + Self::evict_terminal_order_state(client_order_id, status.order_id, orders)?; + } + + Ok(()) + } + + fn emit_order_canceled( + order: &TrackedOrder, + venue_order_id: VenueOrderId, + account_id: AccountId, + ts_init: UnixNanos, + exec_sender: &EventSender, + ) -> anyhow::Result<()> { + let event = OrderCanceled::new( + order.trader_id, + order.strategy_id, + order.instrument_id, + order.client_order_id, + UUID4::new(), + ts_init, + ts_init, + false, + Some(venue_order_id), + Some(account_id), + None, + ); + exec_sender + .send(ExecutionEvent::Order(OrderEventAny::Canceled(event))) + .map_err(|e| anyhow::anyhow!("Failed to send order canceled event: {e}")) + } + + fn emit_order_rejected( + order: &TrackedOrder, + account_id: AccountId, + reason: &str, + ts_init: UnixNanos, + exec_sender: &EventSender, + ) -> anyhow::Result<()> { + let event = OrderRejected::new( + order.trader_id, + order.strategy_id, + order.instrument_id, + order.client_order_id, + account_id, + Ustr::from(reason), + UUID4::new(), + ts_init, + ts_init, + false, + false, + ); + exec_sender + .send(ExecutionEvent::Order(OrderEventAny::Rejected(event))) + .map_err(|e| anyhow::anyhow!("Failed to send order rejected event: {e}")) + } + + fn evict_terminal_order_state( + client_order_id: ClientOrderId, + order_id: i32, + orders: &OrderTracker, + ) -> anyhow::Result<()> { + let mut state = orders.lock()?; + if let Some(mut order) = state.active_orders.remove(&order_id) { + order.pending_cancel = false; + order.pending_modify = None; + state.terminal_orders.insert(order_id, order); + } + + state.order_id_map.remove(&client_order_id); + state.venue_order_id_map.remove(&order_id); + Ok(()) + } + + #[allow(clippy::too_many_arguments)] // Execution handling preserves the stream context. + pub(super) async fn handle_execution_data( + exec_data: &ExecutionData, + orders: &OrderTracker, + instrument_provider: &Arc, + exec_sender: &EventSender, + ts_init: UnixNanos, + account_id: AccountId, + ib_account: Ustr, + commission_cache: &Arc>, + position_tracker: &PositionTracker, + venue_ctx: Option<&NoticeVenueContext>, + ) -> anyhow::Result<()> { + if !Self::execution_in_account(exec_data, ib_account)? { + return Ok(()); + } + let (tracking_order_id, tracked_context, correlated_id) = + Self::execution_context(exec_data, orders, instrument_provider, account_id)?; + let client_order_id = correlated_id.unwrap_or_else(|| { + ClientOrderId::from( + parse::ib_venue_order_id(exec_data.execution.order_id, exec_data.execution.perm_id) + .as_str(), + ) + }); + let instrument_id = if let Some(context) = tracked_context.as_ref() { + context.instrument_id + } else if let Some(cached_id) = + instrument_provider.get_instrument_id_by_contract_id(exec_data.contract.contract_id) + { + cached_id + } else { + Self::resolve_contract_instrument_id(instrument_provider, &exec_data.contract)? + }; + + let (commission, commission_currency) = if parse::is_combo_execution(exec_data) { + (0.0, exec_data.contract.currency.to_string()) + } else { + let mut cache = commission_cache.lock(); + let Some((commission, commission_currency)) = + cache.remove(&exec_data.execution.execution_id) + else { + tracing::debug!( + "Execution data {} is waiting for commission report", + exec_data.execution.execution_id + ); + return Ok(()); + }; + (commission, commission_currency) + }; + + record_own_fill( + position_tracker, + &exec_data.execution.execution_id, + exec_data.contract.contract_id, + exec_data.execution.side, + exec_data.execution.shares, + ) + .await?; + + let is_bag = matches!( + exec_data.contract.security_type, + ibapi::contracts::SecurityType::Spread + ); + + let spread_instrument_id = tracked_context + .as_ref() + .map(|context| context.instrument_id); + let is_spread = if let Some(spread_id) = spread_instrument_id { + if let Some(instrument) = instrument_provider.find(&spread_id) { + instrument.is_spread() + } else { + false + } + } else { + false + }; + + let avg_px = tracked_context.as_ref().and_then(|context| context.avg_px); + let venue_order_id = parse::ib_venue_order_id( + tracking_order_id, + if exec_data.execution.perm_id == 0 { + tracked_context + .as_ref() + .map_or(0, |context| context.perm_id) + } else { + exec_data.execution.perm_id + }, + ); + + if tracked_context.is_some() { + Self::emit_order_accepted_for_fill_if_needed( + tracking_order_id, + venue_order_id, + account_id, + parse_execution_time(&exec_data.execution.time)?, + orders, + exec_sender, + )?; + } + + if is_bag && is_spread { + let fill_id = exec_data.execution.execution_id.clone(); + let mut state = orders.lock()?; + if let Some(order) = state.order_mut(tracking_order_id) { + if order.spread_fill_ids.contains(&fill_id) { + tracing::debug!( + "Combo fill {} already processed for order {}, skipping", + fill_id, + client_order_id, + ); + return Ok(()); + } + order.spread_fill_ids.insert(fill_id); + } + } + + if !is_bag + && is_spread + && let Some(spread_id) = spread_instrument_id + && let Some(context) = tracked_context.as_ref() + { + let fill = SpreadFillContext { + client_order_id, + spread_instrument_id: spread_id, + commission, + commission_currency: &commission_currency, + ts_init, + account_id, + }; + + match Self::handle_spread_execution( + exec_data, + &fill, + instrument_provider, + exec_sender, + orders, + context, + ) + .await + { + Err(e) => { + tracing::warn!( + "Error handling spread execution, falling back to regular fill: {e}" + ); + } + Ok(leg_instrument_id) => { + if let Some(leg_instrument_id) = leg_instrument_id { + Self::record_spread_leg_fill( + orders, + tracking_order_id, + leg_instrument_id, + exec_data.execution.shares, + exec_sender, + )?; + } + return Ok(()); + } + } + } + + let mut fill_report = parse_execution_to_fill_report( + &exec_data.execution, + &exec_data.contract, + commission, + &commission_currency, + instrument_id, + account_id, + instrument_provider, + ts_init, + avg_px, + )?; + + fill_report.client_order_id = correlated_id; + + if let Some(context) = tracked_context { + if exec_data.execution.perm_id == 0 && context.perm_id > 0 { + fill_report.venue_order_id = + parse::ib_venue_order_id(tracking_order_id, context.perm_id); + } + let quote_currency = instrument_provider + .find(&context.instrument_id) + .with_context(|| { + format!( + "Instrument {} not found for tracked fill", + context.instrument_id + ) + })? + .quote_currency(); + let event = OrderFilled::new( + context.trader_id, + context.strategy_id, + context.instrument_id, + context.client_order_id, + fill_report.venue_order_id, + fill_report.account_id, + fill_report.trade_id, + context.order_side, + context.order_type, + fill_report.last_qty, + fill_report.last_px, + quote_currency, + fill_report.liquidity_side, + UUID4::new(), + fill_report.ts_event, + fill_report.ts_init, + false, + fill_report.venue_position_id, + Some(fill_report.commission), + None, + ); + + if is_bag && is_spread { + Self::hold_spread_fill(orders, tracking_order_id, event, exec_sender)?; + } else { + exec_sender.send(ExecutionEvent::Order(OrderEventAny::Filled(event)))?; + } + } else { + let target = if exec_data.execution.perm_id > 0 { + IbOrderSelector::PermId(exec_data.execution.perm_id) + } else { + IbOrderSelector::OrderId(exec_data.execution.order_id) + }; + Self::hold_fill( + Some(fill_report), + target, + instrument_id, + correlated_id, + orders, + instrument_provider, + exec_sender, + account_id, + ib_account, + venue_ctx, + )?; + } + + Ok(()) + } + + // Reports a duplicate broker order under its own client order ID. A status showing fills + // the engine has not received resolves them first, so no fill is inferred from the status. + #[allow(clippy::too_many_arguments)] // Resolution shares the update stream context. + fn report_duplicate_order( + mut report: OrderStatusReport, + perm_id: i64, + orders: &OrderTracker, + instrument_provider: &Arc, + exec_sender: &EventSender, + account_id: AccountId, + ib_account: Ustr, + venue_ctx: Option<&NoticeVenueContext>, + ) -> anyhow::Result<()> { + let client_order_id = duplicate_client_order_id(account_id, perm_id)?; + report.client_order_id = Some(client_order_id); + + if venue_ctx.is_some() + && orders + .lock()? + .duplicate_fills_unresolved(perm_id, report.filled_qty.as_f64()) + { + return Self::hold_fill( + None, + IbOrderSelector::PermId(perm_id), + report.instrument_id, + Some(client_order_id), + orders, + instrument_provider, + exec_sender, + account_id, + ib_account, + venue_ctx, + ); + } + exec_sender.send(ExecutionEvent::Report(ExecutionReport::Order(Box::new( + report, + ))))?; + Ok(()) + } + + // Holds a fill for an order this client does not track while a task resolves the order's + // details and executions, so the engine creates the order at its full quantity and applies + // the real fills. Without venue access the fill is forwarded as is. + #[allow(clippy::too_many_arguments)] // Resolution shares the update stream context. + fn hold_fill( + fill: Option, + target: IbOrderSelector, + instrument_id: InstrumentId, + client_order_id: Option, + orders: &OrderTracker, + instrument_provider: &Arc, + exec_sender: &EventSender, + account_id: AccountId, + ib_account: Ustr, + venue_ctx: Option<&NoticeVenueContext>, + ) -> anyhow::Result<()> { + let Some(venue_ctx) = venue_ctx else { + if let Some(fill) = fill { + exec_sender.send(ExecutionEvent::Report(ExecutionReport::Fill(Box::new( + fill, + ))))?; + } + return Ok(()); + }; + + if !orders.lock()?.hold_fill(target, fill) { + return Ok(()); + } + let client = Arc::clone(&venue_ctx.client); + let timeout = FILL_HOLD_TIMEOUT.min(Duration::from_secs(venue_ctx.request_timeout_secs)); + let cancellation = venue_ctx.cancellation.child_token(); + let task_orders = orders.clone(); + let instrument_provider = Arc::clone(instrument_provider); + let task_exec_sender = exec_sender.clone(); + + let future = async move { + tokio::select! { + result = Self::resolve_held_fills( + &client, + target, + instrument_id, + client_order_id, + timeout, + &task_orders, + &instrument_provider, + &task_exec_sender, + account_id, + ib_account, + ) => { + if let Err(e) = result { + tracing::error!("Failed to deliver resolved IB order {}: {e}", target.label()); + } + } + () = cancellation.cancelled() => {} + } + }; + + if let Err(e) = venue_ctx.tasks.spawn(future) { + tracing::warn!("Forwarding held IB fills after shutdown began: {e}"); + let held = orders + .lock()? + .fills_held + .remove(&target) + .unwrap_or_default(); + + for fill in held { + exec_sender.send(ExecutionEvent::Report(ExecutionReport::Fill(Box::new( + fill, + ))))?; + } + } + Ok(()) + } + + // Emits the order details with all its executions, then any held fill the query missed. A + // failed or empty query forwards the held fills as plain reports, leaving any discrepancy to + // position reconciliation. + #[allow(clippy::too_many_arguments)] // Resolution shares the update stream context. + pub(super) async fn resolve_held_fills( + client: &Client, + target: IbOrderSelector, + instrument_id: InstrumentId, + client_order_id: Option, + timeout: Duration, + orders: &OrderTracker, + instrument_provider: &Arc, + exec_sender: &EventSender, + account_id: AccountId, + ib_account: Ustr, + ) -> anyhow::Result<()> { + let result = tokio::time::timeout( + timeout, + Self::order_details_with_fills( + client, + target, + instrument_id, + account_id, + ib_account, + instrument_provider, + get_atomic_clock_realtime().get_time_ns(), + orders, + ), + ) + .await; + let mut held = orders + .lock()? + .fills_held + .remove(&target) + .unwrap_or_default(); + let resolved = match result { + Ok(Ok(Some(resolved))) => Some(resolved), + Ok(Ok(None)) => { + tracing::warn!( + "IB returned no details for order {}; forwarding {} held fills", + target.label(), + held.len() + ); + None + } + Ok(Err(e)) => { + tracing::warn!( + "Failed to resolve IB order {}; forwarding {} held fills: {e:#}", + target.label(), + held.len() + ); + None + } + Err(_) => { + tracing::warn!( + "Timed out resolving IB order {}; forwarding {} held fills", + target.label(), + held.len() + ); + None + } + }; + + if let Some((mut report, mut fills)) = resolved { + if let Some(client_order_id) = client_order_id { + report.client_order_id = Some(client_order_id); + + for fill in &mut fills { + fill.client_order_id = Some(client_order_id); + } + } + + if let IbOrderSelector::PermId(perm_id) = target { + orders + .lock()? + .observe_duplicate_fills_resolved(perm_id, report.filled_qty.as_f64()); + } + held.retain(|fill| { + !fills + .iter() + .any(|resolved| resolved.trade_id == fill.trade_id) + }); + exec_sender.send(ExecutionEvent::Report(ExecutionReport::OrderWithFills( + Box::new(report), + fills, + )))?; + } + + for fill in held { + exec_sender.send(ExecutionEvent::Report(ExecutionReport::Fill(Box::new( + fill, + ))))?; + } + Ok(()) + } + + pub(super) fn update_order_avg_price( + order_id: i32, + instrument_id: &InstrumentId, + avg_fill_price: f64, + filled: f64, + instrument_provider: &Arc, + orders: &OrderTracker, + ) -> anyhow::Result<()> { + if filled <= 0.0 || !parse::should_use_avg_fill_price(avg_fill_price, instrument_id) { + return Ok(()); + } + + let Some(instrument) = instrument_provider.find(instrument_id) else { + anyhow::bail!( + "Cannot update IB order {order_id} average price: instrument {instrument_id} is unavailable" + ); + }; + + let price_magnifier = instrument_provider.get_price_magnifier(instrument_id) as f64; + let converted_avg_price = avg_fill_price * price_magnifier; + let avg_px = Price::new(converted_avg_price, instrument.price_precision()); + + let mut state = orders.lock()?; + let order = state.active_orders.get_mut(&order_id).with_context(|| { + format!("Tracked state not found for Interactive Brokers order {order_id}") + })?; + order.avg_px = Some(avg_px); + + Ok(()) + } + + pub(super) async fn handle_whatif_order( + order_data: &ibapi::orders::OrderData, + orders: &OrderTracker, + instrument_provider: &Arc, + exec_sender: &EventSender, + ts_init: UnixNanos, + account_id: AccountId, + ) -> anyhow::Result<()> { + let Some(client_order_id) = orders + .lock()? + .venue_order_id_map + .get(&order_data.order_id) + .copied() + else { + tracing::debug!( + "What-if order for unknown order ID: {}", + order_data.order_id + ); + return Ok(()); + }; + + let instrument_id = Self::get_mapped_instrument_id(order_data.order_id, orders)? + .map_or_else( + || Self::resolve_contract_instrument_id(instrument_provider, &order_data.contract), + Ok, + )?; + + let (trader_id, strategy_id) = + Self::get_required_order_actor_ids(order_data.order_id, orders)?; + + let reason_json = serde_json::to_string(&order_data.order_state) + .unwrap_or_else(|_| format!("whatIf analysis for order {}", order_data.order_id)); + + let event = OrderRejected::new( + trader_id, + strategy_id, + instrument_id, + client_order_id, + account_id, + Ustr::from(&reason_json), + UUID4::new(), + ts_init, + ts_init, + false, + false, + ); + + exec_sender + .send(ExecutionEvent::Order(OrderEventAny::Rejected(event))) + .map_err(|e| anyhow::anyhow!("Failed to send order rejected event: {e}"))?; + + tracing::debug!( + "What-if analysis completed for order {}: margin change={:?}, commission={:?}", + client_order_id, + order_data + .order_state + .initial_margin_after + .and_then(|after| order_data + .order_state + .initial_margin_before + .map(|before| after - before)), + order_data.order_state.commission + ); + + Ok(()) + } + + pub(super) fn resolve_contract_instrument_id( + instrument_provider: &Arc, + contract: &Contract, + ) -> anyhow::Result { + instrument_provider + .resolve_instrument_id_for_contract(contract) + .context("Failed to resolve IBKR contract to instrument ID") + } +} diff --git a/crates/adapters/interactive_brokers/src/factories.rs b/crates/adapters/interactive_brokers/src/factories.rs index 37282644745a..8adf9ff0c5d0 100644 --- a/crates/adapters/interactive_brokers/src/factories.rs +++ b/crates/adapters/interactive_brokers/src/factories.rs @@ -101,7 +101,7 @@ impl DataClientFactory for InteractiveBrokersDataClientFactory { let instrument_provider = Arc::new(InteractiveBrokersInstrumentProvider::new( ib_config.instrument_provider.clone(), )); - seed_provider_from_cache(&instrument_provider, &cache); + instrument_provider.seed_from_cache(&cache.borrow()); let client = InteractiveBrokersDataClient::new( ClientId::from(name), ib_config, @@ -153,7 +153,7 @@ impl ExecutionClientFactory for InteractiveBrokersExecutionClientFactory { cache: CacheView, _clock: Rc>, ) -> anyhow::Result> { - let mut ib_config = config + let ib_config = config .as_any() .downcast_ref::() .ok_or_else(|| { @@ -168,12 +168,11 @@ impl ExecutionClientFactory for InteractiveBrokersExecutionClientFactory { } else { AccountId::from("IB-001") }; - ib_config.account_id = Some(account_id.to_string()); let instrument_provider = Arc::new(InteractiveBrokersInstrumentProvider::new( ib_config.instrument_provider.clone(), )); - seed_provider_from_cache(&instrument_provider, &cache); + instrument_provider.seed_from_cache(&cache.borrow()); let core = ExecutionClientCore::new( trader_id, @@ -210,28 +209,6 @@ fn resolve_account_id(name: &str, account_id: &str) -> anyhow::Result .map_err(|e| anyhow::anyhow!("Invalid Interactive Brokers account_id: {e}")) } -fn seed_provider_from_cache( - instrument_provider: &InteractiveBrokersInstrumentProvider, - cache: &CacheView, -) { - let instruments = { - let cache = cache.borrow(); - cache - .instrument_ids(None) - .into_iter() - .filter_map(|instrument_id| cache.instrument(instrument_id).cloned()) - .collect::>() - }; - - let count = instrument_provider.add_cached_instruments(instruments); - if count > 0 { - tracing::debug!( - "Seeded Interactive Brokers instrument provider with {} cached instruments", - count - ); - } -} - #[cfg(test)] mod tests { use std::{cell::RefCell, rc::Rc}; diff --git a/crates/adapters/interactive_brokers/src/gateway/dockerized.rs b/crates/adapters/interactive_brokers/src/gateway/dockerized.rs index 10146b59626e..ffa3a354f141 100644 --- a/crates/adapters/interactive_brokers/src/gateway/dockerized.rs +++ b/crates/adapters/interactive_brokers/src/gateway/dockerized.rs @@ -37,13 +37,15 @@ use bollard::query_parameters::{ #[cfg(feature = "gateway")] use futures_util::StreamExt; #[cfg(feature = "gateway")] -use nautilus_core::string::secret::SecretString; +use jiff::Timestamp; #[cfg(feature = "gateway")] +use nautilus_core::string::secret::SecretString; use serde::{Deserialize, Serialize}; -#[cfg(feature = "gateway")] +#[cfg(not(feature = "gateway"))] use crate::config::DockerizedIBGatewayConfig; #[cfg(feature = "gateway")] +use crate::config::{DockerizedIBGatewayConfig, TradingMode}; /// Container status enumeration. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] @@ -140,17 +142,17 @@ impl DockerizedIBGateway { /// Internal VNC port. pub const VNC_PORT_INTERNAL: u16 = 5900; - fn host_port_for_mode(trading_mode: crate::config::TradingMode) -> u16 { + fn host_port_for_mode(trading_mode: TradingMode) -> u16 { match trading_mode { - crate::config::TradingMode::Paper => 4002, - crate::config::TradingMode::Live => 4001, + TradingMode::Paper => 4002, + TradingMode::Live => 4001, } } - fn container_port_for_mode(trading_mode: crate::config::TradingMode) -> u16 { + fn container_port_for_mode(trading_mode: TradingMode) -> u16 { match trading_mode { - crate::config::TradingMode::Paper => 4004, - crate::config::TradingMode::Live => 4003, + TradingMode::Paper => 4004, + TradingMode::Live => 4003, } } @@ -161,12 +163,19 @@ impl DockerizedIBGateway { || logs.contains("Login successful") } + fn session_logs_indicate_ready(logs: &str, started_at: Timestamp) -> bool { + logs.lines().any(|line| { + let Some((timestamp, message)) = line.split_once(' ') else { + return false; + }; + timestamp.parse::().is_ok_and(|timestamp| { + timestamp >= started_at && Self::logs_indicate_ready(message) + }) + }) + } + /// Create a new DockerizedIBGateway from configuration. /// - /// # Arguments - /// - /// * `config` - Configuration for the gateway - /// /// # Errors /// /// Returns an error if: @@ -192,8 +201,8 @@ impl DockerizedIBGateway { // Determine port based on trading mode let mode_str = match config.trading_mode { - crate::config::TradingMode::Paper => "Paper", - crate::config::TradingMode::Live => "Live", + TradingMode::Paper => "Paper", + TradingMode::Live => "Live", }; let port = Self::host_port_for_mode(config.trading_mode); @@ -228,17 +237,27 @@ impl DockerizedIBGateway { /// Check if the container is logged in by examining logs. /// - /// # Arguments - /// - /// * `container_id` - The container ID to check - /// /// # Errors /// /// Returns an error if log retrieval fails. pub async fn is_logged_in(&self, container_id: &str) -> anyhow::Result { + let started_at = self + .docker + .inspect_container(container_id, None) + .await + .context("Failed to inspect gateway container")? + .state + .and_then(|state| state.started_at) + .context("Gateway container start time is missing")? + .parse::() + .context("Gateway container start time is invalid")?; + let since = i32::try_from(started_at.as_second()) + .context("Gateway container start time exceeds the Docker log API range")?; let logs_options = LogsOptions { stdout: true, stderr: true, + since, + timestamps: true, ..Default::default() }; @@ -254,7 +273,7 @@ impl DockerizedIBGateway { LogOutput::StdIn { message } | LogOutput::Console { message } => message, }; let log_string = String::from_utf8_lossy(&log_bytes); - if Self::logs_indicate_ready(&log_string) { + if Self::session_logs_indicate_ready(&log_string, started_at) { logged_in = true; break; } @@ -283,8 +302,7 @@ impl DockerizedIBGateway { c.names .as_ref() .and_then(|names| names.first()) - .map(|name| name.trim_start_matches('/') == self.container_name) - .unwrap_or(false) + .is_some_and(|name| name.trim_start_matches('/') == self.container_name) }); let Some(container) = container else { @@ -294,8 +312,7 @@ impl DockerizedIBGateway { let state = container .state .as_ref() - .map(|state| state.as_ref()) - .unwrap_or("unknown"); + .map_or("unknown", |state| state.as_ref()); match state { "running" => { @@ -318,10 +335,6 @@ impl DockerizedIBGateway { /// Start the gateway container. /// - /// # Arguments - /// - /// * `wait` - Optional wait time in seconds (overrides config timeout) - /// /// # Errors /// /// Returns an error if container creation or startup fails. @@ -360,7 +373,7 @@ impl DockerizedIBGateway { let mut port_bindings = HashMap::new(); port_bindings.insert( - format!("{}/tcp", container_port), + format!("{container_port}/tcp"), Some(vec![PortBinding { host_ip: Some(self.host.clone()), host_port: Some(host_port.to_string()), @@ -379,8 +392,8 @@ impl DockerizedIBGateway { // Prepare environment variables let mode_str = match self.config.trading_mode { - crate::config::TradingMode::Paper => "paper", - crate::config::TradingMode::Live => "live", + TradingMode::Paper => "paper", + TradingMode::Live => "live", }; let env = vec![ format!("TWS_USERID={}", self.username.expose_secret()), @@ -466,10 +479,6 @@ impl DockerizedIBGateway { /// Safely start the gateway, handling container already exists errors. /// - /// # Arguments - /// - /// * `wait` - Optional wait time in seconds - /// /// # Errors /// /// Returns an error if startup fails (other than container exists). @@ -504,36 +513,35 @@ impl DockerizedIBGateway { c.names .as_ref() .and_then(|names| names.first()) - .map(|name| name.trim_start_matches('/') == self.container_name) - .unwrap_or(false) + .is_some_and(|name| name.trim_start_matches('/') == self.container_name) }); - if let Some(container) = container { - if let Some(container_id) = &container.id { - // Stop container if running - if matches!( - container.state.as_ref().map(|state| state.as_ref()), - Some("running") - ) { - self.docker - .stop_container(container_id, None::) - .await - .context("Failed to stop container")?; - } - - // Remove container - let remove_options = RemoveContainerOptions { - force: true, - ..Default::default() - }; - + if let Some(container) = container + && let Some(container_id) = &container.id + { + // Stop container if running + if matches!( + container.state.as_ref().map(|state| state.as_ref()), + Some("running") + ) { self.docker - .remove_container(container_id, Some(remove_options)) + .stop_container(container_id, None::) .await - .context("Failed to remove container")?; - - tracing::debug!("Stopped and removed container `{}`", self.container_name); + .context("Failed to stop container")?; } + + // Remove container + let remove_options = RemoveContainerOptions { + force: true, + ..Default::default() + }; + + self.docker + .remove_container(container_id, Some(remove_options)) + .await + .context("Failed to remove container")?; + + tracing::debug!("Stopped and removed container `{}`", self.container_name); } Ok(()) @@ -550,17 +558,19 @@ impl DockerizedIBGateway { /// # Errors /// /// Returns an error if the Dockerized IB Gateway cannot be created or started. - pub fn new(_config: crate::config::DockerizedIBGatewayConfig) -> anyhow::Result { + pub fn new(_config: DockerizedIBGatewayConfig) -> anyhow::Result { anyhow::bail!("Gateway feature is not enabled. Build with --features gateway") } } #[cfg(all(test, feature = "gateway"))] mod tests { + use jiff::Timestamp; + use nautilus_core::string::secret::SecretString; use rstest::rstest; use super::DockerizedIBGateway; - use crate::config::TradingMode; + use crate::config::{DockerizedIBGatewayConfig, TradingMode}; #[rstest] #[case(TradingMode::Paper, 4002)] @@ -590,9 +600,9 @@ mod tests { #[case(TradingMode::Live, 4001)] fn new_reports_the_host_api_port(#[case] trading_mode: TradingMode, #[case] expected: u16) { let gateway = DockerizedIBGateway::new( - crate::config::DockerizedIBGatewayConfig::builder() - .username("test-user".into()) - .password("test-password".into()) + DockerizedIBGatewayConfig::builder() + .username(SecretString::from("test-user")) + .password(SecretString::from("test-password")) .trading_mode(trading_mode) .build(), ) @@ -615,4 +625,23 @@ mod tests { fn ready_log_markers_are_strict(#[case] logs: &str, #[case] expected: bool) { assert_eq!(DockerizedIBGateway::logs_indicate_ready(logs), expected); } + + #[rstest] + #[case( + "2026-08-16T09:59:59Z Login has completed\n2026-08-16T10:00:01Z Started IB Gateway", + false + )] + #[case( + "2026-08-16T09:59:59Z Login has completed\n2026-08-16T10:00:01Z Login successful", + true + )] + #[case("2026-08-16T10:00:00Z Login has completed", true)] + fn readiness_uses_only_current_session_logs(#[case] logs: &str, #[case] expected: bool) { + let started_at = "2026-08-16T10:00:00Z".parse::().unwrap(); + + assert_eq!( + DockerizedIBGateway::session_logs_indicate_ready(logs, started_at), + expected, + ); + } } diff --git a/crates/adapters/interactive_brokers/src/historical/client.rs b/crates/adapters/interactive_brokers/src/historical/client.rs index 99b263792b02..a862b40e4524 100644 --- a/crates/adapters/interactive_brokers/src/historical/client.rs +++ b/crates/adapters/interactive_brokers/src/historical/client.rs @@ -27,8 +27,8 @@ use ibapi::{ use jiff::Timestamp; use nautilus_core::UnixNanos; use nautilus_model::{ - data::{Bar, BarSpecification, BarType, Data, QuoteTick, TradeTick}, - enums::{AggregationSource, AggressorSide, BarAggregation, PriceType}, + data::{Bar, BarSpecification, BarType, Data, QuoteTick}, + enums::{AggregationSource, BarAggregation, PriceType}, identifiers::InstrumentId, instruments::{Instrument, any::InstrumentAny}, types::{Price, Quantity}, @@ -38,16 +38,23 @@ use crate::{ common::{ enums::IbHistoricalTickType, shared_client::{self, SharedClientHandle}, + symbology::is_crypto_contract, }, - config::InteractiveBrokersDataClientConfig, - data::convert::{ - apply_bar_price_magnifier, apply_price_magnifier, bar_request_segments, - bar_type_to_ib_bar_size, ib_bar_to_nautilus_bar, ib_timestamp_to_unix_nanos, - jiff_to_ib_datetime, price_type_to_ib_what_to_show_for_security, + config::{InteractiveBrokersDataClientConfig, MarketDataType}, + data::{ + convert::{ + apply_bar_price_magnifier, apply_price_magnifier, bar_request_segments, + bar_type_to_ib_bar_size, calculate_duration_segments, extend_historical_tick_batch, + ib_bar_to_nautilus_bar, ib_timestamp_to_unix_nanos, jiff_to_ib_datetime, + price_type_to_ib_what_to_show_for_security, retain_historical_ticks_in_range, + }, + parse::parse_trade_tick, }, providers::instruments::InteractiveBrokersInstrumentProvider, }; +const HISTORICAL_TICK_DEFAULT_LIMIT: usize = 10_000; + /// Historical data client for Interactive Brokers. /// /// This client provides methods for requesting historical bars and ticks @@ -96,11 +103,6 @@ impl Debug for HistoricalInteractiveBrokersClient { impl HistoricalInteractiveBrokersClient { /// Create a new historical data client. - /// - /// # Arguments - /// - /// * `ib_client` - The IB API client - /// * `instrument_provider` - The instrument provider pub fn new( ib_client: Arc, instrument_provider: Arc, @@ -133,7 +135,7 @@ impl HistoricalInteractiveBrokersClient { .await?; let client = shared_client.as_arc(); - if config.market_data_type != crate::config::MarketDataType::Realtime { + if config.market_data_type != MarketDataType::Realtime { let market_data_type: ibapi::market_data::MarketDataType = config.market_data_type.into(); client.switch_market_data_type(market_data_type).await?; @@ -180,7 +182,7 @@ impl HistoricalInteractiveBrokersClient { .await?; let client = shared_client.as_arc(); - if config.market_data_type != crate::config::MarketDataType::Realtime { + if config.market_data_type != MarketDataType::Realtime { let market_data_type: ibapi::market_data::MarketDataType = config.market_data_type.into(); client.switch_market_data_type(market_data_type).await?; @@ -206,17 +208,6 @@ impl HistoricalInteractiveBrokersClient { /// A warning is logged when the requested end date/time is in the past or /// the range spans more than one duration segment. /// - /// # Arguments - /// - /// * `bar_specifications` - List of bar specifications (e.g., "1-HOUR-LAST") - /// * `end_date_time` - End date for bars - /// * `start_date_time` - Optional start date - /// * `duration` - Optional duration string (e.g., "1 D") - /// * `contracts` - List of IB contracts - /// * `instrument_ids` - List of instrument IDs - /// * `use_rth` - Use regular trading hours only - /// * `timeout` - Request timeout in seconds - /// /// # Errors /// /// Returns an error if the request fails. @@ -243,11 +234,15 @@ impl HistoricalInteractiveBrokersClient { anyhow::bail!("Start date must be before end date"); } - if let Some(duration) = duration { - duration.parse::().with_context(|| { - format!("duration must be in format: 'int S|D|W|M|Y', was '{duration}'") + let duration = duration + .map(str::parse::) + .transpose() + .with_context(|| { + format!( + "duration must be in format: 'int S|D|W|M|Y', was '{}'", + duration.unwrap_or_default() + ) })?; - } let contracts = contracts.unwrap_or_default(); let instrument_ids = instrument_ids.unwrap_or_default(); @@ -327,40 +322,22 @@ impl HistoricalInteractiveBrokersClient { // Parse bar spec (e.g., "1-HOUR-LAST") let parts: Vec<&str> = bar_spec_str.split('-').collect(); if parts.len() != 3 { - anyhow::bail!("Invalid bar specification format: {}", bar_spec_str); + anyhow::bail!("Invalid bar specification format: {bar_spec_str}"); } let step = parts[0].parse::()?; let aggregation = parts[1].to_lowercase(); let price_type = parts[2].to_uppercase(); + let price_type = PriceType::from_str(&price_type) + .with_context(|| format!("Invalid bar price type: {}", parts[2]))?; let bar_spec = match aggregation.as_str() { - "second" => BarSpecification::new( - step, - BarAggregation::Second, - PriceType::from_str(&price_type).unwrap_or(PriceType::Last), - ), - "minute" => BarSpecification::new( - step, - BarAggregation::Minute, - PriceType::from_str(&price_type).unwrap_or(PriceType::Last), - ), - "hour" => BarSpecification::new( - step, - BarAggregation::Hour, - PriceType::from_str(&price_type).unwrap_or(PriceType::Last), - ), - "day" => BarSpecification::new( - step, - BarAggregation::Day, - PriceType::from_str(&price_type).unwrap_or(PriceType::Last), - ), - "week" => BarSpecification::new( - step, - BarAggregation::Week, - PriceType::from_str(&price_type).unwrap_or(PriceType::Last), - ), - _ => anyhow::bail!("Unsupported aggregation: {}", aggregation), + "second" => BarSpecification::new(step, BarAggregation::Second, price_type), + "minute" => BarSpecification::new(step, BarAggregation::Minute, price_type), + "hour" => BarSpecification::new(step, BarAggregation::Hour, price_type), + "day" => BarSpecification::new(step, BarAggregation::Day, price_type), + "week" => BarSpecification::new(step, BarAggregation::Week, price_type), + _ => anyhow::bail!("Unsupported aggregation: {aggregation}"), }; let instrument_id = self.resolve_instrument_id(&contract).await?; @@ -371,14 +348,14 @@ impl HistoricalInteractiveBrokersClient { // request AGGTRADES, not TRADES (TWS rejects TRADES for crypto, // error 10299) - same rule as the live data client's historical path. let ib_bar_size = bar_type_to_ib_bar_size(&bar_type_with_id)?; - let is_crypto = crate::common::parse::is_crypto_contract(&contract); + let is_crypto = is_crypto_contract(&contract); let ib_what_to_show = price_type_to_ib_what_to_show_for_security(bar_spec.price_type, is_crypto); // Omit the end date for continuous futures (IB error 10339). let is_continuous_future = contract.security_type == SecurityType::ContinuousFuture; let segments = bar_request_segments( - self.calculate_duration_segments(start_date_time, end_date_time, duration), + calculate_duration_segments(start_date_time, Some(end_date_time), duration), is_continuous_future, ); @@ -406,17 +383,17 @@ impl HistoricalInteractiveBrokersClient { ) .await .context(format!( - "Historical data request timed out after {} seconds", - timeout + "Historical data request timed out after {timeout} seconds" ))??; - // Get precision from instrument if available - let (price_precision, size_precision) = - if let Some(instrument) = self.instrument_provider.find(&instrument_id) { - (instrument.price_precision(), instrument.size_precision()) - } else { - (5, 0) // Default fallback - }; + let instrument = + self.instrument_provider + .find(&instrument_id) + .with_context(|| { + format!("Instrument {instrument_id} is missing from the provider") + })?; + let price_precision = instrument.price_precision(); + let size_precision = instrument.size_precision(); let price_magnifier = self.instrument_provider.get_price_magnifier(&instrument_id); @@ -445,17 +422,6 @@ impl HistoricalInteractiveBrokersClient { /// Request historical ticks with pagination support. /// - /// # Arguments - /// - /// * `tick_type` - historical tick type. - /// * `start_date_time` - Start date - /// * `end_date_time` - End date - /// * `contracts` - List of IB contracts - /// * `instrument_ids` - List of instrument IDs - /// * `use_rth` - Use regular trading hours only - /// * `timeout` - Request timeout in seconds - /// * `limit` - Maximum number of ticks to return, or 0 for no explicit limit - /// /// # Errors /// /// Returns an error if the request fails. @@ -475,7 +441,11 @@ impl HistoricalInteractiveBrokersClient { anyhow::bail!("Start date must be before end date"); } - let limit = (limit > 0).then_some(limit); + let limit = Some(if limit > 0 { + limit + } else { + HISTORICAL_TICK_DEFAULT_LIMIT + }); if end_date_time.duration_since(start_date_time) > jiff::SignedDuration::from_hours(24) { tracing::warn!( @@ -556,41 +526,44 @@ impl HistoricalInteractiveBrokersClient { for contract in all_contracts { let instrument_id = self.resolve_instrument_id(&contract).await?; - // Get precision from instrument if available - let (price_precision, size_precision) = - if let Some(instrument) = self.instrument_provider.find(&instrument_id) { - (instrument.price_precision(), instrument.size_precision()) - } else { - (5, 0) // Default fallback - }; + let instrument = self + .instrument_provider + .find(&instrument_id) + .with_context(|| { + format!("Instrument {instrument_id} is missing from the provider") + })?; + let price_precision = instrument.price_precision(); + let size_precision = instrument.size_precision(); let price_magnifier = self.instrument_provider.get_price_magnifier(&instrument_id); - let contract_start_len = all_ticks.len(); + let mut contract_ticks = Vec::new(); // Pagination loop for ticks (similar to Python _handle_timestamp_iteration) - let mut current_end_date = end_date_time; - let current_start_date = start_date_time; - let start_date_time_ns = - UnixNanos::from(u64::try_from(start_date_time.as_nanosecond()).unwrap_or_default()); - let end_date_time_ns = - UnixNanos::from(u64::try_from(end_date_time.as_nanosecond()).unwrap_or_default()); + let mut current_end_date = Some(end_date_time); + let current_start_date = Some(start_date_time); + let start_date_time_ns = UnixNanos::from( + u64::try_from(start_date_time.as_nanosecond()) + .context("Historical tick start date precedes the Unix epoch")?, + ); + let end_date_time_ns = UnixNanos::from( + u64::try_from(end_date_time.as_nanosecond()) + .context("Historical tick end date precedes the Unix epoch")?, + ); match tick_type { IbHistoricalTickType::Trades => { - loop { + while let Some(request_end) = current_end_date { // Make request for this batch let subscription = tokio::time::timeout( std::time::Duration::from_secs(timeout), self.ib_client .historical_ticks(&contract, 1000) - .starting(jiff_to_ib_datetime(¤t_start_date)) - .ending(jiff_to_ib_datetime(¤t_end_date)) + .ending(jiff_to_ib_datetime(&request_end)) .trading_hours(trading_hours) .trade(), ) .await .context(format!( - "Historical trades request timed out after {} seconds", - timeout + "Historical trades request timed out after {timeout} seconds" ))??; let mut subscription = subscription.filter_data(); @@ -614,92 +587,65 @@ impl HistoricalInteractiveBrokersClient { let converted_price = apply_price_magnifier(tick.price, price_magnifier); - let price = Price::new(converted_price, price_precision); - let size = Quantity::new(tick.size as f64, size_precision); + let Some(raw_size) = tick.size else { + tracing::warn!( + "Skipping historical trade tick with no size for {}", + instrument_id + ); + continue; + }; - let trade_tick = TradeTick::new( + if raw_size == 0.0 { + tracing::warn!( + "Skipping historical trade tick with zero size for {instrument_id}" + ); + continue; + } + let trade_tick = parse_trade_tick( instrument_id, - price, - size, - AggressorSide::NoAggressor, - crate::common::parse::generate_ib_trade_id( - ts_event, - converted_price, - tick.size as f64, - ), + converted_price, + raw_size, + price_precision, + size_precision, ts_event, ts_init, - ); + None, + ) + .with_context(|| { + format!("Invalid historical trade tick for {instrument_id}") + })?; batch_ticks.push(Data::Trade(trade_tick)); } - if batch_ticks.is_empty() { - break; - } - - // Update current_end_date to the minimum ts_event from this batch for next iteration - // This works backwards in time - if let Some(min_tick) = batch_ticks.iter().min_by_key(|t| match t { - Data::Trade(t) => t.ts_event, - _ => UnixNanos::default(), - }) { - let min_ts_nanos = match min_tick { - Data::Trade(t) => t.ts_event.as_u64(), - _ => break, - }; - - if let Some(new_end) = retreat_end_datetime(min_ts_nanos) { - current_end_date = new_end; - } else { - break; - } - } - - all_ticks.extend(batch_ticks); - - if let Some(limit) = limit - && all_ticks.len() - contract_start_len >= limit - { - break; - } - - // Check if we should continue - need current_end > current_start - if !should_continue_backward_pagination( - current_end_date, + if !extend_historical_tick_batch( + &mut contract_ticks, + batch_ticks, current_start_date, + &mut current_end_date, + Some(start_date_time_ns), + Some(end_date_time_ns), + limit, + data_ts_event, ) { break; } - - // Filter out ticks outside the requested range if needed - all_ticks.retain(|t| match t { - Data::Quote(q) => { - q.ts_event >= start_date_time_ns && q.ts_event <= end_date_time_ns - } - Data::Trade(t) => { - t.ts_event >= start_date_time_ns && t.ts_event <= end_date_time_ns - } - _ => true, - }); } } IbHistoricalTickType::BidAsk => { - loop { + while let Some(request_end) = current_end_date { // Make request for this batch let subscription = tokio::time::timeout( std::time::Duration::from_secs(timeout), self.ib_client .historical_ticks(&contract, 1000) - .starting(jiff_to_ib_datetime(¤t_start_date)) - .ending(jiff_to_ib_datetime(¤t_end_date)) + .ending(jiff_to_ib_datetime(&request_end)) .trading_hours(trading_hours) .bid_ask(IgnoreSize::No), ) .await .context(format!( - "Historical bid/ask ticks request timed out after {} seconds", - timeout + "Historical bid/ask ticks request timed out after {timeout} seconds" ))??; let mut subscription = subscription.filter_data(); @@ -721,16 +667,43 @@ impl HistoricalInteractiveBrokersClient { let ts_init = ts_event; - let bid_price = Price::new( - apply_price_magnifier(tick.price_bid, price_magnifier), - price_precision, - ); - let bid_size = Quantity::new(tick.size_bid as f64, size_precision); - let ask_price = Price::new( - apply_price_magnifier(tick.price_ask, price_magnifier), - price_precision, - ); - let ask_size = Quantity::new(tick.size_ask as f64, size_precision); + let raw_bid_price = + apply_price_magnifier(tick.price_bid, price_magnifier); + let bid_price = Price::new_checked(raw_bid_price, price_precision) + .with_context(|| { + format!( + "Invalid historical bid price {raw_bid_price} for {instrument_id}" + ) + })?; + let (Some(raw_bid_size), Some(raw_ask_size)) = + (tick.size_bid, tick.size_ask) + else { + tracing::warn!( + "Skipping historical quote tick with an absent size for {}", + instrument_id + ); + continue; + }; + let bid_size = Quantity::new_checked(raw_bid_size, size_precision) + .with_context(|| { + format!( + "Invalid historical bid size {raw_bid_size} for {instrument_id}" + ) + })?; + let raw_ask_price = + apply_price_magnifier(tick.price_ask, price_magnifier); + let ask_price = Price::new_checked(raw_ask_price, price_precision) + .with_context(|| { + format!( + "Invalid historical ask price {raw_ask_price} for {instrument_id}" + ) + })?; + let ask_size = Quantity::new_checked(raw_ask_size, size_precision) + .with_context(|| { + format!( + "Invalid historical ask size {raw_ask_size} for {instrument_id}" + ) + })?; let quote_tick = QuoteTick::new( instrument_id, @@ -745,78 +718,40 @@ impl HistoricalInteractiveBrokersClient { batch_ticks.push(Data::Quote(quote_tick)); } - if batch_ticks.is_empty() { - break; - } - - // Update current_end_date to the minimum ts_event from this batch for next iteration - if let Some(min_tick) = batch_ticks.iter().min_by_key(|t| match t { - Data::Quote(q) => q.ts_event, - _ => UnixNanos::default(), - }) { - let min_ts_nanos = match min_tick { - Data::Quote(q) => q.ts_event.as_u64(), - _ => break, - }; - - if let Some(new_end) = retreat_end_datetime(min_ts_nanos) { - current_end_date = new_end; - } else { - break; - } - } - - all_ticks.extend(batch_ticks); - - if let Some(limit) = limit - && all_ticks.len() - contract_start_len >= limit - { - break; - } - - // Check if we should continue - if !should_continue_backward_pagination( - current_end_date, + if !extend_historical_tick_batch( + &mut contract_ticks, + batch_ticks, current_start_date, + &mut current_end_date, + Some(start_date_time_ns), + Some(end_date_time_ns), + limit, + data_ts_event, ) { break; } - - // Filter out ticks outside the requested range if needed - all_ticks.retain(|t| match t { - Data::Quote(q) => { - q.ts_event >= start_date_time_ns && q.ts_event <= end_date_time_ns - } - Data::Trade(t) => { - t.ts_event >= start_date_time_ns && t.ts_event <= end_date_time_ns - } - _ => true, - }); } } } - if let Some(limit) = limit { - let mut contract_ticks = all_ticks.split_off(contract_start_len); - contract_ticks.sort_by_key(|tick| match tick { - Data::Quote(q) => q.ts_event, - Data::Trade(t) => t.ts_event, - _ => UnixNanos::default(), - }); + retain_historical_ticks_in_range( + &mut contract_ticks, + Some(start_date_time_ns), + Some(end_date_time_ns), + data_ts_event, + ); + contract_ticks.sort_by_key(data_ts_event); - if contract_ticks.len() > limit { - contract_ticks = contract_ticks.split_off(contract_ticks.len() - limit); - } - all_ticks.extend(contract_ticks); + if let Some(limit) = limit + && contract_ticks.len() > limit + { + contract_ticks = contract_ticks.split_off(contract_ticks.len() - limit); } + all_ticks.extend(contract_ticks); } // Sort by timestamp - all_ticks.sort_by_key(|tick| match tick { - Data::Quote(q) => q.ts_event, - Data::Trade(t) => t.ts_event, - _ => UnixNanos::default(), - }); + all_ticks.sort_by_key(data_ts_event); Ok(all_ticks) } @@ -825,11 +760,6 @@ impl HistoricalInteractiveBrokersClient { /// /// This method uses the instrument provider to load and return instruments. /// - /// # Arguments - /// - /// * `instrument_ids` - Optional list of instrument IDs - /// * `contracts` - Optional list of IB contracts - /// /// # Returns /// /// Returns a list of instruments. @@ -888,22 +818,9 @@ impl HistoricalInteractiveBrokersClient { // Convert contract to instrument ID using provider's venue determination // This matches Python's logic: venue = instrument_provider.determine_venue_from_contract(contract) let venue = self.instrument_provider.determine_venue(&contract, None); - match self.instrument_provider.symbology_method() { - crate::config::SymbologyMethod::Simplified => { - crate::common::parse::ib_contract_to_instrument_id_simplified( - &contract, - Some(venue), - ) - .ok() - } - crate::config::SymbologyMethod::Raw => { - crate::common::parse::ib_contract_to_instrument_id_raw( - &contract, - Some(venue), - ) - .ok() - } - } + self.instrument_provider + .instrument_id_from_contract(&contract, venue) + .ok() }; if let Some(instrument_id) = instrument_id { @@ -939,10 +856,9 @@ impl HistoricalInteractiveBrokersClient { .instrument_provider .get_instrument(&self.ib_client, &contract) .await + && !loaded_instruments.iter().any(|i| i.id() == instrument.id()) { - if !loaded_instruments.iter().any(|i| i.id() == instrument.id()) { - loaded_instruments.push(instrument); - } + loaded_instruments.push(instrument); } } } @@ -952,110 +868,6 @@ impl HistoricalInteractiveBrokersClient { Ok(loaded_instruments) } - /// Calculate duration segments for a time range. - /// - /// This breaks down large date ranges into smaller segments that IB can handle. - /// - /// # Arguments - /// - /// * `start_date` - Optional start date - /// * `end_date` - End date - /// * `duration` - Optional duration string - /// - /// # Returns - /// - /// Returns a list of (end_date, duration) tuples. - fn calculate_duration_segments( - &self, - start_date: Option, - end_date: Timestamp, - duration: Option<&str>, - ) -> Vec<(Timestamp, historical::Duration)> { - // If duration is specified, use it directly - if let Some(dur_str) = duration { - if let Ok(dur) = dur_str.parse::() { - return vec![(end_date, dur)]; - } else { - tracing::warn!("Invalid duration format: {}, using default", dur_str); - } - } - - // Calculate from start/end dates - matching Python's comprehensive breakdown - if let Some(start) = start_date { - let total_delta = end_date.duration_since(start); - let total_days = total_delta.as_secs() / (24 * 60 * 60); - - let mut segments = Vec::new(); - - // Calculate full years in the time delta (matching Python: years = total_delta.days // 365) - let years = total_days / 365; - let minus_years_date = if years > 0 { - end_date - jiff::SignedDuration::from_hours(24 * (365 * years)) - } else { - end_date - }; - - // Calculate remaining days after subtracting full years (matching Python logic) - let days = if years > 0 { - let remaining_delta = minus_years_date.duration_since(start); - remaining_delta.as_secs() / (24 * 60 * 60) - } else { - total_days - }; - - let minus_days_date = if days > 0 { - minus_years_date - jiff::SignedDuration::from_hours(24 * (days)) - } else { - minus_years_date - }; - - // Calculate remaining time in seconds after subtracting years and days - // Matching Python: hours*3600 + minutes*60 + seconds + subsecond - let remaining_delta = minus_days_date.duration_since(start); - // Extract time components from the remaining delta - let total_secs = remaining_delta.as_secs(); - let hours = total_secs / 3600; - let minutes = (total_secs % 3600) / 60; - let secs = total_secs % 60; - // Check for subsecond precision (milliseconds, microseconds, nanoseconds) - let subsecond = if remaining_delta.as_millis() % 1000 > 0 - || remaining_delta.as_micros() % 1000 > 0 - || remaining_delta.as_nanos() % 1000 > 0 - { - 1 - } else { - 0 - }; - let seconds = hours * 3600 + minutes * 60 + secs + subsecond; - - // Build segments in order: years, days, seconds (matching Python order) - if years > 0 { - segments.push((end_date, historical::Duration::years(years as i32))); - } - - if days > 0 { - segments.push((minus_years_date, historical::Duration::days(days as i32))); - } - - if seconds > 0 { - segments.push(( - minus_days_date, - historical::Duration::seconds(seconds as i32), - )); - } - - if segments.is_empty() { - // Default to 1 day if calculation results in nothing - segments.push((end_date, historical::Duration::days(1))); - } - - segments - } else { - // Default to 1 day if no start date - vec![(end_date, historical::Duration::days(1))] - } - } - async fn resolve_instrument_id(&self, contract: &Contract) -> anyhow::Result { if let Some(instrument_id) = self .instrument_provider @@ -1065,15 +877,10 @@ impl HistoricalInteractiveBrokersClient { } let venue = self.instrument_provider.determine_venue(contract, None); - let parsed = match self.instrument_provider.symbology_method() { - crate::config::SymbologyMethod::Simplified => { - crate::common::parse::ib_contract_to_instrument_id_simplified(contract, Some(venue)) - .ok() - } - crate::config::SymbologyMethod::Raw => { - crate::common::parse::ib_contract_to_instrument_id_raw(contract, Some(venue)).ok() - } - }; + let parsed = self + .instrument_provider + .instrument_id_from_contract(contract, venue) + .ok(); if let Some(instrument_id) = parsed { return Ok(instrument_id); @@ -1096,51 +903,10 @@ impl HistoricalInteractiveBrokersClient { } } -fn retreat_end_datetime(min_ts_nanos: u64) -> Option { - let new_end_nanos = min_ts_nanos.saturating_sub(1_000_000); // 1ms - Timestamp::from_nanosecond(i128::from(new_end_nanos)).ok() -} - -fn should_continue_backward_pagination( - current_end_date: Timestamp, - current_start_date: Timestamp, -) -> bool { - current_end_date > current_start_date -} - -#[cfg(test)] -mod tests { - use jiff::Timestamp; - use rstest::rstest; - - use super::{retreat_end_datetime, should_continue_backward_pagination}; - - #[rstest] - fn test_retreat_end_datetime_subtracts_one_millisecond() { - let ts_nanos = 1_700_000_000_123_456_789_u64; - let result = retreat_end_datetime(ts_nanos).unwrap(); - assert_eq!( - u64::try_from(result.as_nanosecond()).unwrap(), - ts_nanos - 1_000_000 - ); - } - - #[rstest] - fn test_retreat_end_datetime_saturates_at_zero() { - let result = retreat_end_datetime(500_000).unwrap(); - assert_eq!(result.as_nanosecond(), 0); - } - - #[rstest] - fn test_should_continue_backward_pagination_true_when_end_after_start() { - let start = "2025-01-01T00:00:00Z".parse::().unwrap(); - let end = "2025-01-01T00:00:01Z".parse::().unwrap(); - assert!(should_continue_backward_pagination(end, start)); - } - - #[rstest] - fn test_should_continue_backward_pagination_false_when_end_equal_start() { - let start = "2025-01-01T00:00:00Z".parse::().unwrap(); - assert!(!should_continue_backward_pagination(start, start)); +fn data_ts_event(data: &Data) -> UnixNanos { + match data { + Data::Trade(tick) => tick.ts_event, + Data::Quote(tick) => tick.ts_event, + _ => UnixNanos::default(), } } diff --git a/crates/adapters/interactive_brokers/src/lib.rs b/crates/adapters/interactive_brokers/src/lib.rs index 15c7847392a8..b3a99e86f6b6 100644 --- a/crates/adapters/interactive_brokers/src/lib.rs +++ b/crates/adapters/interactive_brokers/src/lib.rs @@ -80,7 +80,7 @@ pub mod common; pub mod config; pub mod data; -pub mod error; +pub mod data_types; pub mod execution; pub mod factories; pub mod gateway; @@ -89,3 +89,6 @@ pub mod providers; #[cfg(feature = "python")] pub mod python; + +#[cfg(test)] +mod stubs; diff --git a/crates/adapters/interactive_brokers/src/providers/instruments.rs b/crates/adapters/interactive_brokers/src/providers/instruments.rs index 0ed9076ea092..181e66a081a8 100644 --- a/crates/adapters/interactive_brokers/src/providers/instruments.rs +++ b/crates/adapters/interactive_brokers/src/providers/instruments.rs @@ -25,6 +25,7 @@ use ibapi::{ subscriptions::SubscriptionItem, }; use jiff::{Span, Timestamp, tz::Offset}; +use nautilus_common::cache::Cache; use nautilus_model::{ identifiers::{InstrumentId, Venue}, instruments::{Instrument, InstrumentAny}, @@ -33,17 +34,26 @@ use serde::{Deserialize, Serialize}; use crate::{ common::{ - contracts::parse_contract_from_json, - enums::IbAction, - parse::{ - create_spread_instrument_id, determine_venue_from_contract, exchange_to_mic_venue, - ib_contract_to_instrument_id_raw, ib_contract_to_instrument_id_simplified, - instrument_id_to_ib_contract, is_spread_instrument_id, - parse_spread_instrument_id_to_legs, possible_exchanges_for_venue, + Symbology, + contracts::{ + ConfiguredContract, KEY_BUILD_FUTURES_CHAIN, KEY_BUILD_OPTIONS_CHAIN, + KEY_MAX_EXPIRY_DAYS, KEY_MIN_EXPIRY_DAYS, KEY_OPTIONS_CHAIN_EXCHANGE, + KEY_OPTIONS_CHAIN_EXCHANGE_ALT, parse_contract_from_json, + }, + enums::{IbAction, IbSecurityType}, + spreads::{ + create_spread_instrument_id, is_spread_instrument_id, + parse_spread_instrument_id_to_legs, + }, + symbology::{ + determine_venue_from_contract, exchange_to_mic_venue, possible_exchanges_for_venue, }, }, config::{InteractiveBrokersInstrumentProviderConfig, SymbologyMethod}, - providers::parse::{parse_ib_contract_to_instrument, parse_spread_instrument_any}, + providers::parse::{ + expiry_timestring_to_unix_nanos, parse_ib_contract_to_instrument, + parse_spread_instrument_any, + }, }; /// Cache structure for persistent instrument caching. @@ -87,6 +97,8 @@ struct InstrumentCache { pub struct InteractiveBrokersInstrumentProvider { /// Configuration for the provider. config: InteractiveBrokersInstrumentProviderConfig, + /// Contract and instrument ID mapping policy. + symbology: Symbology, /// Cache mapping contract IDs to instrument IDs. contract_id_to_instrument_id: Arc>, /// Cache mapping instrument IDs to instruments. @@ -109,7 +121,7 @@ trait StartupInstrumentLoader { async fn load_contract( &self, - contract_spec: &serde_json::Value, + configured: &ConfiguredContract, ) -> anyhow::Result>; } @@ -130,25 +142,22 @@ impl StartupInstrumentLoader for IbStartupInstrumentLoader<'_> { async fn load_contract( &self, - contract_spec: &serde_json::Value, + configured: &ConfiguredContract, ) -> anyhow::Result> { - let contract = parse_contract_from_json(contract_spec) - .context("Failed to parse configured IB contract")?; + let spec = configured.chain_spec_json(); self.provider - .load_contract_spec(self.client, &contract, Some(contract_spec)) + .load_contract_spec(self.client, &configured.contract, spec.as_ref()) .await } } impl InteractiveBrokersInstrumentProvider { /// Create a new `InteractiveBrokersInstrumentProvider`. - /// - /// # Arguments - /// - /// * `config` - Configuration for the provider pub fn new(config: InteractiveBrokersInstrumentProviderConfig) -> Self { + let symbology = Symbology::new(config.symbology_method); Self { config, + symbology, contract_id_to_instrument_id: Arc::new(DashMap::new()), instruments: Arc::new(DashMap::new()), contract_details: Arc::new(DashMap::new()), @@ -274,16 +283,14 @@ impl InteractiveBrokersInstrumentProvider { } } - for (index, contract_spec) in self.config.load_contracts.iter().enumerate() { - let mut contract_ids = - loader.load_contract(contract_spec).await.with_context(|| { - format!( - "Failed to load configured IB contract at index {index}: {contract_spec}" - ) - })?; + for (index, configured) in self.config.load_contracts.iter().enumerate() { + let contract = &configured.contract; + let mut contract_ids = loader.load_contract(configured).await.with_context(|| { + format!("Failed to load configured IB contract at index {index}: {contract:?}") + })?; if contract_ids.is_empty() { - unresolved.push(format!("contract at index {index}: {contract_spec}")); + unresolved.push(format!("contract at index {index}: {contract:?}")); } else { loaded_ids.append(&mut contract_ids); } @@ -301,6 +308,18 @@ impl InteractiveBrokersInstrumentProvider { Ok(loaded_ids) } + pub(crate) fn seed_from_cache(&self, cache: &Cache) -> usize { + let instruments = cache + .instrument_ids(None) + .into_iter() + .filter_map(|instrument_id| cache.instrument(instrument_id).cloned()); + let count = self.add_cached_instruments(instruments); + if count > 0 { + tracing::debug!("Seeded IB instrument provider with {count} cached instruments"); + } + count + } + /// Adds instruments already held by the Nautilus cache into the provider cache. /// /// This mirrors the Python provider's use of `client._cache` for venue resolution and for @@ -316,14 +335,18 @@ impl InteractiveBrokersInstrumentProvider { let Some(contract) = contract_from_instrument_info(&instrument) else { continue; }; - let price_magnifier = price_magnifier_from_instrument_info(&instrument); + let Some(price_magnifier) = + price_magnifier_from_instrument_info(&instrument).filter(|value| *value >= 0) + else { + continue; + }; if self.cache_instrument( instrument_id, instrument, None, Some(contract), - price_magnifier, + Some(price_magnifier), false, ) { added += 1; @@ -337,10 +360,6 @@ impl InteractiveBrokersInstrumentProvider { /// This is equivalent to Python's `determine_venue_from_contract` method. /// It uses the config's symbol-to-venue mapping and exchange-to-venue conversion settings. /// - /// # Arguments - /// - /// * `contract` - The IB contract - /// /// # Returns /// /// The determined venue. @@ -415,16 +434,12 @@ impl InteractiveBrokersInstrumentProvider { } /// Get the symbology method from the provider configuration. - pub fn symbology_method(&self) -> crate::config::SymbologyMethod { - self.config.symbology_method + pub fn symbology_method(&self) -> SymbologyMethod { + self.symbology.method() } /// Get an instrument by its ID. /// - /// # Arguments - /// - /// * `instrument_id` - The instrument ID to look up - /// /// # Returns /// /// Returns the instrument if found, `None` otherwise. @@ -445,26 +460,17 @@ impl InteractiveBrokersInstrumentProvider { /// Get an instrument by contract ID. /// - /// # Arguments - /// - /// * `contract_id` - The IB contract ID to look up - /// /// # Returns /// /// Returns the instrument if found, `None` otherwise. #[must_use] pub fn find_by_contract_id(&self, contract_id: i32) -> Option { - self.contract_id_to_instrument_id - .get(&contract_id) - .and_then(|entry| self.find(entry.value())) + self.get_instrument_id_by_contract_id(contract_id) + .and_then(|instrument_id| self.find(&instrument_id)) } /// Get an instrument ID by contract ID. /// - /// # Arguments - /// - /// * `contract_id` - The IB contract ID to look up - /// /// # Returns /// /// Returns the instrument ID if found, `None` otherwise. @@ -499,17 +505,31 @@ impl InteractiveBrokersInstrumentProvider { let venue = self.determine_venue(contract, None); - match self.config.symbology_method { - SymbologyMethod::Simplified => { - ib_contract_to_instrument_id_simplified(contract, Some(venue)) - } - SymbologyMethod::Raw => ib_contract_to_instrument_id_raw(contract, Some(venue)), - } + self.symbology.instrument_id(contract, Some(venue)) } fn resolve_spread_instrument_id_for_contract( &self, contract: &Contract, + ) -> anyhow::Result { + let spread_instrument_id = self.spread_instrument_id_for_contract(contract)?; + + if self.find(&spread_instrument_id).is_none() { + anyhow::bail!("Resolved BAG spread {spread_instrument_id} is not cached"); + } + + Ok(spread_instrument_id) + } + + /// Returns the spread instrument ID of a BAG contract from its cached leg instruments. + /// + /// # Errors + /// + /// Returns an error if the contract has no combo legs, a leg is not cached, or a leg action + /// is invalid. + pub fn spread_instrument_id_for_contract( + &self, + contract: &Contract, ) -> anyhow::Result { if contract.combo_legs.is_empty() { anyhow::bail!("Cannot resolve BAG contract without combo legs or cached contract ID"); @@ -534,31 +554,19 @@ impl InteractiveBrokersInstrumentProvider { leg_tuples.push((leg_instrument_id, ratio)); } - let spread_instrument_id = create_spread_instrument_id(&leg_tuples) - .context("Failed to create spread instrument ID from BAG combo legs")?; - - if self.find(&spread_instrument_id).is_none() { - anyhow::bail!("Resolved BAG spread {spread_instrument_id} is not cached"); - } - - Ok(spread_instrument_id) + create_spread_instrument_id(&leg_tuples) + .context("Failed to create spread instrument ID from BAG combo legs") } /// Check if a security type should be filtered. /// - /// # Arguments - /// - /// * `sec_type` - The security type to check - /// /// # Returns /// /// Returns `true` if the security type should be filtered. #[must_use] pub fn is_filtered_sec_type(&self, sec_type: &str) -> bool { - self.config - .filter_sec_types - .iter() - .any(|filtered| filtered.eq_ignore_ascii_case(sec_type)) + IbSecurityType::from_str(sec_type) + .is_ok_and(|sec_type| self.config.filter_sec_types.contains(&sec_type)) } /// Get all cached instruments. @@ -593,10 +601,6 @@ impl InteractiveBrokersInstrumentProvider { /// If not found, it falls back to checking contract details. If still not found, /// it returns the default value of 1 and logs a warning if the instrument exists. /// - /// # Arguments - /// - /// * `instrument_id` - The instrument ID to look up - /// /// # Returns /// /// Returns the price magnifier if found, otherwise 1. @@ -637,11 +641,6 @@ impl InteractiveBrokersInstrumentProvider { /// This is equivalent to Python's `get_instrument` method. /// Supports BAG contracts by auto-loading legs. /// - /// # Arguments - /// - /// * `client` - The IB API client - /// * `contract` - The IB contract to get instrument for - /// /// # Returns /// /// Returns the instrument if found, `None` otherwise. @@ -664,7 +663,7 @@ impl InteractiveBrokersInstrumentProvider { contract.last_trade_date_or_contract_month.as_str() ); // Check if security type is filtered - let sec_type_str = security_type_code(&contract.security_type); + let sec_type_str = contract.security_type.to_string(); if self.is_filtered_sec_type(&sec_type_str) { tracing::debug!( "Skipping filtered security type {} for contract", @@ -745,22 +744,23 @@ impl InteractiveBrokersInstrumentProvider { spec: Option<&serde_json::Value>, ) -> anyhow::Result> { let mut loaded_ids = Vec::new(); - let build_futures_chain = json_bool(spec, "build_futures_chain") - || self.config.build_futures_chain.unwrap_or(false); - let build_options_chain = json_bool(spec, "build_options_chain") - || self.config.build_options_chain.unwrap_or(false); - let min_expiry_days = json_u32(spec, "min_expiry_days").or(self.config.min_expiry_days); - let max_expiry_days = json_u32(spec, "max_expiry_days").or(self.config.max_expiry_days); - let options_chain_exchange = json_string(spec, "options_chain_exchange") - .or_else(|| json_string(spec, "optionsChainExchange")); + let build_futures_chain = json_opt_bool(spec, KEY_BUILD_FUTURES_CHAIN) + .or(self.config.build_futures_chain) + .unwrap_or(false); + let build_options_chain = json_opt_bool(spec, KEY_BUILD_OPTIONS_CHAIN) + .or(self.config.build_options_chain) + .unwrap_or(false); + let min_expiry_days = json_u32(spec, KEY_MIN_EXPIRY_DAYS).or(self.config.min_expiry_days); + let max_expiry_days = json_u32(spec, KEY_MAX_EXPIRY_DAYS).or(self.config.max_expiry_days); + let options_chain_exchange = json_string(spec, KEY_OPTIONS_CHAIN_EXCHANGE) + .or_else(|| json_string(spec, KEY_OPTIONS_CHAIN_EXCHANGE_ALT)); let chain_contract = if contract.security_type == SecurityType::ContinuousFuture && (build_futures_chain || build_options_chain) { match client.contract_details(contract).await { Ok(details_vec) => details_vec .into_iter() - .next() - .map(|details| { + .next().map_or_else(|| contract.clone(), |details| { tracing::debug!( "Qualified continuous future contract {}.{} as local_symbol={} trading_class={} con_id={}", contract.symbol.as_str(), @@ -770,8 +770,7 @@ impl InteractiveBrokersInstrumentProvider { details.contract.contract_id, ); details.contract - }) - .unwrap_or_else(|| contract.clone()), + }), Err(e) if e.is_connection_lost() => { return Err(e).context("Failed to qualify continuous future contract"); } @@ -819,22 +818,27 @@ impl InteractiveBrokersInstrumentProvider { if build_options_chain { let expiry_min = expiry_bound_from_days(min_expiry_days); let expiry_max = expiry_bound_from_days(max_expiry_days); + // Options list on the requested exchange, which can differ from the exchange the + // underlying resolves to (SPY requested on SMART resolves to ARCA) + let options_exchange = options_chain_exchange + .as_deref() + .unwrap_or_else(|| contract.exchange.as_str()); let mut underlyings = Vec::new(); if contract.security_type == SecurityType::ContinuousFuture { - if !build_futures_chain { - self.fetch_futures_chain( - client, - chain_contract.symbol.as_str(), - chain_contract.exchange.as_str(), - chain_contract.currency.as_str(), - chain_trading_class, - true, - min_expiry_days, - max_expiry_days, - ) - .await?; - } + // The expiry window bounds the options, not their underlying futures: a weekly + // ES option expiring this week belongs to a future that expires months later + self.fetch_futures_chain( + client, + chain_contract.symbol.as_str(), + chain_contract.exchange.as_str(), + chain_contract.currency.as_str(), + chain_trading_class, + true, + Some(0), + Some(u32::MAX), + ) + .await?; underlyings.extend( self.cached_contracts_for( @@ -860,7 +864,7 @@ impl InteractiveBrokersInstrumentProvider { &underlying, expiry_min.as_deref(), expiry_max.as_deref(), - options_chain_exchange.as_deref(), + Some(options_exchange), ) .await?; tracing::debug!( @@ -871,15 +875,11 @@ impl InteractiveBrokersInstrumentProvider { ); } - loaded_ids.extend( - self.cached_contract_ids_for( - contract.symbol.as_str(), - options_chain_exchange - .as_deref() - .unwrap_or_else(|| contract.exchange.as_str()), - &[SecurityType::Option, SecurityType::FuturesOption], - ), - ); + loaded_ids.extend(self.cached_contract_ids_for( + contract.symbol.as_str(), + options_exchange, + &[SecurityType::Option, SecurityType::FuturesOption], + )); } if !build_futures_chain @@ -935,10 +935,6 @@ impl InteractiveBrokersInstrumentProvider { /// /// This is equivalent to Python's `instrument_id_to_ib_contract_details` method. /// - /// # Arguments - /// - /// * `instrument_id` - The instrument ID to convert - /// /// # Returns /// /// Returns the contract details if found, `None` otherwise. @@ -978,7 +974,12 @@ impl InteractiveBrokersInstrumentProvider { return Ok(contract); } - instrument_id_to_ib_contract(instrument_id, None) + let currency = self + .instruments + .get(&instrument_id) + .map(|instrument| instrument.quote_currency().code.to_string()); + self.symbology + .contract_with_currency(instrument_id, None, currency.as_deref()) } pub async fn resolve_contract_for_instrument_async( @@ -1003,46 +1004,10 @@ impl InteractiveBrokersInstrumentProvider { self.resolve_contract_for_instrument(instrument_id) } - /// Load a single instrument (does not return loaded IDs). - /// - /// This is equivalent to Python's `load_async` method. - /// - /// # Arguments - /// - /// * `client` - The IB API client - /// * `instrument_id` - The instrument ID to load - /// * `force_instrument_update` - If true, force re-fetch even if already cached - /// - /// # Errors - /// - /// Returns an error if loading fails. - pub async fn load_async( - &self, - client: &ibapi::Client, - instrument_id: InstrumentId, - filters: Option>, - ) -> anyhow::Result<()> { - let filters: Option> = filters; - let force_instrument_update = filters - .as_ref() - .and_then(|f| f.get("force_instrument_update")) - .map(|v| v == "true") - .unwrap_or(false); - - self.fetch_contract_details(client, instrument_id, force_instrument_update, filters) - .await - } - /// Load a single instrument and return the loaded instrument ID. /// /// This is equivalent to Python's `load_with_return_async` method. /// - /// # Arguments - /// - /// * `client` - The IB API client - /// * `instrument_id` - The instrument ID to load - /// * `force_instrument_update` - If true, force re-fetch even if already cached - /// /// # Returns /// /// Returns the loaded instrument ID if successful, `None` otherwise. @@ -1060,8 +1025,7 @@ impl InteractiveBrokersInstrumentProvider { let force_instrument_update = filters .as_ref() .and_then(|f| f.get("force_instrument_update")) - .map(|v| v == "true") - .unwrap_or(false); + .is_some_and(|v| v == "true"); if is_spread_instrument_id(&instrument_id) { self.fetch_spread_instrument(client, instrument_id, force_instrument_update, filters) @@ -1078,78 +1042,10 @@ impl InteractiveBrokersInstrumentProvider { } } - pub async fn load_contract_with_return_async( - &self, - client: &ibapi::Client, - contract: &Contract, - spec: Option<&serde_json::Value>, - ) -> anyhow::Result> { - self.load_contract_spec(client, contract, spec).await - } - - /// Load multiple instruments (does not return loaded IDs). - /// - /// This is equivalent to Python's `load_ids_async` method. - /// - /// # Arguments - /// - /// * `client` - The IB API client - /// * `instrument_ids` - Vector of instrument IDs to load - /// * `force_instrument_update` - If true, force re-fetch even if already cached - /// - /// # Errors - /// - /// Returns an error if loading fails. - pub async fn load_ids_async( - &self, - client: &ibapi::Client, - instrument_ids: Vec, - filters: Option>, - ) -> anyhow::Result<()> { - let filters: Option> = filters; - let force_instrument_update = filters - .as_ref() - .and_then(|f| f.get("force_instrument_update")) - .map(|v| v == "true") - .unwrap_or(false); - - for instrument_id in instrument_ids { - let load_result = if is_spread_instrument_id(&instrument_id) { - self.fetch_spread_instrument( - client, - instrument_id, - force_instrument_update, - filters.clone(), - ) - .await - .map(|_| ()) - } else { - self.fetch_contract_details( - client, - instrument_id, - force_instrument_update, - filters.clone(), - ) - .await - }; - - if let Err(e) = load_result { - tracing::warn!("Failed to load instrument {}: {}", instrument_id, e); - } - } - Ok(()) - } - /// Load multiple instruments and return the loaded instrument IDs. /// /// This is equivalent to Python's `load_ids_with_return_async` method. /// - /// # Arguments - /// - /// * `client` - The IB API client - /// * `instrument_ids` - Vector of instrument IDs to load - /// * `force_instrument_update` - If true, force re-fetch even if already cached - /// /// # Returns /// /// Returns a vector of successfully loaded instrument IDs. @@ -1214,15 +1110,15 @@ impl InteractiveBrokersInstrumentProvider { .collect(); Ok(Contract { - contract_id: 0, symbol: first_details.contract.symbol.clone(), security_type: SecurityType::Spread, exchange: Exchange::from("SMART"), currency: first_details.contract.currency.clone(), local_symbol: instrument_id.map_or_else(String::new, |id| id.symbol.to_string()), - combo_legs_description: instrument_id - .map(|id| format!("Spread: {}", id.symbol)) - .unwrap_or_else(|| "Spread".to_string()), + combo_legs_description: instrument_id.map_or_else( + || "Spread".to_string(), + |id| format!("Spread: {}", id.symbol), + ), combo_legs, ..Default::default() }) @@ -1234,12 +1130,6 @@ impl InteractiveBrokersInstrumentProvider { /// It parses the spread instrument ID to extract leg tuples, loads each leg, /// and then creates the spread instrument. /// - /// # Arguments - /// - /// * `client` - The IB API client - /// * `spread_instrument_id` - The spread instrument ID to fetch - /// * `force_instrument_update` - If true, force re-fetch even if already cached - /// /// # Returns /// /// Returns `true` if the spread instrument was successfully loaded, `false` otherwise. @@ -1293,7 +1183,7 @@ impl InteractiveBrokersInstrumentProvider { filters.clone(), ) .await - .with_context(|| format!("Failed to load leg instrument: {}", leg_instrument_id))?; + .with_context(|| format!("Failed to load leg instrument: {leg_instrument_id}"))?; // Get the contract details for this leg let leg_details = self @@ -1302,8 +1192,7 @@ impl InteractiveBrokersInstrumentProvider { .map(|entry| entry.value().clone()) .ok_or_else(|| { anyhow::anyhow!( - "Leg instrument {} not found in contract details after loading", - leg_instrument_id + "Leg instrument {leg_instrument_id} not found in contract details after loading" ) })?; @@ -1351,13 +1240,6 @@ impl InteractiveBrokersInstrumentProvider { /// Python version loads from config's `_load_ids_on_start` and `_load_contracts_on_start`. /// Rust version accepts these as parameters for flexibility. /// - /// # Arguments - /// - /// * `client` - The IB API client - /// * `instrument_ids` - Optional vector of instrument IDs to load - /// * `contracts` - Optional vector of IB contracts to load - /// * `force_instrument_update` - If true, force re-fetch even if already cached - /// /// # Errors /// /// Returns an error if loading fails. @@ -1372,7 +1254,7 @@ impl InteractiveBrokersInstrumentProvider { // Load from instrument IDs let ids_to_load = - instrument_ids.unwrap_or_else(|| self.config.load_ids.iter().cloned().collect()); + instrument_ids.unwrap_or_else(|| self.config.load_ids.iter().copied().collect()); if !ids_to_load.is_empty() { let mut filters = std::collections::HashMap::new(); @@ -1410,29 +1292,19 @@ impl InteractiveBrokersInstrumentProvider { } } } else { - for contract_json in &self.config.load_contracts { - match crate::common::contracts::parse_contract_from_json(contract_json) - .context("Failed to parse contract from config JSON") + for configured in &self.config.load_contracts { + let spec = configured.chain_spec_json(); + match self + .load_contract_spec(client, &configured.contract, spec.as_ref()) + .await { - Ok(contract) => match self - .load_contract_spec(client, &contract, Some(contract_json)) - .await - { - Ok(mut instrument_ids) => { - loaded_ids.append(&mut instrument_ids); - } - Err(e) => { - tracing::warn!( - "Error loading instrument from contract {:?}: {}", - contract, - e - ); - } - }, + Ok(mut instrument_ids) => { + loaded_ids.append(&mut instrument_ids); + } Err(e) => { tracing::warn!( - "Error parsing load contract spec {:?}: {}", - contract_json, + "Error loading instrument from contract {:?}: {}", + configured.contract, e ); } @@ -1458,14 +1330,9 @@ fn normalize_price_magnifier(price_magnifier: i32) -> i32 { } } -fn security_type_code(security_type: &SecurityType) -> String { - security_type.to_string() -} - -fn json_bool(spec: Option<&serde_json::Value>, key: &str) -> bool { +fn json_opt_bool(spec: Option<&serde_json::Value>, key: &str) -> Option { spec.and_then(|value| value.get(key)) .and_then(serde_json::Value::as_bool) - .unwrap_or(false) } fn json_u32(spec: Option<&serde_json::Value>, key: &str) -> Option { @@ -1543,11 +1410,6 @@ fn expiry_bound_from_days(days: Option) -> Option { impl InteractiveBrokersInstrumentProvider { /// Fetch and cache contract details for an instrument ID using the provided IB client. /// - /// # Arguments - /// - /// * `client` - The IB API client - /// * `instrument_id` - The instrument ID to fetch - /// /// # Errors /// /// Returns an error if fetching fails. @@ -1558,17 +1420,16 @@ impl InteractiveBrokersInstrumentProvider { force_instrument_update: bool, filters: Option>, ) -> anyhow::Result<()> { - if !force_instrument_update { - if self.instruments.contains_key(&instrument_id) - && (self.contract_details.contains_key(&instrument_id) - || self.contracts.contains_key(&instrument_id)) - { - tracing::debug!( - "Instrument {} already cached, skipping fetch", - instrument_id - ); - return Ok(()); - } + if !force_instrument_update + && self.instruments.contains_key(&instrument_id) + && (self.contract_details.contains_key(&instrument_id) + || self.contracts.contains_key(&instrument_id)) + { + tracing::debug!( + "Instrument {} already cached, skipping fetch", + instrument_id + ); + return Ok(()); } // Convert instrument ID to IB contract let exchange = filters @@ -1586,8 +1447,10 @@ impl InteractiveBrokersInstrumentProvider { let mut last_error = None; for candidate_exchange in exchanges_to_try { - let contract = instrument_id_to_ib_contract(instrument_id, Some(candidate_exchange.as_str())) - .with_context(|| format!("Failed to convert instrument_id {} to IB contract. Check that the instrument ID format is correct and the venue/symbol are valid.", instrument_id))?; + let contract = self + .symbology + .contract(instrument_id, Some(candidate_exchange.as_str())) + .with_context(|| format!("Failed to convert instrument_id {instrument_id} to IB contract. Check that the instrument ID format is correct and the venue/symbol are valid."))?; match client.contract_details(&contract).await { Ok(result) if !result.is_empty() => { @@ -1651,7 +1514,7 @@ impl InteractiveBrokersInstrumentProvider { tracing::warn!( "Failed to process IB contract details con_id={} sec_type={}: {}", details.contract.contract_id, - security_type_code(&details.contract.security_type), + details.contract.security_type, e ); } @@ -1667,7 +1530,7 @@ impl InteractiveBrokersInstrumentProvider { venue: Option, force_instrument_update: bool, ) -> anyhow::Result> { - let sec_type = security_type_code(&details.contract.security_type); + let sec_type = details.contract.security_type.to_string(); if self.is_filtered_sec_type(&sec_type) { tracing::debug!( "Skipping filtered security type {} for contract {:?}", @@ -1710,17 +1573,12 @@ impl InteractiveBrokersInstrumentProvider { Ok(Some(instrument_id)) } - fn instrument_id_from_contract( + pub(crate) fn instrument_id_from_contract( &self, contract: &Contract, venue: Venue, ) -> anyhow::Result { - match self.config.symbology_method { - SymbologyMethod::Simplified => { - ib_contract_to_instrument_id_simplified(contract, Some(venue)) - } - SymbologyMethod::Raw => ib_contract_to_instrument_id_raw(contract, Some(venue)), - } + self.symbology.instrument_id(contract, Some(venue)) } fn cache_instrument( @@ -1812,12 +1670,6 @@ impl InteractiveBrokersInstrumentProvider { /// /// This method fetches and caches contract details for multiple instrument IDs in parallel. /// - /// # Arguments - /// - /// * `client` - The IB API client - /// * `instrument_ids` - Vector of instrument IDs to load - /// * `filters` - Optional filters to apply (not yet implemented, reserved for future use) - /// /// # Returns /// /// Returns a vector of successfully loaded instrument IDs. @@ -1862,8 +1714,7 @@ impl InteractiveBrokersInstrumentProvider { // Check security type (try to infer from instrument) if let Some(contract_details) = self.contract_details.get(instrument_id) { - let sec_type_str = - security_type_code(&contract_details.contract.security_type); + let sec_type_str = contract_details.contract.security_type.to_string(); if sec_type_str.to_uppercase().contains(&filter.to_uppercase()) { return true; @@ -1914,13 +1765,6 @@ impl InteractiveBrokersInstrumentProvider { /// It uses `contract_details` to fetch options with precise expiry filtering, /// which is more flexible than the basic `option_chain` API. /// - /// # Arguments - /// - /// * `client` - The IB API client - /// * `underlying` - The underlying contract - /// * `expiry_min` - Minimum expiry date string (YYYYMMDD format, can be None for no min) - /// * `expiry_max` - Maximum expiry date string (YYYYMMDD format, can be None for no max) - /// /// # Returns /// /// Returns the number of option instruments loaded. @@ -1951,13 +1795,19 @@ impl InteractiveBrokersInstrumentProvider { // First, get option chain metadata to determine expirations let symbol = underlying.symbol.as_str(); - let mut option_chain_stream = client - .option_chain( - symbol, - exchange, - underlying.security_type.clone(), - underlying.contract_id, - ) + let mut option_chain_request = client.option_chain( + symbol, + underlying.security_type.clone(), + underlying.contract_id, + ); + + // IB names the exchange only for futures options; for any other underlying it returns + // one chain per listing exchange, and naming an exchange returns none + if underlying.security_type == SecurityType::Future { + option_chain_request = option_chain_request.exchange(exchange); + } + let mut option_chain_stream = option_chain_request + .subscribe() .await .context("Failed to request option chain from IB")?; @@ -1980,6 +1830,10 @@ impl InteractiveBrokersInstrumentProvider { chain.strikes.len(), ); + if chain.exchange != exchange { + continue; + } + for expiration in &chain.expirations { // Filter by expiry date string if specified let date_filter_pass = match (expiry_min, expiry_max) { @@ -1994,11 +1848,8 @@ impl InteractiveBrokersInstrumentProvider { // Filter by expiry days from config if specified let days_filter_pass = { let expiry_ns = - crate::providers::parse::expiry_timestring_to_unix_nanos( - expiration.as_str(), - None, - ) - .unwrap_or(now); + expiry_timestring_to_unix_nanos(expiration.as_str(), None) + .unwrap_or(now); let days_until_expiry = (expiry_ns.as_u64().saturating_sub(now.as_u64())) / (24 * 60 * 60 * 1_000_000_000); @@ -2144,13 +1995,6 @@ impl InteractiveBrokersInstrumentProvider { /// This method fetches all futures contracts for a given underlying symbol /// and populates the cache with all individual futures instruments. /// - /// # Arguments - /// - /// * `client` - The IB API client - /// * `symbol` - The underlying symbol - /// * `exchange` - The exchange (use "" for all exchanges) - /// * `currency` - The currency (use USD as default) - /// /// # Returns /// /// Returns the number of futures instruments loaded. @@ -2158,6 +2002,7 @@ impl InteractiveBrokersInstrumentProvider { /// # Errors /// /// Returns an error if fetching fails. + #[allow(clippy::too_many_arguments)] // Public query options map directly to IB filters. pub async fn fetch_futures_chain( &self, client: &ibapi::Client, @@ -2184,7 +2029,7 @@ impl InteractiveBrokersInstrumentProvider { // Build futures contract for lookup let futures_contract = Contract { - contract_id: 0, // 0 for lookup by specification + contract_id: 0, symbol: Symbol::from(symbol.to_string()), security_type: SecurityType::Future, last_trade_date_or_contract_month: String::new(), @@ -2232,7 +2077,7 @@ impl InteractiveBrokersInstrumentProvider { } // Check if security type is filtered - let sec_type_str = security_type_code(&details.contract.security_type); + let sec_type_str = details.contract.security_type.to_string(); if self.is_filtered_sec_type(&sec_type_str) { continue; } @@ -2242,7 +2087,7 @@ impl InteractiveBrokersInstrumentProvider { .contract .last_trade_date_or_contract_month .is_empty() - && let Ok(expiry_ns) = crate::providers::parse::expiry_timestring_to_unix_nanos( + && let Ok(expiry_ns) = expiry_timestring_to_unix_nanos( &details.contract.last_trade_date_or_contract_month, Some(&details), ) @@ -2295,11 +2140,6 @@ impl InteractiveBrokersInstrumentProvider { /// contract details with a BAG contract. The BAG contract should have its /// combo_legs populated with the individual leg contract IDs. /// - /// # Arguments - /// - /// * `client` - The IB API client - /// * `bag_contract` - The BAG contract with populated combo_legs - /// /// # Returns /// /// Returns the number of spread instruments loaded (0 or 1). @@ -2390,21 +2230,10 @@ impl InteractiveBrokersInstrumentProvider { } else { // Load the leg instrument let leg_venue = self.determine_venue(&leg_details.contract, Some(leg_details)); - let leg_instrument_id = match self.config.symbology_method { - crate::config::SymbologyMethod::Simplified => { - crate::common::parse::ib_contract_to_instrument_id_simplified( - &leg_details.contract, - Some(leg_venue), - ) - } - crate::config::SymbologyMethod::Raw => { - crate::common::parse::ib_contract_to_instrument_id_raw( - &leg_details.contract, - Some(leg_venue), - ) - } - } - .context("Failed to convert leg contract to instrument ID")?; + let leg_instrument_id = self + .symbology + .instrument_id(&leg_details.contract, Some(leg_venue)) + .context("Failed to convert leg contract to instrument ID")?; // Parse and cache the leg instrument let leg_instrument = @@ -2437,8 +2266,7 @@ impl InteractiveBrokersInstrumentProvider { .map(|entry| entry.value().clone()) .ok_or_else(|| { anyhow::anyhow!( - "Contract details not found for leg {} after loading", - leg_instrument_id + "Contract details not found for leg {leg_instrument_id} after loading" ) })?; @@ -2534,10 +2362,6 @@ impl InteractiveBrokersInstrumentProvider { /// Save the current instrument cache to disk. /// - /// # Arguments - /// - /// * `cache_path` - Path to the cache file - /// /// # Errors /// /// Returns an error if serialization or file I/O fails. @@ -2594,10 +2418,6 @@ impl InteractiveBrokersInstrumentProvider { /// Load instrument cache from disk if valid. /// - /// # Arguments - /// - /// * `cache_path` - Path to the cache file - /// /// # Returns /// /// Returns `true` if cache was loaded successfully and is valid, `false` otherwise. @@ -2777,7 +2597,7 @@ mod tests { async fn load_contract( &self, - _contract_spec: &serde_json::Value, + _configured: &ConfiguredContract, ) -> anyhow::Result> { self.contract_calls.fetch_add(1, Ordering::SeqCst); @@ -2843,7 +2663,9 @@ mod tests { fn test_qualified_opra_details_preserve_canonical_instrument_identity() { let provider = InteractiveBrokersInstrumentProvider::new(Default::default()); let requested_id = InstrumentId::from("AAPL 270115P00155000.OPRA"); - let request = instrument_id_to_ib_contract(requested_id, None).unwrap(); + let request = provider + .resolve_contract_for_instrument(requested_id) + .unwrap(); assert_eq!(request.security_type, SecurityType::Option); assert_eq!(request.exchange.as_str(), "SMART"); @@ -2943,14 +2765,15 @@ mod tests { #[tokio::test] async fn test_initialize_loads_all_configured_inputs_once() { let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("NASDAQ")); - let contract_spec = serde_json::json!({ - "secType": "STK", - "symbol": "MSFT", - "exchange": "NASDAQ", - }); + let contract_spec = Contract { + security_type: SecurityType::Stock, + symbol: ibapi::contracts::Symbol::from("MSFT"), + exchange: Exchange::from("NASDAQ"), + ..Default::default() + }; let config = InteractiveBrokersInstrumentProviderConfig { load_ids: [instrument_id].into_iter().collect(), - load_contracts: vec![contract_spec], + load_contracts: vec![ConfiguredContract::from(contract_spec)], ..Default::default() }; let provider = InteractiveBrokersInstrumentProvider::new(config); @@ -2975,14 +2798,15 @@ mod tests { #[tokio::test] async fn test_initialize_fails_closed_and_retries_unresolved_input() { let instrument_id = InstrumentId::new(Symbol::from("AAPL"), Venue::from("NASDAQ")); - let contract_spec = serde_json::json!({ - "secType": "STK", - "symbol": "MSFT", - "exchange": "NASDAQ", - }); + let contract_spec = Contract { + security_type: SecurityType::Stock, + symbol: ibapi::contracts::Symbol::from("MSFT"), + exchange: Exchange::from("NASDAQ"), + ..Default::default() + }; let config = InteractiveBrokersInstrumentProviderConfig { load_ids: [instrument_id].into_iter().collect(), - load_contracts: vec![contract_spec], + load_contracts: vec![ConfiguredContract::from(contract_spec)], ..Default::default() }; let provider = InteractiveBrokersInstrumentProvider::new(config); @@ -3200,15 +3024,15 @@ mod tests { } #[rstest] - fn test_filter_sec_types_uses_ib_codes_case_insensitive() { + fn test_filter_sec_types_uses_typed_ib_codes() { let config = InteractiveBrokersInstrumentProviderConfig { - filter_sec_types: [String::from("opt")].into_iter().collect(), + filter_sec_types: [IbSecurityType::Option].into_iter().collect(), ..Default::default() }; let provider = InteractiveBrokersInstrumentProvider::new(config); - assert!(provider.is_filtered_sec_type(&security_type_code(&SecurityType::Option))); - assert!(!provider.is_filtered_sec_type(&security_type_code(&SecurityType::Stock))); + assert!(provider.is_filtered_sec_type(&SecurityType::Option.to_string())); + assert!(!provider.is_filtered_sec_type(&SecurityType::Stock.to_string())); } #[rstest] @@ -3294,4 +3118,54 @@ mod tests { "load_cache should return false for expired cache" ); } + #[rstest] + fn restored_cache_seeds_provider_with_price_magnifier() { + let provider = InteractiveBrokersInstrumentProvider::new(Default::default()); + let mut cache = Cache::default(); + assert_eq!(provider.seed_from_cache(&cache), 0); + let instrument_id = InstrumentId::from("AAPL.NASDAQ"); + let contract = Contract { + contract_id: 265598, + symbol: ibapi::contracts::Symbol::from("AAPL"), + security_type: SecurityType::Stock, + exchange: Exchange::from("SMART"), + currency: ibapi::contracts::Currency::from("USD"), + ..Default::default() + }; + let instrument = create_test_instrument_with_info( + instrument_id, + Some(create_contract_info(&contract, Some(100))), + ); + cache.add_instrument(instrument.clone()).unwrap(); + let seeded = provider.seed_from_cache(&cache); + assert_eq!(seeded, 1); + assert_eq!(provider.find(&instrument_id), Some(instrument)); + assert_eq!(provider.get_price_magnifier(&instrument_id), 100); + assert_eq!( + provider + .resolve_contract_for_instrument(instrument_id) + .unwrap(), + contract + ); + } + + #[rstest] + #[case(None)] + #[case(Some(-1))] + fn cached_instrument_without_valid_magnifier_requires_qualification( + #[case] magnifier: Option, + ) { + let provider = InteractiveBrokersInstrumentProvider::new(Default::default()); + let instrument_id = InstrumentId::from("AAPL.NASDAQ"); + let contract = Contract { + contract_id: 265598, + ..Default::default() + }; + let instrument = create_test_instrument_with_info( + instrument_id, + Some(create_contract_info(&contract, magnifier)), + ); + assert_eq!(provider.add_cached_instruments([instrument]), 0); + assert_eq!(provider.find(&instrument_id), None); + } } diff --git a/crates/adapters/interactive_brokers/src/providers/parse.rs b/crates/adapters/interactive_brokers/src/providers/parse.rs index 6e4bec7ab6cd..8f5a50c4a590 100644 --- a/crates/adapters/interactive_brokers/src/providers/parse.rs +++ b/crates/adapters/interactive_brokers/src/providers/parse.rs @@ -19,13 +19,20 @@ use std::str::FromStr; use anyhow::Context; use ibapi::contracts::SecurityType; -use nautilus_core::{DurationNanos, UnixNanos, time::get_atomic_clock_realtime}; +use jiff::{ + Timestamp, + civil::DateTime, + tz::{AmbiguousOffset, Offset}, +}; +use nautilus_core::{ + DurationNanos, UnixNanos, datetime::get_timezone, time::get_atomic_clock_realtime, +}; use nautilus_model::{ enums::AssetClass, identifiers::{InstrumentId, Symbol}, instruments::{ - Cfd, Commodity, CryptoPerpetual, CurrencyPair, Equity, FuturesContract, FuturesSpread, - IndexInstrument, InstrumentAny, OptionContract, OptionSpread, + Cfd, Commodity, CurrencyPair, Equity, FuturesContract, FuturesSpread, IndexInstrument, + InstrumentAny, OptionContract, OptionSpread, }, types::{Currency, Price, Quantity}, }; @@ -47,7 +54,7 @@ pub fn tick_size_to_precision(tick_size: f64) -> u8 { } // Count decimal places - let s = format!("{:.10}", tick_size); + let s = format!("{tick_size:.10}"); let s = s.trim_end_matches('0'); let parts: Vec<&str> = s.split('.').collect(); @@ -73,87 +80,81 @@ pub fn expiry_timestring_to_unix_nanos( anyhow::bail!("Empty expiry string"); } - // Parse timestamp string - Most contract expirations are %Y%m%d format - // Some exchanges have expirations in %Y%m%d %H:%M:%S %Z - let dt = if expiry.len() == 8 { - // Format: YYYYMMDD - let year = &expiry[0..4]; - let month = &expiry[4..6]; - let day = &expiry[6..8]; - let date = time::Date::from_calendar_date( - year.parse()?, - time::Month::try_from(month.parse::()?)?, - day.parse()?, - )?; - - // If we have trading hours, try to extract the last trade time - // Trading hours format: "20240411:0000-20240411:1800;..." - let mut expiry_time = time::Time::MIDNIGHT; - - if let Some(details) = details { - if !details.trading_hours.is_empty() - && !details.trading_hours.contains(&"CLOSED".to_string()) - { - // Find the session for this date - let expiry_str: &str = expiry; - for session in &details.trading_hours { - if session.as_str().starts_with(expiry_str) && session.as_str().contains('-') { - let parts: Vec<&str> = session.as_str().split('-').collect(); - if let Some(end_part) = parts.get(1) { - let inner_parts: Vec<&str> = end_part.split(':').collect(); - if let Some(time_part) = inner_parts.get(1) { - if time_part.len() >= 4 { - let hour = time_part - .get(0..2) - .and_then(|s: &str| s.parse::().ok()) - .unwrap_or(0); - let minute = time_part - .get(2..4) - .and_then(|s: &str| s.parse::().ok()) - .unwrap_or(0); - expiry_time = time::Time::from_hms(hour, minute, 0) - .unwrap_or(time::Time::MIDNIGHT); - } - } - } - break; - } - } - } - } - time::PrimitiveDateTime::new(date, expiry_time) + let contract_timezone = details + .map(|details| details.time_zone_id.as_str()) + .filter(|timezone| !timezone.is_empty()) + .unwrap_or("UTC"); + + let (date, time, timezone) = if expiry.len() == 8 { + let session_end = details.and_then(|details| { + details + .trading_hours + .iter() + .find(|session| session.starts_with(expiry)) + .and_then(|session| session.split_once('-')) + .map(|(_, end)| end) + }); + let (date, time) = match session_end { + Some(end) => end.split_once(':').unwrap_or((expiry, end)), + // IB lists only the coming week of sessions, so a later expiry takes the + // closing time of the last listed session + None => ( + expiry, + details.and_then(last_session_close).unwrap_or("0000"), + ), + }; + let time = match time.len() { + 4 => format!("{}:{}:00", &time[0..2], &time[2..4]), + _ => anyhow::bail!("Invalid expiry session end '{time}' for {expiry}"), + }; + (date, time, contract_timezone) } else { - // Format: YYYYMMDD HH:MM:SS TZ - let parts: Vec<&str> = expiry.split(' ').collect(); - if parts.len() >= 3 { - let date_part = parts[0]; - let time_part = parts[1]; - let year = &date_part[0..4]; - let month = &date_part[4..6]; - let day = &date_part[6..8]; - - let time_parts: Vec<&str> = time_part.split(':').collect(); - let hour = time_parts.first().unwrap_or(&"0").parse::()?; - let minute = time_parts.get(1).unwrap_or(&"0").parse::()?; - let second = time_parts.get(2).unwrap_or(&"0").parse::()?; - - let date = time::Date::from_calendar_date( - year.parse()?, - time::Month::try_from(month.parse::()?)?, - day.parse()?, - )?; - let time_obj = time::Time::from_hms(hour, minute, second)?; - time::PrimitiveDateTime::new(date, time_obj) - } else { - anyhow::bail!("Invalid expiry format: {}", expiry); + let mut parts = expiry.split_whitespace(); + let date = parts.next().context("Expiry timestamp is missing a date")?; + let time = parts + .next() + .context("Expiry timestamp is missing a time")? + .to_string(); + let timezone = parts.next().unwrap_or(contract_timezone); + if parts.next().is_some() { + anyhow::bail!("Invalid expiry format: {expiry}"); } + (date, time, timezone) }; - // Treat the parsed expiry timestamp as UTC. NautilusTrader expects IB timestamps - // to be configured and interpreted in UTC. - let offset_dt = dt.assume_utc(); - let nanos = offset_dt.unix_timestamp_nanos(); - Ok(UnixNanos::new(nanos as u64)) + let datetime = DateTime::strptime("%Y%m%d %H:%M:%S", format!("{date} {time}")) + .with_context(|| format!("Invalid expiry timestamp: {expiry}"))?; + let timestamp = localize_expiry(datetime, timezone, expiry)?; + let nanos = u64::try_from(timestamp.as_nanosecond()) + .with_context(|| format!("Expiry timestamp precedes the Unix epoch: {expiry}"))?; + Ok(UnixNanos::new(nanos)) +} + +// Returns the `HHMM` closing time of the last listed trading session that is not closed +fn last_session_close(details: &ibapi::contracts::ContractDetails) -> Option<&str> { + details.trading_hours.iter().rev().find_map(|session| { + let (_, end) = session.rsplit_once('-')?; + let close = end.rsplit_once(':').map_or(end, |(_, time)| time); + (close.len() == 4 && close.bytes().all(|byte| byte.is_ascii_digit())).then_some(close) + }) +} + +fn localize_expiry(datetime: DateTime, timezone: &str, expiry: &str) -> anyhow::Result { + if timezone.eq_ignore_ascii_case("UTC") || timezone.eq_ignore_ascii_case("Z") { + return Ok(Offset::UTC.to_timestamp(datetime)?); + } + + let zone = get_timezone(timezone).with_context(|| { + format!("Unknown IB contract timezone '{timezone}' for expiry {expiry}") + })?; + let ambiguous = zone.to_ambiguous_timestamp(datetime); + match ambiguous.offset() { + AmbiguousOffset::Unambiguous { .. } => Ok(ambiguous.unambiguous()?), + AmbiguousOffset::Fold { .. } => Ok(ambiguous.earlier()?), + AmbiguousOffset::Gap { .. } => { + anyhow::bail!("Expiry {expiry} does not exist in timezone '{timezone}'") + } + } } /// Parse an IB ContractDetails to a Nautilus instrument. @@ -172,7 +173,7 @@ pub fn parse_ib_contract_to_instrument( SecurityType::ForexPair => Ok(parse_forex_contract(details, instrument_id)), SecurityType::Crypto => Ok(parse_crypto_contract(details, instrument_id)), SecurityType::Future | SecurityType::ContinuousFuture => { - Ok(parse_futures_contract(details, instrument_id)) + parse_futures_contract(details, instrument_id) } SecurityType::Option => parse_option_contract(details, instrument_id), SecurityType::FuturesOption => parse_option_contract(details, instrument_id), // FOP uses same parsing as OPT @@ -180,20 +181,12 @@ pub fn parse_ib_contract_to_instrument( SecurityType::CFD => Ok(parse_cfd_contract(details, instrument_id)), SecurityType::Commodity => Ok(parse_commodity_contract(details, instrument_id)), SecurityType::Bond => Ok(parse_bond_contract(details, instrument_id)), - _ => anyhow::bail!("Unsupported security type: {:?}", sec_type), + _ => anyhow::bail!("Unsupported security type: {sec_type:?}"), } } fn ib_contract_info(details: &ibapi::contracts::ContractDetails) -> nautilus_core::Params { - let mut info = nautilus_core::Params::new(); - let mut contract = serde_json::Map::new(); - - let contract_params = contract_to_params(&details.contract); - for (key, value) in &contract_params { - contract.insert(key.clone(), value.clone()); - } - - info.insert("contract".to_string(), serde_json::Value::Object(contract)); + let mut info = ib_contract_info_for_contract(&details.contract); info.insert( "priceMagnifier".to_string(), serde_json::Value::from(details.price_magnifier), @@ -260,7 +253,7 @@ fn parse_forex_contract( instrument_id: InstrumentId, ) -> InstrumentAny { let price_precision = tick_size_to_precision(details.min_tick); - let size_precision = tick_size_to_precision(details.min_size); + let (size_precision, size_increment, min_quantity) = parse_contract_size_rules(details, 1.0); let timestamp = get_atomic_clock_realtime().get_time_ns(); let instrument = CurrencyPair::builder() @@ -271,7 +264,8 @@ fn parse_forex_contract( .price_precision(price_precision) .size_precision(size_precision) .price_increment(Price::new(details.min_tick, price_precision)) - .size_increment(Quantity::new(details.size_increment, size_precision)) + .size_increment(size_increment) + .maybe_min_quantity(min_quantity) .info(ib_contract_info(details)) .ts_event(timestamp) .ts_init(timestamp) @@ -287,21 +281,20 @@ fn parse_crypto_contract( instrument_id: InstrumentId, ) -> InstrumentAny { let price_precision = tick_size_to_precision(details.min_tick); - let size_precision = tick_size_to_precision(details.min_size); + let (size_precision, size_increment, min_quantity) = + parse_contract_size_rules(details, 0.00000001); let timestamp = get_atomic_clock_realtime().get_time_ns(); - let instrument = CryptoPerpetual::builder() + let instrument = CurrencyPair::builder() .instrument_id(instrument_id) .raw_symbol(Symbol::from(details.contract.local_symbol.as_str())) .base_currency(Currency::from(details.contract.symbol.to_string())) .quote_currency(Currency::from(details.contract.currency.to_string())) - .settlement_currency(Currency::from(details.contract.currency.to_string())) - .is_inverse(true) .price_precision(price_precision) .size_precision(size_precision) .price_increment(Price::new(details.min_tick, price_precision)) - .size_increment(Quantity::new(details.size_increment, size_precision)) - .min_quantity(Quantity::new(details.min_size, size_precision)) + .size_increment(size_increment) + .maybe_min_quantity(min_quantity) .info(ib_contract_info(details)) .ts_event(timestamp) .ts_init(timestamp) @@ -328,29 +321,32 @@ fn parse_contract_multiplier(multiplier: &str, default: f64) -> Quantity { fn parse_futures_contract( details: &ibapi::contracts::ContractDetails, instrument_id: InstrumentId, -) -> InstrumentAny { +) -> anyhow::Result { let price_precision = tick_size_to_precision(details.min_tick); let timestamp = get_atomic_clock_realtime().get_time_ns(); - // Parse expiration - let expiration_ns = if !details - .contract - .last_trade_date_or_contract_month - .is_empty() - { - expiry_timestring_to_unix_nanos( - &details.contract.last_trade_date_or_contract_month, - Some(details), - ) - .unwrap_or_else(|_| timestamp + NINETY_DAYS) - // Default to +90 days on error - } else { - timestamp + NINETY_DAYS // Default to +90 days if empty + let expiry = &details.contract.last_trade_date_or_contract_month; + let expiration_ns = match expiry_timestring_to_unix_nanos(expiry, Some(details)) { + Ok(expiration_ns) => expiration_ns, + // Continuous futures can report contract details without a last-trade date. + Err(e) + if matches!( + details.contract.security_type, + SecurityType::ContinuousFuture + ) => + { + tracing::warn!( + "Continuous future {} reported expiry '{expiry}', defaulting to 90 days out: {e}", + details.contract.symbol.as_str(), + ); + timestamp + NINETY_DAYS + } + Err(e) => { + return Err(e).with_context(|| format!("Failed to parse futures expiry '{expiry}'")); + } }; - let activation_ns = expiration_ns - .checked_sub(NINETY_DAYS) - .unwrap_or(UnixNanos::from(0)); // -90 days or 0 if underflow + let activation_ns = activation_from_expiration(expiration_ns); let multiplier = parse_contract_multiplier(&details.contract.multiplier, 1.0); @@ -384,7 +380,7 @@ fn parse_futures_contract( .build() .unwrap(); - InstrumentAny::from(instrument) + Ok(InstrumentAny::from(instrument)) } /// Parse option contract (OPT). @@ -395,25 +391,11 @@ fn parse_option_contract( let price_precision = tick_size_to_precision(details.min_tick); let timestamp = get_atomic_clock_realtime().get_time_ns(); - // Parse expiration - let expiration_ns = if !details - .contract - .last_trade_date_or_contract_month - .is_empty() - { - expiry_timestring_to_unix_nanos( - &details.contract.last_trade_date_or_contract_month, - Some(details), - ) - .unwrap_or_else(|_| timestamp + NINETY_DAYS) - // Default to +90 days on error - } else { - timestamp + NINETY_DAYS // Default to +90 days if empty - }; + let expiry = &details.contract.last_trade_date_or_contract_month; + let expiration_ns = expiry_timestring_to_unix_nanos(expiry, Some(details)) + .with_context(|| format!("Failed to parse option expiry '{expiry}'"))?; - let activation_ns = expiration_ns - .checked_sub(NINETY_DAYS) - .unwrap_or(UnixNanos::from(0)); // -90 days or 0 if underflow + let activation_ns = activation_from_expiration(expiration_ns); // Parse option kind (CALL or PUT) let option_kind = details @@ -456,8 +438,38 @@ fn parse_option_contract( Ok(InstrumentAny::from(instrument)) } -#[allow(clippy::items_after_test_module)] +fn activation_from_expiration(expiration_ns: UnixNanos) -> UnixNanos { + expiration_ns.checked_sub(NINETY_DAYS).unwrap_or_default() +} + +fn parse_contract_size_rules( + details: &ibapi::contracts::ContractDetails, + default_increment: f64, +) -> (u8, Quantity, Option) { + let size_increment = details + .size_increment + .or(details.min_size) + .unwrap_or(default_increment); + let size_precision = details.min_size.map_or_else( + || tick_size_to_precision(size_increment), + |min_size| tick_size_to_precision(min_size).max(tick_size_to_precision(size_increment)), + ); + let min_quantity = details + .min_size + .map(|min_size| Quantity::new(min_size, size_precision)); + + ( + size_precision, + Quantity::new(size_increment, size_precision), + min_quantity, + ) +} + #[cfg(test)] +#[allow( + clippy::items_after_test_module, + reason = "private parser tests remain beside the contract helpers they exercise" +)] mod tests { use ibapi::contracts::{ Contract, ContractDetails, Currency, Exchange, OptionRight, SecurityType, Symbol, @@ -472,10 +484,109 @@ mod tests { use ustr::Ustr; use super::{ - parse_contract_multiplier, parse_ib_contract_to_instrument, - parse_option_spread_instrument_id, + expiry_timestring_to_unix_nanos, parse_contract_multiplier, + parse_ib_contract_to_instrument, parse_option_spread_instrument_id, }; + #[rstest] + fn test_parse_crypto_contract_creates_spot_currency_pair() { + let details = ContractDetails { + contract: Contract { + symbol: Symbol::from("BTC"), + security_type: SecurityType::Crypto, + exchange: Exchange::from("PAXOS"), + currency: Currency::from("USD"), + local_symbol: String::from("BTC.USD"), + ..Default::default() + }, + min_tick: 0.01, + min_size: Some(0.0001), + size_increment: Some(0.0001), + ..Default::default() + }; + let instrument_id = InstrumentId::from("BTC/USD.PAXOS"); + + let instrument = parse_ib_contract_to_instrument(&details, instrument_id).unwrap(); + let InstrumentAny::CurrencyPair(pair) = instrument else { + panic!("expected spot currency pair"); + }; + + assert_eq!(pair.base_currency.code.as_str(), "BTC"); + assert_eq!(pair.quote_currency.code.as_str(), "USD"); + assert_eq!(pair.size_precision, 4); + assert_eq!(pair.size_increment, Quantity::from("0.0001")); + } + + #[rstest] + fn test_expiry_session_end_uses_contract_timezone() { + let details = ContractDetails { + time_zone_id: String::from("America/New_York"), + trading_hours: vec![String::from("20260313:0930-20260313:1600")], + ..Default::default() + }; + + let expiry = expiry_timestring_to_unix_nanos("20260313", Some(&details)).unwrap(); + let expected = "2026-03-13T20:00:00Z".parse::().unwrap(); + + assert_eq!( + expiry.as_u64(), + u64::try_from(expected.as_nanosecond()).unwrap() + ); + } + + #[rstest] + #[case::listed_session_closes_later_than_expiry( + vec!["20260929:0930-20260929:1600", "20260930:0930-20260930:1600", "20261001:CLOSED"], + "2026-10-30T20:00:00Z" + )] + #[case::overnight_sessions( + vec!["20260928:1700-20260929:1600", "20260929:1700-20260930:1600"], + "2026-10-30T20:00:00Z" + )] + #[case::only_closed_sessions(vec!["20261001:CLOSED"], "2026-10-30T04:00:00Z")] + #[case::no_sessions(vec![], "2026-10-30T04:00:00Z")] + fn test_expiry_beyond_listed_sessions_uses_last_session_close( + #[case] trading_hours: Vec<&str>, + #[case] expected: &str, + ) { + let details = ContractDetails { + time_zone_id: String::from("America/New_York"), + trading_hours: trading_hours.into_iter().map(String::from).collect(), + ..Default::default() + }; + + let expiry = expiry_timestring_to_unix_nanos("20261030", Some(&details)).unwrap(); + let expected = expected.parse::().unwrap(); + + assert_eq!( + expiry.as_u64(), + u64::try_from(expected.as_nanosecond()).unwrap() + ); + } + + #[rstest] + fn test_parse_future_rejects_missing_expiry() { + let details = ContractDetails { + contract: Contract { + symbol: Symbol::from("ES"), + security_type: SecurityType::Future, + exchange: Exchange::from("CME"), + currency: Currency::from("USD"), + local_symbol: String::from("ESZ6"), + ..Default::default() + }, + min_tick: 0.25, + ..Default::default() + }; + + let result = parse_ib_contract_to_instrument(&details, InstrumentId::from("ESZ6.CME")); + + assert_eq!( + result.unwrap_err().to_string(), + "Failed to parse futures expiry ''" + ); + } + #[rstest] fn test_parse_option_contract_prefixes_index_underlying() { let details = ContractDetails { @@ -637,7 +748,7 @@ fn parse_index_contract( instrument_id: InstrumentId, ) -> InstrumentAny { let price_precision = tick_size_to_precision(details.min_tick); - let size_precision = tick_size_to_precision(details.min_size); + let (size_precision, size_increment, _) = parse_contract_size_rules(details, 1.0); let timestamp = get_atomic_clock_realtime().get_time_ns(); let instrument = IndexInstrument::builder() @@ -647,7 +758,7 @@ fn parse_index_contract( .price_precision(price_precision) .size_precision(size_precision) .price_increment(Price::new(details.min_tick, price_precision)) - .size_increment(Quantity::new(details.size_increment, size_precision)) + .size_increment(size_increment) .info(ib_contract_info(details)) .ts_event(timestamp) .ts_init(timestamp) @@ -680,14 +791,14 @@ pub fn parse_spread_instrument_id( // Extract properties from the first leg contract details let currency = Currency::from(first_contract.currency.to_string()); - let underlying = if !first_details.under_symbol.is_empty() { - Ustr::from(first_details.under_symbol.as_str()) - } else { + let underlying = if first_details.under_symbol.is_empty() { Ustr::from(first_contract.symbol.as_str()) + } else { + Ustr::from(first_details.under_symbol.as_str()) }; // Parse multiplier - let multiplier_str = first_contract.multiplier.to_string(); + let multiplier_str = first_contract.multiplier.clone(); let multiplier = Quantity::from_str(&multiplier_str).unwrap_or_else(|_| Quantity::new(100.0, 0)); // Default to 100 for options @@ -760,13 +871,13 @@ pub fn parse_futures_spread_instrument_id( let (first_details, _) = leg_contract_details[0]; let first_contract = &first_details.contract; let currency = Currency::from(first_contract.currency.to_string()); - let underlying = if !first_details.under_symbol.is_empty() { - Ustr::from(first_details.under_symbol.as_str()) - } else { + let underlying = if first_details.under_symbol.is_empty() { Ustr::from(first_contract.symbol.as_str()) + } else { + Ustr::from(first_details.under_symbol.as_str()) }; - let multiplier = Quantity::from_str(&first_contract.multiplier.to_string()) - .unwrap_or_else(|_| Quantity::new(1.0, 0)); + let multiplier = + Quantity::from_str(&first_contract.multiplier).unwrap_or_else(|_| Quantity::new(1.0, 0)); let min_tick = leg_contract_details .iter() .map(|(details, _)| details.min_tick) @@ -832,7 +943,7 @@ fn parse_cfd_contract( instrument_id: InstrumentId, ) -> InstrumentAny { let price_precision = tick_size_to_precision(details.min_tick); - let size_precision = tick_size_to_precision(details.min_size); + let (size_precision, size_increment, min_quantity) = parse_contract_size_rules(details, 1.0); let timestamp = get_atomic_clock_realtime().get_time_ns(); let base_currency = details @@ -852,7 +963,8 @@ fn parse_cfd_contract( .price_precision(price_precision) .size_precision(size_precision) .price_increment(Price::new(details.min_tick, price_precision)) - .size_increment(Quantity::new(details.size_increment, size_precision)) + .size_increment(size_increment) + .maybe_min_quantity(min_quantity) .info(ib_contract_info(details)) .ts_event(timestamp) .ts_init(timestamp) @@ -868,7 +980,7 @@ fn parse_commodity_contract( instrument_id: InstrumentId, ) -> InstrumentAny { let price_precision = tick_size_to_precision(details.min_tick); - let size_precision = tick_size_to_precision(details.min_size); + let (size_precision, size_increment, min_quantity) = parse_contract_size_rules(details, 1.0); let timestamp = get_atomic_clock_realtime().get_time_ns(); let instrument = Commodity::builder() @@ -879,7 +991,8 @@ fn parse_commodity_contract( .price_precision(price_precision) .size_precision(size_precision) .price_increment(Price::new(details.min_tick, price_precision)) - .size_increment(Quantity::new(details.size_increment, size_precision)) + .size_increment(size_increment) + .maybe_min_quantity(min_quantity) .info(ib_contract_info(details)) .ts_event(timestamp) .ts_init(timestamp) diff --git a/crates/adapters/interactive_brokers/src/providers/tests.rs b/crates/adapters/interactive_brokers/src/providers/tests.rs index 581c8919f2c2..01d8d8998f34 100644 --- a/crates/adapters/interactive_brokers/src/providers/tests.rs +++ b/crates/adapters/interactive_brokers/src/providers/tests.rs @@ -15,378 +15,371 @@ //! Tests for Interactive Brokers instrument provider. -#[cfg(test)] -mod tests { - use ibapi::contracts::{ - ComboLeg, ComboLegOpenClose, Contract, Currency, Exchange, LegAction, SecurityType, Symbol, +use ibapi::contracts::{ + ComboLeg, ComboLegOpenClose, Contract, Currency, Exchange, LegAction, SecurityType, Symbol, +}; +use nautilus_core::UnixNanos; +use nautilus_model::{ + enums::AssetClass, + identifiers::{InstrumentId, Symbol as NautilusSymbol, Venue}, + instruments::{ + Instrument, InstrumentAny, OptionSpread, + stubs::{audusd_sim, equity_aapl, gbpusd_sim}, + }, + types::{Currency as ModelCurrency, Price, Quantity}, +}; +use rstest::rstest; +use ustr::Ustr; + +use crate::{ + common::spreads::{ + create_spread_instrument_id, is_spread_instrument_id, parse_spread_instrument_id_to_legs, + }, + config::InteractiveBrokersInstrumentProviderConfig, + providers::instruments::InteractiveBrokersInstrumentProvider, +}; + +fn create_test_provider() -> InteractiveBrokersInstrumentProvider { + let config = InteractiveBrokersInstrumentProviderConfig::default(); + InteractiveBrokersInstrumentProvider::new(config) +} + +fn create_test_option_spread(instrument_id: InstrumentId) -> OptionSpread { + OptionSpread::builder() + .instrument_id(instrument_id) + .raw_symbol(NautilusSymbol::from(instrument_id.symbol.as_str())) + .asset_class(AssetClass::Equity) + .exchange(Ustr::from("XNAS")) + .underlying(Ustr::from("SPY")) + .strategy_type(Ustr::from("SPY")) + .activation_ns(UnixNanos::default()) + .expiration_ns(UnixNanos::default()) + .currency(ModelCurrency::USD()) + .price_precision(2) + .price_increment(Price::from("0.01")) + .multiplier(Quantity::from(1)) + .lot_size(Quantity::from(1)) + .ts_event(UnixNanos::default()) + .ts_init(UnixNanos::default()) + .build() + .unwrap() +} + +// Note: Tests for load_async, load_with_return_async, load_ids_async, and load_ids_with_return_async +// require a mock IB client with stubbed contract details responses. +// These tests should be added when rust-ibapi stubbing capabilities are available. +// For now, we test the methods that don't require IB client interaction. + +#[rstest] +fn test_get_instrument_id_by_contract_id() { + let provider = create_test_provider(); + let contract_id = 265598; + + // Before loading, should return None + assert!( + provider + .get_instrument_id_by_contract_id(contract_id) + .is_none() + ); +} + +#[rstest] +fn test_resolve_instrument_id_for_contract_uses_cached_contract_id() { + let provider = create_test_provider(); + let instrument = equity_aapl(); + let expected_id = instrument.id(); + provider.insert_test_instrument(InstrumentAny::from(instrument), 265598, 1); + let contract = Contract { + contract_id: 265598, + symbol: Symbol::from("AAPL"), + security_type: SecurityType::Stock, + exchange: Exchange::from("SMART"), + currency: Currency::from("USD"), + ..Default::default() + }; + + let instrument_id = provider + .resolve_instrument_id_for_contract(&contract) + .unwrap(); + + assert_eq!(instrument_id, expected_id); +} + +#[rstest] +fn test_resolve_instrument_id_for_contract_reuses_cached_stock_venue() { + let provider = create_test_provider(); + let instrument = equity_aapl(); + let expected_id = instrument.id(); + provider.insert_test_instrument(InstrumentAny::from(instrument), 265598, 1); + let contract = Contract { + contract_id: 0, + symbol: Symbol::from("AAPL"), + security_type: SecurityType::Stock, + exchange: Exchange::from("SMART"), + currency: Currency::from("USD"), + ..Default::default() + }; + + let instrument_id = provider + .resolve_instrument_id_for_contract(&contract) + .unwrap(); + + assert_eq!(instrument_id, expected_id); +} + +#[rstest] +fn test_resolve_instrument_id_for_bag_contract_uses_cached_combo_legs() { + let provider = create_test_provider(); + let long_leg = InstrumentId::from("SPY C400.SMART"); + let short_leg = InstrumentId::from("SPY C410.SMART"); + let expected_id = create_spread_instrument_id(&[(long_leg, 1), (short_leg, -1)]).unwrap(); + let spread = create_test_option_spread(expected_id); + provider.insert_test_instrument(InstrumentAny::from(spread), 9000, 1); + provider.insert_test_contract_id_mapping(1001, long_leg); + provider.insert_test_contract_id_mapping(1002, short_leg); + let contract = Contract { + contract_id: 0, + symbol: Symbol::from("SPY"), + security_type: SecurityType::Spread, + exchange: Exchange::from("SMART"), + currency: Currency::from("USD"), + combo_legs: vec![ + ComboLeg { + contract_id: 1001, + ratio: 1, + action: LegAction::Buy, + exchange: String::from("SMART"), + open_close: ComboLegOpenClose::Same, + short_sale_slot: 0, + designated_location: String::new(), + exempt_code: 0, + }, + ComboLeg { + contract_id: 1002, + ratio: 1, + action: LegAction::Sell, + exchange: String::from("SMART"), + open_close: ComboLegOpenClose::Same, + short_sale_slot: 0, + designated_location: String::new(), + exempt_code: 0, + }, + ], + ..Default::default() }; - use nautilus_core::UnixNanos; - use nautilus_model::{ - enums::AssetClass, - identifiers::{InstrumentId, Symbol as NautilusSymbol, Venue}, - instruments::{ - Instrument, InstrumentAny, OptionSpread, - stubs::{audusd_sim, equity_aapl, gbpusd_sim}, - }, - types::{Currency as ModelCurrency, Price, Quantity}, + + let instrument_id = provider + .resolve_instrument_id_for_contract(&contract) + .unwrap(); + + assert_eq!(instrument_id, expected_id); +} + +#[rstest] +fn test_instrument_id_to_ib_contract_details() { + let provider = create_test_provider(); + let instrument_id = InstrumentId::new(NautilusSymbol::from("AAPL"), Venue::from("NASDAQ")); + + // Before loading, should return None + assert!( + provider + .instrument_id_to_ib_contract_details(&instrument_id) + .is_none() + ); +} + +#[rstest] +fn test_find_all_returns_only_requested_cached_instruments() { + let provider = create_test_provider(); + let audusd = audusd_sim(); + let gbpusd = gbpusd_sim(); + let aapl = equity_aapl(); + let audusd_id = audusd.id(); + let gbpusd_id = gbpusd.id(); + let aapl_id = aapl.id(); + let missing_id = InstrumentId::from("MSFT.NASDAQ"); + + provider.insert_test_instrument(InstrumentAny::from(audusd), 1, 1); + provider.insert_test_instrument(InstrumentAny::from(gbpusd), 2, 1); + provider.insert_test_instrument(InstrumentAny::from(aapl), 3, 1); + + let instruments = provider.find_all(&[gbpusd_id, missing_id]); + let instrument_ids: Vec = instruments.iter().map(Instrument::id).collect(); + + assert_eq!(instrument_ids, vec![gbpusd_id]); + assert_eq!(provider.count(), 3); + assert!(provider.find(&audusd_id).is_some()); + assert!(provider.find(&aapl_id).is_some()); +} + +#[rstest] +fn test_determine_venue() { + let provider = create_test_provider(); + let contract = Contract { + contract_id: 0, + symbol: Symbol::from("AAPL"), + security_type: SecurityType::Stock, + exchange: Exchange::from("SMART"), + primary_exchange: Exchange::from("NASDAQ"), + currency: Currency::from("USD"), + ..Default::default() }; - use rstest::rstest; - use ustr::Ustr; - use crate::{ - common::parse::create_spread_instrument_id, - config::InteractiveBrokersInstrumentProviderConfig, - providers::instruments::InteractiveBrokersInstrumentProvider, + let venue = provider.determine_venue(&contract, None); + // Should use primaryExchange when exchange is SMART + assert_eq!(venue.as_str(), "NASDAQ"); +} + +#[rstest] +fn test_determine_venue_with_direct_exchange() { + let provider = create_test_provider(); + let contract = Contract { + contract_id: 0, + symbol: Symbol::from("SPY"), + security_type: SecurityType::Stock, + exchange: Exchange::from("ARCA"), + primary_exchange: Exchange::default(), + currency: Currency::from("USD"), + ..Default::default() }; - fn create_test_provider() -> InteractiveBrokersInstrumentProvider { - let config = InteractiveBrokersInstrumentProviderConfig::default(); - InteractiveBrokersInstrumentProvider::new(config) - } - - fn create_test_option_spread(instrument_id: InstrumentId) -> OptionSpread { - OptionSpread::builder() - .instrument_id(instrument_id) - .raw_symbol(NautilusSymbol::from(instrument_id.symbol.as_str())) - .asset_class(AssetClass::Equity) - .exchange(Ustr::from("XNAS")) - .underlying(Ustr::from("SPY")) - .strategy_type(Ustr::from("SPY")) - .activation_ns(UnixNanos::default()) - .expiration_ns(UnixNanos::default()) - .currency(ModelCurrency::USD()) - .price_precision(2) - .price_increment(Price::from("0.01")) - .multiplier(Quantity::from(1)) - .lot_size(Quantity::from(1)) - .ts_event(UnixNanos::default()) - .ts_init(UnixNanos::default()) - .build() - .unwrap() - } - - // Note: Tests for load_async, load_with_return_async, load_ids_async, and load_ids_with_return_async - // require a mock IB client with stubbed contract details responses. - // These tests should be added when rust-ibapi stubbing capabilities are available. - // For now, we test the methods that don't require IB client interaction. - - #[rstest] - fn test_get_instrument_id_by_contract_id() { - let provider = create_test_provider(); - let contract_id = 265598; - - // Before loading, should return None - assert!( - provider - .get_instrument_id_by_contract_id(contract_id) - .is_none() - ); - } - - #[rstest] - fn test_resolve_instrument_id_for_contract_uses_cached_contract_id() { - let provider = create_test_provider(); - let instrument = equity_aapl(); - let expected_id = instrument.id(); - provider.insert_test_instrument(InstrumentAny::from(instrument), 265598, 1); - let contract = Contract { - contract_id: 265598, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("SMART"), - currency: Currency::from("USD"), - ..Default::default() - }; - - let instrument_id = provider - .resolve_instrument_id_for_contract(&contract) - .unwrap(); - - assert_eq!(instrument_id, expected_id); - } - - #[rstest] - fn test_resolve_instrument_id_for_contract_reuses_cached_stock_venue() { - let provider = create_test_provider(); - let instrument = equity_aapl(); - let expected_id = instrument.id(); - provider.insert_test_instrument(InstrumentAny::from(instrument), 265598, 1); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("SMART"), - currency: Currency::from("USD"), - ..Default::default() - }; - - let instrument_id = provider - .resolve_instrument_id_for_contract(&contract) - .unwrap(); - - assert_eq!(instrument_id, expected_id); - } - - #[rstest] - fn test_resolve_instrument_id_for_bag_contract_uses_cached_combo_legs() { - let provider = create_test_provider(); - let long_leg = InstrumentId::from("SPY C400.SMART"); - let short_leg = InstrumentId::from("SPY C410.SMART"); - let expected_id = create_spread_instrument_id(&[(long_leg, 1), (short_leg, -1)]).unwrap(); - let spread = create_test_option_spread(expected_id); - provider.insert_test_instrument(InstrumentAny::from(spread), 9000, 1); - provider.insert_test_contract_id_mapping(1001, long_leg); - provider.insert_test_contract_id_mapping(1002, short_leg); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("SPY"), - security_type: SecurityType::Spread, - exchange: Exchange::from("SMART"), - currency: Currency::from("USD"), - combo_legs: vec![ - ComboLeg { - contract_id: 1001, - ratio: 1, - action: LegAction::Buy, - exchange: String::from("SMART"), - open_close: ComboLegOpenClose::Same, - short_sale_slot: 0, - designated_location: String::new(), - exempt_code: 0, - }, - ComboLeg { - contract_id: 1002, - ratio: 1, - action: LegAction::Sell, - exchange: String::from("SMART"), - open_close: ComboLegOpenClose::Same, - short_sale_slot: 0, - designated_location: String::new(), - exempt_code: 0, - }, - ], - ..Default::default() - }; - - let instrument_id = provider - .resolve_instrument_id_for_contract(&contract) - .unwrap(); - - assert_eq!(instrument_id, expected_id); - } - - #[rstest] - fn test_instrument_id_to_ib_contract_details() { - let provider = create_test_provider(); - let instrument_id = InstrumentId::new(NautilusSymbol::from("AAPL"), Venue::from("NASDAQ")); - - // Before loading, should return None - assert!( - provider - .instrument_id_to_ib_contract_details(&instrument_id) - .is_none() - ); - } - - #[rstest] - fn test_find_all_returns_only_requested_cached_instruments() { - let provider = create_test_provider(); - let audusd = audusd_sim(); - let gbpusd = gbpusd_sim(); - let aapl = equity_aapl(); - let audusd_id = audusd.id(); - let gbpusd_id = gbpusd.id(); - let aapl_id = aapl.id(); - let missing_id = InstrumentId::from("MSFT.NASDAQ"); - - provider.insert_test_instrument(InstrumentAny::from(audusd), 1, 1); - provider.insert_test_instrument(InstrumentAny::from(gbpusd), 2, 1); - provider.insert_test_instrument(InstrumentAny::from(aapl), 3, 1); - - let instruments = provider.find_all(&[gbpusd_id, missing_id]); - let instrument_ids: Vec = instruments.iter().map(Instrument::id).collect(); - - assert_eq!(instrument_ids, vec![gbpusd_id]); - assert_eq!(provider.count(), 3); - assert!(provider.find(&audusd_id).is_some()); - assert!(provider.find(&aapl_id).is_some()); - } - - #[rstest] - fn test_determine_venue() { - let provider = create_test_provider(); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("SMART"), - primary_exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; - - let venue = provider.determine_venue(&contract, None); - // Should use primaryExchange when exchange is SMART - assert_eq!(venue.as_str(), "NASDAQ"); - } - - #[rstest] - fn test_determine_venue_with_direct_exchange() { - let provider = create_test_provider(); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("SPY"), - security_type: SecurityType::Stock, - exchange: Exchange::from("ARCA"), - primary_exchange: Exchange::default(), - currency: Currency::from("USD"), - ..Default::default() - }; - - let venue = provider.determine_venue(&contract, None); - // Should use exchange directly when not SMART - assert_eq!(venue.as_str(), "ARCA"); - } - - #[rstest] - fn test_determine_stock_venue_uses_primary_exchange_over_fill_exchange() { - let provider = create_test_provider(); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("META"), - security_type: SecurityType::Stock, - exchange: Exchange::from("IBEOS"), - primary_exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; - - let venue = provider.determine_venue(&contract, None); - - assert_eq!(venue.as_str(), "NASDAQ"); - } - - #[rstest] - fn test_determine_stock_venue_reuses_compatible_cached_mic_venue() { - let config = InteractiveBrokersInstrumentProviderConfig { - convert_exchange_to_mic_venue: true, - ..Default::default() - }; - let provider = InteractiveBrokersInstrumentProvider::new(config); - provider.insert_test_instrument(InstrumentAny::from(equity_aapl()), 265598, 1); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("IBEOS"), - primary_exchange: Exchange::from("NASDAQ"), - currency: Currency::from("USD"), - ..Default::default() - }; - - let venue = provider.determine_venue(&contract, None); - - assert_eq!(venue.as_str(), "XNAS"); - } - - #[rstest] - fn test_determine_stock_smart_venue_reuses_cached_symbol_venue() { - let provider = create_test_provider(); - provider.insert_test_instrument(InstrumentAny::from(equity_aapl()), 265598, 1); - let contract = Contract { - contract_id: 0, - symbol: Symbol::from("AAPL"), - security_type: SecurityType::Stock, - exchange: Exchange::from("SMART"), - primary_exchange: Exchange::default(), - currency: Currency::from("USD"), - ..Default::default() - }; - - let venue = provider.determine_venue(&contract, None); - - assert_eq!(venue.as_str(), "XNAS"); - } - - #[rstest] - fn test_symbology_method() { - let provider = create_test_provider(); - let method = provider.symbology_method(); - // Default should be Simplified - assert!(matches!(method, crate::config::SymbologyMethod::Simplified)); - } - - #[rstest] - fn test_get_price_magnifier_defaults_zero_to_one() { - let provider = create_test_provider(); - let instrument = equity_aapl(); - let instrument_id = instrument.id(); - - provider.insert_test_instrument(InstrumentAny::from(instrument), 265598, 0); - - assert_eq!(provider.get_price_magnifier(&instrument_id), 1); - } - - #[rstest] - fn test_force_instrument_update_caching() { - // Test that force_instrument_update parameter affects caching behavior - // This is tested indirectly through the load methods - // The actual caching logic is in fetch_contract_details - let provider = create_test_provider(); - let instrument_id = InstrumentId::new(NautilusSymbol::from("AAPL"), Venue::from("NASDAQ")); - - // Initially, instrument should not be cached - assert!(provider.find(&instrument_id).is_none()); - } - - #[rstest] - fn test_is_spread_instrument_id() { - use crate::common::parse::is_spread_instrument_id; - - // Test spread instrument ID detection - let spread_id = InstrumentId::new( - NautilusSymbol::from("(1)AAPL_((2))MSFT"), - Venue::from("NASDAQ"), - ); - assert!(is_spread_instrument_id(&spread_id)); - - // Test non-spread instrument ID - let regular_id = InstrumentId::new(NautilusSymbol::from("AAPL"), Venue::from("NASDAQ")); - assert!(!is_spread_instrument_id(®ular_id)); - } - - #[rstest] - fn test_parse_spread_instrument_id_to_legs() { - use crate::common::parse::{ - create_spread_instrument_id, parse_spread_instrument_id_to_legs, - }; - - let leg_tuples = [ - ( - InstrumentId::new(NautilusSymbol::from("MSFT"), Venue::from("NASDAQ")), - -2, - ), - ( - InstrumentId::new(NautilusSymbol::from("AAPL"), Venue::from("NASDAQ")), - 1, - ), - ]; - let spread_id = create_spread_instrument_id(&leg_tuples).unwrap(); - - assert_eq!(spread_id.symbol.as_str(), "(1)AAPL_((2))MSFT"); - - let result = parse_spread_instrument_id_to_legs(&spread_id); - assert!(result.is_ok()); - - let legs = result.unwrap(); - assert_eq!(legs.len(), 2); - - // Check first leg (positive ratio) - assert_eq!(legs[0].0.symbol.as_str(), "AAPL"); - assert_eq!(legs[0].1, 1); - - // Check second leg (negative ratio) - assert_eq!(legs[1].0.symbol.as_str(), "MSFT"); - assert_eq!(legs[1].1, -2); - } + let venue = provider.determine_venue(&contract, None); + // Should use exchange directly when not SMART + assert_eq!(venue.as_str(), "ARCA"); +} + +#[rstest] +fn test_determine_stock_venue_uses_primary_exchange_over_fill_exchange() { + let provider = create_test_provider(); + let contract = Contract { + contract_id: 0, + symbol: Symbol::from("META"), + security_type: SecurityType::Stock, + exchange: Exchange::from("IBEOS"), + primary_exchange: Exchange::from("NASDAQ"), + currency: Currency::from("USD"), + ..Default::default() + }; + + let venue = provider.determine_venue(&contract, None); + + assert_eq!(venue.as_str(), "NASDAQ"); +} + +#[rstest] +fn test_determine_stock_venue_reuses_compatible_cached_mic_venue() { + let config = InteractiveBrokersInstrumentProviderConfig { + convert_exchange_to_mic_venue: true, + ..Default::default() + }; + let provider = InteractiveBrokersInstrumentProvider::new(config); + provider.insert_test_instrument(InstrumentAny::from(equity_aapl()), 265598, 1); + let contract = Contract { + contract_id: 0, + symbol: Symbol::from("AAPL"), + security_type: SecurityType::Stock, + exchange: Exchange::from("IBEOS"), + primary_exchange: Exchange::from("NASDAQ"), + currency: Currency::from("USD"), + ..Default::default() + }; + + let venue = provider.determine_venue(&contract, None); + + assert_eq!(venue.as_str(), "XNAS"); +} + +#[rstest] +fn test_determine_stock_smart_venue_reuses_cached_symbol_venue() { + let provider = create_test_provider(); + provider.insert_test_instrument(InstrumentAny::from(equity_aapl()), 265598, 1); + let contract = Contract { + contract_id: 0, + symbol: Symbol::from("AAPL"), + security_type: SecurityType::Stock, + exchange: Exchange::from("SMART"), + primary_exchange: Exchange::default(), + currency: Currency::from("USD"), + ..Default::default() + }; + + let venue = provider.determine_venue(&contract, None); + + assert_eq!(venue.as_str(), "XNAS"); +} + +#[rstest] +fn test_symbology_method() { + let provider = create_test_provider(); + let method = provider.symbology_method(); + // Default should be Simplified + assert!(matches!(method, crate::config::SymbologyMethod::Simplified)); +} + +#[rstest] +fn test_get_price_magnifier_defaults_zero_to_one() { + let provider = create_test_provider(); + let instrument = equity_aapl(); + let instrument_id = instrument.id(); + + provider.insert_test_instrument(InstrumentAny::from(instrument), 265598, 0); + + assert_eq!(provider.get_price_magnifier(&instrument_id), 1); +} + +#[rstest] +fn test_force_instrument_update_caching() { + // Test that force_instrument_update parameter affects caching behavior + // This is tested indirectly through the load methods + // The actual caching logic is in fetch_contract_details + let provider = create_test_provider(); + let instrument_id = InstrumentId::new(NautilusSymbol::from("AAPL"), Venue::from("NASDAQ")); + + // Initially, instrument should not be cached + assert!(provider.find(&instrument_id).is_none()); +} + +#[rstest] +fn test_is_spread_instrument_id() { + // Test spread instrument ID detection + let spread_id = InstrumentId::new( + NautilusSymbol::from("(1)AAPL___((2))MSFT"), + Venue::from("NASDAQ"), + ); + assert!(is_spread_instrument_id(&spread_id)); + + // Test non-spread instrument ID + let regular_id = InstrumentId::new(NautilusSymbol::from("AAPL"), Venue::from("NASDAQ")); + assert!(!is_spread_instrument_id(®ular_id)); +} + +#[rstest] +fn test_parse_spread_instrument_id_to_legs() { + let leg_tuples = [ + ( + InstrumentId::new(NautilusSymbol::from("MSFT"), Venue::from("NASDAQ")), + -2, + ), + ( + InstrumentId::new(NautilusSymbol::from("AAPL"), Venue::from("NASDAQ")), + 1, + ), + ]; + let spread_id = create_spread_instrument_id(&leg_tuples).unwrap(); + + assert_eq!(spread_id.symbol.as_str(), "(1)AAPL___((2))MSFT"); + + let result = parse_spread_instrument_id_to_legs(&spread_id); + assert!(result.is_ok()); + + let legs = result.unwrap(); + assert_eq!(legs.len(), 2); + + // Check first leg (positive ratio) + assert_eq!(legs[0].0.symbol.as_str(), "AAPL"); + assert_eq!(legs[0].1, 1); + + // Check second leg (negative ratio) + assert_eq!(legs[1].0.symbol.as_str(), "MSFT"); + assert_eq!(legs[1].1, -2); } diff --git a/crates/adapters/interactive_brokers/src/python/config.rs b/crates/adapters/interactive_brokers/src/python/config.rs index 6e33c1d51009..936f9221a0a0 100644 --- a/crates/adapters/interactive_brokers/src/python/config.rs +++ b/crates/adapters/interactive_brokers/src/python/config.rs @@ -15,14 +15,24 @@ //! Python bindings for Interactive Brokers configuration types. +use std::str::FromStr; + use nautilus_core::{python::to_pyvalue_err, string::secret::SecretString}; use nautilus_model::identifiers::InstrumentId; use pyo3::prelude::*; -use crate::config::{ - DockerizedIBGatewayConfig, InteractiveBrokersDataClientConfig, - InteractiveBrokersExecutionClientConfig, InteractiveBrokersInstrumentProviderConfig, - MarketDataType, TradingMode, +use crate::{ + common::{ + consts::{DEFAULT_CLIENT_ID, DEFAULT_HOST, DEFAULT_PORT}, + contracts::parse_configured_contract_from_json, + enums::IbSecurityType, + }, + config::{ + DockerizedIBGatewayConfig, InteractiveBrokersDataClientConfig, + InteractiveBrokersExecutionClientConfig, InteractiveBrokersInstrumentProviderConfig, + MarketDataType, SymbologyMethod, TradingMode, + }, + python::conversion::py_list_to_json_values, }; fn validate_order_id_client_slot(client_id: i32) -> PyResult<()> { @@ -40,7 +50,7 @@ fn validate_order_id_client_slot(client_id: i32) -> PyResult<()> { impl InteractiveBrokersDataClientConfig { /// Creates a new `InteractiveBrokersDataClientConfig` instance. #[new] - #[pyo3(signature = (host=None, port=None, client_id=None, use_regular_trading_hours=None, market_data_type=None, ignore_quote_tick_size_updates=None, connection_timeout=None, request_timeout=None, handle_revised_bars=None, batch_quotes=None, instrument_provider=None, dockerized_gateway=None))] + #[pyo3(signature = (host=None, port=None, client_id=None, use_regular_trading_hours=None, market_data_type=None, ignore_quote_tick_size_updates=None, connection_timeout=None, request_timeout=None, handle_revised_bars=None, batch_quotes=None, instrument_provider=None, dockerized_gateway=None, all_last_trades=None, subscription_idle_timeout_secs=None))] #[allow(clippy::too_many_arguments)] fn py_new( host: Option, @@ -55,6 +65,8 @@ impl InteractiveBrokersDataClientConfig { batch_quotes: Option, instrument_provider: Option, dockerized_gateway: Option<&DockerizedIBGatewayConfig>, + all_last_trades: Option, + subscription_idle_timeout_secs: Option, ) -> PyResult { if dockerized_gateway.is_some() { return Err(to_pyvalue_err( @@ -62,12 +74,12 @@ impl InteractiveBrokersDataClientConfig { )); } - let host = host.unwrap_or_else(|| crate::common::consts::DEFAULT_HOST.to_string()); - let port = port.unwrap_or(crate::common::consts::DEFAULT_PORT); - let client_id = client_id.unwrap_or(crate::common::consts::DEFAULT_CLIENT_ID); + let host = host.unwrap_or_else(|| DEFAULT_HOST.to_string()); + let port = port.unwrap_or(DEFAULT_PORT); + let client_id = client_id.unwrap_or(DEFAULT_CLIENT_ID); let request_timeout = request_timeout.unwrap_or(60); - Ok(Self { + let config = Self { host, port, client_id, @@ -78,8 +90,12 @@ impl InteractiveBrokersDataClientConfig { request_timeout, handle_revised_bars: handle_revised_bars.unwrap_or(false), batch_quotes: batch_quotes.unwrap_or(true), + all_last_trades: all_last_trades.unwrap_or_else(|| Self::default().all_last_trades), + subscription_idle_timeout_secs, instrument_provider: instrument_provider.unwrap_or_default(), - }) + }; + config.validate().map_err(to_pyvalue_err)?; + Ok(config) } /// Returns the host. @@ -142,6 +158,18 @@ impl InteractiveBrokersDataClientConfig { self.batch_quotes } + /// Returns whether tick-by-tick subscriptions include special-condition trades. + #[getter] + fn all_last_trades(&self) -> bool { + self.all_last_trades + } + + /// Returns the optional subscription idle interval in seconds. + #[getter] + fn subscription_idle_timeout_secs(&self) -> Option { + self.subscription_idle_timeout_secs + } + /// Returns the instrument provider configuration. #[getter] fn instrument_provider(&self) -> InteractiveBrokersInstrumentProviderConfig { @@ -183,9 +211,9 @@ impl InteractiveBrokersExecutionClientConfig { )); } - let host = host.unwrap_or_else(|| crate::common::consts::DEFAULT_HOST.to_string()); - let port = port.unwrap_or(crate::common::consts::DEFAULT_PORT); - let client_id = client_id.unwrap_or(crate::common::consts::DEFAULT_CLIENT_ID); + let host = host.unwrap_or_else(|| DEFAULT_HOST.to_string()); + let port = port.unwrap_or(DEFAULT_PORT); + let client_id = client_id.unwrap_or(DEFAULT_CLIENT_ID); validate_order_id_client_slot(client_id)?; let request_timeout = request_timeout.unwrap_or(60); @@ -221,7 +249,7 @@ impl InteractiveBrokersExecutionClientConfig { self.client_id } - /// Returns the account ID. + /// Returns the raw IB account code. #[getter] fn account_id(&self) -> Option { self.account_id.clone() @@ -276,7 +304,7 @@ impl InteractiveBrokersInstrumentProviderConfig { #[allow(clippy::too_many_arguments)] fn py_new( py: Python<'_>, - symbology_method: Option, + symbology_method: Option, load_ids: Option>, load_contracts: Option>, min_expiry_days: Option, @@ -294,7 +322,17 @@ impl InteractiveBrokersInstrumentProviderConfig { symbology_method: symbology_method.unwrap_or_default(), load_ids: load_ids.unwrap_or_default(), load_contracts: if let Some(c) = load_contracts { - crate::python::conversion::py_list_to_json_values(c.bind(py))? + py_list_to_json_values(c.bind(py))? + .iter() + .enumerate() + .map(|(index, value)| { + parse_configured_contract_from_json(value).map_err(|e| { + to_pyvalue_err(format!( + "Invalid load_contracts entry at index {index}: {e}" + )) + }) + }) + .collect::>>()? } else { Vec::new() }, @@ -305,7 +343,11 @@ impl InteractiveBrokersInstrumentProviderConfig { cache_validity_days, convert_exchange_to_mic_venue: convert_exchange_to_mic_venue.unwrap_or(false), symbol_to_mic_venue: symbol_to_mic_venue.unwrap_or_default(), - filter_sec_types: filter_sec_types.unwrap_or_default(), + filter_sec_types: filter_sec_types + .unwrap_or_default() + .into_iter() + .map(|value| IbSecurityType::from_str(&value).map_err(to_pyvalue_err)) + .collect::>()?, filter_callable, cache_path, }) @@ -313,7 +355,7 @@ impl InteractiveBrokersInstrumentProviderConfig { /// Returns the symbology method. #[getter] - fn symbology_method(&self) -> crate::config::SymbologyMethod { + fn symbology_method(&self) -> SymbologyMethod { self.symbology_method } @@ -329,8 +371,8 @@ impl InteractiveBrokersInstrumentProviderConfig { let json_mod = py.import("json")?; let list = pyo3::types::PyList::empty(py); - for value in &self.load_contracts { - let json_str = value.to_string(); + for contract in &self.load_contracts { + let json_str = contract.to_json_value().to_string(); let dict = json_mod.call_method1("loads", (json_str,))?; list.append(dict)?; } @@ -382,7 +424,10 @@ impl InteractiveBrokersInstrumentProviderConfig { /// Returns the filter security types. #[getter] fn filter_sec_types(&self) -> Vec { - self.filter_sec_types.iter().cloned().collect() + self.filter_sec_types + .iter() + .map(ToString::to_string) + .collect() } /// Returns the custom instrument filter callable path. diff --git a/crates/adapters/interactive_brokers/src/python/conversion.rs b/crates/adapters/interactive_brokers/src/python/conversion.rs index 5ab04dbebe2a..d1bab857f5df 100644 --- a/crates/adapters/interactive_brokers/src/python/conversion.rs +++ b/crates/adapters/interactive_brokers/src/python/conversion.rs @@ -30,16 +30,16 @@ use crate::common::{ /// Convert a Python object to a JSON value. pub fn py_to_json_value(obj: &Bound<'_, PyAny>) -> PyResult { // Try to call .json() first (NautilusConfig) - if let Ok(json_bytes) = obj.call_method0("json") { - if let Ok(bytes) = json_bytes.clone().cast_into::() { - let json_str = std::str::from_utf8(bytes.as_bytes()) - .map_err(|e| to_pyvalue_err(format!("Invalid UTF-8 in json output: {e}")))?; + if let Ok(json_bytes) = obj.call_method0("json") + && let Ok(bytes) = json_bytes.clone().cast_into::() + { + let json_str = std::str::from_utf8(bytes.as_bytes()) + .map_err(|e| to_pyvalue_err(format!("Invalid UTF-8 in json output: {e}")))?; - let value: serde_json::Value = serde_json::from_str(json_str) - .map_err(|e| to_pyvalue_err(format!("Invalid JSON: {e}")))?; + let value: serde_json::Value = serde_json::from_str(json_str) + .map_err(|e| to_pyvalue_err(format!("Invalid JSON: {e}")))?; - return Ok(value); - } + return Ok(value); } // Try to treat as dict @@ -217,11 +217,23 @@ pub fn contract_details_to_pyobject( details_dict.set_item("nextOptionType", &details.next_option_type)?; details_dict.set_item("nextOptionPartial", details.next_option_partial)?; details_dict.set_item("notes", &details.notes)?; - details_dict.set_item("minSize", details.min_size.to_string())?; - details_dict.set_item("sizeIncrement", details.size_increment.to_string())?; + details_dict.set_item( + "minSize", + details + .min_size + .map_or_else(String::new, |value| value.to_string()), + )?; + details_dict.set_item( + "sizeIncrement", + details + .size_increment + .map_or_else(String::new, |value| value.to_string()), + )?; details_dict.set_item( "suggestedSizeIncrement", - details.suggested_size_increment.to_string(), + details + .suggested_size_increment + .map_or_else(String::new, |value| value.to_string()), )?; details_dict.set_item("fundName", &details.fund_name)?; details_dict.set_item("fundFamily", &details.fund_family)?; diff --git a/crates/adapters/interactive_brokers/src/python/enums.rs b/crates/adapters/interactive_brokers/src/python/enums.rs index 8d11a0e66754..86cc41b7dd5a 100644 --- a/crates/adapters/interactive_brokers/src/python/enums.rs +++ b/crates/adapters/interactive_brokers/src/python/enums.rs @@ -11,18 +11,15 @@ use pyo3::prelude::*; -use crate::{ - common::enums::{ - IbAction, IbArticleType, IbAuctionStrategy, IbAuctionType, IbBondIdentifierKind, - IbBuilderTimeInForce, IbComboLegOpenClose, IbConditionConjunction, IbConditionKind, - IbExerciseAction, IbFundAssetType, IbFundDistributionPolicyIndicator, IbHistoricalBarSize, - IbHistoricalTickType, IbHistoricalWhatToShow, IbLegAction, IbLiquidity, IbOcaType, - IbOptionRight, IbOrderOpenClose, IbOrderOrigin, IbOrderStatus, IbOrderType, - IbRealtimeBarSize, IbRealtimeWhatToShow, IbReferencePriceType, IbRiskAversion, IbRule80A, - IbSecurityType, IbShortSaleSlot, IbTickType, IbTimeInForce, IbTradingHours, - IbTriggerMethod, IbTwapStrategyType, IbVolatilityType, - }, - error::{ErrorCategory, InteractiveBrokersErrorKind}, +use crate::common::enums::{ + IbAction, IbArticleType, IbAuctionStrategy, IbAuctionType, IbBondIdentifierKind, + IbBuilderTimeInForce, IbComboLegOpenClose, IbConditionConjunction, IbConditionKind, + IbExerciseAction, IbFundAssetType, IbFundDistributionPolicyIndicator, IbHistoricalBarSize, + IbHistoricalTickType, IbHistoricalWhatToShow, IbLegAction, IbLiquidity, IbOcaType, + IbOptionRight, IbOrderOpenClose, IbOrderOrigin, IbOrderStatus, IbOrderType, IbRealtimeBarSize, + IbRealtimeWhatToShow, IbReferencePriceType, IbRiskAversion, IbRule80A, IbSecurityType, + IbShortSaleSlot, IbTickType, IbTimeInForce, IbTradingHours, IbTriggerMethod, + IbTwapStrategyType, IbVolatilityType, }; #[pymethods] @@ -227,19 +224,6 @@ macro_rules! py_str_enum { }; } -macro_rules! py_marker_enum { - ($ty:ty) => { - #[pymethods] - #[pyo3_stub_gen::derive::gen_stub_pymethods] - impl $ty { - #[pyo3(name = "as_str")] - fn py_as_str(&self) -> String { - format!("{self:?}") - } - } - }; -} - py_i32_enum!(IbTickType); py_i32_enum!(IbOrderOrigin); @@ -259,6 +243,3 @@ py_str_enum!(IbLegAction); py_str_enum!(IbFundDistributionPolicyIndicator); py_str_enum!(IbFundAssetType); py_str_enum!(IbBondIdentifierKind); - -py_marker_enum!(ErrorCategory); -py_marker_enum!(InteractiveBrokersErrorKind); diff --git a/crates/adapters/interactive_brokers/src/python/gateway.rs b/crates/adapters/interactive_brokers/src/python/gateway.rs index 90160fbc996f..055a285a6778 100644 --- a/crates/adapters/interactive_brokers/src/python/gateway.rs +++ b/crates/adapters/interactive_brokers/src/python/gateway.rs @@ -16,7 +16,7 @@ //! Python bindings for the Interactive Brokers gateway management. #[cfg(feature = "gateway")] -use nautilus_common::live::get_runtime; +use nautilus_common::live::block_on_nautilus_with; #[cfg(feature = "gateway")] use nautilus_core::python::to_pyruntime_err; #[cfg(feature = "gateway")] @@ -64,10 +64,6 @@ impl DockerizedIBGateway { } /// Start the gateway. - /// - /// # Arguments - /// - /// * `wait` - Optional wait time in seconds #[pyo3(name = "start")] fn py_start<'py>(&self, py: Python<'py>, wait: Option) -> PyResult> { let mut gateway = self.clone(); @@ -82,16 +78,11 @@ impl DockerizedIBGateway { #[pyo3(name = "start_blocking")] fn py_start_blocking(&self, wait: Option) -> PyResult<()> { let mut gateway = self.clone(); - get_runtime() - .block_on(async move { gateway.start(wait).await }) + block_on_nautilus_with(move || async move { gateway.start(wait).await }) .map_err(|e| to_pyruntime_err(format!("{e}"))) } /// Safely start the gateway. - /// - /// # Arguments - /// - /// * `wait` - Optional wait time in seconds #[pyo3(name = "safe_start")] fn py_safe_start<'py>( &self, @@ -110,8 +101,7 @@ impl DockerizedIBGateway { #[pyo3(name = "safe_start_blocking")] fn py_safe_start_blocking(&self, wait: Option) -> PyResult<()> { let mut gateway = self.clone(); - get_runtime() - .block_on(async move { gateway.safe_start(wait).await }) + block_on_nautilus_with(move || async move { gateway.safe_start(wait).await }) .map_err(|e| to_pyruntime_err(format!("{e}"))) } @@ -130,8 +120,7 @@ impl DockerizedIBGateway { #[pyo3(name = "stop_blocking")] fn py_stop_blocking(&self) -> PyResult<()> { let gateway = self.clone(); - get_runtime() - .block_on(async move { gateway.stop().await }) + block_on_nautilus_with(move || async move { gateway.stop().await }) .map_err(|e| to_pyruntime_err(format!("{e}"))) } diff --git a/crates/adapters/interactive_brokers/src/python/historical.rs b/crates/adapters/interactive_brokers/src/python/historical.rs index 65784984ac6c..2f0870c49f36 100644 --- a/crates/adapters/interactive_brokers/src/python/historical.rs +++ b/crates/adapters/interactive_brokers/src/python/historical.rs @@ -17,7 +17,7 @@ use ibapi::contracts::Contract; use jiff::Timestamp; -use nautilus_common::live::get_runtime; +use nautilus_common::live::block_on_nautilus_with; use nautilus_core::python::{to_pyruntime_err, to_pyvalue_err}; use nautilus_model::{ data::{Bar, Data}, @@ -28,7 +28,9 @@ use nautilus_model::{ use pyo3::{prelude::*, types::PyList}; use crate::{ - common::enums::IbHistoricalTickType, historical::HistoricalInteractiveBrokersClient, + common::enums::IbHistoricalTickType, config::InteractiveBrokersDataClientConfig, + historical::HistoricalInteractiveBrokersClient, + providers::instruments::InteractiveBrokersInstrumentProvider, python::conversion::py_list_to_contracts, }; @@ -38,11 +40,10 @@ impl HistoricalInteractiveBrokersClient { #[new] #[allow(clippy::needless_pass_by_value)] fn py_new( - instrument_provider: crate::providers::instruments::InteractiveBrokersInstrumentProvider, - config: crate::config::InteractiveBrokersDataClientConfig, + instrument_provider: InteractiveBrokersInstrumentProvider, + config: InteractiveBrokersDataClientConfig, ) -> PyResult { - get_runtime() - .block_on(Self::connect_with_provider(instrument_provider, config)) + block_on_nautilus_with(move || Self::connect_with_provider(instrument_provider, config)) .map_err(to_pyruntime_err) } @@ -60,17 +61,6 @@ impl HistoricalInteractiveBrokersClient { /// the returned bars may fall outside `[start_date_time, end_date_time]`. /// A warning is logged when the requested end date/time is in the past or /// the range spans more than one duration segment. - /// - /// # Arguments - /// - /// * `bar_specifications` - List of bar specifications (e.g., ["1-HOUR-LAST"]) - /// * `end_date_time` - End date for bars - /// * `start_date_time` - Optional start date - /// * `duration` - Optional duration string (e.g., "1 D") - /// * `contracts` - Optional list of IB contracts (dicts with symbol, sec_type, exchange, currency, etc.) - /// * `instrument_ids` - Optional list of instrument IDs - /// * `use_rth` - Use regular trading hours only - /// * `timeout` - Request timeout in seconds #[pyo3(signature = (bar_specifications, end_date_time, start_date_time=None, duration=None, contracts=None, instrument_ids=None, use_rth=true, timeout=60))] #[pyo3(name = "request_bars")] #[allow(clippy::too_many_arguments)] @@ -126,17 +116,6 @@ impl HistoricalInteractiveBrokersClient { } /// Request historical ticks (quotes or trades). - /// - /// # Arguments - /// - /// * `tick_type` - Historical tick type. - /// * `start_date_time` - Start date for ticks - /// * `end_date_time` - End date for ticks - /// * `contracts` - Optional list of IB contracts (dicts with symbol, sec_type, exchange, currency, etc.) - /// * `instrument_ids` - Optional list of instrument IDs - /// * `use_rth` - Use regular trading hours only - /// * `timeout` - Request timeout in seconds - /// * `limit` - Maximum number of ticks to return, or 0 for no explicit limit #[pyo3(signature = (tick_type, start_date_time, end_date_time, contracts=None, instrument_ids=None, use_rth=true, timeout=60, limit=0))] #[pyo3(name = "request_ticks")] #[allow(clippy::too_many_arguments)] @@ -182,10 +161,12 @@ impl HistoricalInteractiveBrokersClient { ) .await .map_err(to_pyruntime_err)?; + // Convert Data values to native Python objects. Python::attach(|py| -> PyResult> { let py_list = PyList::empty(py); for data in data_vec { - py_list.append(data_to_pyobject(py, data)?)?; + let py_obj = data_to_pyobject(py, data)?; + py_list.append(py_obj)?; } Ok(py_list.into()) }) @@ -193,11 +174,6 @@ impl HistoricalInteractiveBrokersClient { } /// Request instruments. - /// - /// # Arguments - /// - /// * `instrument_ids` - Optional list of instrument IDs to load - /// * `contracts` - Optional list of IB contracts (dicts with symbol, sec_type, exchange, currency, etc.) #[pyo3(signature = (instrument_ids=None, contracts=None))] #[pyo3(name = "request_instruments")] #[allow(clippy::needless_pass_by_value)] diff --git a/crates/adapters/interactive_brokers/src/python/mod.rs b/crates/adapters/interactive_brokers/src/python/mod.rs index c787f6d1f8a3..addb02f382ae 100644 --- a/crates/adapters/interactive_brokers/src/python/mod.rs +++ b/crates/adapters/interactive_brokers/src/python/mod.rs @@ -17,13 +17,23 @@ use nautilus_common::factories::{ClientConfig, DataClientFactory, ExecutionClientFactory}; use nautilus_core::python::{to_pyruntime_err, to_pyvalue_err}; +use nautilus_model::data::ensure_rust_extractor_registered; use nautilus_system::get_global_pyo3_registry; use pyo3::prelude::*; +#[cfg(feature = "gateway")] +use crate::gateway::dockerized::{ContainerStatus, DockerizedIBGateway}; use crate::{ - common::consts::IB, - config::{InteractiveBrokersDataClientConfig, InteractiveBrokersExecutionClientConfig}, + common::{consts::IB, enums::*}, + config::{ + DockerizedIBGatewayConfig, InteractiveBrokersDataClientConfig, + InteractiveBrokersExecutionClientConfig, InteractiveBrokersInstrumentProviderConfig, + MarketDataType, SymbologyMethod, TradingMode, + }, + data_types::{InteractiveBrokersSubscriptionIdle, register_ib_custom_data}, factories::{InteractiveBrokersDataClientFactory, InteractiveBrokersExecutionClientFactory}, + historical::HistoricalInteractiveBrokersClient, + providers::instruments::InteractiveBrokersInstrumentProvider, }; pub mod config; @@ -41,12 +51,14 @@ fn extract_interactive_brokers_data_factory( py: Python<'_>, factory: Py, ) -> PyResult> { - match factory.extract::(py) { - Ok(f) => Ok(Box::new(f)), - Err(e) => Err(to_pyvalue_err(format!( - "Failed to extract InteractiveBrokersDataClientFactory: {e}" - ))), - } + factory + .extract::(py) + .map(|factory| Box::new(factory) as Box) + .map_err(|e| { + to_pyvalue_err(format!( + "Failed to extract InteractiveBrokersDataClientFactory: {e}" + )) + }) } #[expect(clippy::needless_pass_by_value)] @@ -54,12 +66,14 @@ fn extract_interactive_brokers_exec_factory( py: Python<'_>, factory: Py, ) -> PyResult> { - match factory.extract::(py) { - Ok(f) => Ok(Box::new(f)), - Err(e) => Err(to_pyvalue_err(format!( - "Failed to extract InteractiveBrokersExecutionClientFactory: {e}" - ))), - } + factory + .extract::(py) + .map(|factory| Box::new(factory) as Box) + .map_err(|e| { + to_pyvalue_err(format!( + "Failed to extract InteractiveBrokersExecutionClientFactory: {e}" + )) + }) } #[expect(clippy::needless_pass_by_value)] @@ -67,12 +81,14 @@ fn extract_interactive_brokers_data_config( py: Python<'_>, config: Py, ) -> PyResult> { - match config.extract::(py) { - Ok(c) => Ok(Box::new(c)), - Err(e) => Err(to_pyvalue_err(format!( - "Failed to extract InteractiveBrokersDataClientConfig: {e}" - ))), - } + config + .extract::(py) + .map(|config| Box::new(config) as Box) + .map_err(|e| { + to_pyvalue_err(format!( + "Failed to extract InteractiveBrokersDataClientConfig: {e}" + )) + }) } #[expect(clippy::needless_pass_by_value)] @@ -80,12 +96,14 @@ fn extract_interactive_brokers_exec_config( py: Python<'_>, config: Py, ) -> PyResult> { - match config.extract::(py) { - Ok(c) => Ok(Box::new(c)), - Err(e) => Err(to_pyvalue_err(format!( - "Failed to extract InteractiveBrokersExecutionClientConfig: {e}" - ))), - } + config + .extract::(py) + .map(|config| Box::new(config) as Box) + .map_err(|e| { + to_pyvalue_err(format!( + "Failed to extract InteractiveBrokersExecutionClientConfig: {e}" + )) + }) } /// Exposed through `nautilus_trader.adapters.interactive_brokers`. @@ -95,60 +113,61 @@ fn extract_interactive_brokers_exec_config( /// Returns an error if any bindings fail to register with the Python module. #[pymodule] pub fn interactive_brokers(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + register_ib_custom_data(); + let _ = ensure_rust_extractor_registered::(); + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; #[cfg(feature = "gateway")] { - m.add_class::()?; - m.add_class::()?; + m.add_class::()?; + m.add_class::()?; } let registry = get_global_pyo3_registry(); diff --git a/crates/adapters/interactive_brokers/src/python/providers.rs b/crates/adapters/interactive_brokers/src/python/providers.rs index 5f75452a31fc..6a9f33613b3a 100644 --- a/crates/adapters/interactive_brokers/src/python/providers.rs +++ b/crates/adapters/interactive_brokers/src/python/providers.rs @@ -20,6 +20,7 @@ use nautilus_model::{identifiers::InstrumentId, python::instruments::instrument_ use pyo3::{prelude::*, types::PyList}; use crate::{ + config::InteractiveBrokersInstrumentProviderConfig, providers::instruments::InteractiveBrokersInstrumentProvider, python::conversion::{contract_details_to_pyobject, py_to_contract}, }; @@ -29,7 +30,7 @@ use crate::{ #[pyo3_stub_gen::derive::gen_stub_pymethods] impl InteractiveBrokersInstrumentProvider { #[new] - fn py_new(config: crate::config::InteractiveBrokersInstrumentProviderConfig) -> Self { + fn py_new(config: InteractiveBrokersInstrumentProviderConfig) -> Self { Self::new(config) } @@ -108,10 +109,6 @@ impl InteractiveBrokersInstrumentProvider { /// Save the current instrument cache to disk. /// - /// # Arguments - /// - /// * `cache_path` - Path to the cache file - /// /// # Errors /// /// Returns an error if serialization or file I/O fails. @@ -132,10 +129,6 @@ impl InteractiveBrokersInstrumentProvider { /// Load instrument cache from disk if valid. /// - /// # Arguments - /// - /// * `cache_path` - Path to the cache file - /// /// # Returns /// /// Returns `true` if cache was loaded successfully and is valid, `false` otherwise. diff --git a/crates/adapters/interactive_brokers/src/stubs.rs b/crates/adapters/interactive_brokers/src/stubs.rs new file mode 100644 index 000000000000..1c1e7514428b --- /dev/null +++ b/crates/adapters/interactive_brokers/src/stubs.rs @@ -0,0 +1,47 @@ +// ------------------------------------------------------------------------------------------------- +// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. +// https://nautechsystems.io +// +// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------------------------------- + +//! Test stubs shared by the data and execution tests. + +use futures_util::Stream; +use ibapi::{Error, subscriptions::SubscriptionItem}; + +/// Channel-backed stand-in for an ibapi subscription; items arrive as data. +pub(crate) struct ChannelSubscription(tokio::sync::mpsc::UnboundedReceiver>); + +impl ChannelSubscription { + pub(crate) const fn new( + receiver: tokio::sync::mpsc::UnboundedReceiver>, + ) -> Self { + Self(receiver) + } + + pub(crate) fn close(&mut self) { + self.0.close(); + } +} + +impl Stream for ChannelSubscription { + type Item = Result, Error>; + + fn poll_next( + mut self: std::pin::Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll> { + self.0 + .poll_recv(cx) + .map(|item| item.map(|result| result.map(SubscriptionItem::Data))) + } +} diff --git a/docs/integrations/interactive_brokers.md b/docs/integrations/interactive_brokers.md index a243060540fc..9a8b45291197 100644 --- a/docs/integrations/interactive_brokers.md +++ b/docs/integrations/interactive_brokers.md @@ -1,324 +1,792 @@ # Interactive Brokers -Interactive Brokers (IB) provides market access across equities, options, futures, currencies, -bonds, funds, and other asset classes. The NautilusTrader adapter connects to Trader Workstation -(TWS) or IB Gateway through the [TWS API](https://ibkrcampus.com/campus/ibkr-api-page/twsapi-doc/). +The Interactive Brokers adapter connects NautilusTrader to Trader Workstation (TWS) or IB Gateway +through the [TWS API](https://ibkrcampus.com/campus/ibkr-api-page/twsapi-doc/). It provides live +market data, execution, instrument discovery, historical requests, and optional management of a +Dockerized IB Gateway. -The adapter provides live market data, execution, historical data, instrument loading, and optional -Dockerized IB Gateway management through the same Rust implementation and Python bindings. +The Python package includes the adapter. Follow the [installation guide](../getting_started/installation.md), +then use the examples under [`examples/live/interactive_brokers`](https://github.com/nautechsystems/nautilus_trader/tree/develop/examples/live/interactive_brokers/). -## Installation +## Connect to TWS or IB Gateway -Install NautilusTrader using the [installation guide](../getting_started/installation.md). The -Interactive Brokers adapter and Docker gateway support are included in the Python package; no -adapter-specific extra is required. - -## Examples - -- [Python examples](https://github.com/nautechsystems/nautilus_trader/tree/develop/examples/live/interactive_brokers/) -- [Rust examples](https://github.com/nautechsystems/nautilus_trader/tree/develop/crates/adapters/interactive_brokers/examples/) - -## Getting started - -Run either TWS or IB Gateway before starting a client, and configure that application to accept -socket API connections. - -IB uses different default ports for each application and trading mode: +Enable socket API access in TWS or IB Gateway before starting a client. IB uses these default ports: | Application | Paper trading | Live trading | -| ----------- | ------------: | -----------: | -| TWS | `7497` | `7496` | -| IB Gateway | `4002` | `4001` | +| ----------- | ------------- | ------------ | +| TWS | `7497` | `7496` | +| IB Gateway | `4002` | `4001` | -The adapter defaults to `127.0.0.1:4002`, which matches a local paper-trading IB Gateway. Set the -port explicitly when using TWS or a live account. +The pinned `ibapi 4.0.1` transport requires TWS or IB Gateway server protocol version **213 or newer**. +This is the API protocol version, not the application's release number. Older servers are unsupported +even when their TCP port accepts connections. [Issue #4796](https://github.com/nautechsystems/nautilus_trader/issues/4796) +reports this failure with protocol 187 on Gateway 10.41. Upgrade TWS or IB Gateway before connecting. +The transport rejects an older version before sending `StartApi` when the server completes the initial +handshake. If an old server does not answer that handshake, the connection timeout also applies; +increasing the timeout does not add protocol support. -### Connect to TWS or IB Gateway +The adapter defaults to `127.0.0.1:4002`. Each process connected to the same TWS or Gateway session +needs a distinct IB API `client_id`. An execution client ID cannot be a multiple of `1000` because +the adapter partitions order IDs with `client_id % 1000`. -Import the public configuration types from `nautilus_trader.adapters.interactive_brokers`: +The data and execution clients can share one provider configuration: ```python -from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersDataClientConfig -from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersExecutionClientConfig -from nautilus_trader.adapters.interactive_brokers import MarketDataType +from nautilus_trader.adapters import interactive_brokers as ib +from nautilus_trader.model import AccountId +from nautilus_trader.model import InstrumentId +from nautilus_trader.model import TraderId -data_config = InteractiveBrokersDataClientConfig( +trader_id = TraderId.from_str("TRADER-001") +account_id = AccountId.from_str("IB-DU123456") +provider_config = ib.InteractiveBrokersInstrumentProviderConfig( + symbology_method=ib.SymbologyMethod.SIMPLIFIED, + load_ids={InstrumentId.from_str("AAPL.XNAS")}, +) + +data_config = ib.InteractiveBrokersDataClientConfig( host="127.0.0.1", port=7497, client_id=101, - market_data_type=MarketDataType.DELAYED, + market_data_type=ib.MarketDataType.DELAYED_FROZEN, + instrument_provider=provider_config, ) -exec_config = InteractiveBrokersExecutionClientConfig( +exec_config = ib.InteractiveBrokersExecutionClientConfig( host="127.0.0.1", port=7497, - client_id=101, + client_id=102, account_id="DU123456", + instrument_provider=provider_config, ) + +data_factory = ib.InteractiveBrokersDataClientFactory() +exec_factory = ib.InteractiveBrokersExecutionClientFactory() ``` -Use a distinct client ID for each process connected to the same TWS or IB Gateway session. An -execution client ID cannot be a multiple of `1000` because the adapter partitions order IDs by -`client_id % 1000`. +Pass these factories and configs to `LiveNode.builder`. The +[`connect_with_tws.py`](https://github.com/nautechsystems/nautilus_trader/blob/develop/examples/live/interactive_brokers/connect_with_tws.py) +example shows the complete node setup. Its default invocation constructs the node offline; set +`IB_V2_RUN_NODE=1` to connect. + +### Market data modes + +| Enum value | IB mode | +| ------------------------------- | -------------------------------------------- | +| `MarketDataType.REALTIME` | Live subscribed data. | +| `MarketDataType.FROZEN` | Last available live data after market close. | +| `MarketDataType.DELAYED` | Delayed data. | +| `MarketDataType.DELAYED_FROZEN` | Last available delayed data. | -The current [TWS example](https://github.com/nautechsystems/nautilus_trader/blob/develop/examples/live/interactive_brokers/connect_with_tws.py) -and [Dockerized gateway example](https://github.com/nautechsystems/nautilus_trader/blob/develop/examples/live/interactive_brokers/connect_with_dockerized_gateway.py) -show how to add these configs and their factories to a `LiveNode`. +IB permissions still determine which mode and instruments return data. Delayed or delayed-frozen +data can support paper-account checks outside market hours, but it does not prove a live tick path. -### Use a Dockerized IB Gateway +### Dockerized IB Gateway -The adapter can manage the +`DockerizedIBGateway` manages the [gnzsnz IB Gateway container](https://github.com/gnzsnz/ib-gateway-docker). Supply credentials in -the config or through `TWS_USERNAME` and `TWS_PASSWORD`: +the config or set `TWS_USERNAME` and `TWS_PASSWORD`: ```python -from nautilus_trader.adapters.interactive_brokers import DockerizedIBGateway -from nautilus_trader.adapters.interactive_brokers import DockerizedIBGatewayConfig -from nautilus_trader.adapters.interactive_brokers import TradingMode +from nautilus_trader.adapters import interactive_brokers as ib -gateway = DockerizedIBGateway( - DockerizedIBGatewayConfig( - trading_mode=TradingMode.PAPER, +gateway = ib.DockerizedIBGateway( + ib.DockerizedIBGatewayConfig( + trading_mode=ib.TradingMode.PAPER, read_only_api=True, + timeout=300, ), ) -gateway.start_blocking() +gateway.safe_start_blocking() -print(gateway.host) -print(gateway.port) +data_config = ib.InteractiveBrokersDataClientConfig( + host=gateway.host, + port=gateway.port, + client_id=101, +) ``` -Start `DockerizedIBGateway` separately, then pass its `host` and `port` to the data and execution -configs. Passing a non-`None` `dockerized_gateway` argument to either client config raises -`ValueError` because Python does not own the container lifecycle. - -Set `read_only_api=False` only when the gateway must submit orders. The default container is -`ghcr.io/gnzsnz/ib-gateway:stable`; `vnc_port` accepts ports from `5900` through `5999` when remote -desktop access is required. +Start the gateway before constructing clients that connect. Python owns the container lifecycle +through `DockerizedIBGateway`; the client configs accept only `host` and `port`. Set +`read_only_api=False` only when the gateway must accept orders. Readiness uses login messages from +the current container session, so retained logs from an earlier session cannot report a false ready +state. + +## Architecture + +The adapter is one Rust implementation exposed through the native Rust API and PyO3 bindings. +It translates between IB's TWS API and the Nautilus live engines, and it does not own a +transport: the `ibapi` crate speaks the TWS socket protocol and reconnects it, while the +adapter's job is domain mapping, subscription lifecycle, and policy (commands, events, +instrument conversion, stream resubscription, and execution reconciliation). That boundary +explains the crate's layout, and it is why the `http` and `websocket` modules standard in other +adapters do not exist here. + +```mermaid +flowchart LR + TWS[TWS or IB Gateway] + subgraph ibapi [ibapi crate] + TRANSPORT[Transport: reconnect, routing, decoding] + end + subgraph adapter [nautilus-interactive-brokers] + SHARED[common::shared_client] + DATA[data client] + EXEC[execution client] + PROV[instrument provider] + HIST[historical client] + end + subgraph nautilus [Nautilus live] + DE[DataEngine] + EE[ExecutionEngine] + end + TWS <--> TRANSPORT + TRANSPORT <--> SHARED + SHARED --> DATA + SHARED --> EXEC + SHARED --> PROV + SHARED --> HIST + DATA -- DataEvent --> DE + EXEC -- ExecutionEvent --> EE + PROV --> DATA + PROV --> EXEC +``` -## Components +Data, execution, and historical clients with the same `(host, port, client_id)` share one +reference-counted `ibapi` connection. Concurrent acquisition for the same key converges on one +connection, and the registry removes it after the final client releases its handle. + +| Module | Responsibility | +| ------------ | --------------------------------------------------------------------------------- | +| `common` | Contracts, enums, generic spread IDs, symbology, and shared connections. | +| `providers` | Contract qualification, chain discovery, venue resolution, and instrument cache. | +| `data` | Live subscriptions, historical request routing, conversion, and stream recovery. | +| `execution` | Commands, order state, updates, reports, conditions, spreads, and reconciliation. | +| `historical` | Standalone instrument, bar, and tick requests. | +| `gateway` | Docker container configuration, lifecycle, and readiness. | +| `python` | PyO3 classes, enums, factories, and conversion boundaries. | +| `config` | Typed data, execution, provider, and gateway configuration. | +| `factories` | `DataClientFactory` and `ExecutionClientFactory` implementations. | + +The integration surface with the rest of Nautilus is deliberately thin. The node builder hands +each factory a typed config; the factory builds the client and seeds the instrument provider +from the engine cache, which the adapter never writes. The data client sends typed data events +(ticks, bars, instruments, and correlated request responses) and the execution client sends +typed execution events (order events, fills, and account state) into the engines through their +event channels; these channels are the only paths into the engines. The adapter registers its +factories and config extractors with the global PyO3 registry, which is how Python +`TradingNode` configurations reach the Rust factories. Reconciliation is a pull interface +driven by the execution engine (see [Reconciliation](#reconciliation)). + +### Reconnect and resubscribe + +The `ibapi` transport reconnects the socket with Fibonacci backoff up to a bounded attempt +count, then establishes a complete session; a TCP connection followed by a transient handshake +or startup error consumes an attempt and retries instead of ending recovery. A successful +reconnect resets open subscription channels by delivering a connection-reset error to each; on +exhaustion the transport shuts down and the adapter reports disconnected. Each data stream then +waits for the transport, subscribes again, and retries failed resubscriptions with a delay. One +generic stream driver owns this lifecycle policy for every stream type, so recovery behavior +cannot drift between quotes, trades, depth, bars, greeks, and index prices. Market-data farm +notices use the same stream policy without treating a farm outage as a socket failure. TWS +resets the session's market-data type to realtime after a reconnect; the notice monitor +restores the configured frozen or delayed mode before releasing recovered subscriptions. + +Order-bound error frames arrive on the order update stream as notices carrying the originating +order or request ID, delivered by the transport's routing layer rather than a decoded message +variant, so the update stream stays alive across error frames. + +```mermaid +stateDiagram-v2 + [*] --> Subscribing + Subscribing --> Streaming: subscribed + Subscribing --> Waiting: transport_down + Subscribing --> Backoff: subscribe_failed + Streaming --> Subscribing: connection_reset + Streaming --> Backoff: farm_recovery + Backoff --> Subscribing: retry_delay + Waiting --> Subscribing: transport_connected + Streaming --> [*]: unsubscribe + Waiting --> [*]: unsubscribe +``` -The public Python module exports these main components: +Quotes, trades, depth, bars, option greeks, and index prices follow this lifecycle. Before a depth +stream applies data from a replacement subscription, it clears the existing book so stale levels +cannot survive. If the order-update or global notice stream ends, the execution client reports +disconnected instead of appearing healthy with a dead stream. + +### Order identity and cancellation + +Distinct broker permanent IDs sharing one client order reference remain separate physical orders. +The adapter reports each additional order under its own client order ID, +`DUP-::PERM-`, so the execution engine creates it as an +external order, claimed by the instrument's external order claim or otherwise `EXTERNAL`. An +unresolved duplicate group blocks modification. + +When an execution arrives for an order the adapter does not track, including such a duplicate, the +adapter holds the fill and queries the order's details and executions. It then reports the order +together with its real fills, so the engine creates the order at its full quantity. The query is +bounded by `request_timeout` and at most 3 seconds, below the live node's default +`position_check_threshold_ms` of 5 seconds. If the query fails, returns nothing, or times out, the +adapter logs a warning and forwards the held fills unchanged. + +With `strategy_only=True` (the default), the strategy issues individual cancellations for its own orders. +With `strategy_only=False`, cancel-all is instrument-wide within the selected account and side. +A side-filtered cancel-all skips duplicate orders whose broker side is still unknown. +Canceling the original order +also considers its known working duplicates. The adapter cancels only uniquely bound routes owned by +the configured API client; ambiguous or unbound orders remain explicitly unresolved and require TWS +binding or operator action. One cancellation confirmation does not establish group completion. + +Risk checks and execution use the same account routing. The risk engine denies an order whose +account is unresolved rather than skipping account checks. + +### Execution order flow + +The execution client is split along message direction: outbound commands (submit, modify, +cancel), inbound venue updates, combo-fill assembly, and account streams are separate modules +sharing one order-state owner. That owner holds the client-to-venue ID maps, active orders, and +a bounded terminal-order cache behind a single lock; the terminal cache absorbs IB's execution +replays after a reconnect, so a completed order's re-sent fills deduplicate instead of +duplicating. The client converts each Nautilus order once, applies optional IB tags, and then +submits it through `ibapi`. + +```mermaid +sequenceDiagram + participant EE as ExecutionEngine + participant C as Commands + participant T as Order state + participant IB as ibapi client + participant U as Updates + EE->>C: SubmitOrder + C->>T: track order + C->>IB: submit converted order + IB-->>U: openOrder / orderStatus + U->>T: mark accepted + U-->>EE: OrderAccepted + IB-->>U: order-bound error notice + U->>T: resolve ID, check state + U-->>EE: OrderRejected or OrderModifyRejected + IB-->>U: executionData + commissionReport + U->>T: deduplicate, update fill progress + U-->>EE: OrderFilled +``` -- `InteractiveBrokersDataClientFactory`: creates live market data clients. -- `InteractiveBrokersExecutionClientFactory`: creates live execution clients. -- `InteractiveBrokersInstrumentProvider`: resolves IB contracts and Nautilus instruments. -- `HistoricalInteractiveBrokersClient`: requests historical instruments, bars, and ticks. -- `DockerizedIBGateway`: manages a containerized IB Gateway. +```mermaid +flowchart TD + Command[Submit or modify command] --> Ready{Client ready} + Ready -->|no| Rejected[Emit rejection event] + Ready -->|yes| Transform[Transform Nautilus order] + Transform --> Submit[Submit through ibapi] + Submit --> Track[Record order state] + Track --> Updates[Order and execution updates] + Updates --> Pair[Pair execution with commission] + Pair --> Deduplicate[Deduplicate fill] + Deduplicate --> Event[Emit Nautilus event] +``` -## Symbology and instruments +Every engine-visible order event originates from venue evidence on the update stream: +`OrderAccepted` requires an order status or open-order message, never local send success. An +order-bound IB error notice resolves against tracked orders and is handled by order state. +Before acceptance, an order rejection code (200 to 399) emits a terminal `OrderRejected`; any +other error code triggers a venue query, and the order is rejected only when IB lists it neither +as open nor as completed, since IB sends such codes for orders it keeps working (10349 sets the +time in force from an order preset). A presence query that fails, times out, or ends early leaves the order +working. After acceptance a notice emits `OrderModifyRejected` when +a modify is pending and `OrderCancelRejected` when a cancel is pending, and otherwise triggers a +venue presence query +rather than a terminal event, because IB reports modify rejections with the same codes while +the original order keeps working. Warning codes (2100 to 2169), warning-form order messages +(code 399 with a `Warning:` line), and the cancel confirmation (202) never terminalize an order. +The global notice stream handles error frames that cannot be routed to an individual request or +order. Ambiguous command outcomes resolve through a venue query that recovers missed fills from +execution history before inferring any terminal state, so a fill can never be misreported as a +cancellation. Cancel-all prefers the tracked TWS order ID over the permanent ID, which lets the +originating client cancel its own live orders without a cross-client lookup. + +## Data capabilities + +| Nautilus data type | Live subscribe | Historical request | Notes | +| ------------------ | -------------- | ------------------ | ----------------------------------------- | +| Instrument | No | Yes | Startup loading and explicit requests. | +| Quote tick | Yes | Yes | Tick-by-tick or batched `reqMktData`. | +| Trade tick | Yes | Yes | Tick-by-tick live and historical trades. | +| Bar | Yes | Yes | External time bars. | +| Book delta | Yes | No | Market depth with book clear on recovery. | +| Index price | Yes | No | Index price events. | +| Option greeks | Yes | No | Greeks plus option open interest. | + +Set `batch_quotes=False` to prefer tick-by-tick bid/ask subscriptions. The client falls back to +`reqMktData` when tick-by-tick quotes are unavailable, and BAG spread quotes always use +`reqMktData`. `ignore_quote_tick_size_updates=True` filters quote events whose only change is size. + +The quote cache assembles bid and ask updates into complete quote ticks. IB no-quote sentinels +(price `-1` without a size, or a zero price with zero size) are filtered before ticks are built +and clear the affected book side, so a one-sided market emits nothing rather than stale values. +Genuine zero and negative prices, including a `-1` price with a size, which calendar spreads +produce routinely, flow through. + +For historical data, `HistoricalInteractiveBrokersClient` accepts an instrument provider and data +client config. Constructing this Python client connects immediately, so keep it behind the same +explicit run gate as the request: -`InteractiveBrokersInstrumentProviderConfig` supports two symbology methods: +```python +from nautilus_trader.adapters import interactive_brokers as ib -| Method | Purpose | Example | -| ---------------------------- | --------------------------------------------- | ---------- | -| `SymbologyMethod.SIMPLIFIED` | Uses shorter, readable symbols. | `EUR/USD` | -| `SymbologyMethod.RAW` | Preserves the IB security type in the symbol. | `AAPL=STK` | -The default is `SIMPLIFIED`. Use `RAW` when the security type must remain explicit in the -instrument ID, as in `AAPL=STK.SMART`. +provider_config = ib.InteractiveBrokersInstrumentProviderConfig() +provider = ib.InteractiveBrokersInstrumentProvider(provider_config) +config = ib.InteractiveBrokersDataClientConfig( + host="127.0.0.1", + port=7497, + client_id=180, + market_data_type=ib.MarketDataType.DELAYED, + instrument_provider=provider_config, +) -Configure instruments by Nautilus instrument ID or by IB contract dictionaries: +# This constructor connects to TWS or IB Gateway. +client = ib.HistoricalInteractiveBrokersClient(provider, config) +``` -```python -from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersInstrumentProviderConfig -from nautilus_trader.adapters.interactive_brokers import SymbologyMethod -from nautilus_trader.model import InstrumentId +| Method | Selectors and controls | +| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `request_instruments` | Accepts Nautilus instrument IDs, IB contract dictionaries, or both. | +| `request_bars` | Accepts bar specification strings, an end time, and either a start time or IB duration string; contracts or instrument IDs select the instruments. | +| `request_ticks` | Accepts `IbHistoricalTickType.TRADES` or `BID_ASK`, a bounded time range, contracts or instrument IDs, regular-hours filtering, timeout, and a result limit. | + +Historical tick requests need both a start and an end time, page backward from the end time with a +default cap of 10,000 ticks when no limit is given, send only an end bound (IB misinterprets requests carrying both), and +deduplicate the boundary second that IB's second-resolution end encoding re-delivers. When one +second holds a full page and the window cannot retreat, the request stops with a warning +instead of looping. A bar request whose segment fails returns an error rather than a series +with a silent hole. Historical trade ticks with IB's zero size are skipped before constructing +the positive-quantity Nautilus type; other invalid conversions still fail the request. + +IB controls pacing, available history, supported bar sizes, and regular-trading-hours filtering. +For continuous futures, IB rejects an explicit bar end time. The adapter omits it and requests one +duration segment anchored to the request time, so the result can fall outside the requested start +and end range. + +## Execution capabilities + +### Order types + +| Nautilus order type | IB order type | Price inputs | +| ---------------------- | ------------------- | ------------------------------------- | +| `MARKET` | Market | None. | +| `LIMIT` | Limit | Limit price. | +| `MARKET_TO_LIMIT` | Market to limit | None. | +| `STOP_MARKET` | Stop | Trigger price. | +| `STOP_LIMIT` | Stop limit | Limit and trigger prices. | +| `MARKET_IF_TOUCHED` | Market if touched | Trigger price. | +| `LIMIT_IF_TOUCHED` | Limit if touched | Limit and trigger prices. | +| `TRAILING_STOP_MARKET` | Trailing stop | Trailing offset and optional trigger. | +| `TRAILING_STOP_LIMIT` | Trailing stop limit | Limit and trailing offsets. | + +The adapter maps `DAY`, `GTC`, `IOC`, `FOK`, `GTD`, and `AT_THE_OPEN` time in force values. A market +or limit order with `AT_THE_CLOSE` becomes the corresponding market-on-close or limit-on-close IB +order. GTD orders include their Nautilus expiry time in the IB order. + +The adapter rejects post-only orders. Quote quantity is valid only for inverse instruments, and +trailing orders support price offsets and basis-point offsets, which IB receives as a trailing +percent. These checks emit coded `OrderDenied` reasons before the adapter sends an order to IB. + +### Commands and order attributes + +| Capability | Support | Notes | +| ----------------- | ------- | ------------------------------------------------------------ | +| Submit order | Yes | Single orders and generic spreads. | +| Submit order list | Yes | Includes bracket and linked orders. | +| Modify order | Yes | Quantity, price, trigger, and IB parameter updates. | +| Cancel order | Yes | Uses tracked venue order state. | +| Cancel all orders | Yes | Scoped through the execution client. | +| Query order | Yes | Checks open orders, then executions before inferring cancel. | +| Mass status | Yes | Orders, fills, and positions. | + +Pass `params={"exchange": "IEX"}` to submit, submit-list, or modify commands to override the +cached contract exchange for that command. An omitted or empty value retains the cached exchange. + +IB accepts a new trailing stop price only together with a new trailing amount (error 10067). A +modify that sets a trailing stop's trigger must pass `params={"trailing_offset": }` in the +order's offset units: a price amount for `PRICE` offsets, or basis points for `BASIS_POINTS` +offsets. An offset equal to the current one is rejected by IB, and a trigger modify without the +parameter is rejected before it is sent. + +IB-specific order fields use a tag prefixed with `IBOrderTags:` followed by a JSON object. Keys use +the `ibapi` order field names in snake case or lower camel case. Unknown fields fail the order +conversion. The adapter normalizes `order_type`, `tif` or `timeInForce`, `action`, `oca_type`, +`trigger_method`, `origin`, `short_sale_slot`, `volatility_type`, +`reference_price_type`, `rule_80_a`, `open_close`, and `NonGuaranteed`. Active, stop, cancel, manual, +and good-till timestamps use `%Y%m%d %H:%M:%S UTC`. + +The optional `conditions` array accepts these shapes: + +| Condition | Required fields | Optional fields and defaults | +| ---------------- | --------------- | --------------------------------------------------------------------------- | +| `price` | None | `conId=0`, `exchange="SMART"`, `price=0`, `isMore=True`, `triggerMethod=0`. | +| `time` | None | `time=""`, `isMore=True`. | +| `margin` | None | `percent=0`, `isMore=True`. | +| `execution` | `symbol` | `secType="STK"`, `exchange="SMART"`. | +| `volume` | None | `conId=0`, `exchange="SMART"`, `volume=0`, `isMore=True`. | +| `percent_change` | None | `conId=0`, `exchange="SMART"`, `changePercent=0`, `isMore=True`. | + +Every condition accepts `conjunction="and"` or `"or"`; the default is `"and"`. + +### Account state + +On connect and on each account query, the execution client reads one `reqAccountSummary` +snapshot for the configured account and maps its tags to typed balances and margins. It then +reads one `reqAccountUpdates` snapshot for the same account and cancels that stream once IB +signals the download end. Every raw tag and key from both snapshots lands in `AccountState.info` +under its IB name, so values IB publishes only on `updateAccountValue` (for example +`PostExpirationExcess` and `PostExpirationMargin`) are readable from Python alongside the summary +tags such as `TotalCashValue`. A key reported once per currency keeps the last value IB sends. + +## Reconciliation + +Mass status requests query three independent IB sources: + +- Open and completed orders produce order status reports. +- Executions paired with commission reports produce fill reports. +- Account positions produce position status reports. + +Position updates do not create synthetic order status reports. If IB returns a position without +matching order or fill history, the execution engine can adopt the position from the position report +alone by creating its internal reconciliation fill. This preserves the report boundary: the adapter +reports venue state, while the engine owns inferred state needed to reconcile its cache. + +IB's position average cost includes commissions, while a position rebuilt from fills averages their +prices. When the reported fills of an instrument net to exactly its position quantity, the position +report carries the entry price of those fills instead, so a position held across a restart +reconciles. Otherwise the report keeps IB's average cost, which the engine's reconciliation fill +uses. + +On connect, the execution client resolves the instrument of every open position in the account and +publishes any the cache does not hold, so a position in a contract outside the provider's `load_ids` +is still recovered at startup rather than aborting the node. + +For a pending-cancel order missing from the open-orders snapshot, the adapter checks execution +history before emitting a canceled event. Replayed executions are deduplicated after terminal order +state is evicted, including generated spread-leg fills. + +Set `track_option_exercise_from_position_update=True` only when position changes should drive option +exercise tracking. This setting does not restore synthetic position-derived order reports. + +After a restart, orders a previous session left working return as open external orders. IB's +`PreSubmitted` status, used for example while an order waits for its `goodAfterTime`, maps to +`ACCEPTED`. The execution client resolves each restored order's IB order ID from its `PERM-` +venue order ID and tracks it, so later IB status updates, fills, and cancels reach the owning +strategy. Filled orders from the previous session reconcile from their real executions; the +adapter reports no price for market-type orders, so reconciliation never infers a fill at price 0. + +Replies to execution queries (`reqExecutions`) also arrive on IB's order update stream. The adapter +treats only unsolicited execution details there as live fills, so startup and periodic execution +queries do not apply the same fills again. +## Symbology and instruments -provider_config = InteractiveBrokersInstrumentProviderConfig( - symbology_method=SymbologyMethod.RAW, - load_ids={InstrumentId.from_str("AAPL=STK.SMART")}, - load_contracts=[ - { - "symbol": "MSFT", - "secType": "STK", - "exchange": "SMART", - "currency": "USD", - }, - ], -) +`InteractiveBrokersInstrumentProviderConfig.symbology_method` selects one of two public methods: + +| Method | Use | +| ---------------------------- | --------------------------------------------------------- | +| `SymbologyMethod.SIMPLIFIED` | Readable IDs for normal instrument lookup and examples. | +| `SymbologyMethod.RAW` | IDs that retain the IB security type for explicit lookup. | + +One symbology value built from this setting drives every mapping site in the adapter, so data, +execution, and provider paths always resolve identical IDs. Raw symbology +(`AAPL=STK.SMART`) is the lossless escape hatch: it preserves the IB local symbol and security +type for instruments the simplified format rules cannot express, such as complex international +listings. + +Futures and futures-option IDs carry the local symbol reported by IB, such as `YMM6.XCBT` and +`YMM6 C45000.XCBT`. The adapter does not rewrite the contract year, so identifiers stay stable +across catalogs and caches. + +The symbology method controls symbol conversion. Venue selection is a separate step with this +precedence: + +1. CFD and commodity contracts use the adapter pseudo-venues `IBCFD` and `IBCMDTY`. +1. A matching `symbol_to_mic_venue` prefix wins for other security types. +1. Stock primary exchange and a compatible cached venue take precedence over SMART routing. +1. Contract details can supply a non-SMART valid exchange. +1. `convert_exchange_to_mic_venue=True` converts a known exchange to its MIC. +1. The remaining IB exchange value becomes the venue. + +```mermaid +flowchart TD + A{CFD or commodity contract?} -- yes --> P[Pseudo-venue IBCFD or IBCMDTY] + A -- no --> B{symbol_to_mic_venue prefix match?} + B -- yes --> M[Configured MIC venue] + B -- no --> C{Stock primary exchange with compatible cached venue?} + C -- yes --> S[Primary exchange venue] + C -- no --> D{Contract details supply a non-SMART valid exchange?} + D -- yes --> E[Contract details exchange] + D -- no --> F{convert_exchange_to_mic_venue and known exchange?} + F -- yes --> G[Converted MIC venue] + F -- no --> H[Remaining IB exchange value] ``` -The same provider config can be passed to both data and execution client configs. This keeps -contract resolution and instrument IDs consistent across both clients. +The pseudo-venues are part of the adapter namespace under both symbology methods. For example, +simplified symbology can produce `IBDE30.IBCFD` or `XAUUSD.IBCMDTY`, while raw symbology adds the +security type to the symbol. These venues take precedence over `symbol_to_mic_venue` and +`convert_exchange_to_mic_venue`; they do not describe a separate IB routing mechanism. + +Use the same provider config for data and execution clients so both resolve identical IDs. Startup +`load_ids` and `load_contracts` are required entries: client initialization fails if a configured +instrument cannot be resolved. -### Instrument provider options +### Provider configuration | Option | Default | Purpose | | ------------------------------- | ------------ | ------------------------------------------------------- | -| `symbology_method` | `SIMPLIFIED` | Select simplified or raw instrument symbols. | +| `symbology_method` | `SIMPLIFIED` | Select simplified or raw symbol conversion. | | `load_ids` | Empty | Load Nautilus instrument IDs at startup. | | `load_contracts` | Empty | Load IB contract dictionaries at startup. | -| `min_expiry_days` | `None` | Set the minimum expiry for chain loading. | -| `max_expiry_days` | `None` | Set the maximum expiry for chain loading. | -| `build_options_chain` | `None` | Control full option chain construction. | -| `build_futures_chain` | `None` | Control full futures chain construction. | -| `cache_validity_days` | `None` | Set the lifetime of cached instrument data. | -| `convert_exchange_to_mic_venue` | `False` | Convert IB exchange codes to MIC venues. | -| `symbol_to_mic_venue` | Empty | Override MIC venues for selected symbols. | +| `min_expiry_days` | `None` | Minimum relative expiry for chain loading. | +| `max_expiry_days` | `None` | Maximum relative expiry for chain loading. | +| `build_options_chain` | `None` | Override option chain construction. | +| `build_futures_chain` | `None` | Override futures chain construction. | +| `cache_validity_days` | `None` | Lifetime of persisted instrument data. | +| `convert_exchange_to_mic_venue` | `False` | Convert known IB exchanges to MIC venues. | +| `symbol_to_mic_venue` | Empty | Override venues for matching symbol prefixes. | | `filter_sec_types` | Empty | Exclude selected IB security types. | | `filter_callable` | `None` | Apply a Python callable by fully qualified import path. | | `cache_path` | `None` | Persist the instrument cache at the selected path. | -### Derivative chains and spreads - -Set chain flags on a contract dictionary to use that contract as the underlying or chain seed. -The provider-level `min_expiry_days` and `max_expiry_days` values limit the contracts loaded: +Contract dictionaries use IB field names. Chain bounds are days from the request date: ```python -from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersInstrumentProviderConfig +from nautilus_trader.adapters import interactive_brokers as ib -provider_config = InteractiveBrokersInstrumentProviderConfig( +provider_config = ib.InteractiveBrokersInstrumentProviderConfig( load_contracts=[ { + "secType": ib.IbSecurityType.STOCK.as_str(), "symbol": "SPY", - "secType": "STK", "exchange": "SMART", - "currency": "USD", + "primaryExchange": "ARCA", "build_options_chain": True, + "min_expiry_days": 7, + "max_expiry_days": 45, }, { + "secType": ib.IbSecurityType.CONTINUOUS_FUTURE.as_str(), "symbol": "ES", - "secType": "CONTFUT", "exchange": "CME", - "currency": "USD", "build_futures_chain": True, + "min_expiry_days": 30, + "max_expiry_days": 180, }, ], - min_expiry_days=7, - max_expiry_days=60, ) ``` -When `CONTFUT` has a chain flag, the adapter qualifies it and loads the matching dated futures or -futures options. Without a chain flag, it represents IB's continuous future, which IB limits to -historical data. It cannot provide live market data or accept orders. See the -[IB continuous futures documentation](https://www.interactivebrokers.com/docs/general/contracts/futures/continuous-futures). - -The adapter also resolves IB `BAG` contracts from Nautilus spread instrument IDs. Request a spread -before subscribing to it or trading it: - -```python -from nautilus_trader.model import InstrumentId - - -spread_id = InstrumentId.from_str("(1)SPY C400_((1))SPY C410.SMART") -self.request_instrument(spread_id) -``` - -Single parentheses mark a positive leg ratio; double parentheses mark a negative ratio. All legs -must use the same venue. IB requires a contract ID, ratio, action, and exchange for each combo leg; -see [Spreads in the TWS API](https://www.interactivebrokers.com/docs/general/contracts/spread-contracts/twsapi-spreads/spreads-in-the-tws-api). - -## Historical data - -`HistoricalInteractiveBrokersClient` connects with an instrument provider and data client config. -Its async Python methods support: - -- `request_instruments` for contract and instrument discovery. -- `request_bars` for one or more bar specifications. -- `request_ticks` for historical trade or bid-ask ticks. +Without a chain flag, `CONTFUT` represents IB's continuous future. IB limits continuous futures to +historical data; they do not support live subscriptions or orders. -For `CONTFUT` bar requests, the client omits `end_date_time` because IB rejects an explicit end -date. It requests only the first duration segment, anchored to the current time, so returned bars -may fall outside the requested start and end range. +### Generic spreads -IB controls historical availability, pacing, bar sizes, durations, and regular-trading-hours -filtering. Check the -[official historical bars](https://ibkrcampus.com/campus/ibkr-api-page/twsapi-doc/#historical-bars) -and [historical time and sales](https://ibkrcampus.com/campus/ibkr-api-page/twsapi-doc/#historical-time-sales) -documentation before selecting a request range. - -## Order routing and IB attributes - -Pass `params={"exchange": "..."}` when submitting an order, submitting an order list, or modifying -an order to override the cached contract exchange for that command. An empty or omitted value keeps -the cached exchange: - -```python -self.submit_order(order, params={"exchange": "IEX"}) -``` - -Pass IB-specific order attributes as a tag prefixed with `IBOrderTags:` and followed by a JSON -object. The adapter overlays recognized IB order fields and supports price, time, margin, execution, -volume, and percent-change conditions: +Build spread IDs with `new_generic_spread_id`; do not format their leg grammar by hand. All legs +must use the same venue, and the ratio sign selects the IB leg action: ```python -import json +from nautilus_trader.model import new_generic_spread_id -ib_attributes = { - "ocaGroup": "MY_OCA_GROUP", - "ocaType": 1, - "conditionsCancelOrder": False, - "conditions": [ - { - "type": "price", - "conId": 265598, - "exchange": "SMART", - "isMore": True, - "price": 250.0, - "triggerMethod": 0, - }, +spread_id = new_generic_spread_id( + [ + (long_leg.id, 1), + (short_leg.id, -1), ], -} -tags = [f"IBOrderTags:{json.dumps(ib_attributes)}"] +) ``` -Pass `tags` to the order factory. OCA type `1` cancels the remaining orders with overfill -protection; types `2` and `3` proportionally reduce the remaining orders with and without that -protection. See the [IB order reference](https://www.interactivebrokers.com/docs/tws-api/ref/order-class-reference/introduction) -for the supported order attributes. +Load the leg instruments before requesting the spread. The provider qualifies each leg, constructs +the IB BAG contract, and caches the resulting spread instrument before data subscription or order +submission. -## Configuration +Spread executions come back from IB as a combo-level fill for the spread order and one execution +per leg. Each leg execution becomes an `OrderFilled` event on the leg instrument, under the spread +order's strategy, with a `-LEG-` client order ID; the execution engine applies it to the leg +position without an order of its own. IB sends commission reports for the leg executions only, so +the combo-level fill carries zero commission. On connect, the execution client loads and publishes +the spread and leg instruments of every open or completed combo order, so startup reconciliation +restores those orders even when the node does not load the spread itself. Startup fill reports hold +each combo order's combo-level fills; its leg executions are left out, and the leg positions +reconcile from IB's position reports. + +## Configuration reference ### Data client -| Option | Default | Purpose | -| -------------------------------- | ------------- | ----------------------------------------------------- | -| `host` | `127.0.0.1` | TWS or IB Gateway host. | -| `port` | `4002` | TWS or IB Gateway socket port. | -| `client_id` | `1` | IB API client ID. | -| `use_regular_trading_hours` | `True` | Restrict requests to regular trading hours. | -| `market_data_type` | `REALTIME` | Select real-time, frozen, delayed, or delayed frozen. | -| `ignore_quote_tick_size_updates` | `False` | Ignore quote updates that change size only. | -| `connection_timeout` | `300` seconds | Set the socket connection timeout. | -| `request_timeout` | `60` seconds | Set the IB API request timeout. | -| `handle_revised_bars` | `False` | Process revised real-time bars. | -| `batch_quotes` | `True` | Use `reqMktData` instead of tick-by-tick quotes. | -| `instrument_provider` | Default | Configure contract and instrument loading. | +| Option | Default | Purpose | +| -------------------------------- | ------------------------- | --------------------------------------------------------------------------- | +| `host` | `127.0.0.1` | TWS or IB Gateway host. | +| `port` | `4002` | TWS or IB Gateway socket port. | +| `client_id` | `1` | IB API client ID. | +| `use_regular_trading_hours` | `True` | Restrict requests to regular trading hours. | +| `market_data_type` | `MarketDataType.REALTIME` | Select real-time, frozen, delayed, or delayed-frozen data. | +| `ignore_quote_tick_size_updates` | `False` | Filter quote changes that update only size. | +| `connection_timeout` | `300` seconds | Set the socket connection timeout. | +| `request_timeout` | `60` seconds | Set the IB request timeout. | +| `handle_revised_bars` | `False` | Publish every revision of an open bar instead of each completed bar once. | +| `batch_quotes` | `True` | Prefer `reqMktData` for quote subscriptions. | +| `all_last_trades` | `True` | Include AllLast trades; set `False` to request Last trades only. | +| `subscription_idle_timeout_secs` | `None` | Emit a custom-data event after this many seconds without subscription data. | +| `instrument_provider` | Default provider config | Configure contract and instrument loading. | +| `dockerized_gateway` | `None` | Reserved and rejected; start the gateway separately. | + +With `handle_revised_bars=False`, a bar IB keeps up to date is published once it completes. IB +revises the open bar every 5 seconds and sends its final values about half a second after the bar +closes, so the bar is published when that update arrives. If none arrives, because the last 5 +seconds had no trades, the bar is published 2 seconds after its close or on the next bar's first +update, whichever comes first. + +When `subscription_idle_timeout_secs` is set, each live subscription emits +`InteractiveBrokersSubscriptionIdle` once per interval of inactivity. A new market-data update rearms the +notification. Subscribe to `DataType("InteractiveBrokersSubscriptionIdle")` through the message bus to +receive these events in `on_data`. Each event identifies the data client, instrument, subscription, timeout, +and last local data receipt time. A quiet instrument or closed market can also trigger an event; inactivity +alone does not restart a subscription. ### Execution client -| Option | Default | Purpose | -| -------------------------------------------- | ------------- | ----------------------------------------------- | -| `host` | `127.0.0.1` | TWS or IB Gateway host. | -| `port` | `4002` | TWS or IB Gateway socket port. | -| `client_id` | `1` | IB API client ID. | -| `account_id` | `None` | Select the IB account. | -| `connection_timeout` | `300` seconds | Set the socket connection timeout. | -| `request_timeout` | `60` seconds | Set the IB API request timeout. | -| `fetch_all_open_orders` | `False` | Request all open orders visible to the session. | -| `track_option_exercise_from_position_update` | `False` | Infer option exercise from position updates. | -| `instrument_provider` | Default | Configure contract and instrument loading. | +| Option | Default | Purpose | +| -------------------------------------------- | ----------------------- | ---------------------------------------------------- | +| `host` | `127.0.0.1` | TWS or IB Gateway host. | +| `port` | `4002` | TWS or IB Gateway socket port. | +| `client_id` | `1` | IB API client ID. | +| `account_id` | `None` | Raw IB account code such as `DU123456`. | +| `connection_timeout` | `300` seconds | Set the socket connection timeout. | +| `request_timeout` | `60` seconds | Set the IB request timeout. | +| `fetch_all_open_orders` | `False` | Request all open orders visible to the session. | +| `track_option_exercise_from_position_update` | `False` | Track option exercise through position updates. | +| `instrument_provider` | Default provider config | Configure contract and instrument loading. | +| `dockerized_gateway` | `None` | Reserved and rejected; start the gateway separately. | + +### Dockerized gateway + +| Option | Default | Purpose | +| ----------------- | ---------------------------------- | -------------------------------------------------------------------- | +| `username` | `TWS_USERNAME` | Set the IB login username. | +| `password` | `TWS_PASSWORD` | Set the IB login password; debug output and serialization redact it. | +| `trading_mode` | `TradingMode.PAPER` | Select paper or live login and port mapping. | +| `read_only_api` | `True` | Disable order entry through the Gateway API. | +| `timeout` | `300` seconds | Bound startup readiness; valid values are `1` through `3600`. | +| `container_image` | `ghcr.io/gnzsnz/ib-gateway:stable` | Select the container image reference. | +| `vnc_port` | `None` | Expose VNC on a port from `5900` through `5999`; `None` disables it. | + +`DockerizedIBGateway` exposes asynchronous and blocking `start`, `safe_start`, and `stop` methods, +plus `container_status`. The safe start variants reuse a ready container and replace an unhealthy +one. + +## Python enum reference + +All public Python enum names use `SCREAMING_SNAKE_CASE`. IB conversion enums expose `as_str()` or +`as_i32()` when callers need the corresponding wire value. + +### Connection, contracts, and instruments + +| Enum | Purpose and key members | +| ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `ContainerStatus` | Gateway lifecycle: `NO_CONTAINER`, `CONTAINER_CREATED`, `CONTAINER_STARTING`, `CONTAINER_STOPPED`, `NOT_LOGGED_IN`, `READY`, or `UNKNOWN`. | +| `MarketDataType` | Subscription mode: `REALTIME`, `FROZEN`, `DELAYED`, or `DELAYED_FROZEN`. | +| `SymbologyMethod` | Instrument ID mapping: `SIMPLIFIED` or `RAW`. | +| `TradingMode` | Gateway login mode: `PAPER` or `LIVE`. | +| `IbSecurityType` | IB contract types: stock, option, future, continuous future, index, futures option, FX, spread, warrant, bond, commodity, news, fund, crypto, and CFD. | +| `IbOptionRight` | Option side: `CALL` or `PUT`. | +| `IbBondIdentifierKind` | Bond identifier: `CUSIP` or `ISIN`. | +| `IbFundAssetType` | Fund asset classification: none, other, money market, fixed income, multi-asset, equity, sector, guaranteed, or alternative. | +| `IbFundDistributionPolicyIndicator` | Fund distribution policy: `NONE`, `ACCUMULATION_FUND`, or `INCOME_FUND`. | + +### Market and historical data + +| Enum | Purpose and key members | +| ------------------------ | ----------------------------------------------------------------------------------------------------------------------- | +| `IbHistoricalBarSize` | Historical bar intervals from one second through one month. | +| `IbHistoricalTickType` | Historical ticks: `TRADES` or `BID_ASK`. | +| `IbHistoricalWhatToShow` | Historical source such as trades, midpoint, bid, ask, volatility, fee rate, schedule, or adjusted last. | +| `IbRealtimeBarSize` | IB real-time bar size; IB supports `SEC5`. | +| `IbRealtimeWhatToShow` | Real-time bar source: `TRADES`, `MIDPOINT`, `BID`, or `ASK`. | +| `IbTradingHours` | Regular or extended trading hours. | +| `IbTickType` | The full IB numeric tick-field set, including live and delayed prices, sizes, option values, yields, NAVs, and volumes. | + +### Orders and conditions + +| Enum | Purpose and key members | +| ------------------------ | ------------------------------------------------------------------------------------------------------------------------ | +| `IbAction` | Order action and execution side: buy, sell, bought, sold, sell short, or sell long. | +| `IbOrderType` | Native IB order types used by conversion and `IBOrderTags`; direct Nautilus support is limited to the order table above. | +| `IbOrderStatus` | Native lifecycle status from API pending through submitted, pending cancel, canceled, filled, or inactive. | +| `IbTimeInForce` | IB time in force values used for order conversion. | +| `IbBuilderTimeInForce` | Time in force values accepted by IB order builders, including good-till-crossing and opening auction. | +| `IbOrderOpenClose` | Open or close designation. | +| `IbOrderOrigin` | Customer or firm origin. | +| `IbShortSaleSlot` | No short-sale slot, broker, or third party. | +| `IbAuctionStrategy` | Match, improvement, or transparent auction strategy. | +| `IbAuctionType` | Opening, closing, or volatility auction. | +| `IbReferencePriceType` | Average of NBBO or NBBO. | +| `IbVolatilityType` | Daily or annual volatility. | +| `IbRule80A` | IB Rule 80A account and agency classifications. | +| `IbOcaType` | No OCA, cancel with block, reduce with block, or reduce without block. | +| `IbTriggerMethod` | Default, double bid/ask, last, double last, bid/ask, last or bid/ask, or midpoint. | +| `IbLiquidity` | None, added, removed, routed out, or explicitly unknown liquidity. | +| `IbComboLegOpenClose` | Same, open, close, or unknown combo-leg designation. | +| `IbLegAction` | Buy or sell combo-leg action. | +| `IbConditionKind` | Price, time, margin, execution, volume, or percent-change condition. | +| `IbConditionConjunction` | `AND` or `OR`. | +| `IbExerciseAction` | Exercise or lapse an option. | +| `IbTwapStrategyType` | Marketable, matching midpoint, matching same side, or matching last. | +| `IbRiskAversion` | Get done, aggressive, neutral, or passive. | +| `IbArticleType` | Text or binary news article. | + +## Test the adapter + +For market data, `examples/live/interactive_brokers/data_tester.py` streams the configured +instrument with the built-in `DataTester` actor, and the Rust equivalent runs with +`cargo run --example ib-data-tester --package nautilus-interactive-brokers --features examples`. +Both connect to a locally running TWS or IB Gateway and place no orders. + +The Python examples construct offline by default. Their run flags separate connection from order +submission. The Rust execution tester uses the same boundary: + +```bash +# Construct the tester without connecting. +cargo run --example ib-exec-tester \ + --package nautilus-interactive-brokers \ + --features examples + +# Connect in dry-run mode. This can subscribe but cannot submit tester orders. +NAUTILUS_IB_ACCOUNT_ID=DU123456 \ +NAUTILUS_IB_RUN=1 \ +cargo run --example ib-exec-tester \ + --package nautilus-interactive-brokers \ + --features examples + +# Submit paper-account stop orders only after the second opt-in. +NAUTILUS_IB_ACCOUNT_ID=DU123456 \ +NAUTILUS_IB_RUN=1 \ +NAUTILUS_IB_LIVE_ORDERS=1 \ +NAUTILUS_IB_EXEC_PROFILE=stop \ +cargo run --example ib-exec-tester \ + --package nautilus-interactive-brokers \ + --features examples +``` + +Available execution profiles are `lifecycle`, `cancel-modify`, `rejection`, `options`, `stop`, +`stop-limit`, `trailing`, `bracket`, and `unsupported-flags`. Run live tests only against an account +and instruments intended for the selected profile. ## Troubleshooting -- Confirm TWS or IB Gateway is running and logged in. -- Confirm socket API access is enabled and the configured port matches the application and trading - mode. -- Confirm the API client ID is not already in use. -- Confirm the account has the required market data subscriptions. Use - `MarketDataType.DELAYED` only when delayed data is acceptable. +- Confirm TWS or IB Gateway is running, logged in, and configured for socket API access. +- Confirm the port matches the application and trading mode. +- Confirm no other process uses the same IB API client ID. +- Confirm the account has permission for the requested market data type and instrument. +- Run the ignored connection smoke explicitly when a paper session is available: + `cargo test -p nautilus-interactive-brokers --test connection -- --ignored`. -For IB error codes and connection settings, see the +For IB error codes, pacing, and connection settings, see the [official TWS API reference](https://ibkrcampus.com/campus/ibkr-api-page/twsapi-doc/). - -## Contributing - -For additional features or to contribute to the Interactive Brokers adapter, see the -[contributing guide](https://github.com/nautechsystems/nautilus_trader/blob/develop/CONTRIBUTING.md). diff --git a/examples/live/interactive_brokers/_common.py b/examples/live/interactive_brokers/_common.py index 29b11f6ba221..1a602f0581c4 100644 --- a/examples/live/interactive_brokers/_common.py +++ b/examples/live/interactive_brokers/_common.py @@ -23,9 +23,12 @@ from nautilus_trader.adapters import interactive_brokers from nautilus_trader.common import Environment +from nautilus_trader.config import LiveRiskEngineConfig from nautilus_trader.live import LiveNode +from nautilus_trader.model import AccountId from nautilus_trader.model import InstrumentId from nautilus_trader.model import TraderId +from nautilus_trader.model import new_generic_spread_id from nautilus_trader.trading import ImportableStrategyConfig @@ -75,6 +78,22 @@ def resolve_ib_endpoint() -> tuple[str, int]: return host, port +def market_data_type() -> interactive_brokers.MarketDataType: + value = os.getenv("IB_V2_MARKET_DATA_TYPE", "delayed-frozen").lower() + values = { + "realtime": interactive_brokers.MarketDataType.REALTIME, + "frozen": interactive_brokers.MarketDataType.FROZEN, + "delayed": interactive_brokers.MarketDataType.DELAYED, + "delayed-frozen": interactive_brokers.MarketDataType.DELAYED_FROZEN, + "delayed_frozen": interactive_brokers.MarketDataType.DELAYED_FROZEN, + } + try: + return values[value] + except KeyError as e: + choices = ", ".join(values) + raise ValueError(f"IB_V2_MARKET_DATA_TYPE must be one of: {choices}") from e + + def is_ib_endpoint_reachable(host: str, port: int, timeout: float = 2.0) -> bool: """ Is ib endpoint reachable. @@ -104,10 +123,13 @@ def schedule_node_stop(_node: LiveNode, delay_seconds: int) -> None: ) +def ib_account_id(raw_account_id: str) -> AccountId: + if "-" in raw_account_id: + return AccountId.from_str(raw_account_id) + return AccountId.from_str(f"{IB}-{raw_account_id}") + + def contract_month_code(year: int, month: int) -> str: - """ - Contract month code. - """ return f"{FUTURES_MONTH_CODES[month]}{year % 10}" @@ -173,23 +195,14 @@ def active_monthly_contract( def default_es_future() -> tuple[str, str, str]: - """ - Default es future. - """ return active_quarterly_contract(symbol="ES", venue="XCME") def default_ym_future() -> tuple[str, str, str]: - """ - Default ym future. - """ return active_quarterly_contract(symbol="YM", venue="XCBT") def default_cl_future() -> tuple[str, str, str]: - """ - Default cl future. - """ return active_monthly_contract(symbol="CL", venue="XNYM") @@ -214,6 +227,26 @@ def default_cl_future_instrument_id() -> str: return default_cl_future()[1] +def default_aapl_instrument_id() -> str: + return "AAPL.XNAS" + + +def default_aapl_raw_instrument_id() -> str: + return "AAPL=STK.SMART" + + +def default_spx_index_instrument_id() -> str: + return "^SPX.CBOE" + + +def default_databento_data_instrument_id() -> str: + return "SPY.XNAS" + + +def default_docker_subscription_instrument_id() -> str: + return "EUR/USD.IDEALPRO" + + def format_option_strike(strike: float) -> str: """ Format option strike. @@ -231,35 +264,34 @@ def default_es_put_option_local_symbol(strike: float = 6800.0) -> str: def default_es_put_option_instrument_id(strike: float = 6800.0) -> str: - """ - Default es put option instrument id. - """ - return f"{default_es_put_option_local_symbol(strike)}.XCME" + _, instrument_id, _ = default_es_future() + return f"{instrument_id.removesuffix('.XCME')} P{format_option_strike(strike)}.XCME" def default_es_put_spread_instrument_id( long_strike: float = 6800.0, short_strike: float = 6750.0, ) -> str: - """ - Default es put spread instrument id. - """ - # IB generic spread IDs encode signed leg ratios in the instrument ID - leg_ratios = [ - (default_es_put_option_local_symbol(long_strike), 1), - (default_es_put_option_local_symbol(short_strike), -1), - ] - leg_ratios.sort(key=lambda value: value[0]) - - symbol_parts = [] - - for symbol, ratio in leg_ratios: - if ratio > 0: - symbol_parts.append(f"({ratio}){symbol}") - else: - symbol_parts.append(f"(({abs(ratio)})){symbol}") - - return f"{'_'.join(symbol_parts)}.XCME" + _, instrument_id, _ = default_es_future() + instrument_symbol = instrument_id.removesuffix(".XCME") + return str( + new_generic_spread_id( + [ + ( + InstrumentId.from_str( + f"{instrument_symbol} P{format_option_strike(long_strike)}.XCME", + ), + 1, + ), + ( + InstrumentId.from_str( + f"{instrument_symbol} P{format_option_strike(short_strike)}.XCME", + ), + -1, + ), + ], + ), + ) def default_stock_contracts() -> list[dict[str, object]]: @@ -394,6 +426,82 @@ def instrument_provider_config( ) +def docker_instrument_provider_config() -> ( + interactive_brokers.InteractiveBrokersInstrumentProviderConfig +): + ib = interactive_brokers + cl_local_symbol, cl_instrument_id, _ = default_cl_future() + return instrument_provider_config( + load_ids=[ + "EUR/USD.IDEALPRO", + "BTC/USD.PAXOS", + "SPY.ARCA", + "V.NYSE", + os.getenv("IB_V2_DOCKER_YM_INSTRUMENT_ID", default_ym_future_instrument_id()), + os.getenv("IB_V2_DOCKER_CL_INSTRUMENT_ID", cl_instrument_id), + os.getenv("IB_V2_DOCKER_ES_INSTRUMENT_ID", default_es_future_instrument_id()), + ], + load_contracts=[ + { + "secType": ib.IbSecurityType.STOCK.as_str(), + "symbol": "SPY", + "exchange": "SMART", + "primaryExchange": "ARCA", + "build_options_chain": True, + "min_expiry_days": 7, + "max_expiry_days": 14, + }, + { + "secType": ib.IbSecurityType.CONTINUOUS_FUTURE.as_str(), + "exchange": "CME", + "symbol": "ES", + "build_futures_chain": True, + }, + { + "secType": ib.IbSecurityType.FUTURE.as_str(), + "exchange": "NYMEX", + "localSymbol": os.getenv("IB_V2_DOCKER_CL_LOCAL_SYMBOL", cl_local_symbol), + "build_futures_chain": False, + }, + ], + ) + + +def databento_instrument_provider_config() -> ( + interactive_brokers.InteractiveBrokersInstrumentProviderConfig +): + return instrument_provider_config( + load_ids=[ + "SPY.XNAS", + "AAPL.XNAS", + "V.XNYS", + os.getenv("IB_V2_DATABENTO_CL_INSTRUMENT_ID", default_cl_future_instrument_id()), + os.getenv("IB_V2_DATABENTO_ES_INSTRUMENT_ID", default_es_future_instrument_id()), + ], + ) + + +def set_databento_request_contracts_default() -> None: + ib = interactive_brokers + os.environ.setdefault( + "IB_V2_DATABENTO_REQUEST_CONTRACTS", + json.dumps( + [ + { + "secType": ib.IbSecurityType.STOCK.as_str(), + "symbol": "SPY", + "exchange": "SMART", + "primaryExchange": "CBOE", + "build_options_chain": True, + "min_expiry_days": 0, + "max_expiry_days": 3, + }, + ], + separators=(",", ":"), + ), + ) + + def build_ib_live_node( *, name: str, @@ -413,12 +521,14 @@ def build_ib_live_node( provider_config = provider_config or instrument_provider_config() builder = LiveNode.builder(name, trader, Environment.LIVE) + if env_bool("IB_V2_BYPASS_RISK"): + builder = builder.with_risk_engine_config(LiveRiskEngineConfig(bypass=True)) builder = builder.with_timeout_connection(env_int("IB_V2_NODE_CONNECTION_TIMEOUT", 15)) builder = builder.with_timeout_reconciliation(5) builder = builder.with_timeout_portfolio(5) builder = builder.with_timeout_disconnection_secs(5) - builder = builder.with_delay_post_stop_secs(2) - builder = builder.with_reconciliation(env_bool("IB_V2_RECONCILIATION", default=False)) + builder = builder.with_delay_post_stop_secs(5 if account_id is not None else 2) + builder = builder.with_reconciliation(env_bool("IB_V2_RECONCILIATION", False)) builder = builder.add_data_client( None, ib.InteractiveBrokersDataClientFactory(), @@ -428,7 +538,7 @@ def build_ib_live_node( client_id=data_client_id, connection_timeout=env_int("IB_V2_CONNECTION_TIMEOUT", 10), request_timeout=env_int("IB_V2_REQUEST_TIMEOUT", 30), - market_data_type=ib.MarketDataType.DELAYED_FROZEN, + market_data_type=market_data_type(), instrument_provider=provider_config, ), ) diff --git a/examples/live/interactive_brokers/connect_with_dockerized_gateway.py b/examples/live/interactive_brokers/connect_with_dockerized_gateway.py index 0622dc114f52..67453ddbd242 100644 --- a/examples/live/interactive_brokers/connect_with_dockerized_gateway.py +++ b/examples/live/interactive_brokers/connect_with_dockerized_gateway.py @@ -16,12 +16,10 @@ from _common import add_strategy_from_config from _common import build_ib_live_node -from _common import default_cl_future -from _common import default_es_future_instrument_id -from _common import default_ym_future_instrument_id +from _common import default_docker_subscription_instrument_id +from _common import docker_instrument_provider_config from _common import env_bool from _common import env_int -from _common import instrument_provider_config from _common import is_ib_endpoint_reachable from _common import resolve_ib_endpoint from _common import schedule_node_stop @@ -36,8 +34,11 @@ def main() -> None: ib = interactive_brokers host, port = resolve_ib_endpoint() gateway = None + run_node = env_bool("IB_V2_RUN_NODE") - if not is_ib_endpoint_reachable(host, port) or env_bool("IB_V2_FORCE_DOCKERIZED_GATEWAY"): + if run_node and ( + not is_ib_endpoint_reachable(host, port) or env_bool("IB_V2_FORCE_DOCKERIZED_GATEWAY") + ): username = os.getenv("TWS_USERNAME") password = os.getenv("TWS_PASSWORD") if not username or not password: @@ -57,43 +58,12 @@ def main() -> None: port = gateway.port account_id = os.getenv("TWS_ACCOUNT") if env_bool("IB_V2_ENABLE_EXECUTION") else None - cl_local_symbol, cl_instrument_id, _ = default_cl_future() - os.environ.setdefault("IB_V2_SUBSCRIPTION_INSTRUMENT_ID", "EUR/USD.IDEALPRO") - os.environ.setdefault("IB_V2_SUBSCRIBE_QUOTES", "1") - provider_config = instrument_provider_config( - load_ids=[ - "EUR/USD.IDEALPRO", - "BTC/USD.PAXOS", - "SPY.ARCA", - "V.NYSE", - os.getenv("IB_V2_DOCKER_YM_INSTRUMENT_ID", default_ym_future_instrument_id()), - os.getenv("IB_V2_DOCKER_CL_INSTRUMENT_ID", cl_instrument_id), - os.getenv("IB_V2_DOCKER_ES_INSTRUMENT_ID", default_es_future_instrument_id()), - ], - load_contracts=[ - { - "secType": ib.IbSecurityType.STOCK.as_str(), - "symbol": "SPY", - "exchange": "SMART", - "primaryExchange": "ARCA", - "build_options_chain": True, - "min_expiry_days": 7, - "max_expiry_days": 14, - }, - { - "secType": ib.IbSecurityType.CONTINUOUS_FUTURE.as_str(), - "exchange": "CME", - "symbol": "ES", - "build_futures_chain": True, - }, - { - "secType": ib.IbSecurityType.FUTURE.as_str(), - "exchange": "NYMEX", - "localSymbol": os.getenv("IB_V2_DOCKER_CL_LOCAL_SYMBOL", cl_local_symbol), - "build_futures_chain": False, - }, - ], + os.environ.setdefault( + "IB_V2_SUBSCRIPTION_INSTRUMENT_ID", + default_docker_subscription_instrument_id(), ) + os.environ.setdefault("IB_V2_SUBSCRIBE_QUOTES", "1") + provider_config = docker_instrument_provider_config() node = build_ib_live_node( name="IB-V2-DOCKER-001", trader_id="IB-V2-DOCKER-001", @@ -109,13 +79,22 @@ def main() -> None: "ib_v2_order_strategies:IbV2SubscriptionStrategy", ) - print(f"Running v2 IB node against {host}:{port}; press Ctrl+C to stop.", flush=True) - schedule_node_stop(node, env_int("IB_V2_AUTO_STOP_SECONDS", 0)) - try: - node.run() - finally: - if gateway is not None: - gateway.stop_blocking() + if run_node: + print( + f"Running v2 IB node against {host}:{port}; press Ctrl+C to stop.", + flush=True, + ) + schedule_node_stop(node, env_int("IB_V2_AUTO_STOP_SECONDS", 0)) + try: + node.run() + finally: + if gateway is not None: + gateway.stop_blocking() + else: + print( + "Built v2 Dockerized IB Gateway node. Set IB_V2_RUN_NODE=1 to connect.", + flush=True, + ) if __name__ == "__main__": diff --git a/examples/live/interactive_brokers/connect_with_tws.py b/examples/live/interactive_brokers/connect_with_tws.py index 700cc1d36da1..ff2aef195fcb 100644 --- a/examples/live/interactive_brokers/connect_with_tws.py +++ b/examples/live/interactive_brokers/connect_with_tws.py @@ -16,6 +16,7 @@ from _common import add_strategy_from_config from _common import build_ib_live_node +from _common import default_spx_index_instrument_id from _common import env_bool from _common import env_int from _common import instrument_provider_config @@ -30,7 +31,10 @@ def main() -> None: host, port = resolve_ib_endpoint() account_id = os.getenv("TWS_ACCOUNT") if env_bool("IB_V2_ENABLE_EXECUTION") else None os.environ.setdefault("IB_V2_SUBSCRIBE_INDEX_PRICES", "1") - subscription_id = os.getenv("IB_V2_SUBSCRIPTION_INSTRUMENT_ID", "^SPX.CBOE") + subscription_id = os.getenv( + "IB_V2_SUBSCRIPTION_INSTRUMENT_ID", + default_spx_index_instrument_id(), + ) provider_config = instrument_provider_config( load_ids=[ subscription_id, @@ -52,9 +56,15 @@ def main() -> None: "ib_v2_order_strategies:IbV2SubscriptionStrategy", ) - print(f"Running v2 IB node against {host}:{port}; press Ctrl+C to stop.", flush=True) - schedule_node_stop(node, env_int("IB_V2_AUTO_STOP_SECONDS", 0)) - node.run() + if env_bool("IB_V2_RUN_NODE"): + print( + f"Running v2 IB node against {host}:{port}; press Ctrl+C to stop.", + flush=True, + ) + schedule_node_stop(node, env_int("IB_V2_AUTO_STOP_SECONDS", 0)) + node.run() + else: + print("Built v2 IB TWS node. Set IB_V2_RUN_NODE=1 to connect.", flush=True) if __name__ == "__main__": diff --git a/examples/live/interactive_brokers/contract_download.py b/examples/live/interactive_brokers/contract_download.py index 75904b57f383..9bb6f24727f1 100644 --- a/examples/live/interactive_brokers/contract_download.py +++ b/examples/live/interactive_brokers/contract_download.py @@ -15,6 +15,7 @@ import asyncio from _common import default_stock_contracts +from _common import env_bool from _common import env_int from _common import instrument_provider_config from _common import resolve_ib_endpoint @@ -30,18 +31,23 @@ async def main() -> None: host, port = resolve_ib_endpoint() provider_config = instrument_provider_config() provider = ib.InteractiveBrokersInstrumentProvider(provider_config) - client = ib.HistoricalInteractiveBrokersClient( - provider, - ib.InteractiveBrokersDataClientConfig( - host=host, - port=port, - client_id=env_int("IB_V2_CONTRACT_CLIENT_ID", 181), - connection_timeout=env_int("IB_V2_CONNECTION_TIMEOUT", 10), - request_timeout=env_int("IB_V2_REQUEST_TIMEOUT", 30), - instrument_provider=provider_config, - ), + client_config = ib.InteractiveBrokersDataClientConfig( + host=host, + port=port, + client_id=env_int("IB_V2_CONTRACT_CLIENT_ID", 181), + connection_timeout=env_int("IB_V2_CONNECTION_TIMEOUT", 10), + request_timeout=env_int("IB_V2_REQUEST_TIMEOUT", 30), + instrument_provider=provider_config, ) + if not env_bool("IB_V2_RUN_CLIENT"): + print( + "Built IB contract client. Set IB_V2_RUN_CLIENT=1 to request contracts.", + flush=True, + ) + return + + client = ib.HistoricalInteractiveBrokersClient(provider, client_config) print("Requesting contracts...", flush=True) instruments = await client.request_instruments(contracts=default_stock_contracts()) print(f"Loaded {len(instruments)} instrument(s)", flush=True) diff --git a/examples/live/interactive_brokers/data_tester.py b/examples/live/interactive_brokers/data_tester.py index ef755e8e65b7..a0860c270a86 100644 --- a/examples/live/interactive_brokers/data_tester.py +++ b/examples/live/interactive_brokers/data_tester.py @@ -22,11 +22,12 @@ from __future__ import annotations +from _common import default_es_future_instrument_id + from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersDataClientConfig from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersDataClientFactory from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersInstrumentProviderConfig from nautilus_trader.adapters.interactive_brokers import MarketDataType -from nautilus_trader.adapters.interactive_brokers import SymbologyMethod from nautilus_trader.common import Environment from nautilus_trader.live import LiveNode from nautilus_trader.model import BarType @@ -41,7 +42,7 @@ HOST = "127.0.0.1" PORT = 7497 CLIENT_ID = 101 -INSTRUMENT_ID = InstrumentId.from_str("AAPL=STK.SMART") +INSTRUMENT_ID = InstrumentId.from_str(default_es_future_instrument_id()) BAR_TYPE = BarType.from_str(f"{INSTRUMENT_ID}-1-MINUTE-LAST-EXTERNAL") @@ -50,7 +51,6 @@ def main() -> None: Run the example. """ provider_config = InteractiveBrokersInstrumentProviderConfig( - symbology_method=SymbologyMethod.RAW, load_ids={INSTRUMENT_ID}, ) diff --git a/examples/live/interactive_brokers/exec_tester.py b/examples/live/interactive_brokers/exec_tester.py index 33ca239bb09b..1bb4fd522d06 100644 --- a/examples/live/interactive_brokers/exec_tester.py +++ b/examples/live/interactive_brokers/exec_tester.py @@ -30,13 +30,14 @@ import os from decimal import Decimal +from _common import default_es_future_instrument_id + from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersDataClientConfig from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersDataClientFactory from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersExecutionClientConfig from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersExecutionClientFactory from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersInstrumentProviderConfig from nautilus_trader.adapters.interactive_brokers import MarketDataType -from nautilus_trader.adapters.interactive_brokers import SymbologyMethod from nautilus_trader.common import Environment from nautilus_trader.config import LiveRiskEngineConfig from nautilus_trader.live import LiveNode @@ -61,7 +62,7 @@ HOST = "127.0.0.1" PORT = 7497 CLIENT_ID = 101 -INSTRUMENT_ID = InstrumentId.from_str("AAPL=STK.SMART") +INSTRUMENT_ID = InstrumentId.from_str(default_es_future_instrument_id()) ORDER_QTY = "1" TOB_OFFSET_TICKS = 500 @@ -75,7 +76,6 @@ def main() -> None: raise SystemExit("TWS_ACCOUNT must be set to the target IB account") provider_config = InteractiveBrokersInstrumentProviderConfig( - symbology_method=SymbologyMethod.RAW, load_ids={INSTRUMENT_ID}, ) @@ -111,19 +111,19 @@ def main() -> None: "ExecTester", ExecTesterConfig( strategy_id=STRATEGY_ID, + use_uuid_client_order_ids=True, instrument_id=INSTRUMENT_ID, client_id=ClientId.from_str(IB), - external_order_instrument_ids=[INSTRUMENT_ID], order_qty=Quantity.from_str(ORDER_QTY), subscribe_quotes=True, - subscribe_trades=True, + subscribe_trades=False, open_position_on_start_qty=Decimal(ORDER_QTY), open_position_on_first_quote=True, open_position_time_in_force=TimeInForce.IOC, enable_limit_buys=True, enable_limit_sells=True, tob_offset_ticks=TOB_OFFSET_TICKS, - use_post_only=True, + use_post_only=False, cancel_orders_on_stop=True, close_positions_on_stop=True, reduce_only_on_stop=False, diff --git a/examples/live/interactive_brokers/historical_download.py b/examples/live/interactive_brokers/historical_download.py index fbe8557498f8..a9e4cbbb8dd0 100644 --- a/examples/live/interactive_brokers/historical_download.py +++ b/examples/live/interactive_brokers/historical_download.py @@ -16,13 +16,16 @@ import datetime as dt import os +from _common import default_aapl_instrument_id from _common import default_stock_contracts +from _common import env_bool from _common import env_int from _common import instrument_ids from _common import instrument_provider_config from _common import resolve_ib_endpoint from nautilus_trader.adapters import interactive_brokers +from nautilus_trader.persistence import ParquetDataCatalog def historical_end() -> dt.datetime: @@ -43,23 +46,28 @@ async def main() -> None: host, port = resolve_ib_endpoint() trading_hours = ib.IbTradingHours.EXTENDED requested_ids = [ - os.getenv("IB_V2_HISTORICAL_INSTRUMENT_ID", "AAPL.NASDAQ"), + os.getenv("IB_V2_HISTORICAL_INSTRUMENT_ID", default_aapl_instrument_id()), ] provider_config = instrument_provider_config(load_ids=requested_ids) provider = ib.InteractiveBrokersInstrumentProvider(provider_config) - client = ib.HistoricalInteractiveBrokersClient( - provider, - ib.InteractiveBrokersDataClientConfig( - host=host, - port=port, - client_id=env_int("IB_V2_HIST_CLIENT_ID", 180), - connection_timeout=env_int("IB_V2_CONNECTION_TIMEOUT", 10), - request_timeout=env_int("IB_V2_REQUEST_TIMEOUT", 60), - use_regular_trading_hours=trading_hours.use_rth(), - instrument_provider=provider_config, - ), + client_config = ib.InteractiveBrokersDataClientConfig( + host=host, + port=port, + client_id=env_int("IB_V2_HIST_CLIENT_ID", 180), + connection_timeout=env_int("IB_V2_CONNECTION_TIMEOUT", 10), + request_timeout=env_int("IB_V2_REQUEST_TIMEOUT", 60), + use_regular_trading_hours=trading_hours.use_rth(), + instrument_provider=provider_config, ) + if not env_bool("IB_V2_RUN_CLIENT"): + print( + "Built IB historical client. Set IB_V2_RUN_CLIENT=1 to request data.", + flush=True, + ) + return + + client = ib.HistoricalInteractiveBrokersClient(provider, client_config) print("Requesting instruments...", flush=True) instruments = await client.request_instruments( instrument_ids=instrument_ids(requested_ids), @@ -104,6 +112,12 @@ async def main() -> None: ) print(f"Downloaded {len(quote_ticks)} quote tick(s)", flush=True) + catalog = ParquetDataCatalog("./catalog") + catalog.write_instruments(instruments) + catalog.write_bars(bars) + catalog.write_trade_ticks(trade_ticks) + catalog.write_quote_ticks(quote_ticks) + if __name__ == "__main__": asyncio.run(main()) diff --git a/examples/live/interactive_brokers/ib_v2_order_strategies.py b/examples/live/interactive_brokers/ib_v2_order_strategies.py index d1c0a00ec592..e51d475d4b43 100644 --- a/examples/live/interactive_brokers/ib_v2_order_strategies.py +++ b/examples/live/interactive_brokers/ib_v2_order_strategies.py @@ -15,9 +15,11 @@ from typing import Any from uuid import uuid4 +from _common import default_databento_data_instrument_id from _common import default_es_future_instrument_id from _common import default_es_put_option_instrument_id from _common import default_es_put_spread_instrument_id +from _common import default_spx_index_instrument_id from _common import default_ym_future_instrument_id from nautilus_trader.adapters import interactive_brokers @@ -176,7 +178,10 @@ def __init__(self) -> None: strategy_id=StrategyId.from_str("IB-V2-SUBSCRIPTION-STRATEGY"), ), ) - self.instrument_id = env_instrument_id("IB_V2_SUBSCRIPTION_INSTRUMENT_ID", "^SPX.CBOE") + self.instrument_id = env_instrument_id( + "IB_V2_SUBSCRIPTION_INSTRUMENT_ID", + default_spx_index_instrument_id(), + ) self.bar_type = bar_type_from_env("IB_V2_SUBSCRIPTION_BAR_TYPE", self.instrument_id) self._subscribed = False self._quote_count = 0 @@ -282,7 +287,10 @@ def __init__(self) -> None: strategy_id=StrategyId.from_str("IB-V2-DATABENTO-SUBSCRIPTION"), ), ) - self.instrument_id = env_instrument_id("IB_V2_DATABENTO_DATA_INSTRUMENT_ID", "SPY.XNAS") + self.instrument_id = env_instrument_id( + "IB_V2_DATABENTO_DATA_INSTRUMENT_ID", + default_databento_data_instrument_id(), + ) self.bar_type = bar_type_from_env("IB_V2_DATABENTO_BAR_TYPE", self.instrument_id) self._quote_count = 0 self._bar_count = 0 @@ -299,7 +307,7 @@ def on_start(self) -> None: ) self.subscribe_quotes(self.instrument_id, client_id=databento_client_id()) - if env_bool("IB_V2_DATABENTO_SUBSCRIBE_BARS", default=True): + if env_bool("IB_V2_DATABENTO_SUBSCRIBE_BARS", True): print( f"{self.strategy_id}: subscribing Databento bars for {self.bar_type}", flush=True, @@ -415,6 +423,8 @@ def __init__(self) -> None: ) self.instrument: Any | None = None self._orders_submitted = False + self._quotes_subscribed = False + self._order_run_id = uuid4().hex[:8] def on_start(self) -> None: """ @@ -430,7 +440,7 @@ def on_start(self) -> None: cached_instrument = self.cache.instrument(self.instrument_id) if cached_instrument is not None: self.instrument = cached_instrument - self._submit_example_orders_once() + self._subscribe_quotes_or_submit() return print(f"{self.strategy_id}: requesting {self.instrument_id}", flush=True) @@ -444,7 +454,26 @@ def on_instrument(self, instrument: Any) -> None: return self.instrument = instrument - self._submit_example_orders_once() + self._subscribe_quotes_or_submit() + + def _subscribe_quotes_or_submit(self) -> None: + if not env_bool("IB_V2_WAIT_FOR_QUOTE", True): + self._submit_example_orders_once() + return + + if self._quotes_subscribed: + return + + self._quotes_subscribed = True + print(f"{self.strategy_id}: waiting for a quote for {self.instrument_id}", flush=True) + self.subscribe_quotes(self.instrument_id, client_id=ib_client_id()) + + def on_quote(self, quote: Any) -> None: + """ + On quote. + """ + if quote.instrument_id == self.instrument_id: + self._submit_example_orders_once() def _submit_example_orders_once(self) -> None: if self._orders_submitted: @@ -467,7 +496,7 @@ def client_order_id(self, suffix: str) -> ClientOrderId: """ Client order id. """ - return ClientOrderId.from_str(f"{self.strategy_id}-{suffix}") + return ClientOrderId.from_str(f"IBV2-{self._order_run_id}-{suffix}") def init_id(self) -> UUID4: """ @@ -626,6 +655,20 @@ def on_order_accepted(self, event: Any) -> None: ) self.cancel_order(order.client_order_id, client_id=ib_client_id()) + def on_stop(self) -> None: + """ + On stop. + """ + if not env_bool("IB_V2_ENABLE_ORDER_SUBMISSION"): + return + + if env_bool("IB_V2_CANCEL_ON_STOP", True): + self.cancel_all_orders(self.instrument_id, client_id=ib_client_id()) + if env_bool("IB_V2_CLOSE_ON_STOP", True): + self.close_all_positions(self.instrument_id, client_id=ib_client_id()) + if self._quotes_subscribed: + self.unsubscribe_quotes(self.instrument_id, client_id=ib_client_id()) + class BracketOrderStrategy(IbV2OrderStrategy): """ @@ -1054,3 +1097,4 @@ def on_stop(self) -> None: if self._startup_requested and env_bool("IB_V2_ENABLE_LIVE_BARS"): self.unsubscribe_bars(self.bar_type, client_id=ib_client_id()) + super().on_stop() diff --git a/examples/live/interactive_brokers/notebooks/bracket_order_example.py b/examples/live/interactive_brokers/notebooks/bracket_order_example.py index b8d4e9858eab..f4d3cab21c57 100644 --- a/examples/live/interactive_brokers/notebooks/bracket_order_example.py +++ b/examples/live/interactive_brokers/notebooks/bracket_order_example.py @@ -1,15 +1,157 @@ -# ------------------------------------------------------------------------------------------------- -# Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -# https://nautechsystems.io -# ------------------------------------------------------------------------------------------------- """ -Example of a bracket order. +Interactive Brokers bracket order. """ +# --- +# jupyter: +# jupytext: +# formats: py:percent +# text_representation: +# extension: .py +# format_name: percent +# format_version: '1.3' +# kernelspec: +# display_name: Python 3 (ipykernel) +# language: python +# name: python3 +# --- + +# %% [markdown] +# # Interactive Brokers bracket order +# +# This example selects a current ES futures contract, builds the three linked orders, and then +# configures the IB data and execution clients. It builds offline by default. Set +# `IB_V2_ENABLE_ORDER_SUBMISSION=1` and `IB_V2_RUN_NODE=1` only for a paper-account run. + +# %% from __future__ import annotations -from order_example_driver import run_order_example +import os +import sys +from pathlib import Path + + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +from _common import build_ib_live_node +from _common import default_es_future_instrument_id +from _common import env_bool +from _common import env_int +from _common import instrument_provider_config +from _common import resolve_ib_endpoint +from _common import schedule_node_stop +from ib_v2_order_strategies import IbV2OrderStrategy +from ib_v2_order_strategies import env_price +from ib_v2_order_strategies import env_quantity + +from nautilus_trader.model import ContingencyType +from nautilus_trader.model import OrderListId +from nautilus_trader.model import OrderSide + + +# %% [markdown] +# The default selector chooses a quarterly ES contract with enough time left before expiry. Override +# it with `IB_V2_ORDER_INSTRUMENT_ID` when testing a specific contract. + +# %% +INSTRUMENT_ID = os.getenv( + "IB_V2_ORDER_INSTRUMENT_ID", + default_es_future_instrument_id(), +) + + +# %% [markdown] +# The entry, target, and stop share one order-list ID. Both exits depend on the entry through the +# `OTO` contingency and identify the entry as their parent. + + +# %% +class BracketOrderExample(IbV2OrderStrategy): + """ + Bracket order example. + """ + + strategy_id_value = "IB-V2-BRACKET-STRATEGY" + instrument_id_value = INSTRUMENT_ID + + def submit_example_orders(self) -> None: + """ + Submit example orders. + """ + quantity = env_quantity("IB_V2_BRACKET_QUANTITY") + entry_id = self.client_order_id("ENTRY") + target_id = self.client_order_id("TARGET") + stop_id = self.client_order_id("STOP") + order_list_id = OrderListId.from_str(f"{self.strategy_id}-BRACKET") + linked_ids = [entry_id, target_id, stop_id] + + entry = self.market_order( + entry_id, + OrderSide.BUY, + quantity, + order_list_id=order_list_id, + linked_order_ids=linked_ids, + ) + target = self.limit_order( + target_id, + OrderSide.SELL, + quantity, + env_price("IB_V2_BRACKET_TARGET_PRICE", "6025.00"), + contingency_type=ContingencyType.OTO, + order_list_id=order_list_id, + linked_order_ids=linked_ids, + parent_order_id=entry_id, + ) + stop = self.stop_market_order( + stop_id, + OrderSide.SELL, + quantity, + env_price("IB_V2_BRACKET_STOP_PRICE", "5975.00"), + contingency_type=ContingencyType.OTO, + order_list_id=order_list_id, + linked_order_ids=linked_ids, + parent_order_id=entry_id, + ) + + self.submit_ib_order(entry) + self.submit_ib_order(target) + self.submit_ib_order(stop) + + +# %% [markdown] +# Both clients use the same provider config so the contract resolves to the same Nautilus ID. The +# execution client is included only when order submission is enabled and `TWS_ACCOUNT` is set. + + +# %% +def main() -> None: + """ + Run the example. + """ + host, port = resolve_ib_endpoint() + account_id = os.getenv("TWS_ACCOUNT") if env_bool("IB_V2_ENABLE_ORDER_SUBMISSION") else None + if env_bool("IB_V2_ENABLE_ORDER_SUBMISSION") and account_id is None: + raise RuntimeError("Set TWS_ACCOUNT before enabling bracket order submission") + + provider_config = instrument_provider_config(load_ids=[INSTRUMENT_ID]) + node = build_ib_live_node( + name="IB-V2-BRACKET-001", + trader_id="IB-V2-BRACKET-001", + host=host, + port=port, + data_client_id=env_int("IB_V2_DATA_CLIENT_ID", 1401), + exec_client_id=env_int("IB_V2_EXEC_CLIENT_ID", 1402), + account_id=account_id, + provider_config=provider_config, + ) + node.add_strategy(BracketOrderExample()) + + print(f"Built bracket-order node for {INSTRUMENT_ID}.", flush=True) + if env_bool("IB_V2_RUN_NODE"): + schedule_node_stop(node, env_int("IB_V2_AUTO_STOP_SECONDS", 20)) + node.run() +# %% if __name__ == "__main__": - run_order_example("bracket") + main() diff --git a/examples/live/interactive_brokers/notebooks/market_order_example.py b/examples/live/interactive_brokers/notebooks/market_order_example.py index e95f9ad65da1..d01935dc1422 100644 --- a/examples/live/interactive_brokers/notebooks/market_order_example.py +++ b/examples/live/interactive_brokers/notebooks/market_order_example.py @@ -1,15 +1,114 @@ -# ------------------------------------------------------------------------------------------------- -# Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -# https://nautechsystems.io -# ------------------------------------------------------------------------------------------------- """ -Example of a market order. +Interactive Brokers market order. """ +# --- +# jupyter: +# jupytext: +# formats: py:percent +# text_representation: +# extension: .py +# format_name: percent +# format_version: '1.3' +# kernelspec: +# display_name: Python 3 (ipykernel) +# language: python +# name: python3 +# --- + +# %% [markdown] +# # Interactive Brokers market order +# +# This example selects a current ES futures contract, creates one market order, and configures the +# IB clients. It builds offline by default. Set `IB_V2_ENABLE_ORDER_SUBMISSION=1` and +# `IB_V2_RUN_NODE=1` only for a paper-account run. + +# %% from __future__ import annotations -from order_example_driver import run_order_example +import os +import sys +from pathlib import Path + + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +from _common import build_ib_live_node +from _common import default_es_future_instrument_id +from _common import env_bool +from _common import env_int +from _common import instrument_provider_config +from _common import resolve_ib_endpoint +from _common import schedule_node_stop +from ib_v2_order_strategies import IbV2OrderStrategy +from ib_v2_order_strategies import env_order_side +from ib_v2_order_strategies import env_quantity + +from nautilus_trader.model import OrderSide + + +# %% +INSTRUMENT_ID = os.getenv( + "IB_V2_ORDER_INSTRUMENT_ID", + default_es_future_instrument_id(), +) + + +# %% [markdown] +# The strategy waits until the selected instrument is available, then submits exactly one market +# order. Set `IB_V2_MARKET_SIDE=SELL` to reverse the default side. + + +# %% +class MarketOrderExample(IbV2OrderStrategy): + """ + Market order example. + """ + + strategy_id_value = "IB-V2-MARKET-STRATEGY" + instrument_id_value = INSTRUMENT_ID + + def submit_example_orders(self) -> None: + """ + Submit example orders. + """ + order = self.market_order( + self.client_order_id("MARKET"), + env_order_side("IB_V2_MARKET_SIDE", OrderSide.BUY), + env_quantity("IB_V2_MARKET_QUANTITY"), + ) + self.submit_ib_order(order) + + +# %% +def main() -> None: + """ + Run the example. + """ + host, port = resolve_ib_endpoint() + account_id = os.getenv("TWS_ACCOUNT") if env_bool("IB_V2_ENABLE_ORDER_SUBMISSION") else None + if env_bool("IB_V2_ENABLE_ORDER_SUBMISSION") and account_id is None: + raise RuntimeError("Set TWS_ACCOUNT before enabling market order submission") + + provider_config = instrument_provider_config(load_ids=[INSTRUMENT_ID]) + node = build_ib_live_node( + name="IB-V2-MARKET-001", + trader_id="IB-V2-MARKET-001", + host=host, + port=port, + data_client_id=env_int("IB_V2_DATA_CLIENT_ID", 1511), + exec_client_id=env_int("IB_V2_EXEC_CLIENT_ID", 1512), + account_id=account_id, + provider_config=provider_config, + ) + node.add_strategy(MarketOrderExample()) + + print(f"Built market-order node for {INSTRUMENT_ID}.", flush=True) + if env_bool("IB_V2_RUN_NODE"): + schedule_node_stop(node, env_int("IB_V2_AUTO_STOP_SECONDS", 20)) + node.run() +# %% if __name__ == "__main__": - run_order_example("market") + main() diff --git a/examples/live/interactive_brokers/notebooks/oca_group_example.py b/examples/live/interactive_brokers/notebooks/oca_group_example.py index 9220e4f92b88..e15324281c6f 100644 --- a/examples/live/interactive_brokers/notebooks/oca_group_example.py +++ b/examples/live/interactive_brokers/notebooks/oca_group_example.py @@ -1,15 +1,138 @@ -# ------------------------------------------------------------------------------------------------- -# Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -# https://nautechsystems.io -# ------------------------------------------------------------------------------------------------- """ -Example of an OCA group. +Interactive Brokers OCA group. """ +# --- +# jupyter: +# jupytext: +# formats: py:percent +# text_representation: +# extension: .py +# format_name: percent +# format_version: '1.3' +# kernelspec: +# display_name: Python 3 (ipykernel) +# language: python +# name: python3 +# --- + +# %% [markdown] +# # Interactive Brokers OCA group +# +# This example creates two limit orders with the same IB one-cancels-all group. It builds offline +# by default. Set `IB_V2_ENABLE_ORDER_SUBMISSION=1` and `IB_V2_RUN_NODE=1` only for a paper-account +# run. + +# %% from __future__ import annotations -from order_example_driver import run_order_example +import os +import sys +from pathlib import Path +from uuid import uuid4 + + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +from _common import build_ib_live_node +from _common import default_es_future_instrument_id +from _common import env_bool +from _common import env_int +from _common import ib_order_tags +from _common import instrument_provider_config +from _common import resolve_ib_endpoint +from _common import schedule_node_stop +from ib_v2_order_strategies import IbV2OrderStrategy +from ib_v2_order_strategies import env_ib_oca_type +from ib_v2_order_strategies import env_price +from ib_v2_order_strategies import env_quantity + +from nautilus_trader.adapters import interactive_brokers as ib +from nautilus_trader.model import OrderSide + + +# %% +INSTRUMENT_ID = os.getenv( + "IB_V2_ORDER_INSTRUMENT_ID", + default_es_future_instrument_id(), +) + + +# %% [markdown] +# Both orders carry the same `IBOrderTags` payload. `CANCEL_WITH_BLOCK` tells IB to cancel the other +# order after one order executes and to submit the group in sequence. + + +# %% +class OcaGroupExample(IbV2OrderStrategy): + """ + Oca group example. + """ + + strategy_id_value = "IB-V2-OCA-STRATEGY" + instrument_id_value = INSTRUMENT_ID + + def submit_example_orders(self) -> None: + """ + Submit example orders. + """ + quantity = env_quantity("IB_V2_OCA_QUANTITY") + oca_group = os.getenv("IB_V2_OCA_GROUP", f"TEST_OCA_V2_{uuid4().hex[:12]}") + tag = ib_order_tags( + ocaGroup=oca_group, + ocaType=env_ib_oca_type( + "IB_V2_OCA_TYPE", + ib.IbOcaType.CANCEL_WITH_BLOCK, + ), + ) + buy = self.limit_order( + self.client_order_id("BUY"), + OrderSide.BUY, + quantity, + env_price("IB_V2_OCA_BUY_PRICE", "5975.00"), + tags=[tag], + ) + sell = self.limit_order( + self.client_order_id("SELL"), + OrderSide.SELL, + quantity, + env_price("IB_V2_OCA_SELL_PRICE", "8500.00"), + tags=[tag], + ) + + self.submit_ib_order(buy) + self.submit_ib_order(sell) + + +# %% +def main() -> None: + """ + Run the example. + """ + host, port = resolve_ib_endpoint() + account_id = os.getenv("TWS_ACCOUNT") if env_bool("IB_V2_ENABLE_ORDER_SUBMISSION") else None + if env_bool("IB_V2_ENABLE_ORDER_SUBMISSION") and account_id is None: + raise RuntimeError("Set TWS_ACCOUNT before enabling OCA order submission") + + provider_config = instrument_provider_config(load_ids=[INSTRUMENT_ID]) + node = build_ib_live_node( + name="IB-V2-OCA-001", + trader_id="IB-V2-OCA-001", + host=host, + port=port, + data_client_id=env_int("IB_V2_DATA_CLIENT_ID", 1411), + exec_client_id=env_int("IB_V2_EXEC_CLIENT_ID", 1412), + account_id=account_id, + provider_config=provider_config, + ) + node.add_strategy(OcaGroupExample()) + + print(f"Built OCA node for {INSTRUMENT_ID}.", flush=True) + if env_bool("IB_V2_RUN_NODE"): + schedule_node_stop(node, env_int("IB_V2_AUTO_STOP_SECONDS", 20)) + node.run() +# %% if __name__ == "__main__": - run_order_example("oca") + main() diff --git a/examples/live/interactive_brokers/notebooks/option_chain_example.py b/examples/live/interactive_brokers/notebooks/option_chain_example.py new file mode 100644 index 000000000000..2b2f51e4f8be --- /dev/null +++ b/examples/live/interactive_brokers/notebooks/option_chain_example.py @@ -0,0 +1,173 @@ +""" +Interactive Brokers option chains. +""" + +# --- +# jupyter: +# jupytext: +# formats: py:percent +# text_representation: +# extension: .py +# format_name: percent +# format_version: '1.3' +# kernelspec: +# display_name: Python 3 (ipykernel) +# language: python +# name: python3 +# --- + +# %% [markdown] +# # Interactive Brokers option chains + +# %% [markdown] +# This example loads the option chains of a stock, two indexes, and a future through one instrument +# request, then prints how many options each expiry contains. It builds offline by default. Set +# `IB_V2_RUN_NODE=1` to connect and load the chains; it submits no orders. + +# %% +from __future__ import annotations + +import datetime as dt +import json +import sys +from collections import Counter +from pathlib import Path +from typing import Any + + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +from _common import build_ib_live_node +from _common import env_bool +from _common import env_int +from _common import instrument_provider_config +from _common import resolve_ib_endpoint +from _common import schedule_node_stop +from ib_v2_order_strategies import ib_client_id + +from nautilus_trader.model import StrategyId +from nautilus_trader.trading import Strategy +from nautilus_trader.trading import StrategyConfig + + +# %% [markdown] +# Each contract spec sets `build_options_chain`, and `min_expiry_days` and `max_expiry_days` bound +# the expiries of the options to load. A stock or index underlying loads its options. A continuous +# future also sets `build_futures_chain`, which loads its futures contracts, and then loads the +# futures options of each one. + +# %% +OPTION_CHAIN_CONTRACTS = [ + { + "secType": "STK", + "symbol": "SPY", + "exchange": "SMART", + "primaryExchange": "CBOE", + "build_options_chain": True, + "min_expiry_days": 0, + "max_expiry_days": 3, + }, + { + "secType": "IND", + "symbol": "SPX", + "exchange": "CBOE", + "build_options_chain": True, + "min_expiry_days": 0, + "max_expiry_days": 5, + }, + { + "secType": "CONTFUT", + "exchange": "CME", + "symbol": "ES", + "build_futures_chain": True, + "build_options_chain": True, + "min_expiry_days": 0, + "max_expiry_days": 2, + }, + { + "secType": "IND", + "exchange": "EUREX", + "symbol": "ESTX50", + "build_options_chain": True, + "min_expiry_days": 0, + "max_expiry_days": 2, + }, +] + + +# %% [markdown] +# The strategy requests every chain at start. Each loaded instrument arrives through +# `on_instrument`, and the strategy counts options by instrument type, underlying, and expiry. + + +# %% +class OptionChainExample(Strategy): + """ + Option chain example. + """ + + def __init__(self) -> None: + """ + Initialize the instance. + """ + super().__init__( + StrategyConfig(strategy_id=StrategyId.from_str("IB-V2-OPTION-CHAIN-STRATEGY")), + ) + self.expiries: Counter[tuple[str, str, str]] = Counter() + + def on_start(self) -> None: + """ + On start. + """ + print(f"{self.strategy_id}: requesting option chains", flush=True) + self.request_instruments( + client_id=ib_client_id(), + params={"ib_contracts": json.dumps(OPTION_CHAIN_CONTRACTS)}, + ) + + def on_instrument(self, instrument: Any) -> None: + """ + On instrument. + """ + expiration_ns = getattr(instrument, "expiration_ns", None) + if expiration_ns is None or not hasattr(instrument, "strike_price"): + return + + expiry = dt.datetime.fromtimestamp(expiration_ns / 1e9, tz=dt.UTC) + key = (type(instrument).__name__, str(instrument.underlying), expiry.isoformat()) + self.expiries[key] += 1 + + def on_stop(self) -> None: + """ + On stop. + """ + print(f"{self.strategy_id}: loaded {self.expiries.total()} options", flush=True) + for (kind, underlying, expiry), count in sorted(self.expiries.items()): + print(f"{kind} {underlying} expires {expiry}: {count}", flush=True) + + +# %% +def main() -> None: + """ + Run the example. + """ + host, port = resolve_ib_endpoint() + node = build_ib_live_node( + name="IB-V2-OPTION-CHAIN-001", + trader_id="IB-V2-OPTION-CHAIN-001", + host=host, + port=port, + data_client_id=env_int("IB_V2_DATA_CLIENT_ID", 1531), + provider_config=instrument_provider_config(), + ) + node.add_strategy(OptionChainExample()) + + print("Built option-chain node.", flush=True) + if env_bool("IB_V2_RUN_NODE"): + schedule_node_stop(node, env_int("IB_V2_AUTO_STOP_SECONDS", 60)) + node.run() + + +# %% +if __name__ == "__main__": + main() diff --git a/examples/live/interactive_brokers/notebooks/order_example_driver.py b/examples/live/interactive_brokers/notebooks/order_example_driver.py deleted file mode 100644 index 5016f763721d..000000000000 --- a/examples/live/interactive_brokers/notebooks/order_example_driver.py +++ /dev/null @@ -1,341 +0,0 @@ -# ------------------------------------------------------------------------------------------------- -# Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -# https://nautechsystems.io -# ------------------------------------------------------------------------------------------------- -""" -Example of the IB order driver. -""" - -from __future__ import annotations - -import argparse -import datetime as dt -import json -import os -import sys -from collections.abc import Callable -from pathlib import Path -from typing import Any - - -sys.path.insert(0, str(Path(__file__).resolve().parents[1])) - -from _common import add_strategy_from_config -from _common import build_ib_live_node -from _common import default_es_future_instrument_id -from _common import default_es_put_spread_instrument_id -from _common import default_ym_future_instrument_id -from _common import env_bool -from _common import env_int -from _common import ib_order_tags -from _common import instrument_provider_config -from _common import resolve_ib_endpoint -from _common import schedule_node_stop - -from nautilus_trader.adapters import interactive_brokers - - -ProviderConfigFactory = Callable[ - [], - interactive_brokers.InteractiveBrokersInstrumentProviderConfig, -] -BeforeBuildHook = Callable[[], None] -AfterBuildHook = Callable[[Any], None] - - -class OrderExample: - """ - Collect order example tests. - """ - - def __init__( - self, - *, - node_id: str, - strategy_path: str, - default_data_client_id: int, - default_exec_client_id: int, - provider_config: ProviderConfigFactory, - before_build: BeforeBuildHook | None = None, - after_build: AfterBuildHook | None = None, - auto_stop_seconds: int = 20, - ) -> None: - """ - Initialize the instance. - """ - self.node_id = node_id - self.strategy_path = strategy_path - self.default_data_client_id = default_data_client_id - self.default_exec_client_id = default_exec_client_id - self.provider_config = provider_config - self.before_build = before_build - self.after_build = after_build - self.auto_stop_seconds = auto_stop_seconds - - -def futures_provider_config() -> interactive_brokers.InteractiveBrokersInstrumentProviderConfig: - """ - Futures provider config. - """ - return instrument_provider_config( - load_ids=[ - os.getenv("IB_V2_ORDER_INSTRUMENT_ID", default_es_future_instrument_id()), - ], - ) - - -def spread_provider_config() -> interactive_brokers.InteractiveBrokersInstrumentProviderConfig: - """ - Spread provider config. - """ - return instrument_provider_config( - load_ids=[ - os.getenv("IB_V2_SPREAD_INSTRUMENT_ID", default_es_put_spread_instrument_id()), - ], - ) - - -def databento_provider_config() -> interactive_brokers.InteractiveBrokersInstrumentProviderConfig: - """ - Databento provider config. - """ - return instrument_provider_config( - load_ids=[ - os.getenv("IB_V2_DATABENTO_INSTRUMENT_ID", default_ym_future_instrument_id()), - ], - ) - - -def set_databento_request_contracts_default() -> None: - """ - Set databento request contracts default. - """ - ib = interactive_brokers - os.environ.setdefault( - "IB_V2_DATABENTO_REQUEST_CONTRACTS", - json.dumps( - [ - { - "secType": ib.IbSecurityType.STOCK.as_str(), - "symbol": "SPY", - "exchange": "SMART", - "primaryExchange": "CBOE", - "build_options_chain": True, - "min_expiry_days": 0, - "max_expiry_days": 3, - }, - ], - separators=(",", ":"), - ), - ) - - -def print_bracket_details(_node: object) -> None: - """ - Print bracket details. - """ - instrument_id = os.getenv("IB_V2_ORDER_INSTRUMENT_ID", default_es_future_instrument_id()) - print( - f"Built v2 bracket-order node and registered BracketOrderStrategy for {instrument_id}.", - flush=True, - ) - print( - "Set IB_V2_ENABLE_ORDER_SUBMISSION=1 and IB_V2_RUN_NODE=1 to submit orders.", - flush=True, - ) - - -def print_market_details(_node: object) -> None: - """ - Print market details. - """ - instrument_id = os.getenv("IB_V2_ORDER_INSTRUMENT_ID", default_es_future_instrument_id()) - print( - f"Built v2 market-order node and registered MarketOrderStrategy for {instrument_id}.", - flush=True, - ) - print( - "Set IB_V2_MARKET_SIDE, IB_V2_ENABLE_ORDER_SUBMISSION=1, and IB_V2_RUN_NODE=1 to submit.", - flush=True, - ) - - -def print_oca_details(node: Any) -> None: - """ - Print oca details. - """ - ib = interactive_brokers - oca_group = os.getenv("IB_V2_OCA_GROUP", "") - tag = ib_order_tags(ocaGroup=oca_group, ocaType=ib.IbOcaType.CANCEL_WITH_BLOCK.as_i32()) - print(f"Built v2 OCA node and registered OcaGroupStrategy: {node.trader_id}", flush=True) - print(f"Strategy submits both child orders with tag when enabled: {tag}", flush=True) - - -def print_conditions_details(node: Any) -> None: - """ - Print conditions details. - """ - ib = interactive_brokers - time_condition: dict[str, Any] = { - "type": ib.IbConditionKind.TIME.as_str(), - "time": (dt.datetime.now(dt.UTC) + dt.timedelta(minutes=5)).strftime("%Y%m%d-%H:%M:%S"), - "isMore": True, - "conjunction": ib.IbConditionConjunction.AND.as_str(), - } - conditions = [time_condition] - if env_bool("IB_V2_ENABLE_PRICE_CONDITION"): - conditions.append( - { - "type": ib.IbConditionKind.PRICE.as_str(), - "conId": env_int("IB_V2_CONDITION_CONTRACT_ID", 0), - "exchange": "CME", - "isMore": True, - "price": 6000.0, - "triggerMethod": ib.IbTriggerMethod.DEFAULT.as_i32(), - "conjunction": ib.IbConditionConjunction.AND.as_str(), - }, - ) - - tag = ib_order_tags( - conditions=conditions, - conditionsCancelOrder=env_bool("IB_V2_CONDITIONS_CANCEL_ORDER", default=False), - ) - - print( - f"Built v2 conditional-order node and registered SimpleConditionsStrategy: {node.trader_id}", - flush=True, - ) - print(f"Strategy submits the conditional tag when enabled: {tag}", flush=True) - - -def print_spread_details(_node: object) -> None: - """ - Print spread details. - """ - print( - "Built v2 spread node and registered SpreadOrderStrategy with 2 option legs.", - flush=True, - ) - print( - "Set IB_V2_ENABLE_ORDER_SUBMISSION=1 and IB_V2_RUN_NODE=1 to submit the spread payload.", - flush=True, - ) - - -def print_databento_details(node: Any) -> None: - """ - Print databento details. - """ - print( - f"Built v2 IB node and registered DatabentoInstrumentIdStrategy: {node.trader_id}", - flush=True, - ) - - -ORDER_EXAMPLES = { - "bracket": OrderExample( - node_id="IB-V2-BRACKET-001", - strategy_path="ib_v2_order_strategies:BracketOrderStrategy", - default_data_client_id=1401, - default_exec_client_id=1402, - provider_config=futures_provider_config, - after_build=print_bracket_details, - ), - "market": OrderExample( - node_id="IB-V2-MARKET-001", - strategy_path="ib_v2_order_strategies:MarketOrderStrategy", - default_data_client_id=1511, - default_exec_client_id=1512, - provider_config=futures_provider_config, - after_build=print_market_details, - ), - "oca": OrderExample( - node_id="IB-V2-OCA-001", - strategy_path="ib_v2_order_strategies:OcaGroupStrategy", - default_data_client_id=1411, - default_exec_client_id=1412, - provider_config=futures_provider_config, - after_build=print_oca_details, - ), - "conditions": OrderExample( - node_id="IB-V2-CONDITIONS-001", - strategy_path="ib_v2_order_strategies:SimpleConditionsStrategy", - default_data_client_id=1421, - default_exec_client_id=1422, - provider_config=futures_provider_config, - after_build=print_conditions_details, - ), - "spread": OrderExample( - node_id="IB-V2-SPREAD-001", - strategy_path="ib_v2_order_strategies:SpreadOrderStrategy", - default_data_client_id=111, - default_exec_client_id=112, - provider_config=spread_provider_config, - after_build=print_spread_details, - ), - "databento": OrderExample( - node_id="IB-V2-DB-ID-001", - strategy_path="ib_v2_order_strategies:DatabentoInstrumentIdStrategy", - default_data_client_id=2, - default_exec_client_id=3, - provider_config=databento_provider_config, - before_build=set_databento_request_contracts_default, - after_build=print_databento_details, - auto_stop_seconds=30, - ), -} - - -def run_order_example(strategy: str) -> None: - """ - Run order example. - """ - example = ORDER_EXAMPLES[strategy] - if example.before_build is not None: - example.before_build() - - host, port = resolve_ib_endpoint() - account_id = ( - os.getenv("TWS_ACCOUNT") - if env_bool("IB_V2_ENABLE_EXECUTION") or env_bool("IB_V2_ENABLE_ORDER_SUBMISSION") - else None - ) - node = build_ib_live_node( - name=example.node_id, - trader_id=example.node_id, - host=host, - port=port, - data_client_id=env_int("IB_V2_DATA_CLIENT_ID", example.default_data_client_id), - exec_client_id=env_int("IB_V2_EXEC_CLIENT_ID", example.default_exec_client_id), - account_id=account_id, - provider_config=example.provider_config(), - ) - add_strategy_from_config(node, example.strategy_path) - - if example.after_build is not None: - example.after_build(node) - - if env_bool("IB_V2_RUN_NODE"): - schedule_node_stop( - node, - env_int("IB_V2_AUTO_STOP_SECONDS", example.auto_stop_seconds), - ) - node.run() - - -def main() -> None: - """ - Run the example. - """ - parser = argparse.ArgumentParser() - parser.add_argument( - "--strategy", - choices=ORDER_EXAMPLES.keys(), - default=os.getenv("IB_V2_ORDER_EXAMPLE", "market"), - ) - args = parser.parse_args() - run_order_example(args.strategy) - - -if __name__ == "__main__": - main() diff --git a/examples/live/interactive_brokers/notebooks/reconciliation_example.py b/examples/live/interactive_brokers/notebooks/reconciliation_example.py index ec6594b5551e..4014d4dcadff 100644 --- a/examples/live/interactive_brokers/notebooks/reconciliation_example.py +++ b/examples/live/interactive_brokers/notebooks/reconciliation_example.py @@ -1,40 +1,147 @@ -# ------------------------------------------------------------------------------------------------- -# Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -# https://nautechsystems.io -# ------------------------------------------------------------------------------------------------- """ -Example of IB reconciliation. +Interactive Brokers reconciliation. """ +# --- +# jupyter: +# jupytext: +# formats: py:percent +# text_representation: +# extension: .py +# format_name: percent +# format_version: '1.3' +# kernelspec: +# display_name: Python 3 (ipykernel) +# language: python +# name: python3 +# --- + +# %% [markdown] +# # Interactive Brokers reconciliation +# +# This example enables startup reconciliation, loads a small stock set, and shows the optional data +# subscriptions used while inspecting the reconciled account. It builds offline by default. Set +# `IB_V2_RUN_NODE=1` and `TWS_ACCOUNT` to connect to a paper account. + +# %% from __future__ import annotations import os import sys from pathlib import Path +from typing import Any sys.path.insert(0, str(Path(__file__).resolve().parents[1])) -from _common import add_strategy_from_config from _common import build_ib_live_node +from _common import default_aapl_instrument_id from _common import default_stock_contracts +from _common import env_bool from _common import env_int from _common import instrument_provider_config from _common import resolve_ib_endpoint from _common import schedule_node_stop +from ib_v2_order_strategies import bar_type_from_env +from ib_v2_order_strategies import ib_client_id + +from nautilus_trader.model import InstrumentId +from nautilus_trader.model import StrategyId +from nautilus_trader.trading import Strategy +from nautilus_trader.trading import StrategyConfig + + +# %% +INSTRUMENT_ID = InstrumentId.from_str( + os.getenv("IB_V2_SUBSCRIPTION_INSTRUMENT_ID", default_aapl_instrument_id()), +) +# Paper acceptance has also been verified with ESZ6.XCME while the stock market is closed. + + +# %% +class ReconciliationExample(Strategy): + """ + Reconciliation example. + """ + + def __init__(self) -> None: + """ + Initialize the instance. + """ + super().__init__( + StrategyConfig( + strategy_id=StrategyId.from_str("IB-V2-RECONCILIATION-STRATEGY"), + ), + ) + self.instrument_id = INSTRUMENT_ID + self.bar_type = bar_type_from_env( + "IB_V2_SUBSCRIPTION_BAR_TYPE", + self.instrument_id, + ) + self._subscribed = False + + def on_start(self) -> None: + """ + On start. + """ + print(f"{self.strategy_id}: requesting {self.instrument_id}", flush=True) + self.request_instrument(self.instrument_id, client_id=ib_client_id()) + def on_instrument(self, instrument: Any) -> None: + """ + On instrument. + """ + if instrument.id != self.instrument_id or self._subscribed: + return + self._subscribed = True + if env_bool("IB_V2_SUBSCRIBE_QUOTES"): + self.subscribe_quotes(self.instrument_id, client_id=ib_client_id()) + if env_bool("IB_V2_SUBSCRIBE_TRADES"): + self.subscribe_trades(self.instrument_id, client_id=ib_client_id()) + if env_bool("IB_V2_SUBSCRIBE_BARS"): + self.subscribe_bars(self.bar_type, client_id=ib_client_id()) + + def on_quote(self, quote: Any) -> None: + """ + On quote. + """ + print(f"{self.strategy_id}: quote: {quote}", flush=True) + + def on_trade(self, trade: Any) -> None: + """ + On trade. + """ + print(f"{self.strategy_id}: trade: {trade}", flush=True) + + def on_bar(self, bar: Any) -> None: + """ + On bar. + """ + print(f"{self.strategy_id}: bar: {bar}", flush=True) + + +# %% [markdown] +# Reconciliation is enabled before the node is built. The execution client receives the IB account +# ID, while the provider config preloads the stock contracts used for the optional subscriptions. + + +# %% def main() -> None: """ Run the example. """ account_id = os.getenv("TWS_ACCOUNT") - if account_id is None: - raise RuntimeError("Set TWS_ACCOUNT to run the IB v2 reconciliation example") + run_node = env_bool("IB_V2_RUN_NODE") + if run_node and account_id is None: + raise RuntimeError("Set TWS_ACCOUNT to run the reconciliation example") os.environ.setdefault("IB_V2_RECONCILIATION", "1") host, port = resolve_ib_endpoint() - provider_config = instrument_provider_config(load_contracts=default_stock_contracts()) + provider_config = instrument_provider_config( + load_ids=[str(INSTRUMENT_ID)], + load_contracts=default_stock_contracts(), + ) node = build_ib_live_node( name="IB-V2-RECONCILIATION-001", trader_id="IB-V2-RECONCILIATION-001", @@ -42,21 +149,17 @@ def main() -> None: port=port, data_client_id=env_int("IB_V2_DATA_CLIENT_ID", 1501), exec_client_id=env_int("IB_V2_EXEC_CLIENT_ID", 1502), - account_id=account_id, + account_id=account_id or "U1234567", provider_config=provider_config, ) - add_strategy_from_config( - node, - "ib_v2_order_strategies:IbV2SubscriptionStrategy", - ) + node.add_strategy(ReconciliationExample()) - print( - f"Running v2 IB reconciliation example against {host}:{port}; press Ctrl+C to stop.", - flush=True, - ) - schedule_node_stop(node, env_int("IB_V2_AUTO_STOP_SECONDS", 20)) - node.run() + print(f"Built reconciliation node for {INSTRUMENT_ID}.", flush=True) + if run_node: + schedule_node_stop(node, env_int("IB_V2_AUTO_STOP_SECONDS", 20)) + node.run() +# %% if __name__ == "__main__": main() diff --git a/examples/live/interactive_brokers/notebooks/simple_conditions_example.py b/examples/live/interactive_brokers/notebooks/simple_conditions_example.py index 532a5bc19958..fdf792e1a4f1 100644 --- a/examples/live/interactive_brokers/notebooks/simple_conditions_example.py +++ b/examples/live/interactive_brokers/notebooks/simple_conditions_example.py @@ -1,15 +1,180 @@ -# ------------------------------------------------------------------------------------------------- -# Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -# https://nautechsystems.io -# ------------------------------------------------------------------------------------------------- """ -Example of simple order conditions. +Interactive Brokers order conditions. """ +# --- +# jupyter: +# jupytext: +# formats: py:percent +# text_representation: +# extension: .py +# format_name: percent +# format_version: '1.3' +# kernelspec: +# display_name: Python 3 (ipykernel) +# language: python +# name: python3 +# --- + +# %% [markdown] +# # Interactive Brokers order conditions +# +# This example shows the IB tag payload for time and price conditions before it configures the +# clients. It builds offline by default. Set `IB_V2_ENABLE_ORDER_SUBMISSION=1` and +# `IB_V2_RUN_NODE=1` only for a paper-account run. + +# %% from __future__ import annotations -from order_example_driver import run_order_example +import datetime as dt +import os +import sys +from pathlib import Path + + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +from _common import build_ib_live_node +from _common import default_es_future_instrument_id +from _common import env_bool +from _common import env_int +from _common import ib_order_tags +from _common import instrument_provider_config +from _common import resolve_ib_endpoint +from _common import schedule_node_stop +from ib_v2_order_strategies import IbV2OrderStrategy +from ib_v2_order_strategies import contract_id_from_instrument +from ib_v2_order_strategies import env_ib_trigger_method +from ib_v2_order_strategies import env_price +from ib_v2_order_strategies import env_quantity + +from nautilus_trader.adapters import interactive_brokers as ib +from nautilus_trader.model import OrderSide +from nautilus_trader.model import TimeInForce + + +# %% +INSTRUMENT_ID = os.getenv( + "IB_V2_ORDER_INSTRUMENT_ID", + default_es_future_instrument_id(), +) + + +# %% [markdown] +# The first order activates after a UTC timestamp. The optional second order uses the qualified IB +# contract ID and activates when the configured trigger price is reached. + + +# %% +class ConditionsExample(IbV2OrderStrategy): + """ + Conditions example. + """ + + strategy_id_value = "IB-V2-CONDITIONS-STRATEGY" + instrument_id_value = INSTRUMENT_ID + + def submit_example_orders(self) -> None: + """ + Submit example orders. + """ + time_condition = { + "type": ib.IbConditionKind.TIME.as_str(), + "time": (dt.datetime.now(dt.UTC) + dt.timedelta(minutes=5)).strftime( + "%Y%m%d-%H:%M:%S", + ), + "isMore": True, + "conjunction": ib.IbConditionConjunction.AND.as_str(), + } + time_order = self.limit_order( + self.client_order_id("TIME-CONDITION"), + OrderSide.SELL, + env_quantity("IB_V2_CONDITION_QUANTITY"), + env_price("IB_V2_CONDITION_TIME_LIMIT_PRICE", "6100.00"), + TimeInForce.GTC, + tags=[ + ib_order_tags( + conditions=[time_condition], + conditionsCancelOrder=env_bool( + "IB_V2_CONDITIONS_CANCEL_ORDER", + False, + ), + ), + ], + ) + self.submit_ib_order(time_order) + + if not env_bool("IB_V2_ENABLE_PRICE_CONDITION", True): + return + + contract_id = env_int("IB_V2_CONDITION_CONTRACT_ID", 0) or ( + contract_id_from_instrument(self.instrument) + ) + + if contract_id <= 0: + print("Skipping the price condition because the IB contract ID is missing.") + return + + price_condition = { + "type": ib.IbConditionKind.PRICE.as_str(), + "conId": contract_id, + "exchange": os.getenv("IB_V2_CONDITION_EXCHANGE", "CME"), + "isMore": True, + "price": float(os.getenv("IB_V2_CONDITION_TRIGGER_PRICE", "6000.0")), + "triggerMethod": env_ib_trigger_method( + "IB_V2_CONDITION_TRIGGER_METHOD", + ib.IbTriggerMethod.DEFAULT, + ), + "conjunction": ib.IbConditionConjunction.AND.as_str(), + } + price_order = self.limit_order( + self.client_order_id("PRICE-CONDITION"), + OrderSide.BUY, + env_quantity("IB_V2_CONDITION_QUANTITY"), + env_price("IB_V2_CONDITION_PRICE_LIMIT_PRICE", "5950.00"), + TimeInForce.GTC, + tags=[ + ib_order_tags( + conditions=[price_condition], + conditionsCancelOrder=env_bool( + "IB_V2_CONDITIONS_CANCEL_ORDER", + False, + ), + ), + ], + ) + self.submit_ib_order(price_order) + + +# %% +def main() -> None: + """ + Run the example. + """ + host, port = resolve_ib_endpoint() + account_id = os.getenv("TWS_ACCOUNT") if env_bool("IB_V2_ENABLE_ORDER_SUBMISSION") else None + if env_bool("IB_V2_ENABLE_ORDER_SUBMISSION") and account_id is None: + raise RuntimeError("Set TWS_ACCOUNT before enabling conditional order submission") + + provider_config = instrument_provider_config(load_ids=[INSTRUMENT_ID]) + node = build_ib_live_node( + name="IB-V2-CONDITIONS-001", + trader_id="IB-V2-CONDITIONS-001", + host=host, + port=port, + data_client_id=env_int("IB_V2_DATA_CLIENT_ID", 1421), + exec_client_id=env_int("IB_V2_EXEC_CLIENT_ID", 1422), + account_id=account_id, + provider_config=provider_config, + ) + node.add_strategy(ConditionsExample()) + + print(f"Built conditional-order node for {INSTRUMENT_ID}.", flush=True) + if env_bool("IB_V2_RUN_NODE"): + schedule_node_stop(node, env_int("IB_V2_AUTO_STOP_SECONDS", 20)) + node.run() +# %% if __name__ == "__main__": - run_order_example("conditions") + main() diff --git a/examples/live/interactive_brokers/notebooks/spread_example.py b/examples/live/interactive_brokers/notebooks/spread_example.py index 3380d024bd0b..68e50b3f4596 100644 --- a/examples/live/interactive_brokers/notebooks/spread_example.py +++ b/examples/live/interactive_brokers/notebooks/spread_example.py @@ -1,15 +1,177 @@ -# ------------------------------------------------------------------------------------------------- -# Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -# https://nautechsystems.io -# ------------------------------------------------------------------------------------------------- """ -Example of a spread order. +Interactive Brokers option spread. """ +# --- +# jupyter: +# jupytext: +# formats: py:percent +# text_representation: +# extension: .py +# format_name: percent +# format_version: '1.3' +# kernelspec: +# display_name: Python 3 (ipykernel) +# language: python +# name: python3 +# --- + +# %% [markdown] +# # Interactive Brokers option spread +# +# This example builds a generic two-leg ES option spread and submits it as one market order. It +# builds offline by default. Set `IB_V2_ENABLE_ORDER_SUBMISSION=1` and `IB_V2_RUN_NODE=1` only for +# a paper-account run. + +# %% from __future__ import annotations -from order_example_driver import run_order_example +import os +import sys +from pathlib import Path +from typing import Any + + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +from _common import build_ib_live_node +from _common import default_es_put_option_instrument_id +from _common import env_bool +from _common import env_int +from _common import instrument_provider_config +from _common import resolve_ib_endpoint +from _common import schedule_node_stop +from ib_v2_order_strategies import IbV2OrderStrategy +from ib_v2_order_strategies import env_quantity +from ib_v2_order_strategies import ib_order_tags + +from nautilus_trader.model import InstrumentId +from nautilus_trader.model import OrderSide +from nautilus_trader.model import new_generic_spread_id + + +# %% [markdown] +# `new_generic_spread_id` owns the generic spread grammar. A positive ratio buys the long leg and a +# negative ratio sells the short leg. The option IDs use the same relative contract selector as the +# other notebooks, so the default does not expire on a fixed date. + +# %% +LONG_LEG_ID = InstrumentId.from_str(default_es_put_option_instrument_id(6800.0)) +SHORT_LEG_ID = InstrumentId.from_str(default_es_put_option_instrument_id(6750.0)) +DEFAULT_SPREAD_ID = new_generic_spread_id( + [ + (LONG_LEG_ID, 1), + (SHORT_LEG_ID, -1), + ], +) +SPREAD_ID = InstrumentId.from_str( + os.getenv("IB_V2_SPREAD_INSTRUMENT_ID", str(DEFAULT_SPREAD_ID)), +) + + +# %% +class SpreadOrderExample(IbV2OrderStrategy): + """ + Spread order example. + """ + + strategy_id_value = "IB-V2-SPREAD-STRATEGY" + instrument_id_value = str(SPREAD_ID) + + def __init__(self) -> None: + """ + Initialize the instance. + """ + super().__init__() + self.instrument_id = SPREAD_ID + self._flatten_submitted = False + + def submit_example_orders(self) -> None: + """ + Submit example orders. + """ + if not env_bool("IB_V2_SPREAD_SUBMIT", True): + print(f"{self.strategy_id}: spread submission disabled", flush=True) + return + + tags = ( + [ib_order_tags(non_guaranteed=True)] + if env_bool("IB_V2_SPREAD_NON_GUARANTEED") + else None + ) + order = self.market_order( + self.client_order_id("SPREAD"), + OrderSide.BUY, + env_quantity("IB_V2_SPREAD_QUANTITY"), + tags=tags, + ) + self.submit_ib_order(order) + + def on_order_submitted(self, event: Any) -> None: + """ + On order submitted. + """ + print(f"{self.strategy_id}: order submitted: {event}", flush=True) + + def on_order_rejected(self, event: Any) -> None: + """ + On order rejected. + """ + print(f"{self.strategy_id}: order rejected: {event}", flush=True) + + def on_order_filled(self, event: Any) -> None: + """ + On order filled. + """ + print(f"{self.strategy_id}: order filled: {event}", flush=True) + if not env_bool("IB_V2_SPREAD_FLATTEN_ON_FILL") or self._flatten_submitted: + return + if event.instrument_id != self.instrument_id: + return + + self._flatten_submitted = True + flatten = self.market_order( + self.client_order_id("SPREAD-FLATTEN"), + OrderSide.SELL, + event.last_qty, + tags=( + [ib_order_tags(non_guaranteed=True)] + if env_bool("IB_V2_SPREAD_NON_GUARANTEED") + else None + ), + ) + self.submit_ib_order(flatten) + + +# %% +def main() -> None: + """ + Run the example. + """ + host, port = resolve_ib_endpoint() + account_id = os.getenv("TWS_ACCOUNT") if env_bool("IB_V2_ENABLE_ORDER_SUBMISSION") else None + if env_bool("IB_V2_ENABLE_ORDER_SUBMISSION") and account_id is None: + raise RuntimeError("Set TWS_ACCOUNT before enabling spread order submission") + + provider_config = instrument_provider_config(load_ids=[str(SPREAD_ID)]) + node = build_ib_live_node( + name="IB-V2-SPREAD-001", + trader_id="IB-V2-SPREAD-001", + host=host, + port=port, + data_client_id=env_int("IB_V2_DATA_CLIENT_ID", 111), + exec_client_id=env_int("IB_V2_EXEC_CLIENT_ID", 112), + account_id=account_id, + provider_config=provider_config, + ) + node.add_strategy(SpreadOrderExample()) + + print(f"Built spread node for {SPREAD_ID}.", flush=True) + if env_bool("IB_V2_RUN_NODE"): + schedule_node_stop(node, env_int("IB_V2_AUTO_STOP_SECONDS", 20)) + node.run() +# %% if __name__ == "__main__": - run_order_example("spread") + main() diff --git a/examples/live/interactive_brokers/notebooks/with_databento_instrument_id_example.py b/examples/live/interactive_brokers/notebooks/with_databento_instrument_id_example.py index 1af32868f201..8039f6fb7165 100644 --- a/examples/live/interactive_brokers/notebooks/with_databento_instrument_id_example.py +++ b/examples/live/interactive_brokers/notebooks/with_databento_instrument_id_example.py @@ -1,15 +1,263 @@ -# ------------------------------------------------------------------------------------------------- -# Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved. -# https://nautechsystems.io -# ------------------------------------------------------------------------------------------------- """ -Example of Databento instrument IDs. +Interactive Brokers with a Databento instrument ID. """ +# --- +# jupyter: +# jupytext: +# formats: py:percent +# text_representation: +# extension: .py +# format_name: percent +# format_version: '1.3' +# kernelspec: +# display_name: Python 3 (ipykernel) +# language: python +# name: python3 +# --- + +# %% [markdown] +# # Interactive Brokers with a Databento instrument ID +# +# This example requests an instrument by its Databento-style Nautilus ID, then uses the qualified IB +# contract for historical data, live subscriptions, and an optional bracket order. It builds +# offline by default. + +# %% from __future__ import annotations -from order_example_driver import run_order_example +import os +import sys +from collections.abc import Sequence +from pathlib import Path +from typing import Any + + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +from _common import build_ib_live_node +from _common import default_ym_future_instrument_id +from _common import env_bool +from _common import env_int +from _common import instrument_provider_config +from _common import resolve_ib_endpoint +from _common import schedule_node_stop +from _common import set_databento_request_contracts_default +from ib_v2_order_strategies import IbV2OrderStrategy +from ib_v2_order_strategies import bar_type_from_env +from ib_v2_order_strategies import env_price +from ib_v2_order_strategies import env_quantity +from ib_v2_order_strategies import ib_client_id + +from nautilus_trader.core.datetime import unix_nanos_to_dt +from nautilus_trader.model import Bar +from nautilus_trader.model import ContingencyType +from nautilus_trader.model import OrderListId +from nautilus_trader.model import OrderSide +from nautilus_trader.model import TimeInForce + + +# %% +INSTRUMENT_ID = os.getenv( + "IB_V2_DATABENTO_INSTRUMENT_ID", + default_ym_future_instrument_id(), +) + + +# %% [markdown] +# The instrument callback is the boundary between symbol resolution and data or order activity. The +# strategy requests 30 minutes of bars only after IB returns the qualified instrument. + + +# %% +class DatabentoInstrumentIdExample(IbV2OrderStrategy): + """ + Databento instrument id example. + """ + + strategy_id_value = "IB-V2-DB-ID-STRATEGY" + instrument_id_value = INSTRUMENT_ID + + def __init__(self) -> None: + """ + Initialize the instance. + """ + super().__init__() + self.bar_type = bar_type_from_env( + "IB_V2_DATABENTO_INSTRUMENT_BAR_TYPE", + self.instrument_id, + ) + self._seen_instrument_ids: set[str] = set() + self._startup_requested = False + self._live_trades_subscribed = False + self._trade_count = 0 + self._bar_count = 0 + self._max_prints = env_int("IB_V2_SUBSCRIPTION_MAX_PRINTS", 5) + + def on_start(self) -> None: + """ + On start. + """ + print(f"{self.strategy_id}: requesting {self.instrument_id}", flush=True) + self.request_instrument(self.instrument_id, client_id=ib_client_id()) + + contracts = os.getenv("IB_V2_DATABENTO_REQUEST_CONTRACTS") + if contracts: + self.request_instruments( + client_id=ib_client_id(), + params={"ib_contracts": contracts}, + ) + + def on_instrument(self, instrument: Any) -> None: + """ + On instrument. + """ + instrument_id = str(instrument.id) + if instrument_id not in self._seen_instrument_ids: + self._seen_instrument_ids.add(instrument_id) + print(f"{self.strategy_id}: received instrument: {instrument.id}", flush=True) + + if instrument.id != self.instrument_id or self._startup_requested: + return + + self.instrument = instrument + self._startup_requested = True + start_ns = self.clock.timestamp_ns() - (30 * 60 * 1_000_000_000) + self.request_bars( + self.bar_type, + start=unix_nanos_to_dt(start_ns), + client_id=ib_client_id(), + ) + + if env_bool("IB_V2_ENABLE_LIVE_TRADES"): + self._live_trades_subscribed = True + self.subscribe_trades(self.instrument_id, client_id=ib_client_id()) + + if env_bool("IB_V2_ENABLE_LIVE_BARS"): + self.subscribe_bars( + self.bar_type, + client_id=ib_client_id(), + params={"start_ns": str(start_ns)}, + ) + + if env_bool("IB_V2_ENABLE_ORDER_SUBMISSION"): + self.submit_example_orders() + + def submit_example_orders(self) -> None: + """ + Submit example orders. + """ + if self._orders_submitted: + return + + self._orders_submitted = True + quantity = env_quantity("IB_V2_DATABENTO_BRACKET_QUANTITY") + entry_id = self.client_order_id("ENTRY") + target_id = self.client_order_id("TARGET") + stop_id = self.client_order_id("STOP") + order_list_id = OrderListId.from_str(f"{self.strategy_id}-BRACKET") + linked_ids = [entry_id, target_id, stop_id] + entry = self.market_order( + entry_id, + OrderSide.BUY, + quantity, + order_list_id=order_list_id, + linked_order_ids=linked_ids, + ) + target = self.limit_order( + target_id, + OrderSide.SELL, + quantity, + env_price("IB_V2_DATABENTO_BRACKET_TARGET_PRICE", "46755.00"), + TimeInForce.GTC, + contingency_type=ContingencyType.OTO, + order_list_id=order_list_id, + linked_order_ids=linked_ids, + parent_order_id=entry_id, + ) + stop = self.stop_market_order( + stop_id, + OrderSide.SELL, + quantity, + env_price("IB_V2_DATABENTO_BRACKET_STOP_PRICE", "46735.00"), + contingency_type=ContingencyType.OTO, + order_list_id=order_list_id, + linked_order_ids=linked_ids, + parent_order_id=entry_id, + ) + self.submit_ib_order(entry) + self.submit_ib_order(target) + self.submit_ib_order(stop) + + def on_trade(self, trade: Any) -> None: + """ + On trade. + """ + self._trade_count += 1 + if self._trade_count <= self._max_prints: + print(f"{self.strategy_id}: trade #{self._trade_count}: {trade}", flush=True) + + def on_bar(self, bar: Any) -> None: + """ + On bar. + """ + self._bar_count += 1 + if self._bar_count <= self._max_prints: + print(f"{self.strategy_id}: bar #{self._bar_count}: {bar}", flush=True) + + def on_historical_bars(self, bars: Sequence[Bar]) -> None: + """ + On historical bars. + """ + print(f"{self.strategy_id}: received {len(bars)} historical bar(s)", flush=True) + + def on_position_opened(self, event: Any) -> None: + """ + On position opened. + """ + print(f"{self.strategy_id}: position opened: {event}", flush=True) + + def on_stop(self) -> None: + """ + On stop. + """ + if self._live_trades_subscribed: + self.unsubscribe_trades(self.instrument_id, client_id=ib_client_id()) + if self._startup_requested and env_bool("IB_V2_ENABLE_LIVE_BARS"): + self.unsubscribe_bars(self.bar_type, client_id=ib_client_id()) + super().on_stop() + + +# %% +def main() -> None: + """ + Run the example. + """ + set_databento_request_contracts_default() + host, port = resolve_ib_endpoint() + account_id = os.getenv("TWS_ACCOUNT") if env_bool("IB_V2_ENABLE_ORDER_SUBMISSION") else None + if env_bool("IB_V2_ENABLE_ORDER_SUBMISSION") and account_id is None: + raise RuntimeError("Set TWS_ACCOUNT before enabling bracket order submission") + + provider_config = instrument_provider_config(load_ids=[INSTRUMENT_ID]) + node = build_ib_live_node( + name="IB-V2-DB-ID-001", + trader_id="IB-V2-DB-ID-001", + host=host, + port=port, + data_client_id=env_int("IB_V2_DATA_CLIENT_ID", 2), + exec_client_id=env_int("IB_V2_EXEC_CLIENT_ID", 3), + account_id=account_id, + provider_config=provider_config, + ) + node.add_strategy(DatabentoInstrumentIdExample()) + + print(f"Built Databento-ID node for {INSTRUMENT_ID}.", flush=True) + if env_bool("IB_V2_RUN_NODE"): + schedule_node_stop(node, env_int("IB_V2_AUTO_STOP_SECONDS", 30)) + node.run() +# %% if __name__ == "__main__": - run_order_example("databento") + main() diff --git a/examples/live/interactive_brokers/option_greeks.py b/examples/live/interactive_brokers/option_greeks.py index fa06949bc04d..6e081f1d7cd6 100644 --- a/examples/live/interactive_brokers/option_greeks.py +++ b/examples/live/interactive_brokers/option_greeks.py @@ -36,13 +36,12 @@ async def main() -> None: """ ib = interactive_brokers host, port = resolve_ib_endpoint() - if not is_ib_endpoint_reachable(host, port): - print(f"IB Gateway/TWS is not reachable at {host}:{port}", flush=True) - return - provider_config = instrument_provider_config( load_ids=[ - os.getenv("IB_V2_OPTION_UNDERLYING_INSTRUMENT_ID", default_es_future_instrument_id()), + os.getenv( + "IB_V2_OPTION_UNDERLYING_INSTRUMENT_ID", + default_es_future_instrument_id(), + ), os.getenv("IB_V2_OPTION_INSTRUMENT_ID", default_es_put_option_instrument_id()), ], ) @@ -56,6 +55,18 @@ async def main() -> None: market_data_type=ib.MarketDataType.DELAYED, instrument_provider=provider_config, ) + + if not env_bool("IB_V2_RUN_CLIENT"): + print( + "Built IB option greeks client. Set IB_V2_RUN_CLIENT=1 to request data.", + flush=True, + ) + return + + if not is_ib_endpoint_reachable(host, port): + print(f"IB Gateway/TWS is not reachable at {host}:{port}", flush=True) + return + try: client = ib.HistoricalInteractiveBrokersClient(provider, client_config) except RuntimeError as e: diff --git a/examples/live/interactive_brokers/with_databento_client.py b/examples/live/interactive_brokers/with_databento_client.py index aaac86eee045..c24e625c7776 100644 --- a/examples/live/interactive_brokers/with_databento_client.py +++ b/examples/live/interactive_brokers/with_databento_client.py @@ -16,11 +16,9 @@ from pathlib import Path from _common import add_strategy_from_config -from _common import default_cl_future_instrument_id -from _common import default_es_future_instrument_id +from _common import databento_instrument_provider_config from _common import env_bool from _common import env_int -from _common import instrument_provider_config from _common import resolve_ib_endpoint from _common import schedule_node_stop @@ -56,15 +54,7 @@ def main() -> None: host, port = resolve_ib_endpoint() trader_id = TraderId.from_str("IB-V2-DATABENTO-001") account_id = os.getenv("TWS_ACCOUNT") if env_bool("IB_V2_ENABLE_EXECUTION") else None - provider_config = instrument_provider_config( - load_ids=[ - "SPY.XNAS", - "AAPL.XNAS", - "V.XNYS", - os.getenv("IB_V2_DATABENTO_CL_INSTRUMENT_ID", default_cl_future_instrument_id()), - os.getenv("IB_V2_DATABENTO_ES_INSTRUMENT_ID", default_es_future_instrument_id()), - ], - ) + provider_config = databento_instrument_provider_config() builder = LiveNode.builder( "IB-V2-DATABENTO-001", diff --git a/python/nautilus_trader/adapters/interactive_brokers/__init__.py b/python/nautilus_trader/adapters/interactive_brokers/__init__.py index 446faea9d520..2aa6dd1d9e69 100644 --- a/python/nautilus_trader/adapters/interactive_brokers/__init__.py +++ b/python/nautilus_trader/adapters/interactive_brokers/__init__.py @@ -27,12 +27,52 @@ "DockerizedIBGateway", "DockerizedIBGatewayConfig", "HistoricalInteractiveBrokersClient", + "IbAction", + "IbArticleType", + "IbAuctionStrategy", + "IbAuctionType", + "IbBondIdentifierKind", + "IbBuilderTimeInForce", + "IbComboLegOpenClose", + "IbConditionConjunction", + "IbConditionKind", + "IbExerciseAction", + "IbFundAssetType", + "IbFundDistributionPolicyIndicator", + "IbHistoricalBarSize", + "IbHistoricalTickType", + "IbHistoricalWhatToShow", + "IbLegAction", + "IbLiquidity", + "IbOcaType", + "IbOptionRight", + "IbOrderOpenClose", + "IbOrderOrigin", + "IbOrderStatus", + "IbOrderType", + "IbRealtimeBarSize", + "IbRealtimeWhatToShow", + "IbReferencePriceType", + "IbRiskAversion", + "IbRule80A", + "IbSecurityType", + "IbShortSaleSlot", + "IbTickType", + "IbTimeInForce", + "IbTradingHours", + "IbTriggerMethod", + "IbTwapStrategyType", + "IbVolatilityType", "InteractiveBrokersDataClientConfig", "InteractiveBrokersDataClientFactory", "InteractiveBrokersExecutionClientConfig", "InteractiveBrokersExecutionClientFactory", "InteractiveBrokersInstrumentProvider", "InteractiveBrokersInstrumentProviderConfig", + "InteractiveBrokersSubscriptionIdle", + "MarketDataType", + "SymbologyMethod", + "TradingMode", ] fixup_module_names(globals(), __name__) diff --git a/python/nautilus_trader/adapters/interactive_brokers/__init__.pyi b/python/nautilus_trader/adapters/interactive_brokers/__init__.pyi index 08bf243d83f1..e3acafd06af8 100644 --- a/python/nautilus_trader/adapters/interactive_brokers/__init__.pyi +++ b/python/nautilus_trader/adapters/interactive_brokers/__init__.pyi @@ -12,12 +12,52 @@ __all__ = [ "DockerizedIBGateway", "DockerizedIBGatewayConfig", "HistoricalInteractiveBrokersClient", + "IbAction", + "IbArticleType", + "IbAuctionStrategy", + "IbAuctionType", + "IbBondIdentifierKind", + "IbBuilderTimeInForce", + "IbComboLegOpenClose", + "IbConditionConjunction", + "IbConditionKind", + "IbExerciseAction", + "IbFundAssetType", + "IbFundDistributionPolicyIndicator", + "IbHistoricalBarSize", + "IbHistoricalTickType", + "IbHistoricalWhatToShow", + "IbLegAction", + "IbLiquidity", + "IbOcaType", + "IbOptionRight", + "IbOrderOpenClose", + "IbOrderOrigin", + "IbOrderStatus", + "IbOrderType", + "IbRealtimeBarSize", + "IbRealtimeWhatToShow", + "IbReferencePriceType", + "IbRiskAversion", + "IbRule80A", + "IbSecurityType", + "IbShortSaleSlot", + "IbTickType", + "IbTimeInForce", + "IbTradingHours", + "IbTriggerMethod", + "IbTwapStrategyType", + "IbVolatilityType", "InteractiveBrokersDataClientConfig", "InteractiveBrokersDataClientFactory", "InteractiveBrokersExecutionClientConfig", "InteractiveBrokersExecutionClientFactory", "InteractiveBrokersInstrumentProvider", "InteractiveBrokersInstrumentProviderConfig", + "InteractiveBrokersSubscriptionIdle", + "MarketDataType", + "SymbologyMethod", + "TradingMode", ] @typing.final @@ -112,6 +152,8 @@ class InteractiveBrokersDataClientConfig: batch_quotes: bool | None = None, instrument_provider: InteractiveBrokersInstrumentProviderConfig | None = None, dockerized_gateway: DockerizedIBGatewayConfig | None = None, + all_last_trades: bool | None = None, + subscription_idle_timeout_secs: int | None = None, ) -> None: ... @property def host(self) -> str: ... @@ -134,6 +176,10 @@ class InteractiveBrokersDataClientConfig: @property def batch_quotes(self) -> bool: ... @property + def all_last_trades(self) -> bool: ... + @property + def subscription_idle_timeout_secs(self) -> int | None: ... + @property def instrument_provider(self) -> InteractiveBrokersInstrumentProviderConfig: ... @instrument_provider.setter def instrument_provider( @@ -249,6 +295,36 @@ class InteractiveBrokersInstrumentProviderConfig: @cache_path.setter def cache_path(self, cache_path: str | None) -> None: ... +@typing.final +class InteractiveBrokersSubscriptionIdle: + @property + def client_id(self) -> model.ClientId: ... + @property + def instrument_id(self) -> model.InstrumentId: ... + @property + def subscription(self) -> str: ... + @property + def idle_timeout_secs(self) -> int: ... + @property + def last_data_received_ns(self) -> typing.Any: ... + @property + def ts_event(self) -> int: ... + @property + def ts_init(self) -> int: ... + def __new__( + cls, + client_id: model.ClientId, + instrument_id: model.InstrumentId, + subscription: str, + idle_timeout_secs: int, + last_data_received_ns: typing.Any, + ts_event: int, + ts_init: int, + ) -> InteractiveBrokersSubscriptionIdle: ... + def to_json(self) -> str: ... + @classmethod + def from_json(cls, data: typing.Any) -> typing.Any: ... + @typing.final class ContainerStatus(enum.Enum): NO_CONTAINER = ... @@ -259,17 +335,6 @@ class ContainerStatus(enum.Enum): READY = ... UNKNOWN = ... -@typing.final -class ErrorCategory(enum.Enum): - CLIENT_ERROR = ... - CONNECTIVITY_ERROR = ... - SUBSCRIPTION_ERROR = ... - ORDER_ERROR = ... - MARKET_DATA_ERROR = ... - UNKNOWN = ... - - def as_str(self) -> str: ... - @typing.final class IbAction(enum.Enum): BUY = ... @@ -443,6 +508,7 @@ class IbLiquidity(enum.Enum): ADDED_LIQUIDITY = ... REMOVED_LIQUIDITY = ... LIQUIDITY_ROUTED_OUT = ... + UNKNOWN = ... def as_i32(self) -> int: ... @@ -752,21 +818,6 @@ class IbVolatilityType(enum.Enum): def as_i32(self) -> int: ... -@typing.final -class InteractiveBrokersErrorKind(enum.Enum): - CONNECTION = ... - AUTHENTICATION = ... - CONFIGURATION = ... - REQUEST = ... - PARSE = ... - INSTRUMENT = ... - ORDER = ... - MARKET_DATA = ... - IB_API = ... - INTERNAL = ... - - def as_str(self) -> str: ... - @typing.final class MarketDataType(enum.Enum): REALTIME = ... diff --git a/python/pyproject.toml b/python/pyproject.toml index fd7e189182e0..b6c7ccda88db 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -380,6 +380,9 @@ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" "TD003", ] # Optional timer demo is left commented on purpose "examples/live/interactive_brokers/_common.py" = ["S603"] +"examples/live/interactive_brokers/notebooks/**/*.py" = [ + "ERA001", +] # Jupytext notebook headers are not commented-out code "examples/live/polymarket/updown_smoke_tester.py" = ["TRY004"] "tests/**/*.py" = [ "A002", diff --git a/python/tests/unit/adapters/example_modules.py b/python/tests/unit/adapters/example_modules.py index b6b1067f90d1..6ca0216b9264 100644 --- a/python/tests/unit/adapters/example_modules.py +++ b/python/tests/unit/adapters/example_modules.py @@ -50,7 +50,11 @@ def load_example_module(adapter: str, module: str) -> ModuleType: assert spec.loader is not None loaded_module = importlib.util.module_from_spec(spec) sys.modules[module_name] = loaded_module - spec.loader.exec_module(loaded_module) + sys.path.insert(0, str(module_path.parent)) + try: + spec.loader.exec_module(loaded_module) + finally: + sys.path.pop(0) return loaded_module diff --git a/python/tests/unit/adapters/interactive_brokers/test_interactive_brokers_factories.py b/python/tests/unit/adapters/interactive_brokers/test_interactive_brokers_factories.py index 001297d18424..b76649da1ca7 100644 --- a/python/tests/unit/adapters/interactive_brokers/test_interactive_brokers_factories.py +++ b/python/tests/unit/adapters/interactive_brokers/test_interactive_brokers_factories.py @@ -16,6 +16,9 @@ Test interactive brokers factories behavior. """ +from pathlib import Path +from uuid import UUID + import pytest from unit.adapters.example_modules import capture_data_tester_main from unit.adapters.example_modules import capture_exec_tester_main @@ -27,18 +30,26 @@ from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersExecutionClientFactory from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersInstrumentProvider from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersInstrumentProviderConfig +from nautilus_trader.adapters.interactive_brokers import InteractiveBrokersSubscriptionIdle from nautilus_trader.adapters.interactive_brokers import MarketDataType from nautilus_trader.adapters.interactive_brokers import SymbologyMethod from nautilus_trader.common import Environment from nautilus_trader.live import LiveNode from nautilus_trader.live import LiveRiskEngineConfig +from nautilus_trader.model import ClientId +from nautilus_trader.model import CustomData +from nautilus_trader.model import DataType from nautilus_trader.model import InstrumentId from nautilus_trader.model import TraderId IB = "IB" +IB_EXAMPLES_DIR = Path(__file__).resolve().parents[5] / "examples" / "live" / "interactive_brokers" +IB_NOTEBOOK_SCRIPTS = sorted((IB_EXAMPLES_DIR / "notebooks").glob("*.py")) ib_data_tester = load_example_module("interactive_brokers", "data_tester") ib_exec_tester = load_example_module("interactive_brokers", "exec_tester") +ib_order_strategies = load_example_module("interactive_brokers", "ib_v2_order_strategies") +ib_common = load_example_module("interactive_brokers", "_common") def test_interactive_brokers_factories_expose_python_names() -> None: @@ -74,7 +85,28 @@ def test_interactive_brokers_instrument_provider_config_and_empty_surface() -> N assert config.symbology_method == SymbologyMethod.RAW assert config.load_ids == {instrument_id} - assert config.load_contracts == [contract] + assert config.load_contracts == [ + { + "comboLegsDescrip": "", + "conId": 0, + "currency": "", + "description": "", + "exchange": "SMART", + "includeExpired": False, + "issuerId": "", + "lastTradeDateOrContractMonth": "", + "localSymbol": "", + "multiplier": "", + "primaryExchange": "", + "right": None, + "secId": "", + "secIdType": None, + "secType": "STK", + "strike": 0.0, + "symbol": "MSFT", + "tradingClass": "", + }, + ] assert config.min_expiry_days == 2 assert config.max_expiry_days == 30 assert config.build_options_chain is True @@ -90,6 +122,23 @@ def test_interactive_brokers_instrument_provider_config_and_empty_surface() -> N assert provider.find(instrument_id) is None +def test_interactive_brokers_example_futures_symbols_keep_the_venue_local_symbol() -> None: + """ + Test interactive brokers example futures symbols keep the venue local symbol. + """ + assert ib_common.contract_month_code(2026, 12) == "Z6" + assert ib_common.active_quarterly_contract( + symbol="ES", + venue="XCME", + today=ib_common.dt.date(2026, 1, 1), + ) == ("ESH6", "ESH6.XCME", "20260320") + assert ib_common.active_monthly_contract( + symbol="CL", + venue="XNYM", + today=ib_common.dt.date(2026, 1, 1), + ) == ("CLH6", "CLH6.XNYM", "202603") + + def test_live_node_builder_accepts_interactive_brokers_data_factory() -> None: """ Test live node builder accepts interactive brokers data factory. @@ -176,12 +225,150 @@ def test_interactive_brokers_exec_tester_runs_live_orders( monkeypatch.setenv("TWS_ACCOUNT", "U1234567") captured = capture_exec_tester_main(monkeypatch, ib_exec_tester) kwargs = captured["exec_tester_kwargs"] - _, _, exec_config = captured["exec_client_args"] + _, _, _exec_config = captured["exec_client_args"] assert isinstance(kwargs, dict) - assert isinstance(exec_config, InteractiveBrokersExecutionClientConfig) - assert exec_config.account_id == "U1234567" + assert kwargs["use_uuid_client_order_ids"] is True + assert "external_order_claims" not in kwargs assert kwargs["dry_run"] is False assert kwargs["enable_limit_buys"] is True assert kwargs["enable_limit_sells"] is True + assert kwargs["subscribe_trades"] is False + assert kwargs["use_post_only"] is False + assert kwargs["cancel_orders_on_stop"] is True + assert kwargs["close_positions_on_stop"] is True assert captured["run_called"] is True + + +def test_interactive_brokers_order_examples_use_per_run_client_order_ids( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """ + Test interactive brokers order examples use per run client order ids. + """ + run_ids = iter( + [ + UUID("11111111-1111-4111-8111-111111111111"), + UUID("22222222-2222-4222-8222-222222222222"), + ], + ) + monkeypatch.setattr(ib_order_strategies, "uuid4", lambda: next(run_ids)) + + first = ib_order_strategies.IbV2OrderStrategy() + second = ib_order_strategies.IbV2OrderStrategy() + + assert str(first.client_order_id("ENTRY")) == "IBV2-11111111-ENTRY" + assert str(second.client_order_id("ENTRY")) == "IBV2-22222222-ENTRY" + + +def test_interactive_brokers_order_examples_clean_state_on_stop( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """ + Test interactive brokers order examples clean state on stop. + """ + calls: list[tuple[str, object, object]] = [] + + def record_cancel(strategy: object, instrument_id: object, client_id: object) -> None: + calls.append(("cancel", instrument_id, client_id)) + + def record_close(strategy: object, instrument_id: object, client_id: object) -> None: + calls.append(("close", instrument_id, client_id)) + + def record_unsubscribe(strategy: object, instrument_id: object, client_id: object) -> None: + calls.append(("unsubscribe", instrument_id, client_id)) + + monkeypatch.setenv("IB_V2_ENABLE_ORDER_SUBMISSION", "1") + monkeypatch.setattr( + ib_order_strategies.IbV2OrderStrategy, + "cancel_all_orders", + record_cancel, + ) + monkeypatch.setattr( + ib_order_strategies.IbV2OrderStrategy, + "close_all_positions", + record_close, + ) + monkeypatch.setattr( + ib_order_strategies.IbV2OrderStrategy, + "unsubscribe_quotes", + record_unsubscribe, + ) + strategy = ib_order_strategies.IbV2OrderStrategy() + strategy._quotes_subscribed = True + + strategy.on_stop() + + assert calls == [ + ("cancel", strategy.instrument_id, ib_order_strategies.ib_client_id()), + ("close", strategy.instrument_id, ib_order_strategies.ib_client_id()), + ("unsubscribe", strategy.instrument_id, ib_order_strategies.ib_client_id()), + ] + + +@pytest.mark.parametrize( + "script_path", + IB_NOTEBOOK_SCRIPTS, + ids=lambda path: path.name, +) +def test_interactive_brokers_notebook_inlines_setup_and_strategy(script_path: Path) -> None: + """ + Test interactive brokers notebook inlines setup and strategy. + """ + source = script_path.read_text(encoding="utf-8") + + assert "# %% [markdown]" in source + assert "build_ib_live_node" in source + assert "node.add_strategy(" in source + assert "order_example_driver" not in source + assert "add_strategy_from_config" not in source + + +def test_trade_selection_and_idle_config() -> None: + """ + Test trade selection and idle config. + """ + default = InteractiveBrokersDataClientConfig() + selected = InteractiveBrokersDataClientConfig( + all_last_trades=False, + subscription_idle_timeout_secs=17, + ) + assert default.all_last_trades is True + assert default.subscription_idle_timeout_secs is None + assert selected.all_last_trades is False + assert selected.subscription_idle_timeout_secs == 17 + + +@pytest.mark.parametrize("timeout", [0, 2**64 - 1]) +def test_invalid_subscription_idle_timeout(timeout: int) -> None: + """ + Test invalid subscription idle timeout. + """ + with pytest.raises(ValueError, match="subscription_idle_timeout_secs"): + InteractiveBrokersDataClientConfig(subscription_idle_timeout_secs=timeout) + + +@pytest.mark.parametrize("last_received", [None, 123]) +def test_subscription_idle_custom_data_preserves_fields(last_received: int | None) -> None: + """ + Test subscription idle custom data preserves fields. + """ + event = InteractiveBrokersSubscriptionIdle( + ClientId("IB-DATA-17"), + InstrumentId.from_str("AAPL=STK.SMART"), + "trades", + 17, + last_received, + 456, + 789, + ) + custom = CustomData(DataType("InteractiveBrokersSubscriptionIdle"), event) + payload = custom.data + assert isinstance(payload, InteractiveBrokersSubscriptionIdle) + assert payload.client_id == ClientId("IB-DATA-17") + assert payload.instrument_id == InstrumentId.from_str("AAPL=STK.SMART") + assert payload.subscription == "trades" + assert payload.idle_timeout_secs == 17 + assert payload.last_data_received_ns == last_received + assert payload.ts_event == 456 + assert payload.ts_init == 789