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