nautilus_model/reports/
fill.rs

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