nautilus_execution/messages/
cancel.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::{cell::RefCell, fmt::Display, rc::Rc};
17
18use derive_builder::Builder;
19use nautilus_core::{UUID4, UnixNanos};
20use nautilus_model::{
21    identifiers::{ClientId, ClientOrderId, InstrumentId, StrategyId, TraderId, VenueOrderId},
22    orders::OrderAny,
23};
24use serde::{Deserialize, Serialize};
25
26use crate::order_emulator::emulator::OrderEmulator;
27
28#[derive(Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Builder)]
29#[builder(default)]
30#[serde(tag = "type")]
31pub struct CancelOrder {
32    pub trader_id: TraderId,
33    pub client_id: ClientId,
34    pub strategy_id: StrategyId,
35    pub instrument_id: InstrumentId,
36    pub client_order_id: ClientOrderId,
37    pub venue_order_id: VenueOrderId,
38    pub command_id: UUID4,
39    pub ts_init: UnixNanos,
40}
41
42impl CancelOrder {
43    /// Creates a new [`CancelOrder`] instance.
44    #[allow(clippy::too_many_arguments)]
45    pub const fn new(
46        trader_id: TraderId,
47        client_id: ClientId,
48        strategy_id: StrategyId,
49        instrument_id: InstrumentId,
50        client_order_id: ClientOrderId,
51        venue_order_id: VenueOrderId,
52        command_id: UUID4,
53        ts_init: UnixNanos,
54    ) -> anyhow::Result<Self> {
55        Ok(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        })
65    }
66}
67
68impl Display for CancelOrder {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        write!(
71            f,
72            "CancelOrder(instrument_id={}, client_order_id={}, venue_order_id={})",
73            self.instrument_id, self.client_order_id, self.venue_order_id,
74        )
75    }
76}
77
78pub trait CancelOrderHandler {
79    fn handle_cancel_order(&self, order: &OrderAny);
80}
81
82pub enum CancelOrderHandlerAny {
83    OrderEmulator(Rc<RefCell<OrderEmulator>>),
84}
85
86impl CancelOrderHandler for CancelOrderHandlerAny {
87    fn handle_cancel_order(&self, order: &OrderAny) {
88        match self {
89            Self::OrderEmulator(order_emulator) => {
90                order_emulator.borrow_mut().cancel_order(order);
91            }
92        }
93    }
94}
95
96////////////////////////////////////////////////////////////////////////////////
97// Tests
98////////////////////////////////////////////////////////////////////////////////
99#[cfg(test)]
100mod tests {}