Skip to main content

nautilus_model/events/order/
modify_rejected.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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 nautilus_core::{UUID4, UnixNanos, serialization::from_bool_as_u8};
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/// Represents an event where a `ModifyOrder` command has been rejected by the
37/// trading venue.
38#[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 OrderModifyRejected {
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 modify was rejected.
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    /// If the event was generated during reconciliation.
67    #[serde(deserialize_with = "from_bool_as_u8")]
68    pub reconciliation: u8, // TODO: Change to bool once Cython removed
69    /// The venue order ID associated with the event.
70    pub venue_order_id: Option<VenueOrderId>,
71    /// The account ID associated with the event.
72    pub account_id: Option<AccountId>,
73}
74
75impl OrderModifyRejected {
76    /// Creates a new [`OrderModifyRejected`] instance.
77    #[expect(clippy::too_many_arguments)]
78    #[must_use]
79    pub fn new(
80        trader_id: TraderId,
81        strategy_id: StrategyId,
82        instrument_id: InstrumentId,
83        client_order_id: ClientOrderId,
84        reason: Ustr,
85        event_id: UUID4,
86        ts_event: UnixNanos,
87        ts_init: UnixNanos,
88        reconciliation: bool,
89        venue_order_id: Option<VenueOrderId>,
90        account_id: Option<AccountId>,
91    ) -> Self {
92        Self {
93            trader_id,
94            strategy_id,
95            instrument_id,
96            client_order_id,
97            reason,
98            event_id,
99            ts_event,
100            ts_init,
101            reconciliation: u8::from(reconciliation),
102            venue_order_id,
103            account_id,
104        }
105    }
106}
107
108impl Debug for OrderModifyRejected {
109    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110        write!(
111            f,
112            "{}(trader_id={}, strategy_id={}, instrument_id={}, client_order_id={}, venue_order_id={}, account_id={}, reason='{}', event_id={}, ts_event={}, ts_init={})",
113            stringify!(OrderModifyRejected),
114            self.trader_id,
115            self.strategy_id,
116            self.instrument_id,
117            self.client_order_id,
118            self.venue_order_id
119                .map_or("None".to_string(), |venue_order_id| format!(
120                    "{venue_order_id}"
121                )),
122            self.account_id
123                .map_or("None".to_string(), |account_id| format!("{account_id}")),
124            self.reason,
125            self.event_id,
126            self.ts_event,
127            self.ts_init
128        )
129    }
130}
131
132impl Display for OrderModifyRejected {
133    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
134        write!(
135            f,
136            "{}(instrument_id={}, client_order_id={}, venue_order_id={}, account_id={}, reason='{}', ts_event={})",
137            stringify!(OrderModifyRejected),
138            self.instrument_id,
139            self.client_order_id,
140            self.venue_order_id
141                .map_or("None".to_string(), |venue_order_id| format!(
142                    "{venue_order_id}"
143                )),
144            self.account_id
145                .map_or("None".to_string(), |account_id| format!("{account_id}")),
146            self.reason,
147            self.ts_event
148        )
149    }
150}
151
152impl OrderEvent for OrderModifyRejected {
153    fn id(&self) -> UUID4 {
154        self.event_id
155    }
156
157    fn type_name(&self) -> &'static str {
158        stringify!(OrderModifyRejected)
159    }
160
161    fn order_type(&self) -> Option<OrderType> {
162        None
163    }
164
165    fn order_side(&self) -> Option<OrderSide> {
166        None
167    }
168
169    fn trader_id(&self) -> TraderId {
170        self.trader_id
171    }
172
173    fn strategy_id(&self) -> StrategyId {
174        self.strategy_id
175    }
176
177    fn instrument_id(&self) -> InstrumentId {
178        self.instrument_id
179    }
180
181    fn trade_id(&self) -> Option<TradeId> {
182        None
183    }
184
185    fn currency(&self) -> Option<Currency> {
186        None
187    }
188
189    fn client_order_id(&self) -> ClientOrderId {
190        self.client_order_id
191    }
192
193    fn reason(&self) -> Option<Ustr> {
194        Some(self.reason)
195    }
196
197    fn quantity(&self) -> Option<Quantity> {
198        None
199    }
200
201    fn time_in_force(&self) -> Option<TimeInForce> {
202        None
203    }
204
205    fn liquidity_side(&self) -> Option<LiquiditySide> {
206        None
207    }
208
209    fn post_only(&self) -> Option<bool> {
210        None
211    }
212
213    fn reduce_only(&self) -> Option<bool> {
214        None
215    }
216
217    fn quote_quantity(&self) -> Option<bool> {
218        None
219    }
220
221    fn reconciliation(&self) -> bool {
222        false
223    }
224
225    fn price(&self) -> Option<Price> {
226        None
227    }
228
229    fn last_px(&self) -> Option<Price> {
230        None
231    }
232
233    fn last_qty(&self) -> Option<Quantity> {
234        None
235    }
236
237    fn trigger_price(&self) -> Option<Price> {
238        None
239    }
240
241    fn trigger_type(&self) -> Option<TriggerType> {
242        None
243    }
244
245    fn limit_offset(&self) -> Option<Decimal> {
246        None
247    }
248
249    fn trailing_offset(&self) -> Option<Decimal> {
250        None
251    }
252
253    fn trailing_offset_type(&self) -> Option<TrailingOffsetType> {
254        None
255    }
256
257    fn expire_time(&self) -> Option<UnixNanos> {
258        None
259    }
260
261    fn display_qty(&self) -> Option<Quantity> {
262        None
263    }
264
265    fn emulation_trigger(&self) -> Option<TriggerType> {
266        None
267    }
268
269    fn trigger_instrument_id(&self) -> Option<InstrumentId> {
270        None
271    }
272
273    fn contingency_type(&self) -> Option<ContingencyType> {
274        None
275    }
276
277    fn order_list_id(&self) -> Option<OrderListId> {
278        None
279    }
280
281    fn linked_order_ids(&self) -> Option<Vec<ClientOrderId>> {
282        None
283    }
284
285    fn parent_order_id(&self) -> Option<ClientOrderId> {
286        None
287    }
288
289    fn exec_algorithm_id(&self) -> Option<ExecAlgorithmId> {
290        None
291    }
292
293    fn exec_spawn_id(&self) -> Option<ClientOrderId> {
294        None
295    }
296
297    fn venue_order_id(&self) -> Option<VenueOrderId> {
298        self.venue_order_id
299    }
300
301    fn account_id(&self) -> Option<AccountId> {
302        self.account_id
303    }
304
305    fn position_id(&self) -> Option<PositionId> {
306        None
307    }
308
309    fn commission(&self) -> Option<Money> {
310        None
311    }
312
313    fn ts_event(&self) -> UnixNanos {
314        self.ts_event
315    }
316
317    fn ts_init(&self) -> UnixNanos {
318        self.ts_init
319    }
320}
321
322#[cfg(test)]
323mod tests {
324    use rstest::rstest;
325
326    use crate::events::order::{modify_rejected::OrderModifyRejected, stubs::*};
327
328    #[rstest]
329    fn test_order_modified_rejected(order_modify_rejected: OrderModifyRejected) {
330        let display = format!("{order_modify_rejected}");
331        assert_eq!(
332            display,
333            "OrderModifyRejected(instrument_id=BTCUSDT.COINBASE, client_order_id=O-19700101-000000-001-001-1, \
334            venue_order_id=001, account_id=SIM-001, reason='ORDER_DOES_NOT_EXIST', ts_event=0)"
335        );
336    }
337
338    #[rstest]
339    fn test_order_modify_rejected_serialization() {
340        let original = OrderModifyRejected::default();
341        let json = serde_json::to_string(&original).unwrap();
342        let deserialized: OrderModifyRejected = serde_json::from_str(&json).unwrap();
343        assert_eq!(original, deserialized);
344    }
345}