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    #[getter]
88    #[pyo3(name = "trader_id")]
89    fn py_trader_id(&self) -> TraderId {
90        self.trader_id
91    }
92
93    #[getter]
94    #[pyo3(name = "strategy_id")]
95    fn py_strategy_id(&self) -> StrategyId {
96        self.strategy_id
97    }
98
99    #[getter]
100    #[pyo3(name = "instrument_id")]
101    fn py_instrument_id(&self) -> InstrumentId {
102        self.instrument_id
103    }
104
105    #[getter]
106    #[pyo3(name = "client_order_id")]
107    fn py_client_order_id(&self) -> ClientOrderId {
108        self.client_order_id
109    }
110
111    #[getter]
112    #[pyo3(name = "venue_order_id")]
113    fn py_venue_order_id(&self) -> Option<VenueOrderId> {
114        self.venue_order_id
115    }
116
117    #[getter]
118    #[pyo3(name = "account_id")]
119    fn py_account_id(&self) -> Option<AccountId> {
120        self.account_id
121    }
122
123    #[getter]
124    #[pyo3(name = "quantity")]
125    fn py_quantity(&self) -> Quantity {
126        self.quantity
127    }
128
129    #[getter]
130    #[pyo3(name = "price")]
131    fn py_price(&self) -> Option<Price> {
132        self.price
133    }
134
135    #[getter]
136    #[pyo3(name = "trigger_price")]
137    fn py_trigger_price(&self) -> Option<Price> {
138        self.trigger_price
139    }
140
141    #[getter]
142    #[pyo3(name = "event_id")]
143    fn py_event_id(&self) -> UUID4 {
144        self.event_id
145    }
146
147    #[getter]
148    #[pyo3(name = "ts_event")]
149    fn py_ts_event(&self) -> u64 {
150        self.ts_event.as_u64()
151    }
152
153    #[getter]
154    #[pyo3(name = "ts_init")]
155    fn py_ts_init(&self) -> u64 {
156        self.ts_init.as_u64()
157    }
158
159    #[getter]
160    #[pyo3(name = "reconciliation")]
161    fn py_reconciliation(&self) -> bool {
162        self.reconciliation != 0
163    }
164
165    #[pyo3(name = "to_dict")]
166    fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
167        let dict = PyDict::new(py);
168        dict.set_item("type", stringify!(OrderUpdated))?;
169        dict.set_item("trader_id", self.trader_id.to_string())?;
170        dict.set_item("strategy_id", self.strategy_id.to_string())?;
171        dict.set_item("instrument_id", self.instrument_id.to_string())?;
172        dict.set_item("client_order_id", self.client_order_id.to_string())?;
173        dict.set_item("quantity", self.quantity.to_string())?;
174        dict.set_item("event_id", self.event_id.to_string())?;
175        dict.set_item("ts_event", self.ts_event.as_u64())?;
176        dict.set_item("ts_init", self.ts_init.as_u64())?;
177        dict.set_item("reconciliation", self.reconciliation)?;
178        match self.venue_order_id {
179            Some(venue_order_id) => dict.set_item("venue_order_id", venue_order_id.to_string())?,
180            None => dict.set_item("venue_order_id", py.None())?,
181        }
182        match self.account_id {
183            Some(account_id) => dict.set_item("account_id", account_id.to_string())?,
184            None => dict.set_item("account_id", py.None())?,
185        }
186        match self.price {
187            Some(price) => dict.set_item("price", price.to_string())?,
188            None => dict.set_item("price", py.None())?,
189        }
190        match self.trigger_price {
191            Some(trigger_price) => dict.set_item("trigger_price", trigger_price.to_string())?,
192            None => dict.set_item("trigger_price", py.None())?,
193        }
194        Ok(dict.into())
195    }
196}