Skip to main content

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