nautilus_model/events/order/
canceled.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::fmt::{Debug, Display};
17
18use derive_builder::Builder;
19use nautilus_core::{UUID4, UnixNanos, serialization::from_bool_as_u8};
20use rust_decimal::Decimal;
21use serde::{Deserialize, Serialize};
22use ustr::Ustr;
23
24use crate::{
25    enums::{
26        ContingencyType, LiquiditySide, OrderSide, OrderType, TimeInForce, TrailingOffsetType,
27        TriggerType,
28    },
29    events::OrderEvent,
30    identifiers::{
31        AccountId, ClientOrderId, ExecAlgorithmId, InstrumentId, OrderListId, PositionId,
32        StrategyId, TradeId, TraderId, VenueOrderId,
33    },
34    types::{Currency, Money, Price, Quantity},
35};
36
37/// Represents an event where an order has been canceled at the trading venue.
38#[repr(C)]
39#[derive(Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, Builder)]
40#[builder(default)]
41#[serde(tag = "type")]
42#[cfg_attr(
43    feature = "python",
44    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
45)]
46pub struct OrderCanceled {
47    /// The trader ID associated with the event.
48    pub trader_id: TraderId,
49    /// The strategy ID associated with the event.
50    pub strategy_id: StrategyId,
51    /// The instrument ID associated with the event.
52    pub instrument_id: InstrumentId,
53    /// The client order ID associated with the event.
54    pub client_order_id: ClientOrderId,
55    /// The unique identifier for the event.
56    pub event_id: UUID4,
57    /// UNIX timestamp (nanoseconds) when the event occurred.
58    pub ts_event: UnixNanos,
59    /// UNIX timestamp (nanoseconds) when the event was initialized.
60    pub ts_init: UnixNanos,
61    /// If the event was generated during reconciliation.
62    #[serde(deserialize_with = "from_bool_as_u8")]
63    pub reconciliation: u8, // TODO: Change to bool once Cython removed
64    /// The venue order ID associated with the event.
65    pub venue_order_id: Option<VenueOrderId>,
66    /// The account ID associated with the event.
67    pub account_id: Option<AccountId>,
68}
69
70impl OrderCanceled {
71    /// Creates a new [`OrderCanceled`] instance.
72    #[allow(clippy::too_many_arguments)]
73    pub fn new(
74        trader_id: TraderId,
75        strategy_id: StrategyId,
76        instrument_id: InstrumentId,
77        client_order_id: ClientOrderId,
78        event_id: UUID4,
79        ts_event: UnixNanos,
80        ts_init: UnixNanos,
81        reconciliation: bool,
82        venue_order_id: Option<VenueOrderId>,
83        account_id: Option<AccountId>,
84    ) -> Self {
85        Self {
86            trader_id,
87            strategy_id,
88            instrument_id,
89            client_order_id,
90            event_id,
91            ts_event,
92            ts_init,
93            reconciliation: u8::from(reconciliation),
94            venue_order_id,
95            account_id,
96        }
97    }
98}
99
100impl Debug for OrderCanceled {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        write!(
103            f,
104            "{}(trader_id={}, strategy_id={}, instrument_id={}, client_order_id={}, venue_order_id={}, account_id={}, event_id={}, ts_event={}, ts_init={})",
105            stringify!(OrderCanceled),
106            self.trader_id,
107            self.strategy_id,
108            self.instrument_id,
109            self.client_order_id,
110            self.venue_order_id.map_or_else(
111                || "None".to_string(),
112                |venue_order_id| format!("{venue_order_id}")
113            ),
114            self.account_id
115                .map_or_else(|| "None".to_string(), |account_id| format!("{account_id}")),
116            self.event_id,
117            self.ts_event,
118            self.ts_init
119        )
120    }
121}
122
123impl Display for OrderCanceled {
124    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125        write!(
126            f,
127            "{}(instrument_id={}, client_order_id={}, venue_order_id={}, account_id={}, ts_event={})",
128            stringify!(OrderCanceled),
129            self.instrument_id,
130            self.client_order_id,
131            self.venue_order_id
132                .map_or("None".to_string(), |venue_order_id| format!(
133                    "{venue_order_id}"
134                )),
135            self.account_id
136                .map_or("None".to_string(), |account_id| format!("{account_id}")),
137            self.ts_event
138        )
139    }
140}
141
142impl OrderEvent for OrderCanceled {
143    fn id(&self) -> UUID4 {
144        self.event_id
145    }
146
147    fn kind(&self) -> &str {
148        stringify!(OrderCanceled)
149    }
150
151    fn order_type(&self) -> Option<OrderType> {
152        None
153    }
154
155    fn order_side(&self) -> Option<OrderSide> {
156        None
157    }
158
159    fn trader_id(&self) -> TraderId {
160        self.trader_id
161    }
162
163    fn strategy_id(&self) -> StrategyId {
164        self.strategy_id
165    }
166
167    fn instrument_id(&self) -> InstrumentId {
168        self.instrument_id
169    }
170
171    fn trade_id(&self) -> Option<TradeId> {
172        None
173    }
174
175    fn currency(&self) -> Option<Currency> {
176        None
177    }
178
179    fn client_order_id(&self) -> ClientOrderId {
180        self.client_order_id
181    }
182
183    fn reason(&self) -> Option<Ustr> {
184        None
185    }
186
187    fn quantity(&self) -> Option<Quantity> {
188        None
189    }
190
191    fn time_in_force(&self) -> Option<TimeInForce> {
192        None
193    }
194
195    fn liquidity_side(&self) -> Option<LiquiditySide> {
196        None
197    }
198
199    fn post_only(&self) -> Option<bool> {
200        None
201    }
202
203    fn reduce_only(&self) -> Option<bool> {
204        None
205    }
206
207    fn quote_quantity(&self) -> Option<bool> {
208        None
209    }
210
211    fn reconciliation(&self) -> bool {
212        false
213    }
214
215    fn price(&self) -> Option<Price> {
216        None
217    }
218
219    fn last_px(&self) -> Option<Price> {
220        None
221    }
222
223    fn last_qty(&self) -> Option<Quantity> {
224        None
225    }
226
227    fn trigger_price(&self) -> Option<Price> {
228        None
229    }
230
231    fn trigger_type(&self) -> Option<TriggerType> {
232        None
233    }
234
235    fn limit_offset(&self) -> Option<Decimal> {
236        None
237    }
238
239    fn trailing_offset(&self) -> Option<Decimal> {
240        None
241    }
242
243    fn trailing_offset_type(&self) -> Option<TrailingOffsetType> {
244        None
245    }
246
247    fn expire_time(&self) -> Option<UnixNanos> {
248        None
249    }
250
251    fn display_qty(&self) -> Option<Quantity> {
252        None
253    }
254
255    fn emulation_trigger(&self) -> Option<TriggerType> {
256        None
257    }
258
259    fn trigger_instrument_id(&self) -> Option<InstrumentId> {
260        None
261    }
262
263    fn contingency_type(&self) -> Option<ContingencyType> {
264        None
265    }
266
267    fn order_list_id(&self) -> Option<OrderListId> {
268        None
269    }
270
271    fn linked_order_ids(&self) -> Option<Vec<ClientOrderId>> {
272        None
273    }
274
275    fn parent_order_id(&self) -> Option<ClientOrderId> {
276        None
277    }
278
279    fn exec_algorithm_id(&self) -> Option<ExecAlgorithmId> {
280        None
281    }
282
283    fn exec_spawn_id(&self) -> Option<ClientOrderId> {
284        None
285    }
286
287    fn venue_order_id(&self) -> Option<VenueOrderId> {
288        self.venue_order_id
289    }
290
291    fn account_id(&self) -> Option<AccountId> {
292        self.account_id
293    }
294
295    fn position_id(&self) -> Option<PositionId> {
296        None
297    }
298
299    fn commission(&self) -> Option<Money> {
300        None
301    }
302
303    fn ts_event(&self) -> UnixNanos {
304        self.ts_event
305    }
306
307    fn ts_init(&self) -> UnixNanos {
308        self.ts_init
309    }
310}
311
312////////////////////////////////////////////////////////////////////////////////
313// Tests
314////////////////////////////////////////////////////////////////////////////////
315#[cfg(test)]
316mod tests {}