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