Skip to main content

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