Skip to main content

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