nautilus_execution/reports/
mass_status.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 indexmap::IndexMap;
17use nautilus_core::{UnixNanos, UUID4};
18use nautilus_model::identifiers::{AccountId, ClientId, InstrumentId, Venue, VenueOrderId};
19use serde::{Deserialize, Serialize};
20
21use crate::reports::{fill::FillReport, order::OrderStatusReport, position::PositionStatusReport};
22
23/// Represents an execution mass status report for an execution client - including
24/// status of all orders, trades for those orders and open positions.
25#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
26#[serde(tag = "type")]
27#[cfg_attr(
28    feature = "python",
29    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.execution")
30)]
31pub struct ExecutionMassStatus {
32    /// The client ID for the report.
33    pub client_id: ClientId,
34    /// The account ID for the report.
35    pub account_id: AccountId,
36    /// The venue for the report.
37    pub venue: Venue,
38    /// The report ID.
39    pub report_id: UUID4,
40    /// UNIX timestamp (nanoseconds) when the object was initialized.
41    pub ts_init: UnixNanos,
42    /// The order status reports.
43    order_reports: IndexMap<VenueOrderId, OrderStatusReport>,
44    /// The fill reports.
45    fill_reports: IndexMap<VenueOrderId, Vec<FillReport>>,
46    /// The position status reports.
47    position_reports: IndexMap<InstrumentId, Vec<PositionStatusReport>>,
48}
49
50impl ExecutionMassStatus {
51    /// Creates a new execution mass status report.
52    #[must_use]
53    pub fn new(
54        client_id: ClientId,
55        account_id: AccountId,
56        venue: Venue,
57        ts_init: UnixNanos,
58        report_id: Option<UUID4>,
59    ) -> Self {
60        Self {
61            client_id,
62            account_id,
63            venue,
64            report_id: report_id.unwrap_or_default(),
65            ts_init,
66            order_reports: IndexMap::new(),
67            fill_reports: IndexMap::new(),
68            position_reports: IndexMap::new(),
69        }
70    }
71
72    /// Get a copy of the order reports map.
73    #[must_use]
74    pub fn order_reports(&self) -> IndexMap<VenueOrderId, OrderStatusReport> {
75        self.order_reports.clone()
76    }
77
78    /// Get a copy of the fill reports map.
79    #[must_use]
80    pub fn fill_reports(&self) -> IndexMap<VenueOrderId, Vec<FillReport>> {
81        self.fill_reports.clone()
82    }
83
84    /// Get a copy of the position reports map.
85    #[must_use]
86    pub fn position_reports(&self) -> IndexMap<InstrumentId, Vec<PositionStatusReport>> {
87        self.position_reports.clone()
88    }
89
90    /// Add order reports to the mass status.
91    pub fn add_order_reports(&mut self, reports: Vec<OrderStatusReport>) {
92        for report in reports {
93            self.order_reports.insert(report.venue_order_id, report);
94        }
95    }
96
97    /// Add fill reports to the mass status.
98    pub fn add_fill_reports(&mut self, reports: Vec<FillReport>) {
99        for report in reports {
100            self.fill_reports
101                .entry(report.venue_order_id)
102                .or_default()
103                .push(report);
104        }
105    }
106
107    /// Add position reports to the mass status.
108    pub fn add_position_reports(&mut self, reports: Vec<PositionStatusReport>) {
109        for report in reports {
110            self.position_reports
111                .entry(report.instrument_id)
112                .or_default()
113                .push(report);
114        }
115    }
116}
117
118impl std::fmt::Display for ExecutionMassStatus {
119    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120        write!(
121            f,
122            "ExecutionMassStatus(client_id={}, account_id={}, venue={}, order_reports={:?}, fill_reports={:?}, position_reports={:?}, report_id={}, ts_init={})",
123            self.client_id,
124            self.account_id,
125            self.venue,
126            self.order_reports,
127            self.fill_reports,
128            self.position_reports,
129            self.report_id,
130            self.ts_init,
131        )
132    }
133}