Skip to main content

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