nautilus_model/events/order/
canceled.rs1use std::fmt::{Debug, Display};
17
18use nautilus_core::{UUID4, UnixNanos};
19use rust_decimal::Decimal;
20use serde::{Deserialize, Serialize};
21use ustr::Ustr;
22
23use crate::{
24 enums::{
25 ContingencyType, LiquiditySide, OrderSide, OrderType, TimeInForce, TrailingOffsetType,
26 TriggerType,
27 },
28 events::OrderEvent,
29 identifiers::{
30 AccountId, ClientOrderId, ExecAlgorithmId, InstrumentId, OrderListId, PositionId,
31 StrategyId, TradeId, TraderId, VenueOrderId,
32 },
33 types::{Currency, Money, Price, Quantity},
34};
35
36#[repr(C)]
38#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
39#[serde(tag = "type")]
40#[cfg_attr(
41 feature = "python",
42 pyo3::pyclass(module = "nautilus_trader.model", from_py_object)
43)]
44#[cfg_attr(
45 feature = "python",
46 pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
47)]
48pub struct OrderCanceled {
49 pub trader_id: TraderId,
51 pub strategy_id: StrategyId,
53 pub instrument_id: InstrumentId,
55 pub client_order_id: ClientOrderId,
57 pub event_id: UUID4,
59 pub ts_event: UnixNanos,
61 pub ts_init: UnixNanos,
63 pub reconciliation: bool,
65 pub venue_order_id: Option<VenueOrderId>,
67 pub account_id: Option<AccountId>,
69 #[serde(default, skip_serializing_if = "Option::is_none")]
71 pub reason: Option<Ustr>,
72 #[serde(default, skip_serializing_if = "Option::is_none")]
74 pub causation_id: Option<UUID4>,
75}
76
77impl OrderCanceled {
78 #[expect(clippy::too_many_arguments)]
80 #[must_use]
81 pub fn new(
82 trader_id: TraderId,
83 strategy_id: StrategyId,
84 instrument_id: InstrumentId,
85 client_order_id: ClientOrderId,
86 event_id: UUID4,
87 ts_event: UnixNanos,
88 ts_init: UnixNanos,
89 reconciliation: bool,
90 venue_order_id: Option<VenueOrderId>,
91 account_id: Option<AccountId>,
92 reason: Option<Ustr>,
93 ) -> Self {
94 Self {
95 trader_id,
96 strategy_id,
97 instrument_id,
98 client_order_id,
99 event_id,
100 ts_event,
101 ts_init,
102 reconciliation,
103 venue_order_id,
104 account_id,
105 reason,
106 causation_id: None,
107 }
108 }
109}
110
111impl Debug for OrderCanceled {
112 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113 write!(
114 f,
115 "{}(trader_id={}, strategy_id={}, instrument_id={}, client_order_id={}, venue_order_id={}, account_id={}, event_id={}, ts_event={}, ts_init={})",
116 stringify!(OrderCanceled),
117 self.trader_id,
118 self.strategy_id,
119 self.instrument_id,
120 self.client_order_id,
121 self.venue_order_id.map_or_else(
122 || "None".to_string(),
123 |venue_order_id| format!("{venue_order_id}")
124 ),
125 self.account_id
126 .map_or_else(|| "None".to_string(), |account_id| format!("{account_id}")),
127 self.event_id,
128 self.ts_event,
129 self.ts_init
130 )
131 }
132}
133
134impl Display for OrderCanceled {
135 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136 write!(
137 f,
138 "{}(instrument_id={}, client_order_id={}, venue_order_id={}, account_id={}, reason={}, ts_event={})",
139 stringify!(OrderCanceled),
140 self.instrument_id,
141 self.client_order_id,
142 self.venue_order_id
143 .map_or("None".to_string(), |venue_order_id| format!(
144 "{venue_order_id}"
145 )),
146 self.account_id
147 .map_or("None".to_string(), |account_id| format!("{account_id}")),
148 self.reason.map_or("None", |reason| reason.as_str()),
149 self.ts_event
150 )
151 }
152}
153
154impl OrderEvent for OrderCanceled {
155 fn id(&self) -> UUID4 {
156 self.event_id
157 }
158
159 fn type_name(&self) -> &'static str {
160 stringify!(OrderCanceled)
161 }
162
163 fn order_type(&self) -> Option<OrderType> {
164 None
165 }
166
167 fn order_side(&self) -> Option<OrderSide> {
168 None
169 }
170
171 fn trader_id(&self) -> TraderId {
172 self.trader_id
173 }
174
175 fn strategy_id(&self) -> StrategyId {
176 self.strategy_id
177 }
178
179 fn instrument_id(&self) -> InstrumentId {
180 self.instrument_id
181 }
182
183 fn trade_id(&self) -> Option<TradeId> {
184 None
185 }
186
187 fn currency(&self) -> Option<Currency> {
188 None
189 }
190
191 fn client_order_id(&self) -> ClientOrderId {
192 self.client_order_id
193 }
194
195 fn reason(&self) -> Option<Ustr> {
196 self.reason
197 }
198
199 fn quantity(&self) -> Option<Quantity> {
200 None
201 }
202
203 fn time_in_force(&self) -> Option<TimeInForce> {
204 None
205 }
206
207 fn liquidity_side(&self) -> Option<LiquiditySide> {
208 None
209 }
210
211 fn post_only(&self) -> Option<bool> {
212 None
213 }
214
215 fn reduce_only(&self) -> Option<bool> {
216 None
217 }
218
219 fn quote_quantity(&self) -> Option<bool> {
220 None
221 }
222
223 fn reconciliation(&self) -> bool {
224 self.reconciliation
225 }
226
227 fn price(&self) -> Option<Price> {
228 None
229 }
230
231 fn last_px(&self) -> Option<Price> {
232 None
233 }
234
235 fn last_qty(&self) -> Option<Quantity> {
236 None
237 }
238
239 fn activation_price(&self) -> Option<Price> {
240 None
241 }
242
243 fn trigger_price(&self) -> Option<Price> {
244 None
245 }
246
247 fn trigger_type(&self) -> Option<TriggerType> {
248 None
249 }
250
251 fn limit_offset(&self) -> Option<Decimal> {
252 None
253 }
254
255 fn trailing_offset(&self) -> Option<Decimal> {
256 None
257 }
258
259 fn trailing_offset_type(&self) -> Option<TrailingOffsetType> {
260 None
261 }
262
263 fn expire_time(&self) -> Option<UnixNanos> {
264 None
265 }
266
267 fn display_qty(&self) -> Option<Quantity> {
268 None
269 }
270
271 fn emulation_trigger(&self) -> Option<TriggerType> {
272 None
273 }
274
275 fn trigger_instrument_id(&self) -> Option<InstrumentId> {
276 None
277 }
278
279 fn contingency_type(&self) -> Option<ContingencyType> {
280 None
281 }
282
283 fn order_list_id(&self) -> Option<OrderListId> {
284 None
285 }
286
287 fn linked_order_ids(&self) -> Option<Vec<ClientOrderId>> {
288 None
289 }
290
291 fn parent_order_id(&self) -> Option<ClientOrderId> {
292 None
293 }
294
295 fn exec_algorithm_id(&self) -> Option<ExecAlgorithmId> {
296 None
297 }
298
299 fn exec_spawn_id(&self) -> Option<ClientOrderId> {
300 None
301 }
302
303 fn venue_order_id(&self) -> Option<VenueOrderId> {
304 self.venue_order_id
305 }
306
307 fn account_id(&self) -> Option<AccountId> {
308 self.account_id
309 }
310
311 fn position_id(&self) -> Option<PositionId> {
312 None
313 }
314
315 fn commission(&self) -> Option<Money> {
316 None
317 }
318
319 fn ts_event(&self) -> UnixNanos {
320 self.ts_event
321 }
322
323 fn ts_init(&self) -> UnixNanos {
324 self.ts_init
325 }
326 fn causation_id(&self) -> Option<UUID4> {
327 self.causation_id
328 }
329}
330
331#[cfg(test)]
332mod tests {
333 use rstest::rstest;
334
335 use super::*;
336
337 #[rstest]
338 fn test_serialization_roundtrip_with_reason() {
339 let original = OrderCanceled {
340 reason: Some(Ustr::from("not-enough-liquidity")),
341 ..OrderCanceled::default()
342 };
343 let json = serde_json::to_string(&original).unwrap();
344 let deserialized: OrderCanceled = serde_json::from_str(&json).unwrap();
345 assert_eq!(original, deserialized);
346 assert_eq!(
347 deserialized.reason(),
348 Some(Ustr::from("not-enough-liquidity"))
349 );
350 }
351
352 #[rstest]
353 fn test_deserialization_defaults_missing_reason_to_none() {
354 let payload = serde_json::to_value(OrderCanceled::default()).unwrap();
355 assert!(payload.get("reason").is_none());
356
357 let deserialized: OrderCanceled = serde_json::from_value(payload).unwrap();
358
359 assert_eq!(deserialized.reason(), None);
360 }
361}