nautilus_interactive_brokers/python/
gateway.rs1#[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 #[getter("container_name")]
50 fn py_container_name(&self) -> String {
51 self.container_name().to_string()
52 }
53
54 #[getter("host")]
56 fn py_host(&self) -> String {
57 self.host().to_string()
58 }
59
60 #[getter("port")]
62 fn py_port(&self) -> u16 {
63 self.port()
64 }
65
66 #[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 #[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 #[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 #[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}