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