nautilus_execution/python/
mass_status.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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
16use indexmap::IndexMap;
17use nautilus_core::{
18    UUID4,
19    python::{IntoPyObjectNautilusExt, serialization::from_dict_pyo3},
20};
21use nautilus_model::identifiers::{AccountId, ClientId, InstrumentId, Venue, VenueOrderId};
22use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
23
24use crate::reports::{
25    fill::FillReport, mass_status::ExecutionMassStatus, order::OrderStatusReport,
26    position::PositionStatusReport,
27};
28
29#[pymethods]
30impl ExecutionMassStatus {
31    #[new]
32    #[pyo3(signature = (client_id, account_id, venue, ts_init, report_id=None))]
33    fn py_new(
34        client_id: ClientId,
35        account_id: AccountId,
36        venue: Venue,
37        ts_init: u64,
38        report_id: Option<UUID4>,
39    ) -> PyResult<Self> {
40        Ok(Self::new(
41            client_id,
42            account_id,
43            venue,
44            ts_init.into(),
45            report_id,
46        ))
47    }
48
49    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
50        match op {
51            CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
52            CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
53            _ => py.NotImplemented(),
54        }
55    }
56
57    fn __repr__(&self) -> String {
58        self.to_string()
59    }
60
61    fn __str__(&self) -> String {
62        self.to_string()
63    }
64
65    #[getter]
66    #[pyo3(name = "client_id")]
67    const fn py_client_id(&self) -> ClientId {
68        self.client_id
69    }
70
71    #[getter]
72    #[pyo3(name = "account_id")]
73    const fn py_account_id(&self) -> AccountId {
74        self.account_id
75    }
76
77    #[getter]
78    #[pyo3(name = "venue")]
79    const fn py_venue(&self) -> Venue {
80        self.venue
81    }
82
83    #[getter]
84    #[pyo3(name = "report_id")]
85    const fn py_report_id(&self) -> UUID4 {
86        self.report_id
87    }
88
89    #[getter]
90    #[pyo3(name = "ts_init")]
91    const fn py_ts_init(&self) -> u64 {
92        self.ts_init.as_u64()
93    }
94
95    #[getter]
96    #[pyo3(name = "order_reports")]
97    fn py_order_reports(&self) -> PyResult<IndexMap<VenueOrderId, OrderStatusReport>> {
98        Ok(self.order_reports())
99    }
100
101    #[getter]
102    #[pyo3(name = "fill_reports")]
103    fn py_fill_reports(&self) -> PyResult<IndexMap<VenueOrderId, Vec<FillReport>>> {
104        Ok(self.fill_reports())
105    }
106
107    #[getter]
108    #[pyo3(name = "position_reports")]
109    fn py_position_reports(&self) -> PyResult<IndexMap<InstrumentId, Vec<PositionStatusReport>>> {
110        Ok(self.position_reports())
111    }
112
113    #[pyo3(name = "add_order_reports")]
114    fn py_add_order_reports(&mut self, reports: Vec<OrderStatusReport>) -> PyResult<()> {
115        self.add_order_reports(reports);
116        Ok(())
117    }
118
119    #[pyo3(name = "add_fill_reports")]
120    fn py_add_fill_reports(&mut self, reports: Vec<FillReport>) -> PyResult<()> {
121        self.add_fill_reports(reports);
122        Ok(())
123    }
124
125    #[pyo3(name = "add_position_reports")]
126    fn py_add_position_reports(&mut self, reports: Vec<PositionStatusReport>) -> PyResult<()> {
127        self.add_position_reports(reports);
128        Ok(())
129    }
130
131    #[staticmethod]
132    #[pyo3(name = "from_dict")]
133    pub fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
134        from_dict_pyo3(py, values)
135    }
136
137    #[pyo3(name = "to_dict")]
138    fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
139        let dict = PyDict::new(py);
140        dict.set_item("type", stringify!(ExecutionMassStatus))?;
141        dict.set_item("client_id", self.client_id.to_string())?;
142        dict.set_item("account_id", self.account_id.to_string())?;
143        dict.set_item("venue", self.venue.to_string())?;
144        dict.set_item("report_id", self.report_id.to_string())?;
145        dict.set_item("ts_init", self.ts_init.as_u64())?;
146
147        let order_reports_dict = PyDict::new(py);
148        for (key, value) in &self.order_reports() {
149            order_reports_dict.set_item(key.to_string(), value.py_to_dict(py)?)?;
150        }
151        dict.set_item("order_reports", order_reports_dict)?;
152
153        let fill_reports_dict = PyDict::new(py);
154        for (key, value) in &self.fill_reports() {
155            let reports: PyResult<Vec<_>> = value.iter().map(|r| r.py_to_dict(py)).collect();
156            fill_reports_dict.set_item(key.to_string(), reports?)?;
157        }
158        dict.set_item("fill_reports", fill_reports_dict)?;
159
160        let position_reports_dict = PyDict::new(py);
161        for (key, value) in &self.position_reports() {
162            let reports: PyResult<Vec<_>> = value.iter().map(|r| r.py_to_dict(py)).collect();
163            position_reports_dict.set_item(key.to_string(), reports?)?;
164        }
165        dict.set_item("position_reports", position_reports_dict)?;
166
167        Ok(dict.into())
168    }
169}