nautilus_model/python/events/order/
rejected.rs

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