nautilus_model/python/events/order/
denied.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    python::{serialization::from_dict_pyo3, to_pyvalue_err},
20    UUID4,
21};
22use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
23use ustr::Ustr;
24
25use crate::{
26    events::OrderDenied,
27    identifiers::{ClientOrderId, InstrumentId, StrategyId, TraderId},
28};
29
30#[pymethods]
31impl OrderDenied {
32    #[allow(clippy::too_many_arguments)]
33    #[new]
34    fn py_new(
35        trader_id: TraderId,
36        strategy_id: StrategyId,
37        instrument_id: InstrumentId,
38        client_order_id: ClientOrderId,
39        reason: &str,
40        event_id: UUID4,
41        ts_event: u64,
42        ts_init: u64,
43    ) -> PyResult<Self> {
44        let reason = Ustr::from_str(reason).map_err(to_pyvalue_err)?;
45        Ok(Self::new(
46            trader_id,
47            strategy_id,
48            instrument_id,
49            client_order_id,
50            reason,
51            event_id,
52            ts_event.into(),
53            ts_init.into(),
54        ))
55    }
56
57    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
58        match op {
59            CompareOp::Eq => self.eq(other).into_py(py),
60            CompareOp::Ne => self.ne(other).into_py(py),
61            _ => py.NotImplemented(),
62        }
63    }
64
65    fn __repr__(&self) -> String {
66        format!("{:?}", self)
67    }
68
69    fn __str__(&self) -> String {
70        self.to_string()
71    }
72
73    #[staticmethod]
74    #[pyo3(name = "from_dict")]
75    fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
76        from_dict_pyo3(py, values)
77    }
78
79    #[pyo3(name = "to_dict")]
80    fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
81        let dict = PyDict::new(py);
82        dict.set_item("type", stringify!(OrderDenied))?;
83        dict.set_item("trader_id", self.trader_id.to_string())?;
84        dict.set_item("strategy_id", self.strategy_id.to_string())?;
85        dict.set_item("instrument_id", self.instrument_id.to_string())?;
86        dict.set_item("client_order_id", self.client_order_id.to_string())?;
87        dict.set_item("reason", self.reason.to_string())?;
88        dict.set_item("event_id", self.event_id.to_string())?;
89        dict.set_item("ts_event", self.ts_event.as_u64())?;
90        dict.set_item("ts_init", self.ts_init.as_u64())?;
91        Ok(dict.into())
92    }
93}