nautilus_model/python/events/order/
updated.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use 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))]
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    ) -> Self {
48        Self::new(
49            trader_id,
50            strategy_id,
51            instrument_id,
52            client_order_id,
53            quantity,
54            event_id,
55            ts_event.into(),
56            ts_init.into(),
57            reconciliation,
58            venue_order_id,
59            account_id,
60            price,
61            trigger_price,
62        )
63    }
64
65    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
66        match op {
67            CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
68            CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
69            _ => py.NotImplemented(),
70        }
71    }
72
73    fn __repr__(&self) -> String {
74        format!("{:?}", self)
75    }
76
77    fn __str__(&self) -> String {
78        self.to_string()
79    }
80
81    #[staticmethod]
82    #[pyo3(name = "from_dict")]
83    fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
84        from_dict_pyo3(py, values)
85    }
86
87    #[pyo3(name = "to_dict")]
88    fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
89        let dict = PyDict::new(py);
90        dict.set_item("type", stringify!(OrderUpdated))?;
91        dict.set_item("trader_id", self.trader_id.to_string())?;
92        dict.set_item("strategy_id", self.strategy_id.to_string())?;
93        dict.set_item("instrument_id", self.instrument_id.to_string())?;
94        dict.set_item("client_order_id", self.client_order_id.to_string())?;
95        dict.set_item("quantity", self.quantity.to_string())?;
96        dict.set_item("event_id", self.event_id.to_string())?;
97        dict.set_item("ts_event", self.ts_event.as_u64())?;
98        dict.set_item("ts_init", self.ts_init.as_u64())?;
99        dict.set_item("reconciliation", self.reconciliation)?;
100        match self.venue_order_id {
101            Some(venue_order_id) => dict.set_item("venue_order_id", venue_order_id.to_string())?,
102            None => dict.set_item("venue_order_id", py.None())?,
103        }
104        match self.account_id {
105            Some(account_id) => dict.set_item("account_id", account_id.to_string())?,
106            None => dict.set_item("account_id", py.None())?,
107        }
108        match self.price {
109            Some(price) => dict.set_item("price", price.to_string())?,
110            None => dict.set_item("price", py.None())?,
111        }
112        match self.trigger_price {
113            Some(trigger_price) => dict.set_item("trigger_price", trigger_price.to_string())?,
114            None => dict.set_item("trigger_price", py.None())?,
115        }
116        Ok(dict.into())
117    }
118}