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