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