nautilus_model/python/events/order/
updated.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::OrderUpdated,
24 identifiers::{AccountId, ClientOrderId, InstrumentId, StrategyId, TraderId, VenueOrderId},
25 types::{Price, Quantity},
26};
27
28#[pymethods]
29impl OrderUpdated {
30 #[allow(clippy::too_many_arguments)]
31 #[new]
32 #[pyo3(signature = (trader_id, strategy_id, instrument_id, client_order_id, quantity, event_id, ts_event, ts_init, reconciliation, venue_order_id=None, account_id=None, price=None, trigger_price=None, protection_price=None))]
33 fn py_new(
34 trader_id: TraderId,
35 strategy_id: StrategyId,
36 instrument_id: InstrumentId,
37 client_order_id: ClientOrderId,
38 quantity: Quantity,
39 event_id: UUID4,
40 ts_event: u64,
41 ts_init: u64,
42 reconciliation: bool,
43 venue_order_id: Option<VenueOrderId>,
44 account_id: Option<AccountId>,
45 price: Option<Price>,
46 trigger_price: Option<Price>,
47 protection_price: Option<Price>,
48 ) -> Self {
49 Self::new(
50 trader_id,
51 strategy_id,
52 instrument_id,
53 client_order_id,
54 quantity,
55 event_id,
56 ts_event.into(),
57 ts_init.into(),
58 reconciliation,
59 venue_order_id,
60 account_id,
61 price,
62 trigger_price,
63 protection_price,
64 )
65 }
66
67 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
68 match op {
69 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
70 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
71 _ => py.NotImplemented(),
72 }
73 }
74
75 fn __repr__(&self) -> String {
76 format!("{self:?}")
77 }
78
79 fn __str__(&self) -> String {
80 self.to_string()
81 }
82
83 #[staticmethod]
84 #[pyo3(name = "from_dict")]
85 fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
86 from_dict_pyo3(py, values)
87 }
88
89 #[getter]
90 #[pyo3(name = "trader_id")]
91 fn py_trader_id(&self) -> TraderId {
92 self.trader_id
93 }
94
95 #[getter]
96 #[pyo3(name = "strategy_id")]
97 fn py_strategy_id(&self) -> StrategyId {
98 self.strategy_id
99 }
100
101 #[getter]
102 #[pyo3(name = "instrument_id")]
103 fn py_instrument_id(&self) -> InstrumentId {
104 self.instrument_id
105 }
106
107 #[getter]
108 #[pyo3(name = "client_order_id")]
109 fn py_client_order_id(&self) -> ClientOrderId {
110 self.client_order_id
111 }
112
113 #[getter]
114 #[pyo3(name = "venue_order_id")]
115 fn py_venue_order_id(&self) -> Option<VenueOrderId> {
116 self.venue_order_id
117 }
118
119 #[getter]
120 #[pyo3(name = "account_id")]
121 fn py_account_id(&self) -> Option<AccountId> {
122 self.account_id
123 }
124
125 #[getter]
126 #[pyo3(name = "quantity")]
127 fn py_quantity(&self) -> Quantity {
128 self.quantity
129 }
130
131 #[getter]
132 #[pyo3(name = "price")]
133 fn py_price(&self) -> Option<Price> {
134 self.price
135 }
136
137 #[getter]
138 #[pyo3(name = "trigger_price")]
139 fn py_trigger_price(&self) -> Option<Price> {
140 self.trigger_price
141 }
142
143 #[getter]
144 #[pyo3(name = "event_id")]
145 fn py_event_id(&self) -> UUID4 {
146 self.event_id
147 }
148
149 #[getter]
150 #[pyo3(name = "ts_event")]
151 fn py_ts_event(&self) -> u64 {
152 self.ts_event.as_u64()
153 }
154
155 #[getter]
156 #[pyo3(name = "ts_init")]
157 fn py_ts_init(&self) -> u64 {
158 self.ts_init.as_u64()
159 }
160
161 #[getter]
162 #[pyo3(name = "reconciliation")]
163 fn py_reconciliation(&self) -> bool {
164 self.reconciliation != 0
165 }
166
167 #[pyo3(name = "to_dict")]
168 fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
169 let dict = PyDict::new(py);
170 dict.set_item("type", stringify!(OrderUpdated))?;
171 dict.set_item("trader_id", self.trader_id.to_string())?;
172 dict.set_item("strategy_id", self.strategy_id.to_string())?;
173 dict.set_item("instrument_id", self.instrument_id.to_string())?;
174 dict.set_item("client_order_id", self.client_order_id.to_string())?;
175 dict.set_item("quantity", self.quantity.to_string())?;
176 dict.set_item("event_id", self.event_id.to_string())?;
177 dict.set_item("ts_event", self.ts_event.as_u64())?;
178 dict.set_item("ts_init", self.ts_init.as_u64())?;
179 dict.set_item("reconciliation", self.reconciliation)?;
180 match self.venue_order_id {
181 Some(venue_order_id) => dict.set_item("venue_order_id", venue_order_id.to_string())?,
182 None => dict.set_item("venue_order_id", py.None())?,
183 }
184 match self.account_id {
185 Some(account_id) => dict.set_item("account_id", account_id.to_string())?,
186 None => dict.set_item("account_id", py.None())?,
187 }
188 match self.price {
189 Some(price) => dict.set_item("price", price.to_string())?,
190 None => dict.set_item("price", py.None())?,
191 }
192 match self.trigger_price {
193 Some(trigger_price) => dict.set_item("trigger_price", trigger_price.to_string())?,
194 None => dict.set_item("trigger_price", py.None())?,
195 }
196 Ok(dict.into())
197 }
198}