nautilus_common/messages/execution/
cancel.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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::{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, Default, Serialize, Deserialize, Builder)]
27#[builder(default)]
28#[serde(tag = "type")]
29pub struct CancelOrder {
30    pub trader_id: TraderId,
31    pub client_id: ClientId,
32    pub strategy_id: StrategyId,
33    pub instrument_id: InstrumentId,
34    pub client_order_id: ClientOrderId,
35    pub venue_order_id: VenueOrderId,
36    pub command_id: UUID4,
37    pub ts_init: UnixNanos,
38}
39
40impl CancelOrder {
41    /// Creates a new [`CancelOrder`] instance.
42    ///
43    /// # Errors
44    ///
45    /// Returns an error if parameters are invalid.
46    #[allow(clippy::too_many_arguments)]
47    pub const fn new(
48        trader_id: TraderId,
49        client_id: ClientId,
50        strategy_id: StrategyId,
51        instrument_id: InstrumentId,
52        client_order_id: ClientOrderId,
53        venue_order_id: VenueOrderId,
54        command_id: UUID4,
55        ts_init: UnixNanos,
56    ) -> anyhow::Result<Self> {
57        Ok(Self {
58            trader_id,
59            client_id,
60            strategy_id,
61            instrument_id,
62            client_order_id,
63            venue_order_id,
64            command_id,
65            ts_init,
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, Default, Serialize, Deserialize, Builder)]
81#[builder(default)]
82#[serde(tag = "type")]
83pub struct CancelAllOrders {
84    pub trader_id: TraderId,
85    pub client_id: ClientId,
86    pub strategy_id: StrategyId,
87    pub instrument_id: InstrumentId,
88    pub order_side: OrderSide,
89    pub command_id: UUID4,
90    pub ts_init: UnixNanos,
91}
92
93impl CancelAllOrders {
94    /// Creates a new [`CancelAllOrders`] instance.
95    ///
96    /// # Errors
97    ///
98    /// Returns an error if parameters are invalid.
99    #[allow(clippy::too_many_arguments)]
100    pub const fn new(
101        trader_id: TraderId,
102        client_id: ClientId,
103        strategy_id: StrategyId,
104        instrument_id: InstrumentId,
105        order_side: OrderSide,
106        command_id: UUID4,
107        ts_init: UnixNanos,
108    ) -> anyhow::Result<Self> {
109        Ok(Self {
110            trader_id,
111            client_id,
112            strategy_id,
113            instrument_id,
114            order_side,
115            command_id,
116            ts_init,
117        })
118    }
119}
120
121impl Display for CancelAllOrders {
122    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123        write!(
124            f,
125            "CancelAllOrders(instrument_id={}, order_side={})",
126            self.instrument_id, self.order_side,
127        )
128    }
129}
130
131#[derive(Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Builder)]
132#[builder(default)]
133#[serde(tag = "type")]
134pub struct BatchCancelOrders {
135    pub trader_id: TraderId,
136    pub client_id: ClientId,
137    pub strategy_id: StrategyId,
138    pub instrument_id: InstrumentId,
139    pub cancels: Vec<CancelOrder>,
140    pub command_id: UUID4,
141    pub ts_init: UnixNanos,
142}
143
144impl BatchCancelOrders {
145    /// Creates a new [`BatchCancelOrders`] instance.
146    ///
147    /// # Errors
148    ///
149    /// Returns an error if parameters are invalid.
150    #[allow(clippy::too_many_arguments)]
151    pub const fn new(
152        trader_id: TraderId,
153        client_id: ClientId,
154        strategy_id: StrategyId,
155        instrument_id: InstrumentId,
156        cancels: Vec<CancelOrder>,
157        command_id: UUID4,
158        ts_init: UnixNanos,
159    ) -> anyhow::Result<Self> {
160        Ok(Self {
161            trader_id,
162            client_id,
163            strategy_id,
164            instrument_id,
165            cancels,
166            command_id,
167            ts_init,
168        })
169    }
170}
171
172impl Display for BatchCancelOrders {
173    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
174        write!(
175            f,
176            "BatchCancelOrders(instrument_id={}, cancels=TBD)",
177            self.instrument_id,
178        )
179    }
180}
181
182////////////////////////////////////////////////////////////////////////////////
183// Tests
184////////////////////////////////////////////////////////////////////////////////
185#[cfg(test)]
186mod tests {}