Skip to main content

nautilus_model/python/events/order/
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::OrderRejected,
27    identifiers::{AccountId, ClientOrderId, InstrumentId, StrategyId, TraderId},
28};
29#[pymethods]
30#[pyo3_stub_gen::derive::gen_stub_pymethods]
31impl OrderRejected {
32    /// Represents an event where an order has been rejected by the trading venue.
33    #[expect(clippy::too_many_arguments)]
34    #[new]
35    fn py_new(
36        trader_id: TraderId,
37        strategy_id: StrategyId,
38        instrument_id: InstrumentId,
39        client_order_id: ClientOrderId,
40        account_id: AccountId,
41        reason: &str,
42        event_id: UUID4,
43        ts_event: u64,
44        ts_init: u64,
45        reconciliation: bool,
46    ) -> PyResult<Self> {
47        let reason = Ustr::from_str(reason).map_err(to_pyvalue_err)?;
48        Ok(Self::new(
49            trader_id,
50            strategy_id,
51            instrument_id,
52            client_order_id,
53            account_id,
54            reason,
55            event_id,
56            ts_event.into(),
57            ts_init.into(),
58            reconciliation,
59            false, // due_post_only defaults to false for Python constructor
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    #[staticmethod]
80    #[pyo3(name = "from_dict")]
81    fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
82        from_dict_pyo3(py, values)
83    }
84
85    #[getter]
86    #[pyo3(name = "trader_id")]
87    fn py_trader_id(&self) -> TraderId {
88        self.trader_id
89    }
90
91    #[getter]
92    #[pyo3(name = "strategy_id")]
93    fn py_strategy_id(&self) -> StrategyId {
94        self.strategy_id
95    }
96
97    #[getter]
98    #[pyo3(name = "instrument_id")]
99    fn py_instrument_id(&self) -> InstrumentId {
100        self.instrument_id
101    }
102
103    #[getter]
104    #[pyo3(name = "client_order_id")]
105    fn py_client_order_id(&self) -> ClientOrderId {
106        self.client_order_id
107    }
108
109    #[getter]
110    #[pyo3(name = "account_id")]
111    fn py_account_id(&self) -> AccountId {
112        self.account_id
113    }
114
115    #[getter]
116    #[pyo3(name = "reason")]
117    fn py_reason(&self) -> String {
118        self.reason.as_str().to_string()
119    }
120
121    #[getter]
122    #[pyo3(name = "event_id")]
123    fn py_event_id(&self) -> UUID4 {
124        self.event_id
125    }
126
127    #[getter]
128    #[pyo3(name = "ts_event")]
129    fn py_ts_event(&self) -> u64 {
130        self.ts_event.as_u64()
131    }
132
133    #[getter]
134    #[pyo3(name = "ts_init")]
135    fn py_ts_init(&self) -> u64 {
136        self.ts_init.as_u64()
137    }
138
139    #[getter]
140    #[pyo3(name = "reconciliation")]
141    fn py_reconciliation(&self) -> bool {
142        self.reconciliation != 0
143    }
144
145    #[getter]
146    #[pyo3(name = "due_post_only")]
147    fn py_due_post_only(&self) -> bool {
148        self.due_post_only != 0
149    }
150
151    #[pyo3(name = "to_dict")]
152    fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
153        let dict = PyDict::new(py);
154        dict.set_item("type", stringify!(OrderRejected))?;
155        dict.set_item("trader_id", self.trader_id.to_string())?;
156        dict.set_item("strategy_id", self.strategy_id.to_string())?;
157        dict.set_item("instrument_id", self.instrument_id.to_string())?;
158        dict.set_item("client_order_id", self.client_order_id.to_string())?;
159        dict.set_item("account_id", self.account_id.to_string())?;
160        dict.set_item("reason", self.reason.to_string())?;
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        dict.set_item("due_post_only", self.due_post_only)?;
166        Ok(dict.into())
167    }
168}