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