Skip to main content

nautilus_interactive_brokers/execution/
core.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Core execution client implementation for Interactive Brokers.
17
18#[cfg(test)]
19mod tests;
20
21pub(super) use std::{
22    fmt::Debug,
23    str::FromStr,
24    sync::{
25        Arc,
26        atomic::{AtomicBool, Ordering},
27    },
28    time::Duration,
29};
30
31pub(super) use ahash::{AHashMap, AHashSet};
32pub(super) use anyhow::Context;
33pub(super) use ibapi::{
34    Notice, NoticeCategory,
35    accounts::PositionUpdate,
36    client::Client,
37    contracts::{Contract, Exchange, SecurityType},
38    orders::{
39        ExecutionData, ExecutionFilter, Executions, OrderStatus as IBOrderStatus, OrderStatusKind,
40        OrderUpdate, Orders,
41    },
42    prelude::{StreamExt, SubscriptionItemStreamExt},
43    subscriptions::SubscriptionItem,
44};
45pub(super) use nautilus_common::{
46    cache::{Cache, fifo::FifoCacheMap},
47    clients::{ExecutionClient, ExecutionReportTask},
48    enums::LogLevel,
49    factories::OrderEventFactory,
50    live::{
51        runner::{get_data_event_sender, get_exec_event_sender},
52        sender::EventSender,
53    },
54    messages::{
55        DataEvent, ExecutionEvent,
56        execution::{
57            BatchCancelOrders, CancelAllOrders, CancelOrder, ExecutionReport, GenerateFillReports,
58            GenerateFillReportsBuilder, GenerateOrderStatusReport, GenerateOrderStatusReports,
59            GenerateOrderStatusReportsBuilder, GeneratePositionStatusReports,
60            GeneratePositionStatusReportsBuilder, ModifyOrder, QueryAccount, QueryOrder,
61            SubmitOrder, SubmitOrderList,
62        },
63    },
64};
65pub(super) use nautilus_core::{
66    DurationNanos, Params, UUID4, UnixNanos,
67    time::{AtomicTime, get_atomic_clock_realtime},
68};
69pub(super) use nautilus_live::{
70    ExecutionClientCore,
71    execution::failure::CommandFailure,
72    task::{TaskGroup, TaskGroupGuard, TaskSpawner},
73};
74pub(super) use nautilus_model::{
75    accounts::AccountAny,
76    enums::{
77        LiquiditySide, OmsType, OrderSide, OrderStatus, OrderType, PositionSide, TrailingOffsetType,
78    },
79    events::{
80        OrderAccepted, OrderCancelDenied, OrderCancelRejected, OrderCanceled, OrderDenied,
81        OrderDeniedReason, OrderEventAny, OrderFilled, OrderModifyDenied, OrderModifyRejected,
82        OrderPendingCancel, OrderRejected, OrderSubmitted, OrderUpdated,
83    },
84    identifiers::{
85        AccountId, ClientId, ClientOrderId, InstrumentId, StrategyId, TradeId, TraderId, Venue,
86        VenueOrderId,
87    },
88    instruments::{Instrument, InstrumentAny},
89    orders::{Order, any::OrderAny},
90    reports::{ExecutionMassStatus, FillReport, OrderStatusReport, PositionStatusReport},
91    types::{AccountBalance, Currency, MarginBalance, Money, Price, Quantity},
92};
93pub(super) use parking_lot::Mutex;
94pub(super) use rust_decimal::Decimal;
95pub(super) use tokio::sync::Mutex as AsyncMutex;
96pub(super) use tokio_util::sync::CancellationToken;
97pub(super) use ustr::Ustr;
98
99pub(super) use super::{
100    account::{
101        AccountSnapshot, PositionTracker, create_position_tracker, ib_account_code,
102        initialize_position_tracking, record_own_fill, stream_account_summary,
103        subscribe_account_summary, subscribe_positions,
104    },
105    parse,
106    parse::{
107        ib_venue_order_id, parse_execution_time, parse_execution_to_fill_report,
108        parse_order_status_to_report,
109    },
110    reports::{IbReportClient, IbReportContext},
111    transform::nautilus_order_to_ib_order,
112};
113use super::{incarnations::OrderIncarnationGroup, parse::parse_order_data_to_report};
114pub(super) use crate::{
115    common::shared_client::{SharedClientHandle, get_or_connect},
116    config::InteractiveBrokersExecutionClientConfig,
117    providers::instruments::InteractiveBrokersInstrumentProvider,
118};
119
120pub(super) const MODIFY_TRAILING_OFFSET_PARAM: &str = "trailing_offset";
121
122pub(super) const DENIAL_CLIENT_NOT_READY: &str = "IB_CLIENT_NOT_READY";
123pub(super) const DENIAL_ORDER_INVALID: &str = "ORDER_INVALID";
124pub(super) const DENIAL_ORDER_LIST_INVALID: &str = "ORDER_LIST_INVALID";
125pub(super) const DENIAL_ORDER_LIST_SIBLING_SUBMIT_FAILED: &str = "ORDER_LIST_SIBLING_SUBMIT_FAILED";
126pub(super) const DENIAL_TRAILING_OFFSET_TYPE_UNSUPPORTED: &str = "UNSUPPORTED_TRAILING_OFFSET_TYPE";
127const ORDER_ID_PARTITION_SIZE: i32 = 1_000_000;
128
129pub(super) fn coded_denial_reason(code: &str, detail: &str) -> String {
130    format!("{code}: {detail}")
131}
132
133/// Interactive Brokers execution client.
134///
135/// This client provides order execution functionality using the `rust-ibapi` library.
136/// It manages order submission, modification, cancellation, and execution reporting.
137#[cfg_attr(
138    feature = "python",
139    pyo3::pyclass(module = "nautilus_trader.adapters.interactive_brokers", unsendable)
140)]
141pub struct InteractiveBrokersExecutionClient {
142    /// Core execution client functionality.
143    pub(super) core: ExecutionClientCore,
144    /// Configuration for the client.
145    pub(super) config: InteractiveBrokersExecutionClientConfig,
146    /// Raw IB account code sent to TWS, derived once from the configured `account_id`.
147    pub(super) ib_account: Ustr,
148    /// Instrument provider.
149    pub(super) instrument_provider: Arc<InteractiveBrokersInstrumentProvider>,
150    /// Connection state.
151    pub(super) is_connected: Arc<AtomicBool>,
152    /// IB API client (shared per host/port/client_id when both data and execution connect).
153    pub(super) ib_client: Option<SharedClientHandle>,
154    /// Tasks serving execution commands.
155    pub(super) pending_tasks: TaskGroup,
156    /// Tasks serving the active connection session.
157    pub(super) session_tasks: TaskGroup,
158    /// Order ID counter.
159    pub(super) next_order_id: Arc<Mutex<i32>>,
160    /// Serializes order submissions so TWS receives monotonically increasing order IDs.
161    pub(super) order_submit_lock: Arc<AsyncMutex<()>>,
162    /// Per-order state and indexes.
163    pub(super) orders: OrderTracker,
164    /// Commission cache by execution ID (to merge with fill reports).
165    pub(super) commission_cache: Arc<Mutex<CommissionCache>>,
166    /// Execution cache by execution ID while awaiting commission reports.
167    pub(super) pending_execution_cache: Arc<Mutex<PendingExecutionCache>>,
168    /// Position tracker for detecting external position changes (e.g., option exercises).
169    pub(super) position_tracker: PositionTracker,
170}
171
172pub(super) type CommissionCache = FifoCacheMap<String, (f64, String), 10_000>;
173pub(super) type PendingExecutionCache =
174    FifoCacheMap<String, (tokio::time::Instant, ExecutionData), 10_000>;
175
176#[derive(Clone)]
177pub(super) struct OrderTracker {
178    pub(super) state: Arc<Mutex<OrderTrackerState>>,
179}
180
181pub(super) struct OrderTrackerState {
182    pub(super) groups: AHashMap<ClientOrderId, OrderIncarnationGroup>,
183    pub(super) group_history: FifoCacheMap<ClientOrderId, OrderIncarnationGroup, 10_000>,
184    pub(super) incarnation_parents: AHashMap<i64, ClientOrderId>,
185    pub(super) order_snapshots: FifoCacheMap<i64, ibapi::orders::OrderData, 10_000>,
186    pub(super) auxiliary_orders: AHashMap<ClientOrderId, TrackedOrder>,
187    pub(super) client_id: i32,
188    pub(super) order_id_map: AHashMap<ClientOrderId, i32>,
189    pub(super) venue_order_id_map: AHashMap<i32, ClientOrderId>,
190    pub(super) active_orders: AHashMap<i32, TrackedOrder>,
191    pub(super) terminal_orders: FifoCacheMap<i32, TrackedOrder, 10_000>,
192    /// Fills held for untracked orders while their details resolve; a key marks a resolution
193    /// in flight.
194    pub(super) fills_held: AHashMap<IbOrderSelector, Vec<FillReport>>,
195    /// Orders whose submission is under way but not yet tracked (while their price magnifier
196    /// loads, say). A cancel takes an order from here, and its submission then stops before IB.
197    pub(super) awaiting_submission: AHashMap<ClientOrderId, AwaitingSubmission>,
198}
199
200impl OrderTrackerState {
201    pub(super) fn order(&self, order_id: i32) -> Option<&TrackedOrder> {
202        self.active_orders
203            .get(&order_id)
204            .or_else(|| self.terminal_orders.get(&order_id))
205    }
206
207    pub(super) fn order_mut(&mut self, order_id: i32) -> Option<&mut TrackedOrder> {
208        if self.active_orders.contains_key(&order_id) {
209            self.active_orders.get_mut(&order_id)
210        } else {
211            self.terminal_orders.get_mut(&order_id)
212        }
213    }
214
215    pub(super) fn order_by_client(&self, client_order_id: ClientOrderId) -> Option<&TrackedOrder> {
216        if let Some(order) = self.auxiliary_orders.get(&client_order_id) {
217            return Some(order);
218        }
219
220        self.order_id_map
221            .get(&client_order_id)
222            .and_then(|order_id| self.order(*order_id))
223    }
224
225    pub(super) fn order_by_client_mut(
226        &mut self,
227        client_order_id: ClientOrderId,
228    ) -> Option<&mut TrackedOrder> {
229        if self.auxiliary_orders.contains_key(&client_order_id) {
230            return self.auxiliary_orders.get_mut(&client_order_id);
231        }
232
233        let order_id = self.order_id_map.get(&client_order_id).copied()?;
234        self.order_mut(order_id)
235    }
236
237    pub(super) fn cancel_selector(
238        &self,
239        client_order_id: ClientOrderId,
240        venue_order_id: Option<&VenueOrderId>,
241    ) -> anyhow::Result<Option<IbOrderSelector>> {
242        // A duplicate member shares its raw order ID with another physical order, so
243        // only its permanent ID identifies it
244        if let Some(order) = self.auxiliary_orders.get(&client_order_id)
245            && order.perm_id > 0
246        {
247            return Ok(Some(IbOrderSelector::PermId(order.perm_id)));
248        }
249
250        if let Some(order_id) = self.order_id_map.get(&client_order_id) {
251            return Ok(Some(IbOrderSelector::OrderId(*order_id)));
252        }
253
254        venue_order_id
255            .map(IbOrderSelector::from_venue_order_id)
256            .transpose()
257    }
258}
259
260impl OrderTracker {
261    pub(super) fn new(client_id: i32) -> Self {
262        Self {
263            state: Arc::new(Mutex::new(OrderTrackerState {
264                groups: AHashMap::new(),
265                group_history: FifoCacheMap::new(),
266                incarnation_parents: AHashMap::new(),
267                order_snapshots: FifoCacheMap::new(),
268                auxiliary_orders: AHashMap::new(),
269                client_id,
270                order_id_map: AHashMap::new(),
271                venue_order_id_map: AHashMap::new(),
272                active_orders: AHashMap::new(),
273                terminal_orders: FifoCacheMap::new(),
274                fills_held: AHashMap::new(),
275                awaiting_submission: AHashMap::new(),
276            })),
277        }
278    }
279
280    #[expect(
281        clippy::unnecessary_wraps,
282        reason = "Retain the fallible tracker interface used throughout execution callbacks"
283    )]
284    pub(super) fn lock(&self) -> anyhow::Result<parking_lot::MutexGuard<'_, OrderTrackerState>> {
285        Ok(self.state.lock())
286    }
287}
288
289#[derive(Clone, Copy)]
290pub(super) struct SpreadFillContext<'a> {
291    pub(super) client_order_id: ClientOrderId,
292    pub(super) spread_instrument_id: InstrumentId,
293    pub(super) commission: f64,
294    pub(super) commission_currency: &'a str,
295    pub(super) ts_init: UnixNanos,
296    pub(super) account_id: AccountId,
297}
298
299#[derive(Debug)]
300pub(super) enum OrderCorrelation {
301    Tracked {
302        order_id: i32,
303        context: TrackedOrder,
304    },
305    Duplicate {
306        order_id: i32,
307        context: TrackedOrder,
308    },
309    Untracked {
310        client_order_id: Option<ClientOrderId>,
311    },
312}
313
314/// An order submitted to the client that has not yet reached IB order tracking.
315#[derive(Clone, Copy, Debug, PartialEq, Eq)]
316pub(super) struct AwaitingSubmission {
317    pub(super) trader_id: TraderId,
318    pub(super) strategy_id: StrategyId,
319    pub(super) instrument_id: InstrumentId,
320    pub(super) order_side: OrderSide,
321}
322
323#[derive(Clone, Debug)]
324pub(super) struct TrackedOrder {
325    pub(super) client_order_id: ClientOrderId,
326    pub(super) trader_id: TraderId,
327    pub(super) strategy_id: StrategyId,
328    pub(super) instrument_id: InstrumentId,
329    pub(super) order_side: OrderSide,
330    pub(super) order_type: OrderType,
331    pub(super) accepted: bool,
332    pub(super) avg_px: Option<Price>,
333    pub(super) pending_cancel: bool,
334    pub(super) pending_modify: Option<PendingModifyValues>,
335    pub(super) perm_id: i64,
336    pub(super) spread_fill_ids: ahash::AHashSet<String>,
337    pub(super) spread_fills_held: HeldSpreadFills,
338    /// Quantity, price, and trigger price of the last `OrderUpdated` sent from an openOrder.
339    pub(super) last_update: Option<(Quantity, Option<Price>, Option<Price>)>,
340}
341
342/// Spread fills waiting for their leg fills, so a spread fill is the last event of its group.
343///
344/// IB reports the combo execution before the leg executions, and leg fills carry the account
345/// changes, so a spread fill only goes out once the legs cover its quantity.
346#[derive(Clone, Debug, Default)]
347pub(super) struct HeldSpreadFills {
348    pub(super) fills: std::collections::VecDeque<(tokio::time::Instant, OrderFilled)>,
349    /// Leg quantity sent but not yet matched to a spread fill.
350    pub(super) leg_quantities: AHashMap<InstrumentId, f64>,
351}
352
353/// Requested modify values in IB units, kept until an openOrder reflects them or the
354/// modify is rejected, so unrelated openOrder refreshes cannot clear the pending flag.
355///
356/// Only fields the modify writes are set; `None` leaves a field unchecked. IB reports
357/// fields an order type does not use (such as a limit or trail stop price on a plain stop)
358/// with undocumented values that can change between openOrder messages.
359#[derive(Clone, Copy, Debug, PartialEq)]
360pub(super) struct PendingModifyValues {
361    pub(super) total_quantity: Option<f64>,
362    pub(super) limit_price: Option<f64>,
363    pub(super) aux_price: Option<f64>,
364    pub(super) trail_stop_price: Option<f64>,
365}
366
367impl PendingModifyValues {
368    pub(super) fn matches(&self, order: &ibapi::orders::Order) -> bool {
369        const EPS: f64 = 1e-9;
370        let approx = |a: f64, b: f64| (a - b).abs() <= EPS;
371
372        let reflects = |requested: Option<f64>, reported: Option<f64>| {
373            requested.is_none_or(|requested| {
374                reported.is_some_and(|reported| approx(requested, reported))
375            })
376        };
377
378        reflects(self.total_quantity, Some(order.total_quantity))
379            && reflects(self.limit_price, order.limit_price)
380            && reflects(self.aux_price, order.aux_price)
381            && reflects(self.trail_stop_price, order.trail_stop_price)
382    }
383}
384
385#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
386pub(super) enum IbOrderSelector {
387    OrderId(i32),
388    PermId(i64),
389}
390
391impl IbOrderSelector {
392    pub(super) fn from_venue_order_id(venue_order_id: &VenueOrderId) -> anyhow::Result<Self> {
393        let raw = venue_order_id.as_str();
394        if let Some(perm_id) = raw.strip_prefix("PERM-") {
395            return Ok(Self::PermId(perm_id.parse::<i64>().with_context(|| {
396                format!("Failed to parse venue_order_id {raw:?} as IB perm_id")
397            })?));
398        }
399
400        Ok(Self::OrderId(raw.parse::<i32>().with_context(|| {
401            format!("Failed to parse venue_order_id {raw:?} as IB order_id")
402        })?))
403    }
404
405    pub(super) fn matches(self, order_id: i32, perm_id: i64) -> bool {
406        match self {
407            Self::OrderId(target_order_id) => order_id == target_order_id,
408            Self::PermId(target_perm_id) => perm_id == target_perm_id,
409        }
410    }
411
412    pub(super) fn venue_order_id(self) -> VenueOrderId {
413        match self {
414            Self::OrderId(order_id) => VenueOrderId::from(order_id.to_string()),
415            Self::PermId(perm_id) => VenueOrderId::from(format!("PERM-{perm_id}")),
416        }
417    }
418
419    pub(super) fn label(self) -> String {
420        self.venue_order_id().to_string()
421    }
422}
423
424impl Debug for InteractiveBrokersExecutionClient {
425    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
426        f.debug_struct(stringify!(InteractiveBrokersExecutionClient))
427            .field("core", &self.core)
428            .field("config", &self.config)
429            .field("instrument_provider", &self.instrument_provider)
430            .field("is_connected", &self.is_connected.load(Ordering::Relaxed))
431            .field("ib_client", &self.ib_client.is_some())
432            .finish_non_exhaustive()
433    }
434}
435
436impl InteractiveBrokersExecutionClient {
437    /// Creates a new [`InteractiveBrokersExecutionClient`].
438    ///
439    /// # Errors
440    ///
441    /// Returns an error if client creation fails.
442    pub fn new(
443        core: ExecutionClientCore,
444        config: InteractiveBrokersExecutionClientConfig,
445        instrument_provider: Arc<InteractiveBrokersInstrumentProvider>,
446    ) -> anyhow::Result<Self> {
447        config.validate()?;
448
449        anyhow::ensure!(
450            !config.client_id.unsigned_abs().is_multiple_of(1000),
451            "Interactive Brokers execution client_id must not be a multiple of 1000 because order ID partitioning uses client_id % 1000, was {}",
452            config.client_id
453        );
454
455        let ib_account = ib_account_code(config.account_id.as_deref(), core.account_id);
456        let pending_tasks = TaskGroup::new();
457        let session_tasks = TaskGroup::new();
458
459        let orders = OrderTracker::new(config.client_id);
460        Ok(Self {
461            core,
462            config,
463            ib_account,
464            instrument_provider,
465            is_connected: Arc::new(AtomicBool::new(false)),
466            ib_client: None,
467            pending_tasks,
468            session_tasks,
469            next_order_id: Arc::new(Mutex::new(0)),
470            order_submit_lock: Arc::new(AsyncMutex::new(())),
471            orders,
472            commission_cache: Arc::new(Mutex::new(CommissionCache::new())),
473            pending_execution_cache: Arc::new(Mutex::new(PendingExecutionCache::new())),
474            position_tracker: create_position_tracker(),
475        })
476    }
477
478    pub(super) async fn order_details_with_fills(
479        client: &Client,
480        target: IbOrderSelector,
481        instrument_id: InstrumentId,
482        account_id: AccountId,
483        ib_account: Ustr,
484        provider: &InteractiveBrokersInstrumentProvider,
485        ts_init: UnixNanos,
486        orders: &OrderTracker,
487    ) -> anyhow::Result<Option<(OrderStatusReport, Vec<FillReport>)>> {
488        let mut found = None;
489        let mut found_completed = false;
490
491        for completed in [false, true] {
492            let subscription = if completed {
493                client.completed_orders(false).await?
494            } else {
495                client.all_open_orders().await?
496            };
497
498            let mut subscription = subscription.filter_data();
499            while let Some(item) = subscription.next().await {
500                let Orders::OrderData(data) = item? else {
501                    continue;
502                };
503
504                if data.order.account != ib_account
505                    || !target.matches(data.order_id, data.order.perm_id)
506                {
507                    continue;
508                }
509
510                if let Some(previous) = &found {
511                    let previous: &ibapi::orders::OrderData = previous;
512                    anyhow::ensure!(
513                        previous.order.perm_id == data.order.perm_id,
514                        "IB API order ID identifies multiple permanent IDs"
515                    );
516                }
517
518                found_completed = completed;
519                found = Some(data);
520            }
521
522            if found.is_some() {
523                break;
524            }
525        }
526
527        let Some(data) = found else {
528            return Ok(None);
529        };
530
531        let resolved = provider.resolve_instrument_id_for_contract(&data.contract)?;
532        anyhow::ensure!(
533            resolved == instrument_id,
534            "IB order query resolved an unexpected instrument {resolved}"
535        );
536        orders
537            .lock()?
538            .observe_order_data(&data, account_id, found_completed)?;
539        let mut report =
540            parse_order_data_to_report(&data, instrument_id, account_id, provider, ts_init)?;
541        let mut subscription = client
542            .executions(Self::execution_filter(ib_account, None))
543            .await?
544            .filter_data();
545        let mut executions = AHashMap::new();
546        let mut commissions = AHashMap::new();
547
548        while let Some(item) = subscription.next().await {
549            match item? {
550                Executions::ExecutionData(data)
551                    if data.execution.account_number == ib_account
552                        && target.matches(data.execution.order_id, data.execution.perm_id) =>
553                {
554                    executions.insert(data.execution.execution_id.clone(), data);
555                }
556                Executions::CommissionReport(commission) => {
557                    commissions.insert(
558                        commission.execution_id,
559                        (commission.commission, commission.currency),
560                    );
561                }
562                _ => {}
563            }
564        }
565
566        let mut fills = Vec::with_capacity(executions.len());
567        for (id, data) in executions {
568            let (commission, currency) = commissions
569                .remove(&id)
570                .with_context(|| format!("IB execution {id} has no commission report"))?;
571            fills.push(parse_execution_to_fill_report(
572                &data.execution,
573                &data.contract,
574                commission,
575                &currency,
576                instrument_id,
577                account_id,
578                provider,
579                ts_init,
580                None,
581            )?);
582        }
583
584        fills.sort_by_key(|fill| (fill.ts_event, fill.trade_id));
585
586        let filled = fills.iter().try_fold(Decimal::ZERO, |sum, fill| {
587            sum.checked_add(fill.last_qty.as_decimal())
588                .context("IB execution quantity overflow")
589        })?;
590
591        let filled = Quantity::from_decimal_dp(filled, report.quantity.precision)?;
592        report.filled_qty = report.filled_qty.max(filled);
593        if report.quantity.is_positive() && report.filled_qty >= report.quantity {
594            report.order_status = OrderStatus::Filled;
595        }
596
597        Ok(Some((report, fills)))
598    }
599
600    fn submit_order_list_with_orders(
601        &self,
602        cmd: SubmitOrderList,
603        orders: Vec<OrderAny>,
604    ) -> anyhow::Result<()> {
605        self.seed_instruments_from_cache(orders.iter().map(|order| order.instrument_id()));
606        let client = self.ib_client.as_ref().context("IB client not connected")?;
607        self.seed_price_magnifiers_from_cache(orders.iter().map(OrderAny::instrument_id));
608
609        for order in &orders {
610            Self::register_awaiting_submission(
611                order.client_order_id(),
612                AwaitingSubmission {
613                    trader_id: order.trader_id(),
614                    strategy_id: cmd.strategy_id,
615                    instrument_id: order.instrument_id(),
616                    order_side: order.order_side(),
617                },
618                &self.orders,
619            )?;
620        }
621        let client_order_ids: Vec<_> = orders.iter().map(OrderAny::client_order_id).collect();
622
623        let order_tracker = self.orders.clone();
624        let next_order_id = Arc::clone(&self.next_order_id);
625        let instrument_provider = Arc::clone(&self.instrument_provider);
626        let exec_sender = get_exec_event_sender();
627        let clock = get_atomic_clock_realtime();
628        let account_id = self.core.account_id;
629        let ib_account = self.ib_account;
630        let strategy_id = cmd.strategy_id;
631        let client_clone = client.as_arc().clone();
632        let order_submit_lock = Arc::clone(&self.order_submit_lock);
633        let request_timeout_secs = self.config.request_timeout;
634
635        let future = async move {
636            if let Err(e) = Self::handle_submit_order_list_async(
637                &cmd,
638                &orders,
639                &client_clone,
640                &order_tracker,
641                &next_order_id,
642                &instrument_provider,
643                &exec_sender,
644                clock,
645                account_id,
646                ib_account,
647                strategy_id,
648                &order_submit_lock,
649                request_timeout_secs,
650            )
651            .await
652            {
653                tracing::error!("Error submitting order list: {e}");
654            }
655        };
656
657        if let Err(e) = self.pending_tasks.spawn(future) {
658            let mut state = self.orders.lock()?;
659            for client_order_id in client_order_ids {
660                state.take_awaiting_submission(client_order_id);
661            }
662            return Err(e).context("failed to register IB execution command task");
663        }
664
665        Ok(())
666    }
667
668    // The provider is seeded from the cache when the client is created, so an instrument loaded
669    // later (by a data client, say) reaches the cache with its price magnifier but not the
670    // provider. The command handlers request any magnifier still missing from IB.
671    pub(super) fn seed_price_magnifiers_from_cache(
672        &self,
673        instrument_ids: impl IntoIterator<Item = InstrumentId>,
674    ) {
675        let cache = self.core.cache();
676
677        for instrument_id in instrument_ids {
678            self.instrument_provider
679                .seed_price_magnifier_from_cache(&cache, &instrument_id);
680        }
681    }
682
683    pub(super) fn reserve_next_local_order_id(
684        next_order_id: &Arc<Mutex<i32>>,
685    ) -> anyhow::Result<i32> {
686        let mut guard = next_order_id.lock();
687        anyhow::ensure!(
688            *guard > 0,
689            "No valid Interactive Brokers order ID available"
690        );
691        let order_id = *guard;
692        *guard += 1;
693        Ok(order_id)
694    }
695
696    // IB refuses every order ID below the client's next valid ID (error 103). When that ID lies
697    // above the client's partition no ID in it can be used, so this fails instead of mapping
698    fn apply_client_order_id_floor(next_id: i32, client_id: i32) -> anyhow::Result<i32> {
699        let client_slot = client_id.unsigned_abs() % 1000;
700        if client_slot == 0 {
701            return Ok(next_id);
702        }
703
704        let order_id_floor = (client_slot as i32) * ORDER_ID_PARTITION_SIZE;
705        let order_id_ceiling = order_id_floor + ORDER_ID_PARTITION_SIZE;
706        anyhow::ensure!(
707            next_id < order_id_ceiling,
708            "IB next valid order ID {next_id} for client_id {client_id} is above its partition ({} to {}), so IB would refuse every order with error 103; use another client_id",
709            order_id_floor + 1,
710            order_id_ceiling - 1,
711        );
712
713        if next_id > order_id_floor {
714            Ok(next_id)
715        } else {
716            Ok(order_id_floor + next_id.rem_euclid(ORDER_ID_PARTITION_SIZE).max(1))
717        }
718    }
719
720    fn is_order_id_in_client_partition(order_id: i32, client_id: i32) -> bool {
721        let client_slot = client_id.unsigned_abs() % 1000;
722        if client_slot == 0 {
723            return true;
724        }
725
726        let order_id_floor = (client_slot as i32) * ORDER_ID_PARTITION_SIZE;
727        order_id > order_id_floor && order_id < order_id_floor + ORDER_ID_PARTITION_SIZE
728    }
729
730    // IB refuses an order ID at or below one the same client already used (error 103). A next
731    // valid ID below the client's partition says nothing about this client's IDs, so the start
732    // also clears the highest ID this client has in orders IB lists
733    fn initial_order_id(
734        next_id: i32,
735        client_id: i32,
736        highest_used_order_id: Option<i32>,
737    ) -> anyhow::Result<i32> {
738        let client_scoped_next_id = Self::apply_client_order_id_floor(next_id, client_id)?;
739        Ok(
740            highest_used_order_id.map_or(client_scoped_next_id, |order_id| {
741                client_scoped_next_id.max(order_id.saturating_add(1))
742            }),
743        )
744    }
745
746    /// Gets the next valid order ID from IB.
747    ///
748    /// # Errors
749    ///
750    /// Returns an error if getting the next order ID fails.
751    async fn get_next_order_id(&self) -> anyhow::Result<i32> {
752        let client = self.ib_client.as_ref().context("IB client not connected")?;
753
754        let timeout_dur = Duration::from_secs(self.config.request_timeout);
755        let order_id = tokio::time::timeout(timeout_dur, client.next_valid_order_id())
756            .await
757            .context("Timeout getting next order ID")??;
758        Ok(order_id)
759    }
760
761    async fn get_highest_open_order_id(&self, client: &Client) -> anyhow::Result<Option<i32>> {
762        let timeout_dur = Duration::from_secs(self.config.request_timeout);
763        let subscription = tokio::time::timeout(timeout_dur, client.all_open_orders())
764            .await
765            .context("Timeout requesting open orders for next order ID initialization")??;
766        let mut subscription = subscription.filter_data();
767        let mut highest_order_id = None;
768        let mut snapshots = Vec::new();
769
770        while let Some(order_result) = subscription.next().await {
771            match order_result {
772                Ok(Orders::OrderData(data)) => {
773                    if data.order.account == self.ib_account {
774                        snapshots.push(data.clone());
775                    }
776
777                    if data.order.client_id != self.config.client_id
778                        || !Self::is_order_id_in_client_partition(
779                            data.order_id,
780                            self.config.client_id,
781                        )
782                    {
783                        continue;
784                    }
785
786                    highest_order_id = Some(
787                        highest_order_id
788                            .map_or(data.order_id, |current: i32| current.max(data.order_id)),
789                    );
790                }
791                Ok(_) => {}
792                Err(e) => {
793                    tracing::debug!(
794                        "Ignoring open-order event while initializing next order ID: {e}"
795                    );
796                }
797            }
798        }
799
800        {
801            let cache = self.core.cache();
802            let mut state = self.orders.lock()?;
803
804            for data in &snapshots {
805                let venue_id = parse::ib_venue_order_id(data.order_id, data.order.perm_id);
806
807                let Some(id) = cache.client_order_id(&venue_id) else {
808                    continue;
809                };
810
811                let Some(order) = cache.order(id) else {
812                    continue;
813                };
814
815                if cache.client_id(id) != Some(&self.core.client_id)
816                    || order.account_id() != Some(self.core.account_id)
817                    || data.order.client_id != self.config.client_id
818                {
819                    continue;
820                }
821
822                if let Some(existing) = state.venue_order_id_map.get(&data.order_id)
823                    && *existing != *id
824                {
825                    continue;
826                }
827
828                state.order_id_map.insert(*id, data.order_id);
829                state.venue_order_id_map.insert(data.order_id, *id);
830                state
831                    .active_orders
832                    .entry(data.order_id)
833                    .or_insert_with(|| TrackedOrder {
834                        client_order_id: *id,
835                        trader_id: order.trader_id(),
836                        strategy_id: order.strategy_id(),
837                        instrument_id: order.instrument_id(),
838                        order_side: order.order_side(),
839                        order_type: order.order_type(),
840                        accepted: true,
841                        avg_px: None,
842                        pending_cancel: false,
843                        pending_modify: None,
844                        perm_id: data.order.perm_id,
845                        spread_fill_ids: AHashSet::new(),
846                        spread_fills_held: HeldSpreadFills::default(),
847                        last_update: Some((order.quantity(), order.price(), order.trigger_price())),
848                    });
849            }
850
851            for data in &snapshots {
852                state.observe_order_data(data, self.core.account_id, false)?;
853            }
854        }
855
856        Ok(highest_order_id)
857    }
858
859    async fn get_highest_completed_order_id(&self, client: &Client) -> anyhow::Result<Option<i32>> {
860        let deadline =
861            tokio::time::Instant::now() + Duration::from_secs(self.config.request_timeout);
862        let subscription = tokio::time::timeout_at(deadline, client.completed_orders(false))
863            .await
864            .context("Timeout requesting completed orders for next order ID initialization")??;
865
866        Self::highest_completed_order_id(subscription, self.config.client_id, deadline).await
867    }
868
869    // IB lists completed orders for a limited period only. A completed order's API order ID is on
870    // the order itself because its top-level order ID is unset. A snapshot that fails, drops
871    // records, or does not reach IB's end marker by the deadline is an error, since a missing
872    // order could hold the highest ID
873    pub(super) async fn highest_completed_order_id<S>(
874        mut subscription: S,
875        client_id: i32,
876        deadline: tokio::time::Instant,
877    ) -> anyhow::Result<Option<i32>>
878    where
879        S: futures_util::Stream<Item = Result<SubscriptionItem<Orders>, ibapi::Error>> + Unpin,
880    {
881        let read_snapshot = async {
882            let mut highest_order_id = None;
883
884            while let Some(item) = subscription.next().await {
885                let data = match item? {
886                    SubscriptionItem::Data(Orders::OrderData(data)) => data,
887                    SubscriptionItem::Data(_) => continue,
888                    SubscriptionItem::Notice(notice)
889                        if notice.code == ibapi::SUBSCRIPTION_LAG_CODE =>
890                    {
891                        anyhow::bail!("Completed orders snapshot dropped records: {notice}");
892                    }
893                    SubscriptionItem::Notice(notice) => {
894                        tracing::warn!("IB notice on completed orders snapshot: {notice}");
895                        continue;
896                    }
897                };
898
899                let order_id = data.order.order_id;
900                if data.order.client_id == client_id
901                    && Self::is_order_id_in_client_partition(order_id, client_id)
902                {
903                    highest_order_id = highest_order_id.max(Some(order_id));
904                }
905            }
906
907            Ok(highest_order_id)
908        };
909
910        tokio::time::timeout_at(deadline, read_snapshot)
911            .await
912            .context("Timeout reading completed orders for next order ID initialization")?
913            .context("Failed to read completed orders for next order ID initialization")
914    }
915
916    fn begin_task_shutdown(&self) {
917        self.pending_tasks.begin_shutdown();
918        self.session_tasks.begin_shutdown();
919        self.is_connected.store(false, Ordering::Release);
920        self.core.set_disconnected();
921    }
922
923    async fn finish_tasks(&self) -> anyhow::Result<()> {
924        let (session_result, pending_result) = tokio::join!(
925            self.session_tasks
926                .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2)),
927            self.pending_tasks
928                .finish_shutdown(Duration::from_secs(2), Duration::from_secs(2)),
929        );
930        session_result.context("failed to finish IB execution session tasks")?;
931        pending_result.context("failed to finish IB execution command tasks")?;
932        Ok(())
933    }
934
935    async fn prepare_task_groups(&mut self) -> anyhow::Result<()> {
936        if !self.session_tasks.is_open() || !self.pending_tasks.is_open() {
937            self.begin_task_shutdown();
938            self.finish_tasks().await?;
939            self.ib_client = None;
940            self.session_tasks
941                .start_generation()
942                .context("failed to start IB execution session task generation")?;
943            self.pending_tasks
944                .start_generation()
945                .context("failed to start IB execution command task generation")?;
946        }
947
948        Ok(())
949    }
950
951    async fn teardown_partial_connect(&mut self) -> anyhow::Result<()> {
952        self.begin_task_shutdown();
953        self.ib_client = None;
954        let tasks_result = self.finish_tasks().await;
955        self.is_connected.store(false, Ordering::Release);
956        self.core.set_disconnected();
957        tasks_result
958    }
959}
960
961// Implementation of ExecutionClient trait
962#[async_trait::async_trait(?Send)]
963impl ExecutionClient for InteractiveBrokersExecutionClient {
964    fn is_connected(&self) -> bool {
965        self.is_connected.load(Ordering::Relaxed)
966    }
967
968    fn client_id(&self) -> ClientId {
969        self.core.client_id
970    }
971
972    fn account_id(&self) -> AccountId {
973        self.core.account_id
974    }
975
976    fn venue(&self) -> Venue {
977        self.core.venue
978    }
979
980    // IB uses a broker venue for the client while routing exchange-MIC instruments;
981    // contract transformation remains the authority for actual venue support.
982    fn handles_order_venue(&self, _venue: Venue) -> bool {
983        true
984    }
985
986    fn oms_type(&self) -> OmsType {
987        self.core.oms_type
988    }
989
990    fn get_account(&self) -> Option<AccountAny> {
991        self.core.cache().account_owned(&self.core.account_id)
992    }
993
994    fn generate_account_state(
995        &self,
996        balances: Vec<AccountBalance>,
997        margins: Vec<MarginBalance>,
998        reported: bool,
999        ts_event: UnixNanos,
1000        info: Option<Params>,
1001    ) -> anyhow::Result<()> {
1002        let factory = OrderEventFactory::new(
1003            self.core.trader_id,
1004            self.core.account_id,
1005            self.core.account_type,
1006            self.core.base_currency,
1007        );
1008        let state = factory.generate_account_state(
1009            balances,
1010            margins,
1011            reported,
1012            ts_event,
1013            get_atomic_clock_realtime().get_time_ns(),
1014            info,
1015        );
1016        get_exec_event_sender()
1017            .send(ExecutionEvent::Account(state))
1018            .map_err(|e| anyhow::anyhow!("Failed to send account state: {e}"))
1019    }
1020
1021    fn start(&mut self) -> anyhow::Result<()> {
1022        // Start is handled by connect() for live clients
1023        Ok(())
1024    }
1025
1026    fn stop(&mut self) -> anyhow::Result<()> {
1027        self.begin_task_shutdown();
1028        Ok(())
1029    }
1030
1031    fn submit_order(&self, cmd: SubmitOrder) -> anyhow::Result<()> {
1032        let order = self.core.get_order(&cmd.client_order_id)?;
1033        if let Err(reason) = validate_order(&order) {
1034            let reason = reason.to_string();
1035            Self::send_order_denied(
1036                cmd.order_init.trader_id,
1037                cmd.strategy_id,
1038                cmd.instrument_id,
1039                cmd.order_init.client_order_id,
1040                &reason,
1041            )?;
1042            return Ok(());
1043        }
1044
1045        if let Err(reason) = self.ensure_client_ready_for_order_request("submit order") {
1046            self.deny_submit_order_not_ready(&cmd, &reason)?;
1047            return Ok(());
1048        }
1049
1050        self.seed_instruments_from_cache([cmd.instrument_id]);
1051        self.seed_price_magnifiers_from_cache([cmd.instrument_id]);
1052
1053        let client = self.ib_client.as_ref().context("IB client not connected")?;
1054        let client_order_id = cmd.order_init.client_order_id;
1055        Self::register_awaiting_submission(
1056            client_order_id,
1057            AwaitingSubmission {
1058                trader_id: cmd.order_init.trader_id,
1059                strategy_id: cmd.strategy_id,
1060                instrument_id: cmd.instrument_id,
1061                order_side: cmd.order_init.order_side,
1062            },
1063            &self.orders,
1064        )?;
1065
1066        let orders = self.orders.clone();
1067        let next_order_id = Arc::clone(&self.next_order_id);
1068        let instrument_provider = Arc::clone(&self.instrument_provider);
1069        let exec_sender = get_exec_event_sender();
1070        let clock = get_atomic_clock_realtime();
1071        let order_submit_lock = Arc::clone(&self.order_submit_lock);
1072
1073        let client_clone = client.as_arc().clone();
1074
1075        let account_id = self.core.account_id;
1076        let ib_account = self.ib_account;
1077        let request_timeout_secs = self.config.request_timeout;
1078
1079        let future = async move {
1080            if let Err(e) = Self::handle_submit_order_async(
1081                &cmd,
1082                &client_clone,
1083                &orders,
1084                &next_order_id,
1085                &instrument_provider,
1086                &exec_sender,
1087                clock,
1088                account_id,
1089                ib_account,
1090                &order_submit_lock,
1091                request_timeout_secs,
1092            )
1093            .await
1094            {
1095                tracing::error!("Error submitting order: {e}");
1096            }
1097        };
1098
1099        if let Err(e) = self.pending_tasks.spawn(future) {
1100            self.orders
1101                .lock()?
1102                .take_awaiting_submission(client_order_id);
1103            return Err(e).context("failed to register IB execution command task");
1104        }
1105
1106        Ok(())
1107    }
1108
1109    async fn connect(&mut self) -> anyhow::Result<()> {
1110        if self.is_connected.load(Ordering::Relaxed)
1111            && self.session_tasks.is_open()
1112            && self.pending_tasks.is_open()
1113        {
1114            log::debug!("Interactive Brokers execution client already connected");
1115            return Ok(());
1116        }
1117
1118        self.prepare_task_groups().await?;
1119        let setup_guard =
1120            TaskGroupGuard::new(&[&self.session_tasks, &self.pending_tasks], move || {});
1121
1122        tracing::info!("Connecting Interactive Brokers execution client...");
1123
1124        log::debug!(
1125            "Execution client config host={} port={} client_id={} account_id={:?} request_timeout={} connection_timeout={} fetch_all_open_orders={} track_option_exercise_from_position_update={}",
1126            self.config.host,
1127            self.config.port,
1128            self.config.client_id,
1129            self.config.account_id,
1130            self.config.request_timeout,
1131            self.config.connection_timeout,
1132            self.config.fetch_all_open_orders,
1133            self.config.track_option_exercise_from_position_update
1134        );
1135
1136        let handle = get_or_connect(
1137            &self.config.host,
1138            self.config.port,
1139            self.config.client_id,
1140            self.config.connection_timeout,
1141            self.config.max_reconnect_attempts,
1142        )
1143        .await
1144        .context("Failed to connect to IB Gateway/TWS")?;
1145        let client = Arc::clone(handle.as_arc());
1146
1147        tracing::info!(
1148            "Connected to IB Gateway/TWS at {}:{} (client_id: {})",
1149            self.config.host,
1150            self.config.port,
1151            self.config.client_id
1152        );
1153
1154        // Initialize provider and load instruments from cache/config if configured
1155        log::debug!("Initializing IB execution instrument provider");
1156
1157        if let Err(e) = self
1158            .instrument_provider
1159            .initialize_with_client(client.as_ref())
1160            .await
1161        {
1162            if !self.config.instrument_provider.load_ids.is_empty()
1163                || !self.config.instrument_provider.load_contracts.is_empty()
1164            {
1165                return Err(e).context("Failed to load configured IB instruments on startup");
1166            }
1167
1168            tracing::warn!("Failed to load instruments on startup: {}", e);
1169        }
1170
1171        self.ib_client = Some(handle);
1172
1173        let session_result = async {
1174
1175        log::debug!("Preloading cached instruments for execution client");
1176        self.preload_cached_instruments(client.as_ref()).await;
1177
1178        // Get initial next order ID (uses self.ib_client internally)
1179        log::debug!("Requesting next valid IB order ID");
1180        let next_id = self.get_next_order_id().await?;
1181        log::debug!("Requesting highest open IB order ID");
1182        let highest_open_order_id = self.get_highest_open_order_id(client.as_ref()).await?;
1183        log::debug!("Requesting highest completed IB order ID");
1184        let highest_completed_order_id =
1185            self.get_highest_completed_order_id(client.as_ref()).await?;
1186
1187        let starting_order_id = Self::initial_order_id(
1188            next_id,
1189            self.config.client_id,
1190            highest_open_order_id.max(highest_completed_order_id),
1191        )?;
1192
1193        if starting_order_id == next_id {
1194            tracing::debug!(
1195                "Initialized next Interactive Brokers order ID to {}",
1196                starting_order_id
1197            );
1198        } else {
1199            tracing::debug!(
1200                "Adjusted next Interactive Brokers order ID from {} to {} based on client ID/open and completed orders",
1201                next_id,
1202                starting_order_id
1203            );
1204        }
1205
1206        {
1207            let mut id = self
1208                .next_order_id
1209                .lock();
1210            *id = starting_order_id;
1211        }
1212
1213        // Start order update subscription (uses self.ib_client internally)
1214        log::debug!("Starting IB order update stream");
1215        self.start_order_updates().await?;
1216
1217        // Subscribe to account summary and keep it open so the account state follows the broker
1218        // Wait for the initial account summary to load before proceeding
1219        let account_id = self.core.account_id;
1220        log::debug!("Subscribing to IB account summary for {account_id}");
1221
1222        let send_account_state = self.account_state_sender();
1223
1224        if let Err(e) = stream_account_summary(
1225            &client,
1226            self.ib_account,
1227            Duration::from_secs(self.config.request_timeout),
1228            &self.session_tasks,
1229            send_account_state,
1230        )
1231        .await
1232        {
1233            tracing::warn!("Failed to subscribe to account summary: {}", e);
1234        }
1235
1236        // Initialize position tracking with existing positions
1237        // This avoids processing duplicates from execDetails
1238        let client_for_positions_init = Arc::clone(&client);
1239        let position_tracker_init = Arc::clone(&self.position_tracker);
1240
1241        log::debug!("Initializing IB execution position tracking");
1242
1243        match initialize_position_tracking(
1244            &client_for_positions_init,
1245            self.ib_account,
1246            position_tracker_init,
1247        )
1248        .await
1249        {
1250            Ok(contracts) => {
1251                self.publish_position_instruments(client.as_ref(), contracts)
1252                    .await;
1253            }
1254            Err(e) => tracing::warn!("Failed to initialize position tracking: {}", e),
1255        }
1256
1257        self.publish_combo_order_instruments(client.as_ref()).await;
1258
1259        // Subscribe to PnL updates
1260        let client_for_pnl = Arc::clone(&client); // Clone Arc
1261
1262        log::debug!("Subscribing to IB PnL updates");
1263
1264        if let Err(e) = crate::execution::account::subscribe_pnl(
1265            &client_for_pnl,
1266            self.ib_account,
1267            &self.session_tasks,
1268        )
1269        .await
1270        {
1271            tracing::warn!("Failed to subscribe to PnL: {}", e);
1272        }
1273
1274        // Subscribe to position updates for option exercise tracking if enabled
1275        if self.config.track_option_exercise_from_position_update {
1276            let client_for_positions = Arc::clone(&client);
1277            let position_tracker_clone = Arc::clone(&self.position_tracker);
1278            let instrument_provider_clone = Arc::clone(&self.instrument_provider);
1279
1280            log::debug!("Subscribing to IB position updates for option exercise tracking");
1281
1282            if let Err(e) = subscribe_positions(
1283                &client_for_positions,
1284                self.core.account_id,
1285                self.ib_account,
1286                position_tracker_clone,
1287                instrument_provider_clone,
1288                &self.session_tasks,
1289            )
1290            .await
1291            {
1292                tracing::warn!("Failed to subscribe to positions: {}", e);
1293            }
1294        }
1295
1296        Ok::<(), anyhow::Error>(())
1297        }
1298        .await;
1299
1300        if let Err(e) = session_result {
1301            if let Err(teardown_error) = self.teardown_partial_connect().await {
1302                return Err(e.context(format!(
1303                    "IB execution startup teardown failed: {teardown_error}"
1304                )));
1305            }
1306
1307            return Err(e);
1308        }
1309
1310        self.is_connected.store(true, Ordering::Relaxed);
1311        self.core.set_connected();
1312        setup_guard.disarm();
1313
1314        tracing::info!("Connected Interactive Brokers execution client");
1315        Ok(())
1316    }
1317
1318    async fn disconnect(&mut self) -> anyhow::Result<()> {
1319        if !self.is_connected.load(Ordering::Relaxed)
1320            && self.ib_client.is_none()
1321            && self.session_tasks.is_open()
1322            && self.session_tasks.is_empty()
1323            && self.pending_tasks.is_open()
1324            && self.pending_tasks.is_empty()
1325        {
1326            log::debug!("Interactive Brokers execution client already disconnected");
1327            return Ok(());
1328        }
1329
1330        tracing::info!("Disconnecting Interactive Brokers execution client...");
1331
1332        self.teardown_partial_connect().await?;
1333
1334        tracing::info!("Disconnected Interactive Brokers execution client");
1335        Ok(())
1336    }
1337
1338    fn generate_order_status_report_task(
1339        &self,
1340        cmd: &GenerateOrderStatusReport,
1341    ) -> Option<ExecutionReportTask<Option<OrderStatusReport>>> {
1342        let report_client = self.report_client().ok()?;
1343        let command = cmd.clone();
1344        Some(ExecutionReportTask::new(
1345            async move { report_client.generate_order_status_report(&command).await },
1346            Ok,
1347        ))
1348    }
1349
1350    async fn generate_order_status_report(
1351        &self,
1352        cmd: &GenerateOrderStatusReport,
1353    ) -> anyhow::Result<Option<OrderStatusReport>> {
1354        self.report_client()?
1355            .generate_order_status_report(cmd)
1356            .await
1357    }
1358
1359    fn generate_order_status_reports_task(
1360        &self,
1361        cmd: &GenerateOrderStatusReports,
1362    ) -> Option<ExecutionReportTask<Vec<OrderStatusReport>>> {
1363        let report_client = self.report_client().ok()?;
1364        let command = cmd.clone();
1365        Some(ExecutionReportTask::new(
1366            async move { report_client.generate_order_status_reports(&command).await },
1367            Ok,
1368        ))
1369    }
1370
1371    async fn generate_order_status_reports(
1372        &self,
1373        cmd: &GenerateOrderStatusReports,
1374    ) -> anyhow::Result<Vec<OrderStatusReport>> {
1375        self.report_client()?
1376            .generate_order_status_reports(cmd)
1377            .await
1378    }
1379
1380    fn generate_fill_reports_task(
1381        &self,
1382        cmd: &GenerateFillReports,
1383    ) -> Option<ExecutionReportTask<Vec<FillReport>>> {
1384        let report_client = self.report_client().ok()?;
1385        let command = cmd.clone();
1386        Some(ExecutionReportTask::new(
1387            async move { report_client.generate_fill_reports(command).await },
1388            Ok,
1389        ))
1390    }
1391
1392    async fn generate_fill_reports(
1393        &self,
1394        cmd: GenerateFillReports,
1395    ) -> anyhow::Result<Vec<FillReport>> {
1396        self.report_client()?.generate_fill_reports(cmd).await
1397    }
1398
1399    fn generate_position_status_reports_task(
1400        &self,
1401        cmd: &GeneratePositionStatusReports,
1402    ) -> Option<ExecutionReportTask<Vec<PositionStatusReport>>> {
1403        let report_client = self.report_client().ok()?;
1404        let command = cmd.clone();
1405
1406        Some(ExecutionReportTask::new(
1407            async move {
1408                report_client
1409                    .generate_position_status_reports(&command)
1410                    .await
1411            },
1412            Ok,
1413        ))
1414    }
1415
1416    async fn generate_position_status_reports(
1417        &self,
1418        cmd: &GeneratePositionStatusReports,
1419    ) -> anyhow::Result<Vec<PositionStatusReport>> {
1420        self.report_client()?
1421            .generate_position_status_reports(cmd)
1422            .await
1423    }
1424
1425    async fn generate_mass_status(
1426        &self,
1427        lookback_mins: Option<u64>,
1428    ) -> anyhow::Result<Option<ExecutionMassStatus>> {
1429        let ts_now = get_atomic_clock_realtime().get_time_ns();
1430        let start = lookback_mins
1431            .map(DurationNanos::try_from_mins)
1432            .transpose()?
1433            .map(|lookback| ts_now.saturating_sub(lookback));
1434
1435        let order_cmd = GenerateOrderStatusReportsBuilder::default()
1436            .ts_init(ts_now)
1437            .open_only(false)
1438            .start(start)
1439            .build()
1440            .map_err(|e| anyhow::anyhow!("{e}"))?;
1441
1442        let fill_cmd = GenerateFillReportsBuilder::default()
1443            .ts_init(ts_now)
1444            .start(start)
1445            .build()
1446            .map_err(|e| anyhow::anyhow!("{e}"))?;
1447
1448        let position_cmd = GeneratePositionStatusReportsBuilder::default()
1449            .ts_init(ts_now)
1450            .start(start)
1451            .build()
1452            .map_err(|e| anyhow::anyhow!("{e}"))?;
1453
1454        let report_client = self.report_client()?;
1455        let (mut order_reports, fill_evidence, mut position_reports) = tokio::try_join!(
1456            self.generate_order_status_reports(&order_cmd),
1457            report_client.generate_fill_evidence(fill_cmd),
1458            self.generate_position_status_reports(&position_cmd),
1459        )?;
1460
1461        tracing::info!(
1462            "generate_mass_status: {} order reports, {} fill reports, {} position reports",
1463            order_reports.len(),
1464            fill_evidence.reports.len(),
1465            position_reports.len()
1466        );
1467
1468        let mut mass_status = ExecutionMassStatus::new(
1469            self.core.client_id,
1470            self.core.account_id,
1471            self.core.venue,
1472            ts_now,
1473            Some(UUID4::new()),
1474        );
1475        // IB `reqExecutions` and completed orders are session-scoped, so without a
1476        // configured lookback the actual venue bound is unknown and the report set
1477        // cannot be claimed complete.
1478        mass_status.set_report_window(start, start.is_some());
1479
1480        parse::fill_missing_avg_px(&mut order_reports, &fill_evidence.reports);
1481        parse::fill_position_avg_px_from_fills(
1482            &mut position_reports,
1483            &fill_evidence,
1484            &self.instrument_provider,
1485        );
1486        let fill_reports = fill_evidence.reports;
1487        mass_status.add_order_reports(order_reports);
1488        mass_status.add_fill_reports(fill_reports);
1489        mass_status.add_position_reports(position_reports);
1490
1491        Ok(Some(mass_status))
1492    }
1493
1494    fn query_account(&self, _cmd: QueryAccount) -> anyhow::Result<()> {
1495        let client = self.ib_client.as_ref().context("IB client not connected")?;
1496
1497        let client_clone = client.as_arc().clone();
1498        let ib_account = self.ib_account;
1499        let request_timeout_secs = self.config.request_timeout;
1500        let send_account_state = self.account_state_sender();
1501
1502        let future = async move {
1503            let timeout_dur = Duration::from_secs(request_timeout_secs);
1504            let result = tokio::time::timeout(
1505                timeout_dur,
1506                subscribe_account_summary(&client_clone, ib_account),
1507            )
1508            .await;
1509
1510            match result {
1511                Ok(Ok(snapshot)) => send_account_state(snapshot),
1512                Ok(Err(e)) => {
1513                    tracing::error!("Failed to query account state: {e}");
1514                }
1515                Err(_) => {
1516                    tracing::error!("Timeout waiting for account summary");
1517                }
1518            }
1519        };
1520
1521        self.pending_tasks
1522            .spawn(future)
1523            .context("failed to register IB execution command task")?;
1524
1525        Ok(())
1526    }
1527
1528    fn query_order(&self, cmd: QueryOrder) -> anyhow::Result<()> {
1529        let client = self.ib_client.as_ref().context("IB client not connected")?;
1530        let client_order_id = cmd.client_order_id;
1531        let trader_id = cmd.trader_id;
1532        let strategy_id = cmd.strategy_id;
1533        let instrument_id = cmd.instrument_id;
1534
1535        let target_order = if let Some(venue_order_id) = &cmd.venue_order_id {
1536            IbOrderSelector::from_venue_order_id(venue_order_id)?
1537        } else {
1538            let state = self.orders.lock()?;
1539            IbOrderSelector::OrderId(
1540                *state
1541                    .order_id_map
1542                    .get(&cmd.client_order_id)
1543                    .context("No venue order id for client_order_id")?,
1544            )
1545        };
1546
1547        let order_quantity = self
1548            .core
1549            .cache()
1550            .order(&cmd.client_order_id)
1551            .map(|order| order.quantity().as_decimal());
1552
1553        let client_clone = client.as_arc().clone();
1554        let orders = self.orders.clone();
1555        let instrument_provider = Arc::clone(&self.instrument_provider);
1556        let account_id = self.core.account_id;
1557        let exec_sender = get_exec_event_sender();
1558        let ts_init = get_atomic_clock_realtime().get_time_ns();
1559        let request_timeout_secs = self.config.request_timeout;
1560        let ib_account = self.ib_account;
1561
1562        let future = async move {
1563            let timeout_dur = Duration::from_secs(request_timeout_secs);
1564
1565            let subscription =
1566                match tokio::time::timeout(timeout_dur, client_clone.all_open_orders()).await {
1567                    Ok(Ok(s)) => s,
1568                    Ok(Err(e)) => {
1569                        tracing::warn!("query_order: failed to request open orders: {e}");
1570                        return;
1571                    }
1572                    Err(_) => {
1573                        tracing::warn!("query_order: timeout requesting open orders");
1574                        return;
1575                    }
1576                };
1577
1578            let mut subscription = subscription.filter_data();
1579
1580            while let Some(order_result) = subscription.next().await {
1581                if let Ok(Orders::OrderData(data)) = order_result {
1582                    if !data.order.account.is_empty() && data.order.account != ib_account {
1583                        continue;
1584                    }
1585
1586                    if !target_order.matches(data.order_id, data.order.perm_id) {
1587                        continue;
1588                    }
1589
1590                    let instrument_id = match orders.lock() {
1591                        Ok(state) => state.order(data.order_id).map(|order| order.instrument_id),
1592                        Err(_) => None,
1593                    };
1594
1595                    let instrument_id = match instrument_id {
1596                        Some(id) => id,
1597                        None => match instrument_provider
1598                            .resolve_instrument_id_for_contract(&data.contract)
1599                        {
1600                            Ok(id) => id,
1601                            Err(e) => {
1602                                tracing::warn!("query_order: failed to convert contract: {e}");
1603                                return;
1604                            }
1605                        },
1606                    };
1607
1608                    let report = match parse_order_data_to_report(
1609                        &data,
1610                        instrument_id,
1611                        account_id,
1612                        &instrument_provider,
1613                        ts_init,
1614                    ) {
1615                        Ok(r) => r,
1616                        Err(e) => {
1617                            tracing::warn!("query_order: failed to parse order status: {e}");
1618                            return;
1619                        }
1620                    };
1621
1622                    if exec_sender
1623                        .send(ExecutionEvent::Report(ExecutionReport::Order(Box::new(
1624                            report,
1625                        ))))
1626                        .is_err()
1627                    {
1628                        tracing::error!("query_order: failed to send order status report");
1629                    }
1630
1631                    return;
1632                }
1633            }
1634
1635            let is_pending_cancel = orders.lock().is_ok_and(|state| {
1636                state
1637                    .order_by_client(client_order_id)
1638                    .is_some_and(|order| order.pending_cancel)
1639            });
1640
1641            if is_pending_cancel {
1642                let filter = Self::execution_filter(ib_account, None);
1643
1644                let executions = match tokio::time::timeout(
1645                    timeout_dur,
1646                    client_clone.executions(filter),
1647                )
1648                .await
1649                {
1650                    Ok(Ok(subscription)) => subscription,
1651                    Ok(Err(e)) => {
1652                        tracing::error!(
1653                            "query_order: failed to request executions before inferring cancel: {e}"
1654                        );
1655                        return;
1656                    }
1657                    Err(_) => {
1658                        tracing::error!(
1659                            "query_order: timeout requesting executions before inferring cancel"
1660                        );
1661                        return;
1662                    }
1663                };
1664
1665                let mut executions = executions.filter_data();
1666                let mut execution_data = AHashMap::new();
1667                let mut commissions = AHashMap::new();
1668
1669                while let Some(result) = executions.next().await {
1670                    match result {
1671                        Ok(Executions::ExecutionData(data))
1672                            if target_order
1673                                .matches(data.execution.order_id, data.execution.perm_id) =>
1674                        {
1675                            execution_data.insert(data.execution.execution_id.clone(), data);
1676                        }
1677                        Ok(Executions::CommissionReport(commission)) => {
1678                            commissions.insert(
1679                                commission.execution_id,
1680                                (commission.commission, commission.currency),
1681                            );
1682                        }
1683                        Ok(_) => {}
1684                        Err(e) => {
1685                            tracing::error!(
1686                                "query_order: execution query failed before cancel resolution: {e}"
1687                            );
1688                            return;
1689                        }
1690                    }
1691                }
1692
1693                if !execution_data.is_empty() {
1694                    let cumulative_filled = execution_data
1695                        .values()
1696                        .map(|data| data.execution.cumulative_quantity)
1697                        .fold(0.0_f64, f64::max);
1698
1699                    for (execution_id, data) in execution_data {
1700                        let Some((commission, currency)) = commissions.remove(&execution_id) else {
1701                            tracing::error!(
1702                                "query_order: execution {execution_id} has no commission report; cancel remains unresolved"
1703                            );
1704                            return;
1705                        };
1706
1707                        let report = match parse_execution_to_fill_report(
1708                            &data.execution,
1709                            &data.contract,
1710                            commission,
1711                            &currency,
1712                            instrument_id,
1713                            account_id,
1714                            &instrument_provider,
1715                            ts_init,
1716                            None,
1717                        ) {
1718                            Ok(report) => report,
1719                            Err(e) => {
1720                                tracing::error!(
1721                                    "query_order: failed to parse execution {execution_id}: {e}"
1722                                );
1723                                return;
1724                            }
1725                        };
1726
1727                        let context =
1728                            Self::get_tracked_order_context(data.execution.order_id, &orders)
1729                                .ok()
1730                                .flatten();
1731
1732                        let event = if let Some(context) = context {
1733                            let quote_currency = instrument_provider
1734                                .find(&context.instrument_id)
1735                                .map_or(report.commission.currency, |instrument| {
1736                                    instrument.quote_currency()
1737                                });
1738
1739                            ExecutionEvent::Order(OrderEventAny::Filled(OrderFilled::new(
1740                                context.trader_id,
1741                                context.strategy_id,
1742                                context.instrument_id,
1743                                context.client_order_id,
1744                                report.venue_order_id,
1745                                report.account_id,
1746                                report.trade_id,
1747                                context.order_side,
1748                                context.order_type,
1749                                report.last_qty,
1750                                report.last_px,
1751                                quote_currency,
1752                                report.liquidity_side,
1753                                UUID4::new(),
1754                                report.ts_event,
1755                                report.ts_init,
1756                                false,
1757                                report.venue_position_id,
1758                                Some(report.commission),
1759                                None,
1760                            )))
1761                        } else {
1762                            ExecutionEvent::Report(ExecutionReport::Fill(Box::new(report)))
1763                        };
1764
1765                        if exec_sender.send(event).is_err() {
1766                            tracing::error!(
1767                                "query_order: failed to send fill resolved from execution history"
1768                            );
1769                            return;
1770                        }
1771                    }
1772
1773                    if let Ok(mut state) = orders.lock()
1774                        && let Some(order) = state.order_by_client_mut(client_order_id)
1775                    {
1776                        order.pending_cancel = false;
1777                    }
1778
1779                    // The order is absent from open orders, so any unfilled remainder
1780                    // was cancelled at the venue. Quantize to the instrument's size
1781                    // precision so float dust from the f64 cumulative quantity cannot
1782                    // register as a phantom remainder after a full fill.
1783                    let size_precision = instrument_provider
1784                        .find(&instrument_id)
1785                        .map(|instrument| u32::from(instrument.size_precision()));
1786
1787                    let remainder = order_quantity.and_then(|total| {
1788                        Decimal::from_f64_retain(cumulative_filled).map(|filled| {
1789                            let remainder = total - filled;
1790                            size_precision.map_or(remainder, |dp| remainder.round_dp(dp))
1791                        })
1792                    });
1793
1794                    match remainder {
1795                        Some(remainder) if remainder > Decimal::ZERO => {
1796                            Self::send_inferred_order_canceled(
1797                                trader_id,
1798                                strategy_id,
1799                                instrument_id,
1800                                client_order_id,
1801                                target_order.venue_order_id(),
1802                                account_id,
1803                                ts_init,
1804                                &exec_sender,
1805                                "cancelled remainder after partial-fill recovery",
1806                            );
1807                        }
1808                        Some(_) => {}
1809                        None => {
1810                            tracing::debug!(
1811                                "query_order: cannot determine cancelled remainder for {} (order not in cache)",
1812                                client_order_id
1813                            );
1814                        }
1815                    }
1816
1817                    return;
1818                }
1819            }
1820
1821            let was_pending_cancel = orders.lock().is_ok_and(|mut state| {
1822                state
1823                    .order_by_client_mut(client_order_id)
1824                    .is_some_and(|order| std::mem::replace(&mut order.pending_cancel, false))
1825            });
1826
1827            if was_pending_cancel {
1828                Self::send_inferred_order_canceled(
1829                    trader_id,
1830                    strategy_id,
1831                    instrument_id,
1832                    client_order_id,
1833                    target_order.venue_order_id(),
1834                    account_id,
1835                    ts_init,
1836                    &exec_sender,
1837                    "missing open order",
1838                );
1839                return;
1840            }
1841
1842            tracing::debug!(
1843                "query_order: order {} not found in open orders (may be filled or canceled)",
1844                target_order.label()
1845            );
1846        };
1847
1848        self.pending_tasks
1849            .spawn(future)
1850            .context("failed to register IB execution command task")?;
1851
1852        Ok(())
1853    }
1854
1855    fn submit_order_list(&self, cmd: SubmitOrderList) -> anyhow::Result<()> {
1856        let orders = self.core.get_orders_for_list(&cmd.order_list)?;
1857        if let Some(reason) = orders.iter().find_map(|order| validate_order(order).err()) {
1858            self.deny_submit_order_list(&cmd, &reason.to_string())?;
1859            return Ok(());
1860        }
1861
1862        if let Err(reason) = self.ensure_client_ready_for_order_request("submit order list") {
1863            let reason = coded_denial_reason(DENIAL_CLIENT_NOT_READY, &reason);
1864            self.deny_submit_order_list(&cmd, &reason)?;
1865            return Ok(());
1866        }
1867
1868        self.submit_order_list_with_orders(cmd, orders)
1869    }
1870
1871    fn modify_order(&self, cmd: ModifyOrder) -> anyhow::Result<()> {
1872        if self
1873            .orders
1874            .lock()?
1875            .group_for_client(cmd.client_order_id)
1876            .is_some()
1877        {
1878            Self::send_order_modify_denied(
1879                &cmd,
1880                "Cannot modify an order with unresolved duplicate broker identities; cancel and reconcile it first",
1881                &get_exec_event_sender(),
1882                get_atomic_clock_realtime().get_time_ns(),
1883                self.core.account_id,
1884            )?;
1885            return Ok(());
1886        }
1887
1888        if let Err(reason) = self.ensure_client_ready_for_order_request("modify order") {
1889            Self::send_order_modify_denied(
1890                &cmd,
1891                &reason,
1892                &get_exec_event_sender(),
1893                get_atomic_clock_realtime().get_time_ns(),
1894                self.core.account_id,
1895            )?;
1896            return Ok(());
1897        }
1898
1899        self.seed_instruments_from_cache([cmd.instrument_id]);
1900        self.seed_price_magnifiers_from_cache([cmd.instrument_id]);
1901
1902        let client = self.ib_client.as_ref().context("IB client not connected")?;
1903
1904        let orders = self.orders.clone();
1905        let instrument_provider = Arc::clone(&self.instrument_provider);
1906        let exec_sender = get_exec_event_sender();
1907        let clock = get_atomic_clock_realtime();
1908        let account_id = self.core.account_id;
1909        let ib_account = self.ib_account;
1910        let client_clone = client.as_arc().clone();
1911        let request_timeout_secs = self.config.request_timeout;
1912
1913        let future = async move {
1914            if let Err(e) = Self::handle_modify_order_async(
1915                &cmd,
1916                &client_clone,
1917                &orders,
1918                &instrument_provider,
1919                ib_account,
1920                request_timeout_secs,
1921            )
1922            .await
1923            {
1924                let reason = format!("Failed to route modify order to IB: {e:#}");
1925
1926                if let Err(send_error) = Self::send_order_modify_denied(
1927                    &cmd,
1928                    &reason,
1929                    &exec_sender,
1930                    clock.get_time_ns(),
1931                    account_id,
1932                ) {
1933                    tracing::error!("{reason}; failed to emit OrderModifyDenied: {send_error}");
1934                }
1935            }
1936        };
1937
1938        self.pending_tasks
1939            .spawn(future)
1940            .context("failed to register IB execution command task")?;
1941
1942        Ok(())
1943    }
1944
1945    fn cancel_order(&self, cmd: CancelOrder) -> anyhow::Result<()> {
1946        let target_order = self.core.get_order(&cmd.client_order_id);
1947        let exec_sender = get_exec_event_sender();
1948        let clock = get_atomic_clock_realtime();
1949        let account_id = self.core.account_id;
1950
1951        let target_order = match target_order {
1952            Ok(order) => Arc::new(order),
1953            Err(e) => {
1954                return Self::send_order_cancel_denied(
1955                    None,
1956                    &cmd,
1957                    &e.to_string(),
1958                    &exec_sender,
1959                    clock.get_time_ns(),
1960                    account_id,
1961                );
1962            }
1963        };
1964
1965        if let Err(e) = Self::validate_cancel_order_target(&cmd, &target_order) {
1966            let reason = format!("Failed to resolve cancel order target: {e:#}");
1967            Self::send_order_cancel_denied(
1968                Some(&target_order),
1969                &cmd,
1970                &reason,
1971                &exec_sender,
1972                clock.get_time_ns(),
1973                account_id,
1974            )?;
1975            return Ok(());
1976        }
1977
1978        // An order still awaiting submission (its price magnifier loading, say) has not reached
1979        // IB, so the cancel completes here and its submission stops before IB
1980        let awaiting = self
1981            .orders
1982            .lock()?
1983            .take_awaiting_submission(cmd.client_order_id);
1984
1985        if let Some(submission) = awaiting {
1986            return Self::send_awaiting_order_canceled(
1987                cmd.client_order_id,
1988                submission,
1989                account_id,
1990                &exec_sender,
1991                clock.get_time_ns(),
1992            );
1993        }
1994
1995        if let Err(reason) = self.ensure_client_ready_for_order_request("cancel order") {
1996            Self::send_order_cancel_denied(
1997                Some(&target_order),
1998                &cmd,
1999                &reason,
2000                &exec_sender,
2001                clock.get_time_ns(),
2002                account_id,
2003            )?;
2004            return Ok(());
2005        }
2006
2007        let client = self.ib_client.as_ref().context("IB client not connected")?;
2008
2009        let orders = self.orders.clone();
2010        let instrument_provider = Arc::clone(&self.instrument_provider);
2011        let exec_sender = get_exec_event_sender();
2012        let clock = get_atomic_clock_realtime();
2013        let account_id = self.core.account_id;
2014        let ib_account = self.ib_account;
2015        let client_clone = client.as_arc().clone();
2016        let request_timeout_secs = self.config.request_timeout;
2017
2018        let future = async move {
2019            if let Err(e) = Self::handle_cancel_order_async(
2020                &cmd,
2021                &target_order,
2022                &client_clone,
2023                &orders,
2024                &instrument_provider,
2025                &exec_sender,
2026                clock.get_time_ns(),
2027                account_id,
2028                ib_account,
2029                request_timeout_secs,
2030            )
2031            .await
2032            {
2033                let reason = format!("Failed to route cancel order to IB: {e:#}");
2034
2035                tracing::warn!("{reason}; cancellation remains unresolved");
2036            }
2037        };
2038
2039        self.pending_tasks
2040            .spawn(future)
2041            .context("failed to register IB execution command task")?;
2042
2043        Ok(())
2044    }
2045
2046    fn cancel_all_orders(&self, cmd: CancelAllOrders) -> anyhow::Result<()> {
2047        // Orders still awaiting submission have not reached IB, so they are canceled here and
2048        // their submissions stop before IB
2049        let awaiting = self
2050            .orders
2051            .lock()?
2052            .take_awaiting_submissions(cmd.instrument_id, cmd.order_side);
2053
2054        if !awaiting.is_empty() {
2055            let exec_sender = get_exec_event_sender();
2056            let ts_event = get_atomic_clock_realtime().get_time_ns();
2057
2058            for (client_order_id, submission) in awaiting {
2059                Self::send_awaiting_order_canceled(
2060                    client_order_id,
2061                    submission,
2062                    self.core.account_id,
2063                    &exec_sender,
2064                    ts_event,
2065                )?;
2066            }
2067        }
2068
2069        if self
2070            .ensure_client_ready_for_order_request("cancel orders")
2071            .is_err()
2072        {
2073            return Ok(());
2074        }
2075
2076        let client = self.ib_client.as_ref().context("IB client not connected")?;
2077        let orders_to_cancel = self.cancel_all_targets(&cmd)?;
2078
2079        if orders_to_cancel.is_empty() {
2080            tracing::debug!("No open orders to cancel");
2081            return Ok(());
2082        }
2083
2084        tracing::debug!(
2085            "Canceling {} open order(s) for instrument {}",
2086            orders_to_cancel.len(),
2087            cmd.instrument_id
2088        );
2089
2090        let client_clone = client.as_arc().clone();
2091        let orders = self.orders.clone();
2092        let exec_sender = get_exec_event_sender();
2093        let clock = get_atomic_clock_realtime();
2094        let account_id = self.core.account_id;
2095        let ib_account = self.ib_account;
2096        let request_timeout_secs = self.config.request_timeout;
2097
2098        let provider = Arc::clone(&self.instrument_provider);
2099
2100        let future = async move {
2101            if let Err(e) = Self::handle_cancel_all_orders_async(
2102                &client_clone,
2103                &orders,
2104                &exec_sender,
2105                clock.get_time_ns(),
2106                account_id,
2107                ib_account,
2108                request_timeout_secs,
2109                orders_to_cancel,
2110                &provider,
2111                cmd.order_side,
2112            )
2113            .await
2114            {
2115                tracing::error!("Error canceling all orders: {e}");
2116            }
2117        };
2118
2119        self.pending_tasks
2120            .spawn(future)
2121            .context("failed to register IB execution command task")?;
2122
2123        Ok(())
2124    }
2125
2126    fn batch_cancel_orders(&self, cmd: BatchCancelOrders) -> anyhow::Result<()> {
2127        // Cancel each order in the batch
2128        for cancel_cmd in cmd.cancels {
2129            self.cancel_order(cancel_cmd)?;
2130        }
2131
2132        Ok(())
2133    }
2134
2135    // Reconciliation creates orders a previous session left working at IB; without tracking,
2136    // their IB status updates are dropped as untracked.
2137    fn register_external_order(
2138        &self,
2139        client_order_id: ClientOrderId,
2140        venue_order_id: VenueOrderId,
2141        _instrument_id: InstrumentId,
2142        _strategy_id: StrategyId,
2143        _ts_init: UnixNanos,
2144    ) {
2145        let order = match self.core.get_order(&client_order_id) {
2146            Ok(order) if order.is_open() => order,
2147            Ok(_) => return,
2148            Err(e) => {
2149                tracing::warn!("Cannot track external IB order {client_order_id}: {e}");
2150                return;
2151            }
2152        };
2153
2154        let order_selector = match IbOrderSelector::from_venue_order_id(&venue_order_id) {
2155            Ok(order_selector) => order_selector,
2156            Err(e) => {
2157                tracing::warn!("Cannot track external IB order {client_order_id}: {e}");
2158                return;
2159            }
2160        };
2161
2162        let Some(client) = self.ib_client.as_ref() else {
2163            tracing::warn!(
2164                "Cannot track external IB order {client_order_id}: IB client not connected"
2165            );
2166            return;
2167        };
2168
2169        let client = client.as_arc().clone();
2170        let orders = self.orders.clone();
2171        let ib_account = self.ib_account;
2172        let request_timeout_secs = self.config.request_timeout;
2173
2174        let future = async move {
2175            let result = Self::resolve_ib_order_id(
2176                &client,
2177                order_selector,
2178                ib_account,
2179                request_timeout_secs,
2180            )
2181            .await
2182            .and_then(|ib_order_id| {
2183                Self::cache_recovered_order_tracking(ib_order_id, &order, &orders)
2184            });
2185
2186            // A filled order awaiting its deferred fills is still open in the cache but no
2187            // longer listed by IB; cancels resolve `PERM-` IDs on demand either way
2188            if let Err(e) = result {
2189                tracing::debug!("Cannot track external IB order {client_order_id}: {e:#}");
2190            }
2191        };
2192
2193        if let Err(e) = self.pending_tasks.spawn(future) {
2194            tracing::warn!("Cannot track external IB order {client_order_id}: {e}");
2195        }
2196    }
2197}
2198
2199fn validate_order(order: &impl Order) -> Result<(), OrderDeniedReason> {
2200    if order.is_reduce_only() {
2201        return Err(OrderDeniedReason::UnsupportedReduceOnly);
2202    }
2203
2204    Ok(())
2205}
2206
2207impl InteractiveBrokersExecutionClient {
2208    // Captures the thread-local exec event sender here so tokio worker threads, which have no
2209    // sender or message bus of their own, can still deliver account states.
2210    fn account_state_sender(&self) -> impl Fn(AccountSnapshot) + Send + 'static {
2211        let factory = OrderEventFactory::new(
2212            self.core.trader_id,
2213            self.core.account_id,
2214            self.core.account_type,
2215            self.core.base_currency,
2216        );
2217        let base_currency = self.core.base_currency;
2218        let mismatch_logged = AtomicBool::new(false);
2219        let exec_sender = get_exec_event_sender();
2220        let clock = get_atomic_clock_realtime();
2221        move |(balances, margins, info)| {
2222            // IB reports the summary tags in the account's base currency, so a balance in
2223            // another currency means the configured base currency is not the account's
2224            if let Some(base_currency) = base_currency
2225                && let Some(balance) = balances.iter().find(|b| b.currency != base_currency)
2226                && !mismatch_logged.swap(true, Ordering::Relaxed)
2227            {
2228                tracing::error!(
2229                    "IB account summary reports a balance in {}, but the configured \
2230                     `base_currency` is {base_currency}",
2231                    balance.currency,
2232                );
2233            }
2234
2235            let ts_now = clock.get_time_ns();
2236            let state =
2237                factory.generate_account_state(balances, margins, true, ts_now, ts_now, info);
2238
2239            if let Err(e) = exec_sender.send(ExecutionEvent::Account(state)) {
2240                tracing::warn!("Failed to send account state: {e}");
2241            }
2242        }
2243    }
2244
2245    // Returns an error while disconnected, so the report hooks fall back to the inline path
2246    // that reports the same error.
2247    fn report_client(&self) -> anyhow::Result<IbReportClient> {
2248        let client = self.ib_client.as_ref().context("IB client not connected")?;
2249
2250        Ok(IbReportClient::new(
2251            client.as_arc().clone(),
2252            IbReportContext::new(Arc::clone(&self.instrument_provider), self.core.account_id),
2253            self.ib_account,
2254            Duration::from_secs(self.config.request_timeout),
2255        ))
2256    }
2257
2258    pub(super) fn execution_filter(ib_account: Ustr, start: Option<UnixNanos>) -> ExecutionFilter {
2259        let time = start.map_or_else(String::new, |start| {
2260            start
2261                .to_datetime_utc()
2262                .strftime("%Y%m%d-%H:%M:%S")
2263                .to_string()
2264        });
2265
2266        ExecutionFilter {
2267            client_id: None,
2268            account_code: ib_account.to_string(),
2269            time,
2270            symbol: String::new(),
2271            security_type: String::new(),
2272            exchange: String::new(),
2273            side: None,
2274            last_n_days: 0,
2275            specific_dates: Vec::new(),
2276        }
2277    }
2278
2279    fn is_ready_for_order_request(&self) -> bool {
2280        if !self.is_connected.load(Ordering::Relaxed) {
2281            return false;
2282        }
2283
2284        if !self
2285            .ib_client
2286            .as_ref()
2287            .is_some_and(|client| client.is_connected())
2288        {
2289            return false;
2290        }
2291
2292        *self.next_order_id.lock() > 0
2293    }
2294
2295    // Selects open orders for the instrument and account from the cache, the tracker, and
2296    // incarnation groups, keeping only the requested side when one is set
2297    fn cancel_all_targets(
2298        &self,
2299        cmd: &CancelAllOrders,
2300    ) -> anyhow::Result<Vec<(ClientOrderId, Option<VenueOrderId>)>> {
2301        let cache = self.core.cache();
2302        let mut selected: Vec<_> = cache
2303            .orders_open(
2304                None,
2305                Some(&cmd.instrument_id),
2306                None,
2307                Some(&self.core.account_id),
2308                cmd.order_side,
2309            )
2310            .iter()
2311            .map(|order| (order.client_order_id(), order.venue_order_id()))
2312            .collect();
2313        let state = self.orders.lock()?;
2314        selected.extend(state.active_orders.iter().filter_map(|(order_id, order)| {
2315            (order.instrument_id == cmd.instrument_id
2316                && cmd.order_side.is_none_or(|side| side == order.order_side))
2317            .then_some((
2318                order.client_order_id,
2319                Some(parse::ib_venue_order_id(*order_id, order.perm_id)),
2320            ))
2321        }));
2322
2323        selected.extend(
2324            state
2325                .group_cancel_candidates(cmd.instrument_id, self.core.account_id, cmd.order_side)
2326                .map(|parent| (parent, None)),
2327        );
2328        selected.sort_by_key(|(id, _)| id.to_string());
2329        selected.dedup_by_key(|(id, _)| *id);
2330        Ok(selected)
2331    }
2332
2333    fn ensure_client_ready_for_order_request(&self, request: &str) -> Result<(), String> {
2334        if self.is_ready_for_order_request() {
2335            return Ok(());
2336        }
2337
2338        let reason = format!("Interactive Brokers client is not ready; refusing to {request}");
2339        tracing::warn!("{reason}");
2340        Err(reason)
2341    }
2342
2343    fn deny_submit_order_not_ready(&self, cmd: &SubmitOrder, reason: &str) -> anyhow::Result<()> {
2344        let reason = coded_denial_reason(DENIAL_CLIENT_NOT_READY, reason);
2345        Self::send_order_denied(
2346            cmd.order_init.trader_id,
2347            cmd.strategy_id,
2348            cmd.instrument_id,
2349            cmd.order_init.client_order_id,
2350            &reason,
2351        )
2352    }
2353
2354    fn deny_submit_order_list(&self, cmd: &SubmitOrderList, reason: &str) -> anyhow::Result<()> {
2355        for order_init in &cmd.order_inits {
2356            Self::send_order_denied(
2357                order_init.trader_id,
2358                cmd.strategy_id,
2359                cmd.instrument_id,
2360                order_init.client_order_id,
2361                reason,
2362            )?;
2363        }
2364
2365        Ok(())
2366    }
2367
2368    #[allow(clippy::too_many_arguments)] // Event construction preserves the query context.
2369    fn send_inferred_order_canceled(
2370        trader_id: TraderId,
2371        strategy_id: StrategyId,
2372        instrument_id: InstrumentId,
2373        client_order_id: ClientOrderId,
2374        venue_order_id: VenueOrderId,
2375        account_id: AccountId,
2376        ts_init: UnixNanos,
2377        exec_sender: &EventSender<ExecutionEvent>,
2378        context: &str,
2379    ) {
2380        let event = OrderCanceled::new(
2381            trader_id,
2382            strategy_id,
2383            instrument_id,
2384            client_order_id,
2385            UUID4::new(),
2386            ts_init,
2387            ts_init,
2388            false,
2389            Some(venue_order_id),
2390            Some(account_id),
2391            None,
2392        );
2393
2394        if exec_sender
2395            .send(ExecutionEvent::Order(OrderEventAny::Canceled(event)))
2396            .is_err()
2397        {
2398            tracing::error!("query_order: failed to send inferred order canceled event");
2399        } else {
2400            tracing::debug!(
2401                "query_order: inferred cancel for {} ({})",
2402                client_order_id,
2403                context
2404            );
2405        }
2406    }
2407
2408    fn send_order_denied(
2409        trader_id: TraderId,
2410        strategy_id: StrategyId,
2411        instrument_id: InstrumentId,
2412        client_order_id: ClientOrderId,
2413        reason: &str,
2414    ) -> anyhow::Result<()> {
2415        let ts_event = get_atomic_clock_realtime().get_time_ns();
2416        let exec_sender = get_exec_event_sender();
2417        Self::send_order_denied_to(
2418            trader_id,
2419            strategy_id,
2420            instrument_id,
2421            client_order_id,
2422            reason,
2423            &exec_sender,
2424            ts_event,
2425        )
2426    }
2427
2428    pub(super) fn send_order_denied_to(
2429        trader_id: TraderId,
2430        strategy_id: StrategyId,
2431        instrument_id: InstrumentId,
2432        client_order_id: ClientOrderId,
2433        reason: &str,
2434        exec_sender: &EventSender<ExecutionEvent>,
2435        ts_event: UnixNanos,
2436    ) -> anyhow::Result<()> {
2437        let event = OrderDenied::new(
2438            trader_id,
2439            strategy_id,
2440            instrument_id,
2441            client_order_id,
2442            Ustr::from(reason),
2443            UUID4::new(),
2444            ts_event,
2445            ts_event,
2446        );
2447
2448        exec_sender
2449            .send(ExecutionEvent::Order(OrderEventAny::Denied(event)))
2450            .map_err(|e| anyhow::anyhow!("Failed to send order denied event: {e}"))
2451    }
2452
2453    fn send_order_modify_denied(
2454        cmd: &ModifyOrder,
2455        reason: &str,
2456        exec_sender: &EventSender<ExecutionEvent>,
2457        ts_event: UnixNanos,
2458        account_id: AccountId,
2459    ) -> anyhow::Result<()> {
2460        let mut event = OrderModifyDenied::new(
2461            cmd.trader_id,
2462            cmd.strategy_id,
2463            cmd.instrument_id,
2464            cmd.client_order_id,
2465            Ustr::from(reason),
2466            UUID4::new(),
2467            ts_event,
2468            ts_event,
2469            cmd.venue_order_id,
2470            Some(account_id),
2471        );
2472        event.causation_id = Some(cmd.command_id);
2473        exec_sender
2474            .send(ExecutionEvent::Order(OrderEventAny::ModifyDenied(event)))
2475            .map_err(|e| anyhow::anyhow!("Failed to send order modify denied event: {e}"))
2476    }
2477
2478    fn send_order_cancel_denied(
2479        target_order: Option<&OrderAny>,
2480        cmd: &CancelOrder,
2481        reason: &str,
2482        exec_sender: &EventSender<ExecutionEvent>,
2483        ts_event: UnixNanos,
2484        account_id: AccountId,
2485    ) -> anyhow::Result<()> {
2486        let mut event = OrderCancelDenied::new(
2487            target_order.map_or(cmd.trader_id, |order| order.trader_id()),
2488            target_order.map_or(cmd.strategy_id, |order| order.strategy_id()),
2489            target_order.map_or(cmd.instrument_id, |order| order.instrument_id()),
2490            target_order.map_or(cmd.client_order_id, |order| order.client_order_id()),
2491            Ustr::from(reason),
2492            UUID4::new(),
2493            ts_event,
2494            ts_event,
2495            target_order
2496                .and_then(|order| order.venue_order_id())
2497                .or(cmd.venue_order_id),
2498            Some(account_id),
2499        );
2500        event.causation_id = Some(cmd.command_id);
2501        exec_sender
2502            .send(ExecutionEvent::Order(OrderEventAny::CancelDenied(event)))
2503            .map_err(|e| anyhow::anyhow!("Failed to send order cancel denied event: {e}"))
2504    }
2505
2506    fn send_order_cancel_rejected(
2507        target_order: &OrderAny,
2508        causation_id: UUID4,
2509        reason: &str,
2510        exec_sender: &EventSender<ExecutionEvent>,
2511        ts_event: UnixNanos,
2512        account_id: AccountId,
2513    ) -> anyhow::Result<()> {
2514        let mut event = OrderCancelRejected::new(
2515            target_order.trader_id(),
2516            target_order.strategy_id(),
2517            target_order.instrument_id(),
2518            target_order.client_order_id(),
2519            Ustr::from(reason),
2520            UUID4::new(),
2521            ts_event,
2522            ts_event,
2523            false,
2524            target_order.venue_order_id(),
2525            Some(account_id),
2526        );
2527        event.causation_id = Some(causation_id);
2528        exec_sender
2529            .send(ExecutionEvent::Order(OrderEventAny::CancelRejected(event)))
2530            .map_err(|e| anyhow::anyhow!("Failed to send order cancel rejected event: {e}"))
2531    }
2532}
2533
2534impl InteractiveBrokersExecutionClient {
2535    fn validate_cancel_order_target(
2536        cmd: &CancelOrder,
2537        target_order: &OrderAny,
2538    ) -> anyhow::Result<()> {
2539        anyhow::ensure!(
2540            cmd.client_order_id == target_order.client_order_id(),
2541            "command client order ID {} does not match cached order {}",
2542            cmd.client_order_id,
2543            target_order.client_order_id()
2544        );
2545        anyhow::ensure!(
2546            cmd.instrument_id == target_order.instrument_id(),
2547            "command instrument ID {} does not match cached order {}",
2548            cmd.instrument_id,
2549            target_order.instrument_id()
2550        );
2551
2552        // Command actor IDs identify the requester and are not ownership evidence.
2553        if let (Some(command_venue_order_id), Some(target_venue_order_id)) =
2554            (cmd.venue_order_id.as_ref(), target_order.venue_order_id())
2555        {
2556            anyhow::ensure!(
2557                command_venue_order_id == &target_venue_order_id,
2558                "command venue order ID {command_venue_order_id} does not match cached order {target_venue_order_id}"
2559            );
2560        }
2561
2562        Ok(())
2563    }
2564
2565    /// Handles a cancel order asynchronously.
2566    ///
2567    /// # Errors
2568    ///
2569    /// Returns an error if order resolution or identity caching fails.
2570    #[allow(
2571        clippy::too_many_arguments,
2572        reason = "cancel routing requires the request, target order, venue client, and event context"
2573    )]
2574    async fn handle_cancel_order_async(
2575        cmd: &CancelOrder,
2576        target_order: &OrderAny,
2577        client: &Arc<Client>,
2578        orders: &OrderTracker,
2579        instrument_provider: &Arc<InteractiveBrokersInstrumentProvider>,
2580        exec_sender: &EventSender<ExecutionEvent>,
2581        ts_init: UnixNanos,
2582        account_id: AccountId,
2583        ib_account: Ustr,
2584        request_timeout_secs: u64,
2585    ) -> anyhow::Result<()> {
2586        if orders.lock()?.groups.contains_key(&cmd.client_order_id) {
2587            let result = Self::cancel_incarnation_group(
2588                cmd.client_order_id,
2589                client,
2590                orders,
2591                instrument_provider,
2592                exec_sender,
2593                ts_init,
2594                account_id,
2595                ib_account,
2596                request_timeout_secs,
2597                None,
2598                target_order.is_pending_cancel(),
2599            )
2600            .await;
2601
2602            return match result {
2603                Ok(()) => Ok(()),
2604                Err(CommandFailure::NotSent(reason)) => Self::send_order_cancel_denied(
2605                    Some(target_order),
2606                    cmd,
2607                    &reason,
2608                    exec_sender,
2609                    ts_init,
2610                    account_id,
2611                ),
2612                Err(CommandFailure::VenueRejected(reason)) => Self::send_order_cancel_rejected(
2613                    target_order,
2614                    cmd.command_id,
2615                    &reason,
2616                    exec_sender,
2617                    ts_init,
2618                    account_id,
2619                ),
2620                Err(CommandFailure::Ambiguous(reason)) => {
2621                    tracing::warn!("Grouped cancel remains unresolved: {reason}");
2622                    Ok(())
2623                }
2624            };
2625        }
2626
2627        let route = async {
2628            let order_selector = if let Some(venue_order_id) = &cmd.venue_order_id {
2629                IbOrderSelector::from_venue_order_id(venue_order_id)?
2630            } else {
2631                let state = orders.lock()?;
2632                IbOrderSelector::OrderId(
2633                    *state
2634                        .order_id_map
2635                        .get(&cmd.client_order_id)
2636                        .context("No IB order ID mapping found for client order ID")?,
2637                )
2638            };
2639
2640            let ib_order_id =
2641                Self::resolve_ib_order_id(client, order_selector, ib_account, request_timeout_secs)
2642                    .await?;
2643            Self::cache_recovered_order_tracking(ib_order_id, target_order, orders)?;
2644            Ok::<_, anyhow::Error>((order_selector, ib_order_id))
2645        }
2646        .await;
2647
2648        let (order_selector, ib_order_id) = match route {
2649            Ok(route) => route,
2650            Err(e) => {
2651                return Self::send_order_cancel_denied(
2652                    Some(target_order),
2653                    cmd,
2654                    &format!("Failed to resolve cancel route: {e:#}"),
2655                    exec_sender,
2656                    ts_init,
2657                    account_id,
2658                );
2659            }
2660        };
2661
2662        let venue_order_id = target_order
2663            .venue_order_id()
2664            .unwrap_or_else(|| VenueOrderId::from(ib_order_id.to_string()));
2665
2666        let timeout_dur = Duration::from_secs(request_timeout_secs);
2667
2668        let send_error =
2669            match tokio::time::timeout(timeout_dur, client.cancel_order(ib_order_id, "")).await {
2670                Ok(Ok(_subscription)) => None,
2671                Ok(Err(e)) => match Self::classify_order_submit_error(&e) {
2672                    CommandFailure::NotSent(reason) => {
2673                        return Self::send_order_cancel_denied(
2674                            Some(target_order),
2675                            cmd,
2676                            &reason,
2677                            exec_sender,
2678                            ts_init,
2679                            account_id,
2680                        );
2681                    }
2682                    CommandFailure::VenueRejected(reason) => {
2683                        return Self::send_order_cancel_rejected(
2684                            target_order,
2685                            cmd.command_id,
2686                            &reason,
2687                            exec_sender,
2688                            ts_init,
2689                            account_id,
2690                        );
2691                    }
2692                    CommandFailure::Ambiguous(reason) => Some(reason),
2693                },
2694                Err(_) => Some(format!(
2695                    "cancel request timed out after {request_timeout_secs} seconds"
2696                )),
2697            };
2698
2699        if let Some(send_error) = send_error {
2700            tracing::warn!(
2701                "Cancel outcome is unknown after attempting to send order {} to IB: {}; querying venue state",
2702                cmd.client_order_id,
2703                send_error
2704            );
2705            return Self::resolve_ambiguous_cancel(
2706                target_order,
2707                client,
2708                order_selector,
2709                account_id,
2710                ib_account,
2711                request_timeout_secs,
2712                &send_error,
2713                exec_sender,
2714                ts_init,
2715            )
2716            .await;
2717        }
2718
2719        // IB's terminal status can evict an order while its execution details still wait for
2720        // the commission report, so a cancel sent in that gap has no pending state to record
2721        if orders.lock()?.terminal_orders.contains_key(&ib_order_id) {
2722            tracing::debug!(
2723                "Cancel request for order {} was sent after IB reported it terminal",
2724                cmd.client_order_id
2725            );
2726            return Ok(());
2727        }
2728
2729        // A strategy cancel already published PENDING_CANCEL, so only the tracking records it
2730        let result = if target_order.is_pending_cancel() {
2731            Self::track_pending_cancel(cmd.client_order_id, orders).map(|_| ())
2732        } else {
2733            Self::emit_order_pending_cancel(
2734                ib_order_id,
2735                cmd.client_order_id,
2736                venue_order_id,
2737                orders,
2738                exec_sender,
2739                ts_init,
2740                account_id,
2741            )
2742        };
2743
2744        if let Err(e) = result {
2745            tracing::error!(
2746                "Cancel request for order {} was sent, but OrderPendingCancel emission failed: {e}",
2747                cmd.client_order_id
2748            );
2749        }
2750
2751        Ok(())
2752    }
2753
2754    #[allow(clippy::too_many_arguments)] // Resolution needs the original cancel context.
2755    async fn resolve_ambiguous_cancel(
2756        target_order: &OrderAny,
2757        client: &Arc<Client>,
2758        order_selector: IbOrderSelector,
2759        account_id: AccountId,
2760        ib_account: Ustr,
2761        request_timeout_secs: u64,
2762        send_error: &str,
2763        exec_sender: &EventSender<ExecutionEvent>,
2764        ts_init: UnixNanos,
2765    ) -> anyhow::Result<()> {
2766        let timeout_dur = Duration::from_secs(request_timeout_secs);
2767
2768        let remains_open = tokio::time::timeout(timeout_dur, async {
2769            let mut subscription = client.all_open_orders().await?.filter_data();
2770            while let Some(result) = subscription.next().await {
2771                let Orders::OrderData(data) = result? else {
2772                    continue;
2773                };
2774
2775                if !data.order.account.is_empty() && data.order.account != ib_account {
2776                    continue;
2777                }
2778
2779                if order_selector.matches(data.order_id, data.order.perm_id) {
2780                    return Ok::<bool, ibapi::Error>(true);
2781                }
2782            }
2783
2784            Ok(false)
2785        })
2786        .await
2787        .context("Timed out querying open orders after ambiguous cancel")??;
2788
2789        if remains_open {
2790            tracing::warn!(
2791                "Cancel outcome remains unresolved after transport error; order {} remains open at IB: {send_error}",
2792                target_order.client_order_id()
2793            );
2794            return Ok(());
2795        }
2796
2797        let completed_status = tokio::time::timeout(timeout_dur, async {
2798            let mut subscription = client.completed_orders(false).await?.filter_data();
2799            while let Some(result) = subscription.next().await {
2800                let Orders::OrderData(data) = result? else {
2801                    continue;
2802                };
2803
2804                if !data.order.account.is_empty() && data.order.account != ib_account {
2805                    continue;
2806                }
2807
2808                let matches_order = order_selector.matches(data.order_id, data.order.perm_id);
2809                if matches_order {
2810                    return Ok::<Option<OrderStatusKind>, ibapi::Error>(Some(
2811                        data.order_state.status,
2812                    ));
2813                }
2814            }
2815
2816            Ok(None)
2817        })
2818        .await
2819        .context("Timed out querying completed orders after ambiguous cancel")??;
2820
2821        if completed_status.as_ref().is_some_and(|status| {
2822            matches!(
2823                status,
2824                OrderStatusKind::Cancelled | OrderStatusKind::ApiCancelled
2825            )
2826        }) {
2827            let event = OrderCanceled::new(
2828                target_order.trader_id(),
2829                target_order.strategy_id(),
2830                target_order.instrument_id(),
2831                target_order.client_order_id(),
2832                UUID4::new(),
2833                ts_init,
2834                ts_init,
2835                false,
2836                Some(order_selector.venue_order_id()),
2837                Some(account_id),
2838                None,
2839            );
2840            exec_sender
2841                .send(ExecutionEvent::Order(OrderEventAny::Canceled(event)))
2842                .map_err(|e| {
2843                    anyhow::anyhow!("Failed to send resolved order canceled event: {e}")
2844                })?;
2845
2846            return Ok(());
2847        }
2848
2849        let reason = completed_status.map_or_else(
2850            || {
2851                format!(
2852                    "Cancel outcome could not be resolved from IB open or completed orders: {send_error}"
2853                )
2854            },
2855            |status| {
2856                format!(
2857                    "Cancel did not complete; IB reports order status {}",
2858                    status.as_str()
2859                )
2860            },
2861        );
2862
2863        tracing::warn!("{reason}");
2864        Ok(())
2865    }
2866
2867    pub(super) async fn resolve_ib_order_id(
2868        client: &Arc<Client>,
2869        order_selector: IbOrderSelector,
2870        ib_account: Ustr,
2871        request_timeout_secs: u64,
2872    ) -> anyhow::Result<i32> {
2873        let target_perm_id = match order_selector {
2874            IbOrderSelector::OrderId(order_id) => return Ok(order_id),
2875            IbOrderSelector::PermId(perm_id) => perm_id,
2876        };
2877
2878        let timeout_dur = Duration::from_secs(request_timeout_secs);
2879        tokio::time::timeout(timeout_dur, async {
2880            let mut subscription = client.all_open_orders().await?.filter_data();
2881            let mut routes = AHashMap::<(i32, i32), AHashSet<i64>>::new();
2882            let mut target_routes = AHashSet::new();
2883
2884            while let Some(result) = subscription.next().await {
2885                let Orders::OrderData(data) = result? else { continue; };
2886                if data.order.account != ib_account || !Self::is_active_open_order(&data.order) { continue; }
2887                let route = (data.order.client_id, data.order_id);
2888                routes.entry(route).or_default().insert(data.order.perm_id);
2889                if data.order.perm_id == target_perm_id { target_routes.insert(route); }
2890            }
2891
2892            anyhow::ensure!(target_routes.len() == 1,
2893                "Cannot resolve PERM-{target_perm_id}: expected one broker route, found {}", target_routes.len());
2894            let route = *target_routes.iter().next().expect("one route was checked");
2895            anyhow::ensure!(route.0 == client.client_id() && route.1 != 0,
2896                "Cannot resolve PERM-{target_perm_id}: order is not bound to this API client");
2897            anyhow::ensure!(routes.get(&route).is_some_and(|ids| ids.len() == 1),
2898                "Cannot resolve PERM-{target_perm_id}: broker route is shared by distinct permanent IDs");
2899            Ok(route.1)
2900        }).await.context("timed out resolving IB permanent order identity")?
2901    }
2902
2903    pub(super) fn is_active_open_order(order: &ibapi::orders::Order) -> bool {
2904        !order.deactivate
2905    }
2906
2907    pub(super) fn is_definitive_order_submit_error(error: &ibapi::Error) -> bool {
2908        matches!(
2909            error,
2910            ibapi::Error::InvalidArgument(_) | ibapi::Error::ServerVersion(_, _, _)
2911        )
2912    }
2913
2914    pub(super) fn classify_order_submit_error(error: &ibapi::Error) -> CommandFailure {
2915        let reason = error.to_string();
2916        if Self::is_definitive_order_submit_error(error) {
2917            CommandFailure::not_sent(reason)
2918        } else if matches!(
2919            error,
2920            ibapi::Error::Notice(notice)
2921                if notice.category() == ibapi::NoticeCategory::OrderRejection
2922        ) {
2923            CommandFailure::venue_rejected(reason)
2924        } else {
2925            CommandFailure::ambiguous(reason)
2926        }
2927    }
2928
2929    async fn handle_cancel_all_orders_async(
2930        client: &Arc<Client>,
2931        orders: &OrderTracker,
2932        exec_sender: &EventSender<ExecutionEvent>,
2933        ts_init: UnixNanos,
2934        account_id: AccountId,
2935        ib_account: Ustr,
2936        request_timeout_secs: u64,
2937        orders_to_cancel: Vec<(ClientOrderId, Option<VenueOrderId>)>,
2938        provider: &Arc<InteractiveBrokersInstrumentProvider>,
2939        order_side: Option<OrderSide>,
2940    ) -> anyhow::Result<()> {
2941        let mut handled = AHashSet::new();
2942        let mut individual = Vec::new();
2943
2944        for (id, venue_id) in orders_to_cancel {
2945            let parent = orders.lock()?.group_for_client(id);
2946            if let Some(parent) = parent {
2947                if handled.insert(parent) {
2948                    Self::cancel_incarnation_group(
2949                        parent,
2950                        client,
2951                        orders,
2952                        provider,
2953                        exec_sender,
2954                        ts_init,
2955                        account_id,
2956                        ib_account,
2957                        request_timeout_secs,
2958                        order_side,
2959                        false,
2960                    )
2961                    .await
2962                    .map_err(|failure| anyhow::anyhow!("Grouped cancel-all failed: {failure:?}"))?;
2963                }
2964            } else {
2965                individual.push((id, venue_id));
2966            }
2967        }
2968
2969        let orders_to_cancel = individual;
2970
2971        // Get all IB order selectors first, then drop the guard before awaiting
2972        let order_selectors: Vec<(ClientOrderId, IbOrderSelector, Option<VenueOrderId>)> = {
2973            let state = orders.lock()?;
2974            orders_to_cancel
2975                .into_iter()
2976                .filter_map(|(client_order_id, venue_order_id)| {
2977                    match state.cancel_selector(client_order_id, venue_order_id.as_ref()) {
2978                        Ok(Some(order_selector)) => {
2979                            Some((client_order_id, order_selector, venue_order_id))
2980                        }
2981                        Ok(None) => None,
2982                        Err(e) => {
2983                            tracing::error!(
2984                                "Failed to resolve cancel-all order {client_order_id}: {e}"
2985                            );
2986                            None
2987                        }
2988                    }
2989                })
2990                .collect()
2991        };
2992
2993        // Now cancel each order (guard is dropped, so we can await)
2994        for (client_order_id, order_selector, venue_order_id) in order_selectors {
2995            let ib_order_id = match Self::resolve_ib_order_id(
2996                client,
2997                order_selector,
2998                ib_account,
2999                request_timeout_secs,
3000            )
3001            .await
3002            {
3003                Ok(ib_order_id) => ib_order_id,
3004                Err(e) => {
3005                    tracing::error!("Failed resolve cancel-all order {client_order_id}: {e}");
3006                    continue;
3007                }
3008            };
3009
3010            let venue_order_id =
3011                venue_order_id.unwrap_or_else(|| VenueOrderId::from(ib_order_id.to_string()));
3012
3013            if let Err(e) = client.cancel_order(ib_order_id, "").await {
3014                tracing::warn!(
3015                    "Failed to cancel order {} (IB order ID: {}): {e}",
3016                    client_order_id,
3017                    ib_order_id
3018                );
3019            } else {
3020                if let Err(e) = Self::emit_order_pending_cancel(
3021                    ib_order_id,
3022                    client_order_id,
3023                    venue_order_id,
3024                    orders,
3025                    exec_sender,
3026                    ts_init,
3027                    account_id,
3028                ) {
3029                    tracing::error!(
3030                        "Failed to emit pending cancel for order {} (IB order ID: {}): {e}",
3031                        client_order_id,
3032                        ib_order_id
3033                    );
3034                }
3035
3036                tracing::debug!(
3037                    "Canceled order {} (IB order ID: {})",
3038                    client_order_id,
3039                    ib_order_id
3040                );
3041            }
3042        }
3043
3044        tracing::debug!("Finished canceling all orders");
3045
3046        Ok(())
3047    }
3048
3049    pub(super) fn emit_order_pending_cancel(
3050        _order_id: i32,
3051        client_order_id: ClientOrderId,
3052        venue_order_id: VenueOrderId,
3053        orders: &OrderTracker,
3054        exec_sender: &EventSender<ExecutionEvent>,
3055        ts_init: UnixNanos,
3056        account_id: AccountId,
3057    ) -> anyhow::Result<()> {
3058        let Some((trader_id, strategy_id, instrument_id)) =
3059            Self::track_pending_cancel(client_order_id, orders)?
3060        else {
3061            return Ok(());
3062        };
3063
3064        let event = OrderPendingCancel::new(
3065            trader_id,
3066            strategy_id,
3067            instrument_id,
3068            client_order_id,
3069            Some(account_id),
3070            UUID4::new(),
3071            ts_init,
3072            ts_init,
3073            false,
3074            Some(venue_order_id),
3075        );
3076
3077        exec_sender
3078            .send(ExecutionEvent::Order(OrderEventAny::PendingCancel(event)))
3079            .map_err(|e| anyhow::anyhow!("Failed to send order pending cancel event: {e}"))?;
3080
3081        Ok(())
3082    }
3083
3084    // Marks a tracked order as pending cancel, returning its identity when it was not already.
3085    pub(super) fn track_pending_cancel(
3086        client_order_id: ClientOrderId,
3087        orders: &OrderTracker,
3088    ) -> anyhow::Result<Option<(TraderId, StrategyId, InstrumentId)>> {
3089        let mut state = orders.lock()?;
3090        let order = state
3091            .order_by_client_mut(client_order_id)
3092            .context("Tracked state not found for pending cancel order")?;
3093
3094        if order.pending_cancel {
3095            return Ok(None);
3096        }
3097
3098        order.pending_cancel = true;
3099        Ok(Some((
3100            order.trader_id,
3101            order.strategy_id,
3102            order.instrument_id,
3103        )))
3104    }
3105}