Skip to main content

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