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