nautilus_execution/reports/
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 std::fmt::Display;
17
18use nautilus_core::{UUID4, UnixNanos};
19use nautilus_model::{
20    enums::{LiquiditySide, OrderSide},
21    identifiers::{AccountId, ClientOrderId, InstrumentId, PositionId, TradeId, VenueOrderId},
22    types::{Money, Price, Quantity},
23};
24use serde::{Deserialize, Serialize};
25
26/// Represents a fill report of a single order execution.
27#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
28#[serde(tag = "type")]
29#[cfg_attr(
30    feature = "python",
31    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.execution")
32)]
33pub struct FillReport {
34    /// The account ID associated with the position.
35    pub account_id: AccountId,
36    /// The instrument ID associated with the event.
37    pub instrument_id: InstrumentId,
38    /// The venue assigned order ID.
39    pub venue_order_id: VenueOrderId,
40    /// The trade match ID (assigned by the venue).
41    pub trade_id: TradeId,
42    /// The order side.
43    pub order_side: OrderSide,
44    /// The last fill quantity for the position.
45    pub last_qty: Quantity,
46    /// The last fill price for the position.
47    pub last_px: Price,
48    /// The commission generated from the fill.
49    pub commission: Money,
50    /// The liquidity side of the execution.
51    pub liquidity_side: LiquiditySide,
52    /// The unique identifier for the event.
53    pub report_id: UUID4,
54    /// UNIX timestamp (nanoseconds) when the event occurred.
55    pub ts_event: UnixNanos,
56    /// UNIX timestamp (nanoseconds) when the event was initialized.
57    pub ts_init: UnixNanos,
58    /// The client order ID.
59    pub client_order_id: Option<ClientOrderId>,
60    /// The position ID (assigned by the venue).
61    pub venue_position_id: Option<PositionId>,
62}
63
64impl FillReport {
65    /// Creates a new [`FillReport`] instance with required fields.
66    #[allow(clippy::too_many_arguments)]
67    #[must_use]
68    pub fn new(
69        account_id: AccountId,
70        instrument_id: InstrumentId,
71        venue_order_id: VenueOrderId,
72        trade_id: TradeId,
73        order_side: OrderSide,
74        last_qty: Quantity,
75        last_px: Price,
76        commission: Money,
77        liquidity_side: LiquiditySide,
78        client_order_id: Option<ClientOrderId>,
79        venue_position_id: Option<PositionId>,
80        ts_event: UnixNanos,
81        ts_init: UnixNanos,
82        report_id: Option<UUID4>,
83    ) -> Self {
84        Self {
85            account_id,
86            instrument_id,
87            venue_order_id,
88            trade_id,
89            order_side,
90            last_qty,
91            last_px,
92            commission,
93            liquidity_side,
94            report_id: report_id.unwrap_or_default(),
95            ts_event,
96            ts_init,
97            client_order_id,
98            venue_position_id,
99        }
100    }
101
102    /// Checks if the fill has a client order ID.
103    #[must_use]
104    pub const fn has_client_order_id(&self) -> bool {
105        self.client_order_id.is_some()
106    }
107
108    /// Utility method to check if the fill has a venue position ID.
109    #[must_use]
110    pub const fn has_venue_position_id(&self) -> bool {
111        self.venue_position_id.is_some()
112    }
113}
114
115impl Display for FillReport {
116    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117        write!(
118            f,
119            "FillReport(instrument={}, side={}, qty={}, last_px={}, trade_id={}, venue_order_id={}, commission={}, liquidity={})",
120            self.instrument_id,
121            self.order_side,
122            self.last_qty,
123            self.last_px,
124            self.trade_id,
125            self.venue_order_id,
126            self.commission,
127            self.liquidity_side,
128        )
129    }
130}