nautilus_common/messages/execution/
cancel.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 derive_builder::Builder;
19use indexmap::IndexMap;
20use nautilus_core::{UUID4, UnixNanos};
21use nautilus_model::{
22    enums::OrderSide,
23    identifiers::{ClientId, ClientOrderId, InstrumentId, StrategyId, TraderId, VenueOrderId},
24};
25use serde::{Deserialize, Serialize};
26
27#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Builder)]
28#[serde(tag = "type")]
29pub struct CancelOrder {
30    pub trader_id: TraderId,
31    pub client_id: Option<ClientId>,
32    pub strategy_id: StrategyId,
33    pub instrument_id: InstrumentId,
34    pub client_order_id: ClientOrderId,
35    pub venue_order_id: Option<VenueOrderId>,
36    pub command_id: UUID4,
37    pub ts_init: UnixNanos,
38    pub params: Option<IndexMap<String, String>>,
39}
40
41impl CancelOrder {
42    /// Creates a new [`CancelOrder`] instance.
43    #[allow(clippy::too_many_arguments)]
44    #[must_use]
45    pub fn new(
46        trader_id: TraderId,
47        client_id: Option<ClientId>,
48        strategy_id: StrategyId,
49        instrument_id: InstrumentId,
50        client_order_id: ClientOrderId,
51        venue_order_id: Option<VenueOrderId>,
52        command_id: UUID4,
53        ts_init: UnixNanos,
54        params: Option<IndexMap<String, String>>,
55    ) -> Self {
56        Self {
57            trader_id,
58            client_id,
59            strategy_id,
60            instrument_id,
61            client_order_id,
62            venue_order_id,
63            command_id,
64            ts_init,
65            params,
66        }
67    }
68}
69
70impl Display for CancelOrder {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        write!(
73            f,
74            "CancelOrder(instrument_id={}, client_order_id={}, venue_order_id={:?})",
75            self.instrument_id, self.client_order_id, self.venue_order_id,
76        )
77    }
78}
79
80#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Builder)]
81#[serde(tag = "type")]
82pub struct CancelAllOrders {
83    pub trader_id: TraderId,
84    pub client_id: Option<ClientId>,
85    pub strategy_id: StrategyId,
86    pub instrument_id: InstrumentId,
87    pub order_side: OrderSide,
88    pub command_id: UUID4,
89    pub ts_init: UnixNanos,
90    pub params: Option<IndexMap<String, String>>,
91}
92
93impl CancelAllOrders {
94    /// Creates a new [`CancelAllOrders`] instance.
95    #[allow(clippy::too_many_arguments)]
96    #[must_use]
97    pub fn new(
98        trader_id: TraderId,
99        client_id: Option<ClientId>,
100        strategy_id: StrategyId,
101        instrument_id: InstrumentId,
102        order_side: OrderSide,
103        command_id: UUID4,
104        ts_init: UnixNanos,
105        params: Option<IndexMap<String, String>>,
106    ) -> Self {
107        Self {
108            trader_id,
109            client_id,
110            strategy_id,
111            instrument_id,
112            order_side,
113            command_id,
114            ts_init,
115            params,
116        }
117    }
118}
119
120impl Display for CancelAllOrders {
121    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
122        write!(
123            f,
124            "CancelAllOrders(instrument_id={}, order_side={})",
125            self.instrument_id, self.order_side,
126        )
127    }
128}
129
130#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Builder)]
131#[serde(tag = "type")]
132pub struct BatchCancelOrders {
133    pub trader_id: TraderId,
134    pub client_id: Option<ClientId>,
135    pub strategy_id: StrategyId,
136    pub instrument_id: InstrumentId,
137    pub cancels: Vec<CancelOrder>,
138    pub command_id: UUID4,
139    pub ts_init: UnixNanos,
140    pub params: Option<IndexMap<String, String>>,
141}
142
143impl BatchCancelOrders {
144    /// Creates a new [`BatchCancelOrders`] instance.
145    #[allow(clippy::too_many_arguments)]
146    #[must_use]
147    pub fn new(
148        trader_id: TraderId,
149        client_id: Option<ClientId>,
150        strategy_id: StrategyId,
151        instrument_id: InstrumentId,
152        cancels: Vec<CancelOrder>,
153        command_id: UUID4,
154        ts_init: UnixNanos,
155        params: Option<IndexMap<String, String>>,
156    ) -> Self {
157        Self {
158            trader_id,
159            client_id,
160            strategy_id,
161            instrument_id,
162            cancels,
163            command_id,
164            ts_init,
165            params,
166        }
167    }
168}
169
170impl Display for BatchCancelOrders {
171    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
172        write!(
173            f,
174            "BatchCancelOrders(instrument_id={}, cancels=TBD)",
175            self.instrument_id,
176        )
177    }
178}
179
180#[cfg(test)]
181mod tests {}