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]
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    ///
68    /// # Arguments
69    ///
70    /// * `wait` - Optional wait time in seconds
71    #[pyo3(name = "start")]
72    fn py_start<'py>(&self, py: Python<'py>, wait: Option<u64>) -> PyResult<Bound<'py, PyAny>> {
73        let mut gateway = self.clone();
74        pyo3_async_runtimes::tokio::future_into_py(py, async move {
75            gateway
76                .start(wait)
77                .await
78                .map_err(|e| to_pyruntime_err(format!("{e}")))
79        })
80    }
81
82    #[pyo3(name = "start_blocking")]
83    fn py_start_blocking(&self, wait: Option<u64>) -> PyResult<()> {
84        let mut gateway = self.clone();
85        get_runtime()
86            .block_on(async move { gateway.start(wait).await })
87            .map_err(|e| to_pyruntime_err(format!("{e}")))
88    }
89
90    /// Safely start the gateway.
91    ///
92    /// # Arguments
93    ///
94    /// * `wait` - Optional wait time in seconds
95    #[pyo3(name = "safe_start")]
96    fn py_safe_start<'py>(
97        &self,
98        py: Python<'py>,
99        wait: Option<u64>,
100    ) -> PyResult<Bound<'py, PyAny>> {
101        let mut gateway = self.clone();
102        pyo3_async_runtimes::tokio::future_into_py(py, async move {
103            gateway
104                .safe_start(wait)
105                .await
106                .map_err(|e| to_pyruntime_err(format!("{e}")))
107        })
108    }
109
110    #[pyo3(name = "safe_start_blocking")]
111    fn py_safe_start_blocking(&self, wait: Option<u64>) -> PyResult<()> {
112        let mut gateway = self.clone();
113        get_runtime()
114            .block_on(async move { gateway.safe_start(wait).await })
115            .map_err(|e| to_pyruntime_err(format!("{e}")))
116    }
117
118    /// Stop the gateway.
119    #[pyo3(name = "stop")]
120    fn py_stop<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
121        let gateway = self.clone();
122        pyo3_async_runtimes::tokio::future_into_py(py, async move {
123            gateway
124                .stop()
125                .await
126                .map_err(|e| to_pyruntime_err(format!("{e}")))
127        })
128    }
129
130    #[pyo3(name = "stop_blocking")]
131    fn py_stop_blocking(&self) -> PyResult<()> {
132        let gateway = self.clone();
133        get_runtime()
134            .block_on(async move { gateway.stop().await })
135            .map_err(|e| to_pyruntime_err(format!("{e}")))
136    }
137
138    /// Get container status.
139    #[pyo3(name = "container_status")]
140    fn py_container_status<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
141        let gateway = self.clone();
142        pyo3_async_runtimes::tokio::future_into_py(py, async move {
143            gateway
144                .container_status()
145                .await
146                .map_err(|e| to_pyruntime_err(format!("{e}")))
147        })
148    }
149}