Skip to main content

nautilus_common/messages/execution/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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
16//! Execution specific messages such as order commands.
17
18pub mod cancel;
19pub mod modify;
20pub mod query;
21pub mod report;
22pub mod submit;
23
24use nautilus_core::{Params, UnixNanos};
25use nautilus_model::{
26    identifiers::{ClientId, InstrumentId, StrategyId},
27    reports::{ExecutionMassStatus, FillReport, OrderStatusReport, PositionStatusReport},
28};
29use strum::Display;
30
31pub use self::{
32    cancel::{BatchCancelOrders, CancelAllOrders, CancelOrder},
33    modify::{BatchModifyOrders, ModifyOrder},
34    query::{QueryAccount, QueryOrder},
35    report::{
36        GenerateExecutionMassStatus, GenerateExecutionMassStatusBuilder, GenerateFillReports,
37        GenerateFillReportsBuilder, GenerateOrderStatusReport, GenerateOrderStatusReportBuilder,
38        GenerateOrderStatusReports, GenerateOrderStatusReportsBuilder,
39        GeneratePositionStatusReports, GeneratePositionStatusReportsBuilder,
40    },
41    submit::{SubmitOrder, SubmitOrderList},
42};
43
44/// Parameter indicating that a conditional order should close the whole position at trigger time.
45pub const PARAMS_CLOSE_POSITION: &str = "close_position";
46
47/// Execution report variants for reconciliation.
48#[derive(Clone, Debug, Display)]
49pub enum ExecutionReport {
50    Order(Box<OrderStatusReport>),
51    Fill(Box<FillReport>),
52    OrderWithFills(Box<OrderStatusReport>, Vec<FillReport>),
53    Position(Box<PositionStatusReport>),
54    MassStatus(Box<ExecutionMassStatus>),
55}
56
57#[expect(clippy::large_enum_variant)]
58#[derive(Clone, Debug, Eq, PartialEq, Display)]
59pub enum TradingCommand {
60    SubmitOrder(SubmitOrder),
61    SubmitOrderList(SubmitOrderList),
62    ModifyOrder(ModifyOrder),
63    ModifyOrders(BatchModifyOrders),
64    CancelOrder(CancelOrder),
65    CancelOrders(BatchCancelOrders),
66    CancelAllOrders(CancelAllOrders),
67    QueryOrder(QueryOrder),
68    QueryAccount(QueryAccount),
69}
70
71impl TradingCommand {
72    #[must_use]
73    pub const fn client_id(&self) -> Option<ClientId> {
74        match self {
75            Self::SubmitOrder(command) => command.client_id,
76            Self::SubmitOrderList(command) => command.client_id,
77            Self::ModifyOrder(command) => command.client_id,
78            Self::ModifyOrders(command) => command.client_id,
79            Self::CancelOrder(command) => command.client_id,
80            Self::CancelOrders(command) => command.client_id,
81            Self::CancelAllOrders(command) => command.client_id,
82            Self::QueryOrder(command) => command.client_id,
83            Self::QueryAccount(command) => command.client_id,
84        }
85    }
86
87    /// Returns the instrument ID for the command.
88    ///
89    /// # Panics
90    ///
91    /// Panics if the command is `QueryAccount` which does not have an instrument ID.
92    #[must_use]
93    pub const fn instrument_id(&self) -> InstrumentId {
94        match self {
95            Self::SubmitOrder(command) => command.instrument_id,
96            Self::SubmitOrderList(command) => command.instrument_id,
97            Self::ModifyOrder(command) => command.instrument_id,
98            Self::ModifyOrders(command) => command.instrument_id,
99            Self::CancelOrder(command) => command.instrument_id,
100            Self::CancelOrders(command) => command.instrument_id,
101            Self::CancelAllOrders(command) => command.instrument_id,
102            Self::QueryOrder(command) => command.instrument_id,
103            Self::QueryAccount(_) => panic!("No instrument ID for command"),
104        }
105    }
106
107    #[must_use]
108    pub const fn ts_init(&self) -> UnixNanos {
109        match self {
110            Self::SubmitOrder(command) => command.ts_init,
111            Self::SubmitOrderList(command) => command.ts_init,
112            Self::ModifyOrder(command) => command.ts_init,
113            Self::ModifyOrders(command) => command.ts_init,
114            Self::CancelOrder(command) => command.ts_init,
115            Self::CancelOrders(command) => command.ts_init,
116            Self::CancelAllOrders(command) => command.ts_init,
117            Self::QueryOrder(command) => command.ts_init,
118            Self::QueryAccount(command) => command.ts_init,
119        }
120    }
121
122    #[must_use]
123    pub const fn strategy_id(&self) -> Option<StrategyId> {
124        match self {
125            Self::SubmitOrder(command) => Some(command.strategy_id),
126            Self::SubmitOrderList(command) => Some(command.strategy_id),
127            Self::ModifyOrder(command) => Some(command.strategy_id),
128            Self::ModifyOrders(command) => Some(command.strategy_id),
129            Self::CancelOrder(command) => Some(command.strategy_id),
130            Self::CancelOrders(command) => Some(command.strategy_id),
131            Self::CancelAllOrders(command) => Some(command.strategy_id),
132            Self::QueryOrder(command) => Some(command.strategy_id),
133            Self::QueryAccount(_) => None,
134        }
135    }
136
137    #[must_use]
138    pub const fn params(&self) -> Option<&Params> {
139        match self {
140            Self::SubmitOrder(command) => command.params.as_ref(),
141            Self::SubmitOrderList(command) => command.params.as_ref(),
142            Self::ModifyOrder(command) => command.params.as_ref(),
143            Self::ModifyOrders(command) => command.params.as_ref(),
144            Self::CancelOrder(command) => command.params.as_ref(),
145            Self::CancelOrders(command) => command.params.as_ref(),
146            Self::CancelAllOrders(command) => command.params.as_ref(),
147            Self::QueryOrder(command) => command.params.as_ref(),
148            Self::QueryAccount(command) => command.params.as_ref(),
149        }
150    }
151}