nautilus_model/python/events/order/
cancel_rejected.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 std::str::FromStr;
17
18use nautilus_core::{
19    UUID4,
20    python::{IntoPyObjectNautilusExt, serialization::from_dict_pyo3, to_pyvalue_err},
21};
22use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
23use ustr::Ustr;
24
25use crate::{
26    events::OrderCancelRejected,
27    identifiers::{AccountId, ClientOrderId, InstrumentId, StrategyId, TraderId, VenueOrderId},
28};
29
30#[pymethods]
31impl OrderCancelRejected {
32    #[allow(clippy::too_many_arguments)]
33    #[new]
34    #[pyo3(signature = (trader_id, strategy_id, instrument_id, client_order_id, reason, event_id, ts_event, ts_init, reconciliation, venue_order_id=None, account_id=None))]
35    fn py_new(
36        trader_id: TraderId,
37        strategy_id: StrategyId,
38        instrument_id: InstrumentId,
39        client_order_id: ClientOrderId,
40        reason: &str,
41        event_id: UUID4,
42        ts_event: u64,
43        ts_init: u64,
44        reconciliation: bool,
45        venue_order_id: Option<VenueOrderId>,
46        account_id: Option<AccountId>,
47    ) -> PyResult<Self> {
48        let reason = Ustr::from_str(reason).map_err(to_pyvalue_err)?;
49        Ok(Self::new(
50            trader_id,
51            strategy_id,
52            instrument_id,
53            client_order_id,
54            reason,
55            event_id,
56            ts_event.into(),
57            ts_init.into(),
58            reconciliation,
59            venue_order_id,
60            account_id,
61        ))
62    }
63
64    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
65        match op {
66            CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
67            CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
68            _ => py.NotImplemented(),
69        }
70    }
71
72    fn __repr__(&self) -> String {
73        format!("{self:?}")
74    }
75
76    fn __str__(&self) -> String {
77        self.to_string()
78    }
79
80    #[staticmethod]
81    #[pyo3(name = "from_dict")]
82    fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
83        from_dict_pyo3(py, values)
84    }
85
86    #[getter]
87    #[pyo3(name = "trader_id")]
88    fn py_trader_id(&self) -> TraderId {
89        self.trader_id
90    }
91
92    #[getter]
93    #[pyo3(name = "strategy_id")]
94    fn py_strategy_id(&self) -> StrategyId {
95        self.strategy_id
96    }
97
98    #[getter]
99    #[pyo3(name = "instrument_id")]
100    fn py_instrument_id(&self) -> InstrumentId {
101        self.instrument_id
102    }
103
104    #[getter]
105    #[pyo3(name = "client_order_id")]
106    fn py_client_order_id(&self) -> ClientOrderId {
107        self.client_order_id
108    }
109
110    #[getter]
111    #[pyo3(name = "venue_order_id")]
112    fn py_venue_order_id(&self) -> Option<VenueOrderId> {
113        self.venue_order_id
114    }
115
116    #[getter]
117    #[pyo3(name = "account_id")]
118    fn py_account_id(&self) -> Option<AccountId> {
119        self.account_id
120    }
121
122    #[getter]
123    #[pyo3(name = "reason")]
124    fn py_reason(&self) -> String {
125        self.reason.as_str().to_string()
126    }
127
128    #[getter]
129    #[pyo3(name = "event_id")]
130    fn py_event_id(&self) -> UUID4 {
131        self.event_id
132    }
133
134    #[getter]
135    #[pyo3(name = "ts_event")]
136    fn py_ts_event(&self) -> u64 {
137        self.ts_event.as_u64()
138    }
139
140    #[getter]
141    #[pyo3(name = "ts_init")]
142    fn py_ts_init(&self) -> u64 {
143        self.ts_init.as_u64()
144    }
145
146    #[getter]
147    #[pyo3(name = "reconciliation")]
148    fn py_reconciliation(&self) -> bool {
149        self.reconciliation != 0
150    }
151
152    #[pyo3(name = "to_dict")]
153    fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
154        let dict = PyDict::new(py);
155        dict.set_item("type", stringify!(OrderCancelRejected))?;
156        dict.set_item("trader_id", self.trader_id.to_string())?;
157        dict.set_item("strategy_id", self.strategy_id.to_string())?;
158        dict.set_item("instrument_id", self.instrument_id.to_string())?;
159        dict.set_item("client_order_id", self.client_order_id.to_string())?;
160        dict.set_item("reason", self.reason.as_str())?;
161        dict.set_item("event_id", self.event_id.to_string())?;
162        dict.set_item("ts_event", self.ts_event.as_u64())?;
163        dict.set_item("ts_init", self.ts_init.as_u64())?;
164        dict.set_item("reconciliation", self.reconciliation)?;
165        match self.venue_order_id {
166            Some(venue_order_id) => dict.set_item("venue_order_id", venue_order_id.to_string())?,
167            None => dict.set_item("venue_order_id", py.None())?,
168        }
169        match self.account_id {
170            Some(account_id) => dict.set_item("account_id", account_id.to_string())?,
171            None => dict.set_item("account_id", py.None())?,
172        }
173        Ok(dict.into())
174    }
175}