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::DockerizedIBGateway;
29
30#[cfg(feature = "gateway")]
31#[pymethods]
32impl DockerizedIBGateway {
33    #[new]
34    fn py_new(config: DockerizedIBGatewayConfig) -> PyResult<Self> {
35        Self::new(config).map_err(|e| to_pyruntime_err(format!("{e}")))
36    }
37
38    fn __repr__(&self) -> String {
39        format!(
40            "DockerizedIBGateway(container_name={}, host={}, port={})",
41            self.container_name(),
42            self.host(),
43            self.port()
44        )
45    }
46
47    /// Get the container name.
48    #[getter("container_name")]
49    fn py_container_name(&self) -> String {
50        self.container_name().to_string()
51    }
52
53    /// Get the host address.
54    #[getter("host")]
55    fn py_host(&self) -> String {
56        self.host().to_string()
57    }
58
59    /// Get the port.
60    #[getter("port")]
61    fn py_port(&self) -> u16 {
62        self.port()
63    }
64
65    /// Start the gateway.
66    ///
67    /// # Arguments
68    ///
69    /// * `wait` - Optional wait time in seconds
70    #[pyo3(name = "start")]
71    fn py_start<'py>(&self, py: Python<'py>, wait: Option<u64>) -> PyResult<Bound<'py, PyAny>> {
72        let mut gateway = self.clone();
73        pyo3_async_runtimes::tokio::future_into_py(py, async move {
74            gateway
75                .start(wait)
76                .await
77                .map_err(|e| to_pyruntime_err(format!("{e}")))
78        })
79    }
80
81    #[pyo3(name = "start_blocking")]
82    fn py_start_blocking(&self, wait: Option<u64>) -> PyResult<()> {
83        let mut gateway = self.clone();
84        get_runtime()
85            .block_on(async move { gateway.start(wait).await })
86            .map_err(|e| to_pyruntime_err(format!("{e}")))
87    }
88
89    /// Safely start the gateway.
90    ///
91    /// # Arguments
92    ///
93    /// * `wait` - Optional wait time in seconds
94    #[pyo3(name = "safe_start")]
95    fn py_safe_start<'py>(
96        &self,
97        py: Python<'py>,
98        wait: Option<u64>,
99    ) -> PyResult<Bound<'py, PyAny>> {
100        let mut gateway = self.clone();
101        pyo3_async_runtimes::tokio::future_into_py(py, async move {
102            gateway
103                .safe_start(wait)
104                .await
105                .map_err(|e| to_pyruntime_err(format!("{e}")))
106        })
107    }
108
109    #[pyo3(name = "safe_start_blocking")]
110    fn py_safe_start_blocking(&self, wait: Option<u64>) -> PyResult<()> {
111        let mut gateway = self.clone();
112        get_runtime()
113            .block_on(async move { gateway.safe_start(wait).await })
114            .map_err(|e| to_pyruntime_err(format!("{e}")))
115    }
116
117    /// Stop the gateway.
118    #[pyo3(name = "stop")]
119    fn py_stop<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
120        let gateway = self.clone();
121        pyo3_async_runtimes::tokio::future_into_py(py, async move {
122            gateway
123                .stop()
124                .await
125                .map_err(|e| to_pyruntime_err(format!("{e}")))
126        })
127    }
128
129    #[pyo3(name = "stop_blocking")]
130    fn py_stop_blocking(&self) -> PyResult<()> {
131        let gateway = self.clone();
132        get_runtime()
133            .block_on(async move { gateway.stop().await })
134            .map_err(|e| to_pyruntime_err(format!("{e}")))
135    }
136
137    /// Get container status.
138    #[pyo3(name = "container_status")]
139    fn py_container_status<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
140        let gateway = self.clone();
141        pyo3_async_runtimes::tokio::future_into_py(py, async move {
142            gateway
143                .container_status()
144                .await
145                .map_err(|e| to_pyruntime_err(format!("{e}")))
146        })
147    }
148}