1use nautilus_core::{
17 UUID4,
18 python::{IntoPyObjectNautilusExt, serialization::from_dict_pyo3},
19};
20use pyo3::{
21 Py,
22 basic::CompareOp,
23 prelude::*,
24 types::{PyDict, PyList},
25};
26use rust_decimal::Decimal;
27
28use crate::{
29 enums::{
30 ContingencyType, OrderSide, OrderStatus, OrderType, TimeInForce, TrailingOffsetType,
31 TriggerType,
32 },
33 identifiers::{AccountId, ClientOrderId, InstrumentId, OrderListId, PositionId, VenueOrderId},
34 reports::order::OrderStatusReport,
35 types::{Price, Quantity},
36};
37
38#[pymethods]
39#[pyo3_stub_gen::derive::gen_stub_pymethods]
40impl OrderStatusReport {
41 #[new]
43 #[expect(clippy::too_many_arguments)]
44 #[pyo3(signature = (
45 account_id,
46 instrument_id,
47 venue_order_id,
48 order_side,
49 order_type,
50 time_in_force,
51 order_status,
52 quantity,
53 filled_qty,
54 ts_accepted,
55 ts_last,
56 ts_init,
57 client_order_id=None,
58 report_id=None,
59 order_list_id=None,
60 venue_position_id=None,
61 linked_order_ids=None,
62 parent_order_id=None,
63 contingency_type=None,
64 expire_time=None,
65 price=None,
66 trigger_price=None,
67 trigger_type=None,
68 limit_offset=None,
69 trailing_offset=None,
70 trailing_offset_type=None,
71 avg_px=None,
72 display_qty=None,
73 post_only=false,
74 reduce_only=false,
75 cancel_reason=None,
76 ts_triggered=None,
77 ))]
78 fn py_new(
79 account_id: AccountId,
80 instrument_id: InstrumentId,
81 venue_order_id: VenueOrderId,
82 order_side: OrderSide,
83 order_type: OrderType,
84 time_in_force: TimeInForce,
85 order_status: OrderStatus,
86 quantity: Quantity,
87 filled_qty: Quantity,
88 ts_accepted: u64,
89 ts_last: u64,
90 ts_init: u64,
91 client_order_id: Option<ClientOrderId>,
92 report_id: Option<UUID4>,
93 order_list_id: Option<OrderListId>,
94 venue_position_id: Option<PositionId>,
95 linked_order_ids: Option<Vec<ClientOrderId>>,
96 parent_order_id: Option<ClientOrderId>,
97 contingency_type: Option<ContingencyType>,
98 expire_time: Option<u64>,
99 price: Option<Price>,
100 trigger_price: Option<Price>,
101 trigger_type: Option<TriggerType>,
102 limit_offset: Option<Decimal>,
103 trailing_offset: Option<Decimal>,
104 trailing_offset_type: Option<TrailingOffsetType>,
105 avg_px: Option<Decimal>,
106 display_qty: Option<Quantity>,
107 post_only: bool,
108 reduce_only: bool,
109 cancel_reason: Option<String>,
110 ts_triggered: Option<u64>,
111 ) -> Self {
112 let mut report = Self::new(
113 account_id,
114 instrument_id,
115 client_order_id,
116 venue_order_id,
117 order_side,
118 order_type,
119 time_in_force,
120 order_status,
121 quantity,
122 filled_qty,
123 ts_accepted.into(),
124 ts_last.into(),
125 ts_init.into(),
126 report_id,
127 );
128
129 if let Some(order_list_id) = order_list_id {
130 report = report.with_order_list_id(order_list_id);
131 }
132
133 if let Some(venue_position_id) = venue_position_id {
134 report = report.with_venue_position_id(venue_position_id);
135 }
136
137 if let Some(linked_order_ids) = linked_order_ids {
138 report = report.with_linked_order_ids(linked_order_ids);
139 }
140
141 if let Some(parent_order_id) = parent_order_id {
142 report = report.with_parent_order_id(parent_order_id);
143 }
144
145 if let Some(contingency_type) = contingency_type {
146 report = report.with_contingency_type(contingency_type);
147 }
148
149 if let Some(expire_time) = expire_time {
150 report = report.with_expire_time(expire_time.into());
151 }
152
153 if let Some(price) = price {
154 report = report.with_price(price);
155 }
156
157 if let Some(trigger_price) = trigger_price {
158 report = report.with_trigger_price(trigger_price);
159 }
160
161 if let Some(trigger_type) = trigger_type {
162 report = report.with_trigger_type(trigger_type);
163 }
164
165 if let Some(limit_offset) = limit_offset {
166 report = report.with_limit_offset(limit_offset);
167 }
168
169 if let Some(trailing_offset) = trailing_offset {
170 report = report.with_trailing_offset(trailing_offset);
171 }
172
173 if let Some(trailing_offset_type) = trailing_offset_type {
174 report = report.with_trailing_offset_type(trailing_offset_type);
175 }
176
177 if let Some(avg_px) = avg_px {
178 report.avg_px = Some(avg_px);
179 }
180
181 if let Some(display_qty) = display_qty {
182 report = report.with_display_qty(display_qty);
183 }
184
185 if post_only {
186 report = report.with_post_only(post_only);
187 }
188
189 if reduce_only {
190 report = report.with_reduce_only(reduce_only);
191 }
192
193 if let Some(cancel_reason) = cancel_reason {
194 report = report.with_cancel_reason(cancel_reason);
195 }
196
197 if let Some(ts_triggered) = ts_triggered {
198 report = report.with_ts_triggered(ts_triggered.into());
199 }
200
201 report
202 }
203
204 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
205 match op {
206 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
207 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
208 _ => py.NotImplemented(),
209 }
210 }
211
212 fn __repr__(&self) -> String {
213 self.to_string()
214 }
215
216 fn __str__(&self) -> String {
217 self.to_string()
218 }
219
220 #[getter]
221 #[pyo3(name = "account_id")]
222 const fn py_account_id(&self) -> AccountId {
223 self.account_id
224 }
225
226 #[getter]
227 #[pyo3(name = "instrument_id")]
228 const fn py_instrument_id(&self) -> InstrumentId {
229 self.instrument_id
230 }
231
232 #[getter]
233 #[pyo3(name = "venue_order_id")]
234 const fn py_venue_order_id(&self) -> VenueOrderId {
235 self.venue_order_id
236 }
237
238 #[getter]
239 #[pyo3(name = "order_side")]
240 const fn py_order_side(&self) -> OrderSide {
241 self.order_side
242 }
243
244 #[getter]
245 #[pyo3(name = "order_type")]
246 const fn py_order_type(&self) -> OrderType {
247 self.order_type
248 }
249
250 #[getter]
251 #[pyo3(name = "time_in_force")]
252 const fn py_time_in_force(&self) -> TimeInForce {
253 self.time_in_force
254 }
255
256 #[getter]
257 #[pyo3(name = "order_status")]
258 const fn py_order_status(&self) -> OrderStatus {
259 self.order_status
260 }
261
262 #[getter]
263 #[pyo3(name = "quantity")]
264 const fn py_quantity(&self) -> Quantity {
265 self.quantity
266 }
267
268 #[getter]
269 #[pyo3(name = "filled_qty")]
270 const fn py_filled_qty(&self) -> Quantity {
271 self.filled_qty
272 }
273
274 #[getter]
275 #[pyo3(name = "report_id")]
276 const fn py_report_id(&self) -> UUID4 {
277 self.report_id
278 }
279
280 #[getter]
281 #[pyo3(name = "ts_accepted")]
282 const fn py_ts_accepted(&self) -> u64 {
283 self.ts_accepted.as_u64()
284 }
285
286 #[getter]
287 #[pyo3(name = "ts_last")]
288 const fn py_ts_last(&self) -> u64 {
289 self.ts_last.as_u64()
290 }
291
292 #[getter]
293 #[pyo3(name = "ts_init")]
294 const fn py_ts_init(&self) -> u64 {
295 self.ts_init.as_u64()
296 }
297
298 #[getter]
299 #[pyo3(name = "client_order_id")]
300 const fn py_client_order_id(&self) -> Option<ClientOrderId> {
301 self.client_order_id
302 }
303
304 #[getter]
305 #[pyo3(name = "order_list_id")]
306 const fn py_order_list_id(&self) -> Option<OrderListId> {
307 self.order_list_id
308 }
309
310 #[getter]
311 #[pyo3(name = "venue_position_id")]
312 const fn py_venue_position_id(&self) -> Option<PositionId> {
313 self.venue_position_id
314 }
315
316 #[getter]
317 #[pyo3(name = "linked_order_ids")]
318 fn py_linked_order_ids(&self) -> Option<Vec<ClientOrderId>> {
319 self.linked_order_ids.clone()
320 }
321
322 #[getter]
323 #[pyo3(name = "parent_order_id")]
324 fn py_parent_order_id(&self) -> Option<ClientOrderId> {
325 self.parent_order_id
326 }
327
328 #[getter]
329 #[pyo3(name = "contingency_type")]
330 const fn py_contingency_type(&self) -> ContingencyType {
331 self.contingency_type
332 }
333
334 #[getter]
335 #[pyo3(name = "expire_time")]
336 fn py_expire_time(&self) -> Option<u64> {
337 self.expire_time.map(|t| t.as_u64())
338 }
339
340 #[getter]
341 #[pyo3(name = "price")]
342 const fn py_price(&self) -> Option<Price> {
343 self.price
344 }
345
346 #[getter]
347 #[pyo3(name = "trigger_price")]
348 const fn py_trigger_price(&self) -> Option<Price> {
349 self.trigger_price
350 }
351
352 #[getter]
353 #[pyo3(name = "trigger_type")]
354 const fn py_trigger_type(&self) -> Option<TriggerType> {
355 self.trigger_type
356 }
357
358 #[getter]
359 #[pyo3(name = "limit_offset")]
360 const fn py_limit_offset(&self) -> Option<Decimal> {
361 self.limit_offset
362 }
363
364 #[getter]
365 #[pyo3(name = "trailing_offset")]
366 const fn py_trailing_offset(&self) -> Option<Decimal> {
367 self.trailing_offset
368 }
369
370 #[getter]
371 #[pyo3(name = "trailing_offset_type")]
372 const fn py_trailing_offset_type(&self) -> TrailingOffsetType {
373 self.trailing_offset_type
374 }
375
376 #[getter]
377 #[pyo3(name = "avg_px")]
378 fn py_avg_px(&self) -> Option<Decimal> {
379 self.avg_px
380 }
381
382 #[getter]
383 #[pyo3(name = "display_qty")]
384 const fn py_display_qty(&self) -> Option<Quantity> {
385 self.display_qty
386 }
387
388 #[getter]
389 #[pyo3(name = "post_only")]
390 const fn py_post_only(&self) -> bool {
391 self.post_only
392 }
393
394 #[getter]
395 #[pyo3(name = "reduce_only")]
396 const fn py_reduce_only(&self) -> bool {
397 self.reduce_only
398 }
399
400 #[getter]
401 #[pyo3(name = "cancel_reason")]
402 fn py_cancel_reason(&self) -> Option<String> {
403 self.cancel_reason.clone()
404 }
405
406 #[getter]
407 #[pyo3(name = "ts_triggered")]
408 fn py_ts_triggered(&self) -> Option<u64> {
409 self.ts_triggered.map(|t| t.as_u64())
410 }
411
412 #[getter]
413 #[pyo3(name = "is_open")]
414 fn py_is_open(&self) -> bool {
415 matches!(
416 self.order_status,
417 OrderStatus::Accepted
418 | OrderStatus::Triggered
419 | OrderStatus::PendingCancel
420 | OrderStatus::PendingUpdate
421 | OrderStatus::PartiallyFilled
422 )
423 }
424
425 #[staticmethod]
431 #[pyo3(name = "from_dict")]
432 pub fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
433 from_dict_pyo3(py, values)
434 }
435
436 #[pyo3(name = "to_dict")]
442 pub fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
443 let dict = PyDict::new(py);
444 dict.set_item("type", stringify!(OrderStatusReport))?;
445 dict.set_item("account_id", self.account_id.to_string())?;
446 dict.set_item("instrument_id", self.instrument_id.to_string())?;
447 dict.set_item("venue_order_id", self.venue_order_id.to_string())?;
448 dict.set_item("order_side", self.order_side.to_string())?;
449 dict.set_item("order_type", self.order_type.to_string())?;
450 dict.set_item("time_in_force", self.time_in_force.to_string())?;
451 dict.set_item("order_status", self.order_status.to_string())?;
452 dict.set_item("quantity", self.quantity.to_string())?;
453 dict.set_item("filled_qty", self.filled_qty.to_string())?;
454 dict.set_item("report_id", self.report_id.to_string())?;
455 dict.set_item("ts_accepted", self.ts_accepted.as_u64())?;
456 dict.set_item("ts_last", self.ts_last.as_u64())?;
457 dict.set_item("ts_init", self.ts_init.as_u64())?;
458 dict.set_item("contingency_type", self.contingency_type.to_string())?;
459 dict.set_item(
460 "trailing_offset_type",
461 self.trailing_offset_type.to_string(),
462 )?;
463 dict.set_item("post_only", self.post_only)?;
464 dict.set_item("reduce_only", self.reduce_only)?;
465
466 match &self.client_order_id {
467 Some(id) => dict.set_item("client_order_id", id.to_string())?,
468 None => dict.set_item("client_order_id", py.None())?,
469 }
470
471 match &self.order_list_id {
472 Some(id) => dict.set_item("order_list_id", id.to_string())?,
473 None => dict.set_item("order_list_id", py.None())?,
474 }
475
476 match &self.venue_position_id {
477 Some(id) => dict.set_item("venue_position_id", id.to_string())?,
478 None => dict.set_item("venue_position_id", py.None())?,
479 }
480
481 match &self.linked_order_ids {
482 Some(ids) => {
483 let py_list = PyList::new(py, ids.iter().map(|id| id.to_string()))?;
484 dict.set_item("linked_order_ids", py_list)?;
485 }
486 None => dict.set_item("linked_order_ids", py.None())?,
487 }
488
489 match &self.parent_order_id {
490 Some(id) => dict.set_item("parent_order_id", id.to_string())?,
491 None => dict.set_item("parent_order_id", py.None())?,
492 }
493
494 match &self.expire_time {
495 Some(t) => dict.set_item("expire_time", t.as_u64())?,
496 None => dict.set_item("expire_time", py.None())?,
497 }
498
499 match &self.price {
500 Some(p) => dict.set_item("price", p.to_string())?,
501 None => dict.set_item("price", py.None())?,
502 }
503
504 match &self.trigger_price {
505 Some(p) => dict.set_item("trigger_price", p.to_string())?,
506 None => dict.set_item("trigger_price", py.None())?,
507 }
508
509 match &self.trigger_type {
510 Some(t) => dict.set_item("trigger_type", t.to_string())?,
511 None => dict.set_item("trigger_type", py.None())?,
512 }
513
514 match &self.limit_offset {
515 Some(o) => dict.set_item("limit_offset", o.to_string())?,
516 None => dict.set_item("limit_offset", py.None())?,
517 }
518
519 match &self.trailing_offset {
520 Some(o) => dict.set_item("trailing_offset", o.to_string())?,
521 None => dict.set_item("trailing_offset", py.None())?,
522 }
523
524 match &self.avg_px {
525 Some(p) => dict.set_item("avg_px", p)?,
526 None => dict.set_item("avg_px", py.None())?,
527 }
528
529 match &self.display_qty {
530 Some(q) => dict.set_item("display_qty", q.to_string())?,
531 None => dict.set_item("display_qty", py.None())?,
532 }
533
534 match &self.cancel_reason {
535 Some(r) => dict.set_item("cancel_reason", r)?,
536 None => dict.set_item("cancel_reason", py.None())?,
537 }
538
539 match &self.ts_triggered {
540 Some(t) => dict.set_item("ts_triggered", t.as_u64())?,
541 None => dict.set_item("ts_triggered", py.None())?,
542 }
543
544 Ok(dict.into())
545 }
546}