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    #[pyo3(signature = (trader_id, strategy_id, instrument_id, client_order_id, account_id, reason, event_id, ts_event, ts_init, reconciliation, due_post_only=false))]
36    fn py_new(
37        trader_id: TraderId,
38        strategy_id: StrategyId,
39        instrument_id: InstrumentId,
40        client_order_id: ClientOrderId,
41        account_id: AccountId,
42        reason: &str,
43        event_id: UUID4,
44        ts_event: u64,
45        ts_init: u64,
46        reconciliation: bool,
47        due_post_only: bool,
48    ) -> PyResult<Self> {
49        let reason = Ustr::from_str(reason).map_err(to_pyvalue_err)?;
50        Ok(Self::new(
51            trader_id,
52            strategy_id,
53            instrument_id,
54            client_order_id,
55            account_id,
56            reason,
57            event_id,
58            ts_event.into(),
59            ts_init.into(),
60            reconciliation,
61            due_post_only,
62        ))
63    }
64
65    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
66        match op {
67            CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
68            CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
69            _ => py.NotImplemented(),
70        }
71    }
72
73    fn __repr__(&self) -> String {
74        format!("{self:?}")
75    }
76
77    fn __str__(&self) -> String {
78        self.to_string()
79    }
80
81    #[getter]
82    #[pyo3(name = "causation_id")]
83    fn py_causation_id(&self) -> Option<UUID4> {
84        self.causation_id
85    }
86
87    #[staticmethod]
88    #[pyo3(name = "from_dict")]
89    fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
90        from_dict_pyo3(py, values)
91    }
92
93    #[getter]
94    #[pyo3(name = "trader_id")]
95    fn py_trader_id(&self) -> TraderId {
96        self.trader_id
97    }
98
99    #[getter]
100    #[pyo3(name = "strategy_id")]
101    fn py_strategy_id(&self) -> StrategyId {
102        self.strategy_id
103    }
104
105    #[getter]
106    #[pyo3(name = "instrument_id")]
107    fn py_instrument_id(&self) -> InstrumentId {
108        self.instrument_id
109    }
110
111    #[getter]
112    #[pyo3(name = "client_order_id")]
113    fn py_client_order_id(&self) -> ClientOrderId {
114        self.client_order_id
115    }
116
117    #[getter]
118    #[pyo3(name = "account_id")]
119    fn py_account_id(&self) -> AccountId {
120        self.account_id
121    }
122
123    #[getter]
124    #[pyo3(name = "reason")]
125    fn py_reason(&self) -> String {
126        self.reason.to_string()
127    }
128
129    #[getter]
130    #[pyo3(name = "event_id")]
131    fn py_event_id(&self) -> UUID4 {
132        self.event_id
133    }
134
135    #[getter]
136    #[pyo3(name = "ts_event")]
137    fn py_ts_event(&self) -> u64 {
138        self.ts_event.as_u64()
139    }
140
141    #[getter]
142    #[pyo3(name = "ts_init")]
143    fn py_ts_init(&self) -> u64 {
144        self.ts_init.as_u64()
145    }
146
147    #[getter]
148    #[pyo3(name = "reconciliation")]
149    fn py_reconciliation(&self) -> bool {
150        self.reconciliation
151    }
152
153    #[getter]
154    #[pyo3(name = "due_post_only")]
155    fn py_due_post_only(&self) -> bool {
156        self.due_post_only
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!(OrderRejected))?;
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("account_id", self.account_id.to_string())?;
168        dict.set_item("reason", self.reason.to_string())?;
169        dict.set_item("event_id", self.event_id.to_string())?;
170        dict.set_item("ts_event", self.ts_event.as_u64())?;
171        dict.set_item("ts_init", self.ts_init.as_u64())?;
172        dict.set_item("reconciliation", self.reconciliation)?;
173        dict.set_item("due_post_only", self.due_post_only)?;
174        match self.causation_id {
175            Some(causation_id) => dict.set_item("causation_id", causation_id.to_string())?,
176            None => dict.set_item("causation_id", py.None())?,
177        }
178        Ok(dict.into())
179    }
180}