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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Dockerized IB Gateway management.

#[cfg(feature = "gateway")]
use std::{collections::HashMap, fmt::Debug, time::Duration};

#[cfg(feature = "gateway")]
use anyhow::Context;
#[cfg(feature = "gateway")]
use bollard::Docker;
#[cfg(feature = "gateway")]
use bollard::container::LogOutput;
#[cfg(feature = "gateway")]
use bollard::models::{
    ContainerCreateBody, ContainerCreateResponse, HostConfig, PortBinding, RestartPolicy,
    RestartPolicyNameEnum,
};
#[cfg(feature = "gateway")]
use bollard::query_parameters::{
    CreateContainerOptions, ListContainersOptions, LogsOptions, RemoveContainerOptions,
    StartContainerOptions, StopContainerOptions,
};
#[cfg(feature = "gateway")]
use futures_util::StreamExt;
#[cfg(feature = "gateway")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "gateway")]
use crate::config::DockerizedIBGatewayConfig;
#[cfg(feature = "gateway")]

/// Container status enumeration.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(
        module = "nautilus_trader.adapters.interactive_brokers",
        from_py_object,
        rename_all = "SCREAMING_SNAKE_CASE"
    )
)]
#[cfg_attr(
    feature = "python",
    pyo3_stub_gen::derive::gen_stub_pyclass_enum(
        module = "nautilus_trader.adapters.interactive_brokers"
    )
)]
pub enum ContainerStatus {
    /// No container exists.
    NoContainer = 1,
    /// Container has been created but not started.
    ContainerCreated = 2,
    /// Container is starting.
    ContainerStarting = 3,
    /// Container has stopped.
    ContainerStopped = 4,
    /// Container is running but not logged in.
    NotLoggedIn = 5,
    /// Container is ready (running and logged in).
    Ready = 6,
    /// Unknown container status.
    Unknown = 7,
}

/// Dockerized IB Gateway manager.
///
/// This struct manages the lifecycle of Interactive Brokers Gateway Docker containers,
/// including creation, starting, stopping, and status checking.
#[derive(Clone)]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(
        module = "nautilus_trader.adapters.interactive_brokers",
        from_py_object
    )
)]
#[cfg_attr(
    feature = "python",
    pyo3_stub_gen::derive::gen_stub_pyclass(
        module = "nautilus_trader.adapters.interactive_brokers"
    )
)]
#[cfg(feature = "gateway")]
pub struct DockerizedIBGateway {
    /// Configuration for the gateway.
    config: DockerizedIBGatewayConfig,
    /// Docker client.
    pub(crate) docker: Docker,
    /// Username for IB account.
    username: String,
    /// Password for IB account.
    password: String,
    /// Host address (always 127.0.0.1).
    host: String,
    /// Port for the gateway.
    port: u16,
    /// Container name.
    container_name: String,
}

#[cfg(feature = "gateway")]
impl Debug for DockerizedIBGateway {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(stringify!(DockerizedIBGateway))
            .field("host", &self.host)
            .field("port", &self.port)
            .field("container_name", &self.container_name)
            .field("trading_mode", &self.config.trading_mode)
            .finish_non_exhaustive()
    }
}

#[cfg(feature = "gateway")]
impl DockerizedIBGateway {
    /// Base container name.
    pub const CONTAINER_NAME: &'static str = "nautilus-ib-gateway";

    /// Host API ports by trading mode.
    pub const HOST_PORTS: &'static [(&'static str, u16)] = &[("Paper", 4002), ("Live", 4001)];

    /// Container API ports exposed by the IB Gateway image.
    pub const CONTAINER_PORTS: &'static [(&'static str, u16)] = &[("Paper", 4004), ("Live", 4003)];

    /// Internal VNC port.
    pub const VNC_PORT_INTERNAL: u16 = 5900;

    fn host_port_for_mode(trading_mode: crate::config::TradingMode) -> u16 {
        match trading_mode {
            crate::config::TradingMode::Paper => 4002,
            crate::config::TradingMode::Live => 4001,
        }
    }

    fn container_port_for_mode(trading_mode: crate::config::TradingMode) -> u16 {
        match trading_mode {
            crate::config::TradingMode::Paper => 4004,
            crate::config::TradingMode::Live => 4003,
        }
    }

    fn logs_indicate_ready(logs: &str) -> bool {
        logs.contains("Login has completed")
            || logs.contains("Configuration tasks completed")
            || logs.contains("Logged in to")
            || logs.contains("Login successful")
    }

    /// Create a new DockerizedIBGateway from configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - Configuration for the gateway
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Username or password is not provided and not available in environment variables
    /// - Docker client creation fails
    pub fn new(config: DockerizedIBGatewayConfig) -> anyhow::Result<Self> {
        // Load username from config or environment (clone to avoid partial move)
        let username = config
            .username
            .clone()
            .or_else(|| std::env::var("TWS_USERNAME").ok())
            .ok_or_else(|| anyhow::anyhow!("username not set nor available in env TWS_USERNAME"))?;

        // Load password from config or environment (clone to avoid partial move)
        let password = config
            .password
            .clone()
            .or_else(|| std::env::var("TWS_PASSWORD").ok())
            .ok_or_else(|| anyhow::anyhow!("password not set nor available in env TWS_PASSWORD"))?;

        // Connect to Docker
        let docker = Docker::connect_with_local_defaults().context(
            "Failed to connect to the local Docker daemon. Ensure Docker is running and the local Docker socket is available",
        )?;

        // Determine port based on trading mode
        let mode_str = match config.trading_mode {
            crate::config::TradingMode::Paper => "Paper",
            crate::config::TradingMode::Live => "Live",
        };
        let port = Self::host_port_for_mode(config.trading_mode);

        // Generate container name
        let container_name = format!("{}-{}", Self::CONTAINER_NAME, mode_str).to_lowercase();

        Ok(Self {
            config,
            docker,
            username,
            password,
            host: "127.0.0.1".to_string(),
            port,
            container_name,
        })
    }

    /// Get the container name.
    pub fn container_name(&self) -> &str {
        &self.container_name
    }

    /// Get the host address.
    pub fn host(&self) -> &str {
        &self.host
    }

    /// Get the port.
    pub fn port(&self) -> u16 {
        self.port
    }

    /// Check if the container is logged in by examining logs.
    ///
    /// # Arguments
    ///
    /// * `container_id` - The container ID to check
    ///
    /// # Errors
    ///
    /// Returns an error if log retrieval fails.
    pub async fn is_logged_in(&self, container_id: &str) -> anyhow::Result<bool> {
        let logs_options = LogsOptions {
            stdout: true,
            stderr: true,
            ..Default::default()
        };

        let mut logs_stream = self.docker.logs(container_id, Some(logs_options));

        let mut logged_in = false;

        while let Some(log_result) = logs_stream.next().await {
            let log_output = log_result.context("Failed to read log chunk")?;
            // Handle LogOutput enum variants
            let log_bytes = match log_output {
                LogOutput::StdOut { message } | LogOutput::StdErr { message } => message,
                LogOutput::StdIn { message } | LogOutput::Console { message } => message,
            };
            let log_string = String::from_utf8_lossy(&log_bytes);
            if Self::logs_indicate_ready(&log_string) {
                logged_in = true;
                break;
            }
        }

        Ok(logged_in)
    }

    /// Get the current container status.
    ///
    /// # Errors
    ///
    /// Returns an error if container inspection fails.
    pub async fn container_status(&self) -> anyhow::Result<ContainerStatus> {
        let list_options = ListContainersOptions {
            all: true,
            ..Default::default()
        };
        let containers = self
            .docker
            .list_containers(Some(list_options))
            .await
            .context("Failed to list containers")?;

        let container = containers.iter().find(|c| {
            c.names
                .as_ref()
                .and_then(|names| names.first())
                .map(|name| name.trim_start_matches('/') == self.container_name)
                .unwrap_or(false)
        });

        let Some(container) = container else {
            return Ok(ContainerStatus::NoContainer);
        };

        let state = container
            .state
            .as_ref()
            .map(|state| state.as_ref())
            .unwrap_or("unknown");

        match state {
            "running" => {
                let container_id = container
                    .id
                    .as_ref()
                    .ok_or_else(|| anyhow::anyhow!("Container ID missing"))?;

                if self.is_logged_in(container_id).await.unwrap_or(false) {
                    Ok(ContainerStatus::Ready)
                } else {
                    Ok(ContainerStatus::ContainerStarting)
                }
            }
            "stopped" | "exited" => Ok(ContainerStatus::ContainerStopped),
            "created" => Ok(ContainerStatus::ContainerCreated),
            _ => Ok(ContainerStatus::Unknown),
        }
    }

    /// Start the gateway container.
    ///
    /// # Arguments
    ///
    /// * `wait` - Optional wait time in seconds (overrides config timeout)
    ///
    /// # Errors
    ///
    /// Returns an error if container creation or startup fails.
    pub async fn start(&mut self, wait: Option<u64>) -> anyhow::Result<()> {
        tracing::debug!("Ensuring gateway is running");

        let status = self.container_status().await?;

        let broken_statuses = [
            ContainerStatus::NotLoggedIn,
            ContainerStatus::ContainerStopped,
            ContainerStatus::ContainerCreated,
            ContainerStatus::Unknown,
        ];

        match status {
            ContainerStatus::NoContainer => {
                tracing::debug!("No container, starting");
            }
            status if broken_statuses.contains(&status) => {
                tracing::debug!("Status {:?}, removing existing container", status);
                self.stop().await?;
            }
            ContainerStatus::Ready | ContainerStatus::ContainerStarting => {
                tracing::debug!("Status {:?}, using existing container", status);
                return Ok(());
            }
            _ => {}
        }

        tracing::debug!("Starting new container");

        // Determine port mappings
        let host_port = Self::host_port_for_mode(self.config.trading_mode);
        let container_port = Self::container_port_for_mode(self.config.trading_mode);

        let mut port_bindings = HashMap::new();
        port_bindings.insert(
            format!("{}/tcp", container_port),
            Some(vec![PortBinding {
                host_ip: Some(self.host.clone()),
                host_port: Some(host_port.to_string()),
            }]),
        );

        if let Some(vnc_port) = self.config.vnc_port {
            port_bindings.insert(
                format!("{}/tcp", Self::VNC_PORT_INTERNAL),
                Some(vec![PortBinding {
                    host_ip: Some(self.host.clone()),
                    host_port: Some(vnc_port.to_string()),
                }]),
            );
        }

        // Prepare environment variables
        let mode_str = match self.config.trading_mode {
            crate::config::TradingMode::Paper => "paper",
            crate::config::TradingMode::Live => "live",
        };
        let env = vec![
            format!("TWS_USERID={}", self.username),
            format!("TWS_PASSWORD={}", self.password),
            format!("TRADING_MODE={}", mode_str),
            format!(
                "READ_ONLY_API={}",
                if self.config.read_only_api {
                    "yes"
                } else {
                    "no"
                }
            ),
            "EXISTING_SESSION_DETECTED_ACTION=primary".to_string(),
        ];

        // Create container configuration
        let container_config = ContainerCreateBody {
            image: Some(self.config.container_image.clone()),
            hostname: Some(self.container_name.clone()),
            host_config: Some(HostConfig {
                port_bindings: Some(port_bindings),
                restart_policy: Some(RestartPolicy {
                    name: Some(RestartPolicyNameEnum::ALWAYS),
                    maximum_retry_count: None,
                }),
                ..Default::default()
            }),
            env: Some(env),
            ..Default::default()
        };

        // Create container
        let create_options = CreateContainerOptions {
            name: Some(self.container_name.clone()),
            ..Default::default()
        };

        let create_response: ContainerCreateResponse = self
            .docker
            .create_container(Some(create_options), container_config)
            .await
            .context("Failed to create container")?;

        let container_id = create_response.id;

        // Start container
        self.docker
            .start_container(&container_id, None::<StartContainerOptions>)
            .await
            .context("Failed to start container")?;

        tracing::debug!(
            "Container `{}` starting, waiting for ready",
            self.container_name
        );

        // Wait for container to be ready
        let wait_time = wait.unwrap_or(self.config.timeout);
        let mut waited = 0u64;

        while waited < wait_time {
            if self.is_logged_in(&container_id).await.unwrap_or(false) {
                tracing::debug!(
                    "Gateway `{}` ready. VNC port is {:?}",
                    self.container_name,
                    self.config.vnc_port
                );
                return Ok(());
            }

            tracing::debug!("Waiting for IB Gateway to start");
            tokio::time::sleep(Duration::from_secs(1)).await;
            waited += 1;
        }

        anyhow::bail!(
            "Gateway `{}` not ready after {} seconds",
            self.container_name,
            wait_time
        )
    }

    /// Safely start the gateway, handling container already exists errors.
    ///
    /// # Arguments
    ///
    /// * `wait` - Optional wait time in seconds
    ///
    /// # Errors
    ///
    /// Returns an error if startup fails (other than container exists).
    pub async fn safe_start(&mut self, wait: Option<u64>) -> anyhow::Result<()> {
        match self.start(wait).await {
            Ok(()) => Ok(()),
            Err(e) if e.to_string().contains("already exists") => {
                tracing::warn!("Container already exists, continuing");
                Ok(())
            }
            Err(e) => Err(e),
        }
    }

    /// Stop and remove the gateway container.
    ///
    /// # Errors
    ///
    /// Returns an error if container stop or removal fails.
    pub async fn stop(&self) -> anyhow::Result<()> {
        let list_options = ListContainersOptions {
            all: true,
            ..Default::default()
        };
        let containers = self
            .docker
            .list_containers(Some(list_options))
            .await
            .context("Failed to list containers")?;

        let container = containers.iter().find(|c| {
            c.names
                .as_ref()
                .and_then(|names| names.first())
                .map(|name| name.trim_start_matches('/') == self.container_name)
                .unwrap_or(false)
        });

        if let Some(container) = container {
            if let Some(container_id) = &container.id {
                // Stop container if running
                if matches!(
                    container.state.as_ref().map(|state| state.as_ref()),
                    Some("running")
                ) {
                    self.docker
                        .stop_container(container_id, None::<StopContainerOptions>)
                        .await
                        .context("Failed to stop container")?;
                }

                // Remove container
                let remove_options = RemoveContainerOptions {
                    force: true,
                    ..Default::default()
                };

                self.docker
                    .remove_container(container_id, Some(remove_options))
                    .await
                    .context("Failed to remove container")?;

                tracing::debug!("Stopped and removed container `{}`", self.container_name);
            }
        }

        Ok(())
    }
}

/// Stub implementation when gateway feature is disabled.
#[cfg(not(feature = "gateway"))]
#[derive(Debug)]
pub struct DockerizedIBGateway;

#[cfg(not(feature = "gateway"))]
impl DockerizedIBGateway {
    /// # Errors
    ///
    /// Returns an error if the Dockerized IB Gateway cannot be created or started.
    pub fn new(_config: crate::config::DockerizedIBGatewayConfig) -> anyhow::Result<Self> {
        anyhow::bail!("Gateway feature is not enabled. Build with --features gateway")
    }
}

#[cfg(all(test, feature = "gateway"))]
mod tests {
    use rstest::rstest;

    use super::DockerizedIBGateway;
    use crate::config::TradingMode;

    #[rstest]
    #[case(TradingMode::Paper, 4002)]
    #[case(TradingMode::Live, 4001)]
    fn host_port_matches_trading_mode(#[case] trading_mode: TradingMode, #[case] expected: u16) {
        assert_eq!(
            DockerizedIBGateway::host_port_for_mode(trading_mode),
            expected
        );
    }

    #[rstest]
    #[case(TradingMode::Paper, 4004)]
    #[case(TradingMode::Live, 4003)]
    fn container_port_matches_trading_mode(
        #[case] trading_mode: TradingMode,
        #[case] expected: u16,
    ) {
        assert_eq!(
            DockerizedIBGateway::container_port_for_mode(trading_mode),
            expected
        );
    }

    #[rstest]
    #[case(TradingMode::Paper, 4002)]
    #[case(TradingMode::Live, 4001)]
    fn new_reports_the_host_api_port(#[case] trading_mode: TradingMode, #[case] expected: u16) {
        let gateway = DockerizedIBGateway::new(
            crate::config::DockerizedIBGatewayConfig::builder()
                .username("test-user".to_string())
                .password("test-password".to_string())
                .trading_mode(trading_mode)
                .build(),
        )
        .unwrap();

        assert_eq!(gateway.port(), expected);
    }

    #[rstest]
    #[case("Forking ::: Starting IBC Gateway", false)]
    #[case("Started IB Gateway", false)]
    #[case("Login has completed", true)]
    #[case("Configuration tasks completed", true)]
    #[case("Logged in to backend", true)]
    #[case("Login successful", true)]
    fn ready_log_markers_are_strict(#[case] logs: &str, #[case] expected: bool) {
        assert_eq!(DockerizedIBGateway::logs_indicate_ready(logs), expected);
    }
}