nautilus_execution/python/
fill.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 nautilus_model::{
21    enums::{LiquiditySide, OrderSide},
22    identifiers::{AccountId, ClientOrderId, InstrumentId, PositionId, TradeId, VenueOrderId},
23    types::{Money, Price, Quantity},
24};
25use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
26
27use crate::reports::fill::FillReport;
28
29#[pymethods]
30impl FillReport {
31    #[new]
32    #[allow(clippy::too_many_arguments)]
33    #[pyo3(signature = (
34        account_id,
35        instrument_id,
36        venue_order_id,
37        trade_id,
38        order_side,
39        last_qty,
40        last_px,
41        commission,
42        liquidity_side,
43        ts_event,
44        ts_init,
45        client_order_id=None,
46        venue_position_id=None,
47        report_id=None,
48    ))]
49    fn py_new(
50        account_id: AccountId,
51        instrument_id: InstrumentId,
52        venue_order_id: VenueOrderId,
53        trade_id: TradeId,
54        order_side: OrderSide,
55        last_qty: Quantity,
56        last_px: Price,
57        commission: Money,
58        liquidity_side: LiquiditySide,
59        ts_event: u64,
60        ts_init: u64,
61        client_order_id: Option<ClientOrderId>,
62        venue_position_id: Option<PositionId>,
63        report_id: Option<UUID4>,
64    ) -> PyResult<Self> {
65        Ok(Self::new(
66            account_id,
67            instrument_id,
68            venue_order_id,
69            trade_id,
70            order_side,
71            last_qty,
72            last_px,
73            commission,
74            liquidity_side,
75            client_order_id,
76            venue_position_id,
77            ts_event.into(),
78            ts_init.into(),
79            report_id,
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        self.to_string()
93    }
94
95    fn __str__(&self) -> String {
96        self.to_string()
97    }
98
99    #[getter]
100    #[pyo3(name = "account_id")]
101    const fn py_account_id(&self) -> AccountId {
102        self.account_id
103    }
104
105    #[getter]
106    #[pyo3(name = "instrument_id")]
107    const fn py_instrument_id(&self) -> InstrumentId {
108        self.instrument_id
109    }
110
111    #[getter]
112    #[pyo3(name = "venue_order_id")]
113    const fn py_venue_order_id(&self) -> VenueOrderId {
114        self.venue_order_id
115    }
116
117    #[getter]
118    #[pyo3(name = "trade_id")]
119    const fn py_trade_id(&self) -> TradeId {
120        self.trade_id
121    }
122
123    #[getter]
124    #[pyo3(name = "order_side")]
125    const fn py_order_side(&self) -> OrderSide {
126        self.order_side
127    }
128
129    #[getter]
130    #[pyo3(name = "last_qty")]
131    const fn py_last_qty(&self) -> Quantity {
132        self.last_qty
133    }
134
135    #[getter]
136    #[pyo3(name = "last_px")]
137    const fn py_last_px(&self) -> Price {
138        self.last_px
139    }
140
141    #[getter]
142    #[pyo3(name = "commission")]
143    const fn py_commission(&self) -> Money {
144        self.commission
145    }
146
147    #[getter]
148    #[pyo3(name = "liquidity_side")]
149    const fn py_liquidity_side(&self) -> LiquiditySide {
150        self.liquidity_side
151    }
152
153    #[getter]
154    #[pyo3(name = "report_id")]
155    const fn py_report_id(&self) -> UUID4 {
156        self.report_id
157    }
158
159    #[getter]
160    #[pyo3(name = "ts_event")]
161    const fn py_ts_event(&self) -> u64 {
162        self.ts_event.as_u64()
163    }
164
165    #[getter]
166    #[pyo3(name = "ts_init")]
167    const fn py_ts_init(&self) -> u64 {
168        self.ts_init.as_u64()
169    }
170
171    #[getter]
172    #[pyo3(name = "client_order_id")]
173    const fn py_client_order_id(&self) -> Option<ClientOrderId> {
174        self.client_order_id
175    }
176
177    #[getter]
178    #[pyo3(name = "venue_position_id")]
179    const fn py_venue_position_id(&self) -> Option<PositionId> {
180        self.venue_position_id
181    }
182
183    #[staticmethod]
184    #[pyo3(name = "from_dict")]
185    pub fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
186        from_dict_pyo3(py, values)
187    }
188
189    #[pyo3(name = "to_dict")]
190    pub fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
191        let dict = PyDict::new(py);
192        dict.set_item("type", stringify!(FillReport))?;
193        dict.set_item("account_id", self.account_id.to_string())?;
194        dict.set_item("instrument_id", self.instrument_id.to_string())?;
195        dict.set_item("venue_order_id", self.venue_order_id.to_string())?;
196        dict.set_item("trade_id", self.trade_id.to_string())?;
197        dict.set_item("order_side", self.order_side.to_string())?;
198        dict.set_item("last_qty", self.last_qty.to_string())?;
199        dict.set_item("last_px", self.last_px.to_string())?;
200        dict.set_item("commission", self.commission.to_string())?;
201        dict.set_item("liquidity_side", self.liquidity_side.to_string())?;
202        dict.set_item("report_id", self.report_id.to_string())?;
203        dict.set_item("ts_event", self.ts_event.as_u64())?;
204        dict.set_item("ts_init", self.ts_init.as_u64())?;
205
206        match &self.client_order_id {
207            Some(id) => dict.set_item("client_order_id", id.to_string())?,
208            None => dict.set_item("client_order_id", py.None())?,
209        }
210        match &self.venue_position_id {
211            Some(id) => dict.set_item("venue_position_id", id.to_string())?,
212            None => dict.set_item("venue_position_id", py.None())?,
213        }
214
215        Ok(dict.into())
216    }
217}