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::{UUID4, UnixNanos, serialization::from_bool_as_u8};
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!(
104            f,
105            "{}(trader_id={}, strategy_id={}, instrument_id={}, client_order_id={}, venue_order_id={}, account_id={}, event_id={}, ts_event={}, ts_init={})",
106            stringify!(OrderPendingUpdate),
107            self.trader_id,
108            self.strategy_id,
109            self.instrument_id,
110            self.client_order_id,
111            self.venue_order_id.map_or_else(
112                || "None".to_string(),
113                |venue_order_id| format!("{venue_order_id}")
114            ),
115            self.account_id,
116            self.event_id,
117            self.ts_event,
118            self.ts_init
119        )
120    }
121}
122
123impl Display for OrderPendingUpdate {
124    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125        write!(
126            f,
127            "{}(instrument_id={}, client_order_id={}, venue_order_id={}, account_id={}, ts_event={})",
128            stringify!(OrderPendingUpdate),
129            self.instrument_id,
130            self.client_order_id,
131            self.venue_order_id
132                .map_or("None".to_string(), |venue_order_id| format!(
133                    "{venue_order_id}"
134                )),
135            self.account_id,
136            self.ts_event
137        )
138    }
139}
140
141impl OrderEvent for OrderPendingUpdate {
142    fn id(&self) -> UUID4 {
143        self.event_id
144    }
145
146    fn kind(&self) -> &str {
147        stringify!(OrderPendingUpdate)
148    }
149
150    fn order_type(&self) -> Option<OrderType> {
151        None
152    }
153
154    fn order_side(&self) -> Option<OrderSide> {
155        None
156    }
157
158    fn trader_id(&self) -> TraderId {
159        self.trader_id
160    }
161
162    fn strategy_id(&self) -> StrategyId {
163        self.strategy_id
164    }
165
166    fn instrument_id(&self) -> InstrumentId {
167        self.instrument_id
168    }
169
170    fn trade_id(&self) -> Option<TradeId> {
171        None
172    }
173
174    fn currency(&self) -> Option<Currency> {
175        None
176    }
177
178    fn client_order_id(&self) -> ClientOrderId {
179        self.client_order_id
180    }
181
182    fn reason(&self) -> Option<Ustr> {
183        None
184    }
185
186    fn quantity(&self) -> Option<Quantity> {
187        None
188    }
189
190    fn time_in_force(&self) -> Option<TimeInForce> {
191        None
192    }
193
194    fn liquidity_side(&self) -> Option<LiquiditySide> {
195        None
196    }
197
198    fn post_only(&self) -> Option<bool> {
199        None
200    }
201
202    fn reduce_only(&self) -> Option<bool> {
203        None
204    }
205
206    fn quote_quantity(&self) -> Option<bool> {
207        None
208    }
209
210    fn reconciliation(&self) -> bool {
211        false
212    }
213
214    fn price(&self) -> Option<Price> {
215        None
216    }
217
218    fn last_px(&self) -> Option<Price> {
219        None
220    }
221
222    fn last_qty(&self) -> Option<Quantity> {
223        None
224    }
225
226    fn trigger_price(&self) -> Option<Price> {
227        None
228    }
229
230    fn trigger_type(&self) -> Option<TriggerType> {
231        None
232    }
233
234    fn limit_offset(&self) -> Option<Decimal> {
235        None
236    }
237
238    fn trailing_offset(&self) -> Option<Decimal> {
239        None
240    }
241
242    fn trailing_offset_type(&self) -> Option<TrailingOffsetType> {
243        None
244    }
245
246    fn expire_time(&self) -> Option<UnixNanos> {
247        None
248    }
249
250    fn display_qty(&self) -> Option<Quantity> {
251        None
252    }
253
254    fn emulation_trigger(&self) -> Option<TriggerType> {
255        None
256    }
257
258    fn trigger_instrument_id(&self) -> Option<InstrumentId> {
259        None
260    }
261
262    fn contingency_type(&self) -> Option<ContingencyType> {
263        None
264    }
265
266    fn order_list_id(&self) -> Option<OrderListId> {
267        None
268    }
269
270    fn linked_order_ids(&self) -> Option<Vec<ClientOrderId>> {
271        None
272    }
273
274    fn parent_order_id(&self) -> Option<ClientOrderId> {
275        None
276    }
277
278    fn exec_algorithm_id(&self) -> Option<ExecAlgorithmId> {
279        None
280    }
281
282    fn exec_spawn_id(&self) -> Option<ClientOrderId> {
283        None
284    }
285
286    fn venue_order_id(&self) -> Option<VenueOrderId> {
287        self.venue_order_id
288    }
289
290    fn account_id(&self) -> Option<AccountId> {
291        Some(self.account_id)
292    }
293
294    fn position_id(&self) -> Option<PositionId> {
295        None
296    }
297
298    fn commission(&self) -> Option<Money> {
299        None
300    }
301
302    fn ts_event(&self) -> UnixNanos {
303        self.ts_event
304    }
305
306    fn ts_init(&self) -> UnixNanos {
307        self.ts_init
308    }
309}
310
311////////////////////////////////////////////////////////////////////////////////
312// Tests
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}