nautilus_interactive_brokers/python/
gateway.rs1#[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 #[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")]
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 #[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 #[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 #[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}