Skip to main content

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