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