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