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