nautilus-interactive-brokers 0.62.0

Interactive Brokers integration adapter for the Nautilus trading engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
// -------------------------------------------------------------------------------------------------

use super::*;

impl InteractiveBrokersExecutionClient {
    pub(super) fn cached_spread_instrument_ids_for_preload(
        cache: &Cache,
        instrument_provider: &InteractiveBrokersInstrumentProvider,
    ) -> Vec<InstrumentId> {
        let mut spread_ids = ahash::AHashSet::new();

        for client_order_id in cache.iter_client_order_ids(None, None, None, None) {
            if let Some(order) = cache.order(&client_order_id) {
                let instrument_id = order.instrument_id();
                if is_spread_instrument_id(&instrument_id)
                    && instrument_provider.find(&instrument_id).is_none()
                {
                    spread_ids.insert(instrument_id);
                }
            }
        }

        let mut spread_ids: Vec<InstrumentId> = spread_ids.into_iter().collect();
        spread_ids.sort_by_key(|a| a.to_string());
        spread_ids
    }

    pub(super) async fn preload_cached_spread_instruments(
        &self,
        client: &Client,
    ) -> anyhow::Result<()> {
        let spread_ids = {
            let cache = self.core.cache();
            Self::cached_spread_instrument_ids_for_preload(&cache, &self.instrument_provider)
        };

        if spread_ids.is_empty() {
            return Ok(());
        }

        tracing::debug!(
            "Preloading {} cached Interactive Brokers spread instrument(s) before reconciliation",
            spread_ids.len()
        );

        for instrument_id in spread_ids {
            match self
                .instrument_provider
                .fetch_spread_instrument(client, instrument_id, false, None)
                .await
            {
                Ok(true) => {
                    tracing::debug!("Preloaded cached spread instrument {}", instrument_id);
                }
                Ok(false) => {
                    tracing::warn!(
                        "Failed to preload cached spread instrument {}",
                        instrument_id
                    );
                }
                Err(e) => {
                    tracing::warn!(
                        "Failed to preload cached spread instrument {}: {}",
                        instrument_id,
                        e
                    );
                }
            }
        }

        Ok(())
    }

    pub(super) fn get_mapped_instrument_id(
        order_id: i32,
        instrument_id_map: &Arc<Mutex<AHashMap<i32, InstrumentId>>>,
    ) -> anyhow::Result<Option<InstrumentId>> {
        let map = instrument_id_map
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to lock instrument ID map"))?;
        Ok(map.get(&order_id).copied())
    }

    pub(super) fn get_required_order_actor_ids(
        order_id: i32,
        trader_id_map: &Arc<Mutex<AHashMap<i32, TraderId>>>,
        strategy_id_map: &Arc<Mutex<AHashMap<i32, StrategyId>>>,
    ) -> anyhow::Result<(TraderId, StrategyId)> {
        let trader_id = {
            let map = trader_id_map
                .lock()
                .map_err(|_| anyhow::anyhow!("Failed to lock trader ID map"))?;
            map.get(&order_id).copied()
        }
        .with_context(|| format!("Trader ID not found for Interactive Brokers order {order_id}"))?;

        let strategy_id = {
            let map = strategy_id_map
                .lock()
                .map_err(|_| anyhow::anyhow!("Failed to lock strategy ID map"))?;
            map.get(&order_id).copied()
        }
        .with_context(|| {
            format!("Strategy ID not found for Interactive Brokers order {order_id}")
        })?;

        Ok((trader_id, strategy_id))
    }

    pub(super) fn resolve_contract_for_instrument(
        instrument_id: InstrumentId,
        instrument_provider: &Arc<InteractiveBrokersInstrumentProvider>,
    ) -> anyhow::Result<ibapi::contracts::Contract> {
        instrument_provider
            .resolve_contract_for_instrument(instrument_id)
            .context("Failed to convert instrument ID to IB contract")
    }

    pub(super) fn contract_with_order_exchange_param(
        mut contract: ibapi::contracts::Contract,
        params: Option<&nautilus_core::Params>,
    ) -> anyhow::Result<ibapi::contracts::Contract> {
        let Some(params) = params else {
            return Ok(contract);
        };

        let Some(exchange_value) = params.get("exchange") else {
            return Ok(contract);
        };

        let Some(exchange) = exchange_value.as_str() else {
            anyhow::bail!("`exchange` order param must be a string");
        };

        if exchange.is_empty() {
            return Ok(contract);
        }

        contract.exchange = ibapi::contracts::Exchange::from(exchange);
        Ok(contract)
    }

    #[allow(clippy::too_many_arguments)]
    pub(super) fn cache_order_tracking(
        ib_order_id: i32,
        client_order_id: ClientOrderId,
        instrument_id: InstrumentId,
        trader_id: TraderId,
        strategy_id: StrategyId,
        order_side: OrderSide,
        order_type: OrderType,
        order_id_map: &Arc<Mutex<AHashMap<ClientOrderId, i32>>>,
        venue_order_id_map: &Arc<Mutex<AHashMap<i32, ClientOrderId>>>,
        instrument_id_map: &Arc<Mutex<AHashMap<i32, InstrumentId>>>,
        trader_id_map: &Arc<Mutex<AHashMap<i32, TraderId>>>,
        strategy_id_map: &Arc<Mutex<AHashMap<i32, StrategyId>>>,
        active_order_contexts: &Arc<Mutex<AHashMap<i32, TrackedOrderContext>>>,
        terminal_order_contexts: &Arc<Mutex<FifoCacheMap<i32, TrackedOrderContext, 10_000>>>,
    ) -> anyhow::Result<()> {
        {
            let mut order_map = order_id_map
                .lock()
                .map_err(|_| anyhow::anyhow!("Failed to lock order ID map"))?;
            order_map.insert(client_order_id, ib_order_id);
        }

        {
            let mut venue_map = venue_order_id_map
                .lock()
                .map_err(|_| anyhow::anyhow!("Failed to lock venue order ID map"))?;
            venue_map.insert(ib_order_id, client_order_id);
        }

        {
            let mut instrument_map = instrument_id_map
                .lock()
                .map_err(|_| anyhow::anyhow!("Failed to lock instrument ID map"))?;
            instrument_map.insert(ib_order_id, instrument_id);
        }

        {
            let mut trader_map = trader_id_map
                .lock()
                .map_err(|_| anyhow::anyhow!("Failed to lock trader_id map"))?;
            trader_map.insert(ib_order_id, trader_id);
        }

        {
            let mut strategy_map = strategy_id_map
                .lock()
                .map_err(|_| anyhow::anyhow!("Failed to lock strategy_id map"))?;
            strategy_map.insert(ib_order_id, strategy_id);
        }

        terminal_order_contexts
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to lock terminal order contexts"))?
            .remove(&ib_order_id);
        active_order_contexts
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to lock active order contexts"))?
            .insert(
                ib_order_id,
                TrackedOrderContext {
                    client_order_id,
                    trader_id,
                    strategy_id,
                    instrument_id,
                    order_side,
                    order_type,
                    accepted: false,
                    avg_px: None,
                },
            );

        Ok(())
    }

    pub(super) fn get_tracked_order_context(
        ib_order_id: i32,
        active_order_contexts: &Arc<Mutex<AHashMap<i32, TrackedOrderContext>>>,
        terminal_order_contexts: &Arc<Mutex<FifoCacheMap<i32, TrackedOrderContext, 10_000>>>,
    ) -> anyhow::Result<Option<TrackedOrderContext>> {
        if let Some(context) = active_order_contexts
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to lock active order contexts"))?
            .get(&ib_order_id)
            .cloned()
        {
            return Ok(Some(context));
        }

        Ok(terminal_order_contexts
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to lock terminal order contexts"))?
            .get(&ib_order_id)
            .cloned())
    }

    pub(super) fn emit_order_accepted_if_needed(
        ib_order_id: i32,
        venue_order_id: VenueOrderId,
        account_id: AccountId,
        ts_event: UnixNanos,
        active_order_contexts: &Arc<Mutex<AHashMap<i32, TrackedOrderContext>>>,
        exec_sender: &tokio::sync::mpsc::UnboundedSender<ExecutionEvent>,
    ) -> anyhow::Result<bool> {
        let mut contexts = active_order_contexts
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to lock active order contexts"))?;
        let Some(context) = contexts.get_mut(&ib_order_id) else {
            return Ok(false);
        };

        Self::emit_order_accepted(context, venue_order_id, account_id, ts_event, exec_sender)
    }

    #[allow(clippy::too_many_arguments)]
    pub(super) fn emit_order_accepted_for_fill_if_needed(
        ib_order_id: i32,
        venue_order_id: VenueOrderId,
        account_id: AccountId,
        ts_event: UnixNanos,
        active_order_contexts: &Arc<Mutex<AHashMap<i32, TrackedOrderContext>>>,
        terminal_order_contexts: &Arc<Mutex<FifoCacheMap<i32, TrackedOrderContext, 10_000>>>,
        exec_sender: &tokio::sync::mpsc::UnboundedSender<ExecutionEvent>,
    ) -> anyhow::Result<bool> {
        if Self::emit_order_accepted_if_needed(
            ib_order_id,
            venue_order_id,
            account_id,
            ts_event,
            active_order_contexts,
            exec_sender,
        )? {
            return Ok(true);
        }

        let mut contexts = terminal_order_contexts
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to lock terminal order contexts"))?;
        let Some(context) = contexts.get_mut(&ib_order_id) else {
            return Ok(false);
        };

        Self::emit_order_accepted(context, venue_order_id, account_id, ts_event, exec_sender)
    }

    fn emit_order_accepted(
        context: &mut TrackedOrderContext,
        venue_order_id: VenueOrderId,
        account_id: AccountId,
        ts_event: UnixNanos,
        exec_sender: &tokio::sync::mpsc::UnboundedSender<ExecutionEvent>,
    ) -> anyhow::Result<bool> {
        if context.accepted {
            return Ok(false);
        }

        let event = OrderAccepted::new(
            context.trader_id,
            context.strategy_id,
            context.instrument_id,
            context.client_order_id,
            venue_order_id,
            account_id,
            UUID4::new(),
            ts_event,
            ts_event,
            false,
        );
        exec_sender
            .send(ExecutionEvent::Order(OrderEventAny::Accepted(event)))
            .map_err(|e| anyhow::anyhow!("Failed to send order accepted event: {e}"))?;
        context.accepted = true;

        Ok(true)
    }

    #[allow(clippy::too_many_arguments)]
    pub(super) fn remove_order_tracking(
        ib_order_id: i32,
        client_order_id: ClientOrderId,
        order_id_map: &Arc<Mutex<AHashMap<ClientOrderId, i32>>>,
        venue_order_id_map: &Arc<Mutex<AHashMap<i32, ClientOrderId>>>,
        instrument_id_map: &Arc<Mutex<AHashMap<i32, InstrumentId>>>,
        trader_id_map: &Arc<Mutex<AHashMap<i32, TraderId>>>,
        strategy_id_map: &Arc<Mutex<AHashMap<i32, StrategyId>>>,
        active_order_contexts: &Arc<Mutex<AHashMap<i32, TrackedOrderContext>>>,
        terminal_order_contexts: &Arc<Mutex<FifoCacheMap<i32, TrackedOrderContext, 10_000>>>,
    ) -> anyhow::Result<()> {
        order_id_map
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to lock order ID map"))?
            .remove(&client_order_id);
        venue_order_id_map
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to lock venue order ID map"))?
            .remove(&ib_order_id);
        instrument_id_map
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to lock instrument ID map"))?
            .remove(&ib_order_id);
        trader_id_map
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to lock trader ID map"))?
            .remove(&ib_order_id);
        strategy_id_map
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to lock strategy ID map"))?
            .remove(&ib_order_id);
        active_order_contexts
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to lock active order contexts"))?
            .remove(&ib_order_id);
        terminal_order_contexts
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to lock terminal order contexts"))?
            .remove(&ib_order_id);

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use ibapi::contracts::{Contract, Exchange};
    use nautilus_core::Params;
    use rstest::rstest;
    use serde_json::Value;

    use super::*;

    fn contract_with_exchange(exchange: &str) -> Contract {
        Contract {
            exchange: Exchange::from(exchange),
            ..Default::default()
        }
    }

    #[rstest]
    fn test_contract_with_order_exchange_param_overrides_exchange() {
        let contract = contract_with_exchange("SMART");
        let mut params = Params::new();
        params.insert("exchange".to_string(), Value::String("IEX".to_string()));

        let updated = InteractiveBrokersExecutionClient::contract_with_order_exchange_param(
            contract.clone(),
            Some(&params),
        )
        .unwrap();

        assert_eq!(updated.exchange.as_str(), "IEX");
        assert_eq!(contract.exchange.as_str(), "SMART");
    }

    #[rstest]
    fn test_contract_with_order_exchange_param_keeps_contract_without_exchange() {
        let contract = contract_with_exchange("SMART");
        let params = Params::new();

        let updated = InteractiveBrokersExecutionClient::contract_with_order_exchange_param(
            contract,
            Some(&params),
        )
        .unwrap();

        assert_eq!(updated.exchange.as_str(), "SMART");
    }

    #[rstest]
    fn test_contract_with_order_exchange_param_keeps_contract_with_empty_exchange() {
        let contract = contract_with_exchange("SMART");
        let mut params = Params::new();
        params.insert("exchange".to_string(), Value::String(String::new()));

        let updated = InteractiveBrokersExecutionClient::contract_with_order_exchange_param(
            contract,
            Some(&params),
        )
        .unwrap();

        assert_eq!(updated.exchange.as_str(), "SMART");
    }

    #[rstest]
    fn test_contract_with_order_exchange_param_rejects_non_string_exchange() {
        let contract = contract_with_exchange("SMART");
        let mut params = Params::new();
        params.insert("exchange".to_string(), Value::Bool(true));

        let err = InteractiveBrokersExecutionClient::contract_with_order_exchange_param(
            contract,
            Some(&params),
        )
        .unwrap_err();

        assert!(err.to_string().contains("must be a string"));
    }
}