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 #[getter]
81 #[pyo3(name = "trader_id")]
82 fn py_trader_id(&self) -> TraderId {
83 self.trader_id
84 }
85
86 #[getter]
87 #[pyo3(name = "strategy_id")]
88 fn py_strategy_id(&self) -> StrategyId {
89 self.strategy_id
90 }
91
92 #[getter]
93 #[pyo3(name = "instrument_id")]
94 fn py_instrument_id(&self) -> InstrumentId {
95 self.instrument_id
96 }
97
98 #[getter]
99 #[pyo3(name = "client_order_id")]
100 fn py_client_order_id(&self) -> ClientOrderId {
101 self.client_order_id
102 }
103
104 #[getter]
105 #[pyo3(name = "venue_order_id")]
106 fn py_venue_order_id(&self) -> Option<VenueOrderId> {
107 self.venue_order_id
108 }
109
110 #[getter]
111 #[pyo3(name = "account_id")]
112 fn py_account_id(&self) -> Option<AccountId> {
113 self.account_id
114 }
115
116 #[getter]
117 #[pyo3(name = "event_id")]
118 fn py_event_id(&self) -> UUID4 {
119 self.event_id
120 }
121
122 #[getter]
123 #[pyo3(name = "ts_event")]
124 fn py_ts_event(&self) -> u64 {
125 self.ts_event.as_u64()
126 }
127
128 #[getter]
129 #[pyo3(name = "ts_init")]
130 fn py_ts_init(&self) -> u64 {
131 self.ts_init.as_u64()
132 }
133
134 #[getter]
135 #[pyo3(name = "reconciliation")]
136 fn py_reconciliation(&self) -> bool {
137 self.reconciliation != 0
138 }
139
140 #[pyo3(name = "to_dict")]
141 fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
142 let dict = PyDict::new(py);
143 dict.set_item("type", stringify!(OrderCanceled))?;
144 dict.set_item("trader_id", self.trader_id.to_string())?;
145 dict.set_item("strategy_id", self.strategy_id.to_string())?;
146 dict.set_item("instrument_id", self.instrument_id.to_string())?;
147 dict.set_item("client_order_id", self.client_order_id.to_string())?;
148 dict.set_item("event_id", self.event_id.to_string())?;
149 dict.set_item("ts_event", self.ts_event.as_u64())?;
150 dict.set_item("ts_init", self.ts_init.as_u64())?;
151 dict.set_item("reconciliation", self.reconciliation)?;
152 match self.venue_order_id {
153 Some(venue_order_id) => dict.set_item("venue_order_id", venue_order_id.to_string())?,
154 None => dict.set_item("venue_order_id", py.None())?,
155 }
156 match self.account_id {
157 Some(account_id) => dict.set_item("account_id", account_id.to_string())?,
158 None => dict.set_item("account_id", py.None())?,
159 }
160 Ok(dict.into())
161 }
162}