Skip to main content

nautilus_model/events/order/
accepted.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 accepted by the trading venue.
37///
38/// This event often corresponds to a `NEW` `OrdStatus` <39> field in FIX execution reports.
39#[repr(C)]
40#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
41#[serde(tag = "type")]
42#[cfg_attr(
43    feature = "python",
44    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
45)]
46#[cfg_attr(
47    feature = "python",
48    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
49)]
50pub struct OrderAccepted {
51    /// The trader ID associated with the event.
52    pub trader_id: TraderId,
53    /// The strategy ID associated with the event.
54    pub strategy_id: StrategyId,
55    /// The instrument ID associated with the event.
56    pub instrument_id: InstrumentId,
57    /// The client order ID associated with the event.
58    pub client_order_id: ClientOrderId,
59    /// The venue order ID associated with the event.
60    pub venue_order_id: VenueOrderId,
61    /// The account ID associated with the event.
62    pub account_id: AccountId,
63    /// The unique identifier for the event.
64    pub event_id: UUID4,
65    /// UNIX timestamp (nanoseconds) when the event occurred.
66    pub ts_event: UnixNanos,
67    /// UNIX timestamp (nanoseconds) when the event was initialized.
68    pub ts_init: UnixNanos,
69    /// If the event was generated during reconciliation.
70    pub reconciliation: bool,
71    /// The causation ID associated with the event.
72    #[serde(default, skip_serializing_if = "Option::is_none")]
73    pub causation_id: Option<UUID4>,
74}
75
76impl OrderAccepted {
77    /// Creates a new [`OrderAccepted`] instance.
78    #[expect(clippy::too_many_arguments)]
79    #[must_use]
80    pub fn new(
81        trader_id: TraderId,
82        strategy_id: StrategyId,
83        instrument_id: InstrumentId,
84        client_order_id: ClientOrderId,
85        venue_order_id: VenueOrderId,
86        account_id: AccountId,
87        event_id: UUID4,
88        ts_event: UnixNanos,
89        ts_init: UnixNanos,
90        reconciliation: bool,
91    ) -> Self {
92        Self {
93            trader_id,
94            strategy_id,
95            instrument_id,
96            client_order_id,
97            venue_order_id,
98            account_id,
99            event_id,
100            ts_event,
101            ts_init,
102            reconciliation,
103            causation_id: None,
104        }
105    }
106}
107
108impl Debug for OrderAccepted {
109    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110        write!(
111            f,
112            "{}(trader_id={}, strategy_id={}, instrument_id={}, client_order_id={}, venue_order_id={}, account_id={}, event_id={}, ts_event={}, ts_init={})",
113            stringify!(OrderAccepted),
114            self.trader_id,
115            self.strategy_id,
116            self.instrument_id,
117            self.client_order_id,
118            self.venue_order_id,
119            self.account_id,
120            self.event_id,
121            self.ts_event,
122            self.ts_init
123        )
124    }
125}
126
127impl Display for OrderAccepted {
128    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
129        write!(
130            f,
131            "{}(instrument_id={}, client_order_id={}, venue_order_id={}, account_id={}, ts_event={})",
132            stringify!(OrderAccepted),
133            self.instrument_id,
134            self.client_order_id,
135            self.venue_order_id,
136            self.account_id,
137            self.ts_event
138        )
139    }
140}
141
142impl OrderEvent for OrderAccepted {
143    fn id(&self) -> UUID4 {
144        self.event_id
145    }
146
147    fn type_name(&self) -> &'static str {
148        stringify!(OrderAccepted)
149    }
150
151    fn order_type(&self) -> Option<OrderType> {
152        None
153    }
154
155    fn order_side(&self) -> Option<OrderSide> {
156        None
157    }
158
159    fn trader_id(&self) -> TraderId {
160        self.trader_id
161    }
162
163    fn strategy_id(&self) -> StrategyId {
164        self.strategy_id
165    }
166
167    fn instrument_id(&self) -> InstrumentId {
168        self.instrument_id
169    }
170
171    fn trade_id(&self) -> Option<TradeId> {
172        None
173    }
174
175    fn currency(&self) -> Option<Currency> {
176        None
177    }
178
179    fn client_order_id(&self) -> ClientOrderId {
180        self.client_order_id
181    }
182
183    fn reason(&self) -> Option<Ustr> {
184        None
185    }
186
187    fn quantity(&self) -> Option<Quantity> {
188        None
189    }
190
191    fn time_in_force(&self) -> Option<TimeInForce> {
192        None
193    }
194
195    fn liquidity_side(&self) -> Option<LiquiditySide> {
196        None
197    }
198
199    fn post_only(&self) -> Option<bool> {
200        None
201    }
202
203    fn reduce_only(&self) -> Option<bool> {
204        None
205    }
206
207    fn quote_quantity(&self) -> Option<bool> {
208        None
209    }
210
211    fn reconciliation(&self) -> bool {
212        false
213    }
214
215    fn price(&self) -> Option<Price> {
216        None
217    }
218
219    fn last_px(&self) -> Option<Price> {
220        None
221    }
222
223    fn last_qty(&self) -> Option<Quantity> {
224        None
225    }
226
227    fn trigger_price(&self) -> Option<Price> {
228        None
229    }
230
231    fn trigger_type(&self) -> Option<TriggerType> {
232        None
233    }
234
235    fn limit_offset(&self) -> Option<Decimal> {
236        None
237    }
238
239    fn trailing_offset(&self) -> Option<Decimal> {
240        None
241    }
242
243    fn trailing_offset_type(&self) -> Option<TrailingOffsetType> {
244        None
245    }
246
247    fn expire_time(&self) -> Option<UnixNanos> {
248        None
249    }
250
251    fn display_qty(&self) -> Option<Quantity> {
252        None
253    }
254
255    fn emulation_trigger(&self) -> Option<TriggerType> {
256        None
257    }
258
259    fn trigger_instrument_id(&self) -> Option<InstrumentId> {
260        None
261    }
262
263    fn contingency_type(&self) -> Option<ContingencyType> {
264        None
265    }
266
267    fn order_list_id(&self) -> Option<OrderListId> {
268        None
269    }
270
271    fn linked_order_ids(&self) -> Option<Vec<ClientOrderId>> {
272        None
273    }
274
275    fn parent_order_id(&self) -> Option<ClientOrderId> {
276        None
277    }
278
279    fn exec_algorithm_id(&self) -> Option<ExecAlgorithmId> {
280        None
281    }
282
283    fn exec_spawn_id(&self) -> Option<ClientOrderId> {
284        None
285    }
286
287    fn venue_order_id(&self) -> Option<VenueOrderId> {
288        Some(self.venue_order_id)
289    }
290
291    fn account_id(&self) -> Option<AccountId> {
292        Some(self.account_id)
293    }
294
295    fn position_id(&self) -> Option<PositionId> {
296        None
297    }
298
299    fn commission(&self) -> Option<Money> {
300        None
301    }
302
303    fn ts_event(&self) -> UnixNanos {
304        self.ts_event
305    }
306
307    fn ts_init(&self) -> UnixNanos {
308        self.ts_init
309    }
310}
311
312#[cfg(test)]
313mod tests {
314    use nautilus_core::UnixNanos;
315    use rstest::rstest;
316
317    use super::*;
318    use crate::events::order::stubs::*;
319
320    fn create_test_order_accepted() -> OrderAccepted {
321        OrderAccepted::new(
322            TraderId::from("TRADER-001"),
323            StrategyId::from("EMA-CROSS"),
324            InstrumentId::from("EURUSD.SIM"),
325            ClientOrderId::from("O-19700101-000000-001-001-1"),
326            VenueOrderId::from("V-001"),
327            AccountId::from("SIM-001"),
328            UUID4::default(),
329            UnixNanos::from(1_000_000_000),
330            UnixNanos::from(2_000_000_000),
331            false,
332        )
333    }
334
335    #[rstest]
336    fn test_order_accepted_display(order_accepted: OrderAccepted) {
337        let display = format!("{order_accepted}");
338        assert_eq!(
339            display,
340            "OrderAccepted(instrument_id=BTCUSDT.COINBASE, client_order_id=O-19700101-000000-001-001-1, venue_order_id=001, account_id=SIM-001, ts_event=0)"
341        );
342    }
343
344    #[rstest]
345    fn test_order_accepted_serialization() {
346        let original = create_test_order_accepted();
347
348        let json = serde_json::to_string(&original).unwrap();
349        let deserialized: OrderAccepted = serde_json::from_str(&json).unwrap();
350
351        assert_eq!(original, deserialized);
352    }
353}