Skip to main content

nautilus_model/python/events/order/
filled.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    enums::{LiquiditySide, OrderSide, OrderType},
24    events::OrderFilled,
25    identifiers::{
26        AccountId, ClientOrderId, InstrumentId, PositionId, StrategyId, TradeId, TraderId,
27        VenueOrderId,
28    },
29    types::{Currency, Money, Price, Quantity},
30};
31
32#[pymethods]
33#[pyo3_stub_gen::derive::gen_stub_pymethods]
34impl OrderFilled {
35    /// Creates a new `OrderFilled` instance.
36    #[expect(clippy::too_many_arguments)]
37    #[new]
38    #[pyo3(signature = (trader_id, strategy_id, instrument_id, client_order_id, venue_order_id, account_id, trade_id, order_side, order_type, last_qty, last_px, currency, liquidity_side, event_id, ts_event, ts_init, reconciliation, position_id=None, commission=None))]
39    fn py_new(
40        trader_id: TraderId,
41        strategy_id: StrategyId,
42        instrument_id: InstrumentId,
43        client_order_id: ClientOrderId,
44        venue_order_id: VenueOrderId,
45        account_id: AccountId,
46        trade_id: TradeId,
47        order_side: OrderSide,
48        order_type: OrderType,
49        last_qty: Quantity,
50        last_px: Price,
51        currency: Currency,
52        liquidity_side: LiquiditySide,
53        event_id: UUID4,
54        ts_event: u64,
55        ts_init: u64,
56        reconciliation: bool,
57        position_id: Option<PositionId>,
58        commission: Option<Money>,
59    ) -> Self {
60        Self::new(
61            trader_id,
62            strategy_id,
63            instrument_id,
64            client_order_id,
65            venue_order_id,
66            account_id,
67            trade_id,
68            order_side,
69            order_type,
70            last_qty,
71            last_px,
72            currency,
73            liquidity_side,
74            event_id,
75            ts_event.into(),
76            ts_init.into(),
77            reconciliation,
78            position_id,
79            commission,
80        )
81    }
82
83    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
84        match op {
85            CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
86            CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
87            _ => py.NotImplemented(),
88        }
89    }
90
91    fn __repr__(&self) -> String {
92        format!("{self:?}")
93    }
94
95    fn __str__(&self) -> String {
96        self.to_string()
97    }
98
99    #[getter]
100    #[pyo3(name = "is_buy")]
101    fn py_is_buy(&self) -> bool {
102        self.is_buy()
103    }
104
105    #[getter]
106    #[pyo3(name = "is_sell")]
107    fn py_is_sell(&self) -> bool {
108        self.is_sell()
109    }
110
111    #[getter]
112    #[pyo3(name = "trader_id")]
113    fn py_trader_id(&self) -> TraderId {
114        self.trader_id
115    }
116
117    #[getter]
118    #[pyo3(name = "instrument_id")]
119    fn py_instrument_id(&self) -> InstrumentId {
120        self.instrument_id
121    }
122
123    #[getter]
124    #[pyo3(name = "strategy_id")]
125    fn py_strategy_id(&self) -> StrategyId {
126        self.strategy_id
127    }
128
129    #[getter]
130    #[pyo3(name = "client_order_id")]
131    fn py_client_order_id(&self) -> ClientOrderId {
132        self.client_order_id
133    }
134
135    #[getter]
136    #[pyo3(name = "venue_order_id")]
137    fn py_venue_order_id(&self) -> VenueOrderId {
138        self.venue_order_id
139    }
140
141    #[getter]
142    #[pyo3(name = "account_id")]
143    fn py_account_id(&self) -> AccountId {
144        self.account_id
145    }
146
147    #[getter]
148    #[pyo3(name = "trade_id")]
149    fn py_trade_id(&self) -> TradeId {
150        self.trade_id
151    }
152
153    #[getter]
154    #[pyo3(name = "order_side")]
155    fn py_order_side(&self) -> OrderSide {
156        self.order_side
157    }
158
159    #[getter]
160    #[pyo3(name = "last_qty")]
161    fn py_last_qty(&self) -> Quantity {
162        self.last_qty
163    }
164
165    #[getter]
166    #[pyo3(name = "last_px")]
167    fn py_last_px(&self) -> Price {
168        self.last_px
169    }
170
171    #[getter]
172    #[pyo3(name = "currency")]
173    fn py_currency(&self) -> Currency {
174        self.currency
175    }
176
177    #[getter]
178    #[pyo3(name = "liquidity_side")]
179    fn py_liquidity_side(&self) -> LiquiditySide {
180        self.liquidity_side
181    }
182
183    #[getter]
184    #[pyo3(name = "event_id")]
185    fn py_event_id(&self) -> UUID4 {
186        self.event_id
187    }
188
189    #[getter]
190    #[pyo3(name = "ts_event")]
191    fn py_ts_event(&self) -> u64 {
192        self.ts_event.as_u64()
193    }
194
195    #[getter]
196    #[pyo3(name = "ts_init")]
197    fn py_ts_init(&self) -> u64 {
198        self.ts_init.as_u64()
199    }
200
201    #[getter]
202    #[pyo3(name = "reconciliation")]
203    fn py_reconciliation(&self) -> bool {
204        self.reconciliation
205    }
206
207    #[getter]
208    #[pyo3(name = "position_id")]
209    fn py_position_id(&self) -> Option<PositionId> {
210        self.position_id
211    }
212
213    #[getter]
214    #[pyo3(name = "commission")]
215    fn py_commission(&self) -> Option<Money> {
216        self.commission
217    }
218
219    #[getter]
220    #[pyo3(name = "order_type")]
221    fn py_order_type(&self) -> OrderType {
222        self.order_type
223    }
224
225    /// Constructs an [`OrderFilled`] from a Python dict.
226    ///
227    /// # Errors
228    ///
229    /// Returns a `PyErr` if deserialization from the Python dict fails.
230    #[staticmethod]
231    #[pyo3(name = "from_dict")]
232    fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
233        from_dict_pyo3(py, values)
234    }
235
236    /// Converts this [`OrderFilled`] into a Python dict.
237    ///
238    /// # Errors
239    ///
240    /// Returns a `PyErr` if serialization into a Python dict fails.
241    #[pyo3(name = "to_dict")]
242    pub fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
243        let dict = PyDict::new(py);
244        dict.set_item("type", stringify!(OrderFilled))?;
245        dict.set_item("trader_id", self.trader_id.to_string())?;
246        dict.set_item("strategy_id", self.strategy_id.to_string())?;
247        dict.set_item("instrument_id", self.instrument_id.to_string())?;
248        dict.set_item("client_order_id", self.client_order_id.to_string())?;
249        dict.set_item("venue_order_id", self.venue_order_id.to_string())?;
250        dict.set_item("account_id", self.account_id.to_string())?;
251        dict.set_item("trade_id", self.trade_id.to_string())?;
252        dict.set_item("order_side", self.order_side.to_string())?;
253        dict.set_item("order_type", self.order_type.to_string())?;
254        dict.set_item("last_qty", self.last_qty.to_string())?;
255        dict.set_item("last_px", self.last_px.to_string())?;
256        dict.set_item("currency", self.currency.code.to_string())?;
257        dict.set_item("liquidity_side", self.liquidity_side.to_string())?;
258        dict.set_item("event_id", self.event_id.to_string())?;
259        dict.set_item("ts_event", self.ts_event.as_u64())?;
260        dict.set_item("ts_init", self.ts_init.as_u64())?;
261        dict.set_item("reconciliation", self.reconciliation)?;
262        dict.set_item("info", PyDict::new(py))?;
263        match self.position_id {
264            Some(position_id) => dict.set_item("position_id", position_id.to_string())?,
265            None => dict.set_item("position_id", py.None())?,
266        }
267
268        match self.commission {
269            Some(commission) => dict.set_item("commission", commission.to_string())?,
270            None => dict.set_item("commission", py.None())?,
271        }
272        Ok(dict.into())
273    }
274}