Skip to main content

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