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