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