nautilus_common/messages/execution/
submit.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
16use std::fmt::Display;
17
18use indexmap::IndexMap;
19use nautilus_core::{UUID4, UnixNanos};
20use nautilus_model::{
21    identifiers::{
22        ClientId, ClientOrderId, ExecAlgorithmId, InstrumentId, PositionId, StrategyId, TraderId,
23        VenueOrderId,
24    },
25    orders::{Order, OrderAny, OrderList},
26};
27use serde::{Deserialize, Serialize};
28
29#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
30#[serde(tag = "type")]
31pub struct SubmitOrder {
32    pub trader_id: TraderId,
33    pub client_id: Option<ClientId>,
34    pub strategy_id: StrategyId,
35    pub instrument_id: InstrumentId,
36    pub order: OrderAny,
37    pub exec_algorithm_id: Option<ExecAlgorithmId>,
38    pub position_id: Option<PositionId>,
39    pub params: Option<IndexMap<String, String>>,
40    pub command_id: UUID4,
41    pub ts_init: UnixNanos,
42}
43
44impl SubmitOrder {
45    /// Creates a new [`SubmitOrder`] instance.
46    #[allow(clippy::too_many_arguments)]
47    #[must_use]
48    pub fn new(
49        trader_id: TraderId,
50        client_id: Option<ClientId>,
51        strategy_id: StrategyId,
52        instrument_id: InstrumentId,
53        order: OrderAny,
54        exec_algorithm_id: Option<ExecAlgorithmId>,
55        position_id: Option<PositionId>,
56        params: Option<IndexMap<String, String>>,
57        command_id: UUID4,
58        ts_init: UnixNanos,
59    ) -> Self {
60        Self {
61            trader_id,
62            client_id,
63            strategy_id,
64            instrument_id,
65            order,
66            exec_algorithm_id,
67            position_id,
68            params,
69            command_id,
70            ts_init,
71        }
72    }
73
74    /// Returns the client order ID from the contained order.
75    #[must_use]
76    pub fn client_order_id(&self) -> ClientOrderId {
77        self.order.client_order_id()
78    }
79
80    /// Returns the venue order ID from the contained order, if set.
81    #[must_use]
82    pub fn venue_order_id(&self) -> Option<VenueOrderId> {
83        self.order.venue_order_id()
84    }
85}
86
87impl Display for SubmitOrder {
88    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89        write!(
90            f,
91            "SubmitOrder(instrument_id={}, order=TBD, position_id={})",
92            self.instrument_id,
93            self.position_id
94                .map_or("None".to_string(), |position_id| format!("{position_id}")),
95        )
96    }
97}
98
99#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
100#[serde(tag = "type")]
101pub struct SubmitOrderList {
102    pub trader_id: TraderId,
103    pub client_id: Option<ClientId>,
104    pub strategy_id: StrategyId,
105    pub instrument_id: InstrumentId,
106    pub order_list: OrderList,
107    pub exec_algorithm_id: Option<ExecAlgorithmId>,
108    pub position_id: Option<PositionId>,
109    pub params: Option<IndexMap<String, String>>,
110    pub command_id: UUID4,
111    pub ts_init: UnixNanos,
112}
113
114impl SubmitOrderList {
115    /// Creates a new [`SubmitOrderList`] instance.
116    #[allow(clippy::too_many_arguments)]
117    #[must_use]
118    pub const fn new(
119        trader_id: TraderId,
120        client_id: Option<ClientId>,
121        strategy_id: StrategyId,
122        instrument_id: InstrumentId,
123        order_list: OrderList,
124        exec_algorithm_id: Option<ExecAlgorithmId>,
125        position_id: Option<PositionId>,
126        params: Option<IndexMap<String, String>>,
127        command_id: UUID4,
128        ts_init: UnixNanos,
129    ) -> Self {
130        Self {
131            trader_id,
132            client_id,
133            strategy_id,
134            instrument_id,
135            order_list,
136            exec_algorithm_id,
137            position_id,
138            params,
139            command_id,
140            ts_init,
141        }
142    }
143}
144
145impl Display for SubmitOrderList {
146    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
147        write!(
148            f,
149            "SubmitOrderList(instrument_id={}, order_list=TBD, position_id={})",
150            self.instrument_id,
151            self.position_id
152                .map_or("None".to_string(), |position_id| format!("{position_id}")),
153        )
154    }
155}