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