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