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};
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    pub reconciliation: bool,
68    /// The venue order ID associated with the event.
69    pub venue_order_id: Option<VenueOrderId>,
70    /// The account ID associated with the event.
71    pub account_id: Option<AccountId>,
72    /// The causation ID associated with the event.
73    #[serde(default, skip_serializing_if = "Option::is_none")]
74    pub causation_id: Option<UUID4>,
75}
76
77impl OrderModifyRejected {
78    /// Creates a new [`OrderModifyRejected`] instance.
79    #[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 OrderModifyRejected {
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!(OrderModifyRejected),
117            self.trader_id,
118            self.strategy_id,
119            self.instrument_id,
120            self.client_order_id,
121            self.venue_order_id
122                .map_or("None".to_string(), |venue_order_id| format!(
123                    "{venue_order_id}"
124                )),
125            self.account_id
126                .map_or("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 OrderModifyRejected {
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!(OrderModifyRejected),
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 OrderModifyRejected {
156    fn id(&self) -> UUID4 {
157        self.event_id
158    }
159
160    fn type_name(&self) -> &'static str {
161        stringify!(OrderModifyRejected)
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    use rstest::rstest;
328
329    use crate::events::order::{modify_rejected::OrderModifyRejected, stubs::*};
330
331    #[rstest]
332    fn test_order_modified_rejected(order_modify_rejected: OrderModifyRejected) {
333        let display = format!("{order_modify_rejected}");
334        assert_eq!(
335            display,
336            "OrderModifyRejected(instrument_id=BTCUSDT.COINBASE, client_order_id=O-19700101-000000-001-001-1, \
337            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_modify_rejected_serialization() {
343        let original = OrderModifyRejected::default();
344        let json = serde_json::to_string(&original).unwrap();
345        let deserialized: OrderModifyRejected = serde_json::from_str(&json).unwrap();
346        assert_eq!(original, deserialized);
347    }
348}