nautilus_model/python/events/order/
canceled.rs1use nautilus_core::{
17 UUID4,
18 python::{IntoPyObjectNautilusExt, serialization::from_dict_pyo3},
19};
20use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
21
22use crate::{
23 events::OrderCanceled,
24 identifiers::{AccountId, ClientOrderId, InstrumentId, StrategyId, TraderId, VenueOrderId},
25};
26
27#[pymethods]
28impl OrderCanceled {
29 #[allow(clippy::too_many_arguments)]
30 #[new]
31 #[pyo3(signature = (trader_id, strategy_id, instrument_id, client_order_id, event_id, ts_event, ts_init, reconciliation, venue_order_id=None, account_id=None))]
32 fn py_new(
33 trader_id: TraderId,
34 strategy_id: StrategyId,
35 instrument_id: InstrumentId,
36 client_order_id: ClientOrderId,
37 event_id: UUID4,
38 ts_event: u64,
39 ts_init: u64,
40 reconciliation: bool,
41 venue_order_id: Option<VenueOrderId>,
42 account_id: Option<AccountId>,
43 ) -> Self {
44 Self::new(
45 trader_id,
46 strategy_id,
47 instrument_id,
48 client_order_id,
49 event_id,
50 ts_event.into(),
51 ts_init.into(),
52 reconciliation,
53 venue_order_id,
54 account_id,
55 )
56 }
57
58 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
59 match op {
60 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
61 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
62 _ => py.NotImplemented(),
63 }
64 }
65
66 fn __repr__(&self) -> String {
67 format!("{:?}", self)
68 }
69
70 fn __str__(&self) -> String {
71 self.to_string()
72 }
73
74 #[staticmethod]
75 #[pyo3(name = "from_dict")]
76 fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
77 from_dict_pyo3(py, values)
78 }
79
80 #[pyo3(name = "to_dict")]
81 fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
82 let dict = PyDict::new(py);
83 dict.set_item("type", stringify!(OrderCanceled))?;
84 dict.set_item("trader_id", self.trader_id.to_string())?;
85 dict.set_item("strategy_id", self.strategy_id.to_string())?;
86 dict.set_item("instrument_id", self.instrument_id.to_string())?;
87 dict.set_item("client_order_id", self.client_order_id.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 dict.set_item("reconciliation", self.reconciliation)?;
92 match self.venue_order_id {
93 Some(venue_order_id) => dict.set_item("venue_order_id", venue_order_id.to_string())?,
94 None => dict.set_item("venue_order_id", py.None())?,
95 }
96 match self.account_id {
97 Some(account_id) => dict.set_item("account_id", account_id.to_string())?,
98 None => dict.set_item("account_id", py.None())?,
99 }
100 Ok(dict.into())
101 }
102}