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