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