Skip to main content

nautilus_model/python/events/order/
triggered.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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::OrderTriggered,
24    identifiers::{AccountId, ClientOrderId, InstrumentId, StrategyId, TraderId, VenueOrderId},
25};
26
27#[pymethods]
28#[pyo3_stub_gen::derive::gen_stub_pymethods]
29impl OrderTriggered {
30    /// Represents an event where an order has triggered.
31    ///
32    /// Applicable to `StopLimit`, `TrailingStopLimit`, and `LimitIfTouched` orders.
33    #[expect(clippy::too_many_arguments)]
34    #[new]
35    #[pyo3(signature = (trader_id, strategy_id, instrument_id, client_order_id, event_id, ts_event, ts_init, reconciliation, venue_order_id=None, account_id=None))]
36    fn py_new(
37        trader_id: TraderId,
38        strategy_id: StrategyId,
39        instrument_id: InstrumentId,
40        client_order_id: ClientOrderId,
41        event_id: UUID4,
42        ts_event: u64,
43        ts_init: u64,
44        reconciliation: bool,
45        venue_order_id: Option<VenueOrderId>,
46        account_id: Option<AccountId>,
47    ) -> Self {
48        Self::new(
49            trader_id,
50            strategy_id,
51            instrument_id,
52            client_order_id,
53            event_id,
54            ts_event.into(),
55            ts_init.into(),
56            reconciliation,
57            venue_order_id,
58            account_id,
59        )
60    }
61
62    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
63        match op {
64            CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
65            CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
66            _ => py.NotImplemented(),
67        }
68    }
69
70    fn __repr__(&self) -> String {
71        format!("{self:?}")
72    }
73
74    fn __str__(&self) -> String {
75        self.to_string()
76    }
77
78    #[staticmethod]
79    #[pyo3(name = "from_dict")]
80    fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
81        from_dict_pyo3(py, values)
82    }
83
84    #[getter]
85    #[pyo3(name = "trader_id")]
86    fn py_trader_id(&self) -> TraderId {
87        self.trader_id
88    }
89
90    #[getter]
91    #[pyo3(name = "strategy_id")]
92    fn py_strategy_id(&self) -> StrategyId {
93        self.strategy_id
94    }
95
96    #[getter]
97    #[pyo3(name = "instrument_id")]
98    fn py_instrument_id(&self) -> InstrumentId {
99        self.instrument_id
100    }
101
102    #[getter]
103    #[pyo3(name = "client_order_id")]
104    fn py_client_order_id(&self) -> ClientOrderId {
105        self.client_order_id
106    }
107
108    #[getter]
109    #[pyo3(name = "venue_order_id")]
110    fn py_venue_order_id(&self) -> Option<VenueOrderId> {
111        self.venue_order_id
112    }
113
114    #[getter]
115    #[pyo3(name = "account_id")]
116    fn py_account_id(&self) -> Option<AccountId> {
117        self.account_id
118    }
119
120    #[getter]
121    #[pyo3(name = "event_id")]
122    fn py_event_id(&self) -> UUID4 {
123        self.event_id
124    }
125
126    #[getter]
127    #[pyo3(name = "ts_event")]
128    fn py_ts_event(&self) -> u64 {
129        self.ts_event.as_u64()
130    }
131
132    #[getter]
133    #[pyo3(name = "ts_init")]
134    fn py_ts_init(&self) -> u64 {
135        self.ts_init.as_u64()
136    }
137
138    #[getter]
139    #[pyo3(name = "reconciliation")]
140    fn py_reconciliation(&self) -> bool {
141        self.reconciliation != 0
142    }
143
144    #[pyo3(name = "to_dict")]
145    fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
146        let dict = PyDict::new(py);
147        dict.set_item("type", stringify!(OrderTriggered))?;
148        dict.set_item("trader_id", self.trader_id.to_string())?;
149        dict.set_item("strategy_id", self.strategy_id.to_string())?;
150        dict.set_item("instrument_id", self.instrument_id.to_string())?;
151        dict.set_item("client_order_id", self.client_order_id.to_string())?;
152        dict.set_item("event_id", self.event_id.to_string())?;
153        dict.set_item("ts_event", self.ts_event.as_u64())?;
154        dict.set_item("ts_init", self.ts_init.as_u64())?;
155        dict.set_item("reconciliation", self.reconciliation)?;
156        match self.venue_order_id {
157            Some(venue_order_id) => dict.set_item("venue_order_id", venue_order_id.to_string())?,
158            None => dict.set_item("venue_order_id", "None")?,
159        }
160
161        match self.account_id {
162            Some(account_id) => dict.set_item("account_id", account_id.to_string())?,
163            None => dict.set_item("account_id", "None")?,
164        }
165        Ok(dict.into())
166    }
167}