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