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