nautilus_model/events/order/
rejected.rs1use 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#[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 OrderRejected {
47 pub trader_id: TraderId,
49 pub strategy_id: StrategyId,
51 pub instrument_id: InstrumentId,
53 pub client_order_id: ClientOrderId,
55 pub account_id: AccountId,
57 pub reason: Ustr,
59 pub event_id: UUID4,
61 pub ts_event: UnixNanos,
63 pub ts_init: UnixNanos,
65 #[serde(deserialize_with = "from_bool_as_u8")]
67 pub reconciliation: u8, }
69
70impl OrderRejected {
71 #[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 account_id: AccountId,
79 reason: Ustr,
80 event_id: UUID4,
81 ts_event: UnixNanos,
82 ts_init: UnixNanos,
83 reconciliation: bool,
84 ) -> Self {
85 Self {
86 trader_id,
87 strategy_id,
88 instrument_id,
89 client_order_id,
90 account_id,
91 reason,
92 event_id,
93 ts_event,
94 ts_init,
95 reconciliation: u8::from(reconciliation),
96 }
97 }
98}
99
100impl Debug for OrderRejected {
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={}, account_id={}, reason='{}', event_id={}, ts_event={}, ts_init={})",
105 stringify!(OrderRejected),
106 self.trader_id,
107 self.strategy_id,
108 self.instrument_id,
109 self.client_order_id,
110 self.account_id,
111 self.reason,
112 self.event_id,
113 self.ts_event,
114 self.ts_init
115 )
116 }
117}
118
119impl Display for OrderRejected {
120 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
121 write!(
122 f,
123 "{}(instrument_id={}, client_order_id={}, account_id={}, reason='{}', ts_event={})",
124 stringify!(OrderRejected),
125 self.instrument_id,
126 self.client_order_id,
127 self.account_id,
128 self.reason,
129 self.ts_event
130 )
131 }
132}
133
134impl OrderEvent for OrderRejected {
135 fn id(&self) -> UUID4 {
136 self.event_id
137 }
138
139 fn kind(&self) -> &str {
140 stringify!(OrderRejected)
141 }
142
143 fn order_type(&self) -> Option<OrderType> {
144 None
145 }
146
147 fn order_side(&self) -> Option<OrderSide> {
148 None
149 }
150
151 fn trader_id(&self) -> TraderId {
152 self.trader_id
153 }
154
155 fn strategy_id(&self) -> StrategyId {
156 self.strategy_id
157 }
158
159 fn instrument_id(&self) -> InstrumentId {
160 self.instrument_id
161 }
162
163 fn trade_id(&self) -> Option<TradeId> {
164 None
165 }
166
167 fn currency(&self) -> Option<Currency> {
168 None
169 }
170
171 fn client_order_id(&self) -> ClientOrderId {
172 self.client_order_id
173 }
174
175 fn reason(&self) -> Option<Ustr> {
176 Some(self.reason)
177 }
178
179 fn quantity(&self) -> Option<Quantity> {
180 None
181 }
182
183 fn time_in_force(&self) -> Option<TimeInForce> {
184 None
185 }
186
187 fn liquidity_side(&self) -> Option<LiquiditySide> {
188 None
189 }
190
191 fn post_only(&self) -> Option<bool> {
192 None
193 }
194
195 fn reduce_only(&self) -> Option<bool> {
196 None
197 }
198
199 fn quote_quantity(&self) -> Option<bool> {
200 None
201 }
202
203 fn reconciliation(&self) -> bool {
204 false
205 }
206
207 fn price(&self) -> Option<Price> {
208 None
209 }
210
211 fn last_px(&self) -> Option<Price> {
212 None
213 }
214
215 fn last_qty(&self) -> Option<Quantity> {
216 None
217 }
218
219 fn trigger_price(&self) -> Option<Price> {
220 None
221 }
222
223 fn trigger_type(&self) -> Option<TriggerType> {
224 None
225 }
226
227 fn limit_offset(&self) -> Option<Decimal> {
228 None
229 }
230
231 fn trailing_offset(&self) -> Option<Decimal> {
232 None
233 }
234
235 fn trailing_offset_type(&self) -> Option<TrailingOffsetType> {
236 None
237 }
238
239 fn expire_time(&self) -> Option<UnixNanos> {
240 None
241 }
242
243 fn display_qty(&self) -> Option<Quantity> {
244 None
245 }
246
247 fn emulation_trigger(&self) -> Option<TriggerType> {
248 None
249 }
250
251 fn trigger_instrument_id(&self) -> Option<InstrumentId> {
252 None
253 }
254
255 fn contingency_type(&self) -> Option<ContingencyType> {
256 None
257 }
258
259 fn order_list_id(&self) -> Option<OrderListId> {
260 None
261 }
262
263 fn linked_order_ids(&self) -> Option<Vec<ClientOrderId>> {
264 None
265 }
266
267 fn parent_order_id(&self) -> Option<ClientOrderId> {
268 None
269 }
270
271 fn exec_algorithm_id(&self) -> Option<ExecAlgorithmId> {
272 None
273 }
274
275 fn exec_spawn_id(&self) -> Option<ClientOrderId> {
276 None
277 }
278
279 fn venue_order_id(&self) -> Option<VenueOrderId> {
280 None
281 }
282
283 fn account_id(&self) -> Option<AccountId> {
284 Some(self.account_id)
285 }
286
287 fn position_id(&self) -> Option<PositionId> {
288 None
289 }
290
291 fn commission(&self) -> Option<Money> {
292 None
293 }
294
295 fn ts_event(&self) -> UnixNanos {
296 self.ts_event
297 }
298
299 fn ts_init(&self) -> UnixNanos {
300 self.ts_init
301 }
302}
303
304#[cfg(test)]
308mod tests {
309 use rstest::rstest;
310
311 use super::*;
312 use crate::events::order::stubs::*;
313
314 #[rstest]
315 fn test_order_rejected_display(order_rejected_insufficient_margin: OrderRejected) {
316 let display = format!("{order_rejected_insufficient_margin}");
317 assert_eq!(
318 display,
319 "OrderRejected(instrument_id=BTCUSDT.COINBASE, client_order_id=O-19700101-000000-001-001-1, \
320 account_id=SIM-001, reason='INSUFFICIENT_MARGIN', ts_event=0)"
321 );
322 }
323}