Skip to main content

nautilus_interactive_brokers/python/
gateway.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Python bindings for the Interactive Brokers gateway management.
17
18#[cfg(feature = "gateway")]
19use nautilus_common::live::get_runtime;
20#[cfg(feature = "gateway")]
21use nautilus_core::python::to_pyruntime_err;
22#[cfg(feature = "gateway")]
23use pyo3::prelude::*;
24
25#[cfg(feature = "gateway")]
26use crate::config::DockerizedIBGatewayConfig;
27#[cfg(feature = "gateway")]
28use crate::gateway::dockerized::{ContainerStatus, DockerizedIBGateway};
29
30#[cfg(feature = "gateway")]
31#[pymethods]
32impl ContainerStatus {
33    #[classattr]
34    const NO_CONTAINER: Self = Self::NoContainer;
35
36    #[classattr]
37    const CONTAINER_CREATED: Self = Self::ContainerCreated;
38
39    #[classattr]
40    const CONTAINER_STARTING: Self = Self::ContainerStarting;
41
42    #[classattr]
43    const CONTAINER_STOPPED: Self = Self::ContainerStopped;
44
45    #[classattr]
46    const NOT_LOGGED_IN: Self = Self::NotLoggedIn;
47
48    #[classattr]
49    const READY: Self = Self::Ready;
50
51    #[classattr]
52    const UNKNOWN: Self = Self::Unknown;
53}
54
55#[cfg(feature = "gateway")]
56#[pymethods]
57impl DockerizedIBGateway {
58    #[new]
59    fn py_new(config: DockerizedIBGatewayConfig) -> PyResult<Self> {
60        Self::new(config).map_err(|e| to_pyruntime_err(format!("{e}")))
61    }
62
63    fn __repr__(&self) -> String {
64        format!(
65            "DockerizedIBGateway(container_name={}, host={}, port={})",
66            self.container_name(),
67            self.host(),
68            self.port()
69        )
70    }
71
72    /// Get the container name.
73    #[getter("container_name")]
74    fn py_container_name(&self) -> String {
75        self.container_name().to_string()
76    }
77
78    /// Get the host address.
79    #[getter("host")]
80    fn py_host(&self) -> String {
81        self.host().to_string()
82    }
83
84    /// Get the port.
85    #[getter("port")]
86    fn py_port(&self) -> u16 {
87        self.port()
88    }
89
90    /// Start the gateway.
91    ///
92    /// # Arguments
93    ///
94    /// * `wait` - Optional wait time in seconds
95    #[pyo3(name = "start")]
96    fn py_start<'py>(&self, py: Python<'py>, wait: Option<u64>) -> PyResult<Bound<'py, PyAny>> {
97        let mut gateway = self.clone();
98        pyo3_async_runtimes::tokio::future_into_py(py, async move {
99            gateway
100                .start(wait)
101                .await
102                .map_err(|e| to_pyruntime_err(format!("{e}")))
103        })
104    }
105
106    #[pyo3(name = "start_blocking")]
107    fn py_start_blocking(&self, wait: Option<u64>) -> PyResult<()> {
108        let mut gateway = self.clone();
109        get_runtime()
110            .block_on(async move { gateway.start(wait).await })
111            .map_err(|e| to_pyruntime_err(format!("{e}")))
112    }
113
114    /// Safely start the gateway.
115    ///
116    /// # Arguments
117    ///
118    /// * `wait` - Optional wait time in seconds
119    #[pyo3(name = "safe_start")]
120    fn py_safe_start<'py>(
121        &self,
122        py: Python<'py>,
123        wait: Option<u64>,
124    ) -> PyResult<Bound<'py, PyAny>> {
125        let mut gateway = self.clone();
126        pyo3_async_runtimes::tokio::future_into_py(py, async move {
127            gateway
128                .safe_start(wait)
129                .await
130                .map_err(|e| to_pyruntime_err(format!("{e}")))
131        })
132    }
133
134    #[pyo3(name = "safe_start_blocking")]
135    fn py_safe_start_blocking(&self, wait: Option<u64>) -> PyResult<()> {
136        let mut gateway = self.clone();
137        get_runtime()
138            .block_on(async move { gateway.safe_start(wait).await })
139            .map_err(|e| to_pyruntime_err(format!("{e}")))
140    }
141
142    /// Stop the gateway.
143    #[pyo3(name = "stop")]
144    fn py_stop<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
145        let gateway = self.clone();
146        pyo3_async_runtimes::tokio::future_into_py(py, async move {
147            gateway
148                .stop()
149                .await
150                .map_err(|e| to_pyruntime_err(format!("{e}")))
151        })
152    }
153
154    #[pyo3(name = "stop_blocking")]
155    fn py_stop_blocking(&self) -> PyResult<()> {
156        let gateway = self.clone();
157        get_runtime()
158            .block_on(async move { gateway.stop().await })
159            .map_err(|e| to_pyruntime_err(format!("{e}")))
160    }
161
162    /// Get container status.
163    #[pyo3(name = "container_status")]
164    fn py_container_status<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
165        let gateway = self.clone();
166        pyo3_async_runtimes::tokio::future_into_py(py, async move {
167            gateway
168                .container_status()
169                .await
170                .map_err(|e| to_pyruntime_err(format!("{e}")))
171        })
172    }
173}