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