nautilus_model/events/order/
cancel_rejected.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)]
39#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
40#[serde(tag = "type")]
41#[cfg_attr(
42 feature = "python",
43 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
44)]
45#[cfg_attr(
46 feature = "python",
47 pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
48)]
49pub struct OrderCancelRejected {
50 pub trader_id: TraderId,
52 pub strategy_id: StrategyId,
54 pub instrument_id: InstrumentId,
56 pub client_order_id: ClientOrderId,
58 pub reason: Ustr,
60 pub event_id: UUID4,
62 pub ts_event: UnixNanos,
64 pub ts_init: UnixNanos,
66 pub reconciliation: bool,
68 pub venue_order_id: Option<VenueOrderId>,
70 pub account_id: Option<AccountId>,
72 #[serde(default, skip_serializing_if = "Option::is_none")]
74 pub causation_id: Option<UUID4>,
75}
76
77impl OrderCancelRejected {
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 reason: Ustr,
87 event_id: UUID4,
88 ts_event: UnixNanos,
89 ts_init: UnixNanos,
90 reconciliation: bool,
91 venue_order_id: Option<VenueOrderId>,
92 account_id: Option<AccountId>,
93 ) -> Self {
94 Self {
95 trader_id,
96 strategy_id,
97 instrument_id,
98 client_order_id,
99 reason,
100 event_id,
101 ts_event,
102 ts_init,
103 reconciliation,
104 venue_order_id,
105 account_id,
106 causation_id: None,
107 }
108 }
109}
110
111impl Debug for OrderCancelRejected {
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={}, reason='{}', event_id={}, ts_event={}, ts_init={})",
116 stringify!(OrderCancelRejected),
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.reason,
128 self.event_id,
129 self.ts_event,
130 self.ts_init
131 )
132 }
133}
134
135impl Display for OrderCancelRejected {
136 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
137 write!(
138 f,
139 "{}(instrument_id={}, client_order_id={}, venue_order_id={}, account_id={}, reason='{}', ts_event={})",
140 stringify!(OrderCancelRejected),
141 self.instrument_id,
142 self.client_order_id,
143 self.venue_order_id
144 .map_or("None".to_string(), |venue_order_id| format!(
145 "{venue_order_id}"
146 )),
147 self.account_id
148 .map_or("None".to_string(), |account_id| format!("{account_id}")),
149 self.reason,
150 self.ts_event
151 )
152 }
153}
154
155impl OrderEvent for OrderCancelRejected {
156 fn id(&self) -> UUID4 {
157 self.event_id
158 }
159
160 fn type_name(&self) -> &'static str {
161 stringify!(OrderCancelRejected)
162 }
163
164 fn order_type(&self) -> Option<OrderType> {
165 None
166 }
167
168 fn order_side(&self) -> Option<OrderSide> {
169 None
170 }
171
172 fn trader_id(&self) -> TraderId {
173 self.trader_id
174 }
175
176 fn strategy_id(&self) -> StrategyId {
177 self.strategy_id
178 }
179
180 fn instrument_id(&self) -> InstrumentId {
181 self.instrument_id
182 }
183
184 fn trade_id(&self) -> Option<TradeId> {
185 None
186 }
187
188 fn currency(&self) -> Option<Currency> {
189 None
190 }
191
192 fn client_order_id(&self) -> ClientOrderId {
193 self.client_order_id
194 }
195
196 fn reason(&self) -> Option<Ustr> {
197 Some(self.reason)
198 }
199
200 fn quantity(&self) -> Option<Quantity> {
201 None
202 }
203
204 fn time_in_force(&self) -> Option<TimeInForce> {
205 None
206 }
207
208 fn liquidity_side(&self) -> Option<LiquiditySide> {
209 None
210 }
211
212 fn post_only(&self) -> Option<bool> {
213 None
214 }
215
216 fn reduce_only(&self) -> Option<bool> {
217 None
218 }
219
220 fn quote_quantity(&self) -> Option<bool> {
221 None
222 }
223
224 fn reconciliation(&self) -> bool {
225 false
226 }
227
228 fn price(&self) -> Option<Price> {
229 None
230 }
231
232 fn last_px(&self) -> Option<Price> {
233 None
234 }
235
236 fn last_qty(&self) -> Option<Quantity> {
237 None
238 }
239
240 fn trigger_price(&self) -> Option<Price> {
241 None
242 }
243
244 fn trigger_type(&self) -> Option<TriggerType> {
245 None
246 }
247
248 fn limit_offset(&self) -> Option<Decimal> {
249 None
250 }
251
252 fn trailing_offset(&self) -> Option<Decimal> {
253 None
254 }
255
256 fn trailing_offset_type(&self) -> Option<TrailingOffsetType> {
257 None
258 }
259
260 fn expire_time(&self) -> Option<UnixNanos> {
261 None
262 }
263
264 fn display_qty(&self) -> Option<Quantity> {
265 None
266 }
267
268 fn emulation_trigger(&self) -> Option<TriggerType> {
269 None
270 }
271
272 fn trigger_instrument_id(&self) -> Option<InstrumentId> {
273 None
274 }
275
276 fn contingency_type(&self) -> Option<ContingencyType> {
277 None
278 }
279
280 fn order_list_id(&self) -> Option<OrderListId> {
281 None
282 }
283
284 fn linked_order_ids(&self) -> Option<Vec<ClientOrderId>> {
285 None
286 }
287
288 fn parent_order_id(&self) -> Option<ClientOrderId> {
289 None
290 }
291
292 fn exec_algorithm_id(&self) -> Option<ExecAlgorithmId> {
293 None
294 }
295
296 fn exec_spawn_id(&self) -> Option<ClientOrderId> {
297 None
298 }
299
300 fn venue_order_id(&self) -> Option<VenueOrderId> {
301 self.venue_order_id
302 }
303
304 fn account_id(&self) -> Option<AccountId> {
305 self.account_id
306 }
307
308 fn position_id(&self) -> Option<PositionId> {
309 None
310 }
311
312 fn commission(&self) -> Option<Money> {
313 None
314 }
315
316 fn ts_event(&self) -> UnixNanos {
317 self.ts_event
318 }
319
320 fn ts_init(&self) -> UnixNanos {
321 self.ts_init
322 }
323}
324
325#[cfg(test)]
326mod tests {
327
328 use rstest::rstest;
329
330 use crate::events::order::{cancel_rejected::OrderCancelRejected, stubs::*};
331
332 #[rstest]
333 fn test_order_cancel_rejected(order_cancel_rejected: OrderCancelRejected) {
334 let display = format!("{order_cancel_rejected}");
335 assert_eq!(
336 display,
337 "OrderCancelRejected(instrument_id=BTCUSDT.COINBASE, client_order_id=O-19700101-000000-001-001-1, venue_order_id=001, account_id=SIM-001, reason='ORDER_DOES_NOT_EXIST', ts_event=0)"
338 );
339 }
340
341 #[rstest]
342 fn test_order_cancel_rejected_serialization() {
343 let original = OrderCancelRejected::default();
344 let json = serde_json::to_string(&original).unwrap();
345 let deserialized: OrderCancelRejected = serde_json::from_str(&json).unwrap();
346 assert_eq!(original, deserialized);
347 }
348}