Skip to main content

nautilus_model/events/order/
snapshot.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 indexmap::IndexMap;
17use nautilus_core::{UUID4, UnixNanos};
18use rust_decimal::Decimal;
19use serde::{Deserialize, Serialize};
20use ustr::Ustr;
21
22use crate::{
23    enums::{
24        ContingencyType, LiquiditySide, OrderSide, OrderStatus, OrderType, TimeInForce,
25        TrailingOffsetType, TriggerType,
26    },
27    identifiers::{
28        AccountId, ClientOrderId, ExecAlgorithmId, InstrumentId, OrderListId, PositionId,
29        StrategyId, TradeId, TraderId, VenueOrderId,
30    },
31    orders::{Order, OrderAny},
32    types::{Money, Price, Quantity},
33};
34
35/// Represents an order state snapshot as a certain instant.
36#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
37#[cfg_attr(
38    feature = "python",
39    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
40)]
41#[cfg_attr(
42    feature = "python",
43    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
44)]
45pub struct OrderSnapshot {
46    /// The trader ID associated with the order.
47    pub trader_id: TraderId,
48    /// The strategy ID associated with the order.
49    pub strategy_id: StrategyId,
50    /// The order instrument ID.
51    pub instrument_id: InstrumentId,
52    /// The client order ID.
53    pub client_order_id: ClientOrderId,
54    /// The venue assigned order ID.
55    pub venue_order_id: Option<VenueOrderId>,
56    /// The position ID associated with the order.
57    pub position_id: Option<PositionId>,
58    /// The account ID associated with the order.
59    pub account_id: Option<AccountId>,
60    /// The orders last trade match ID.
61    pub last_trade_id: Option<TradeId>,
62    /// The order type.
63    pub order_type: OrderType,
64    /// The order side.
65    pub order_side: OrderSide,
66    /// The order quantity.
67    pub quantity: Quantity,
68    /// The order price (LIMIT).
69    pub price: Option<Price>,
70    /// The order trigger price (STOP).
71    pub trigger_price: Option<Price>,
72    /// The trigger type for the order.
73    pub trigger_type: Option<TriggerType>,
74    /// The trailing offset for the orders limit price.
75    pub limit_offset: Option<Decimal>,
76    /// The trailing offset for the orders trigger price (STOP).
77    pub trailing_offset: Option<Decimal>,
78    /// The trailing offset type.
79    pub trailing_offset_type: Option<TrailingOffsetType>,
80    /// The order time in force.
81    pub time_in_force: TimeInForce,
82    /// The order expiration (UNIX epoch nanoseconds), zero for no expiration.
83    pub expire_time: Option<UnixNanos>,
84    /// The order total filled quantity.
85    pub filled_qty: Quantity,
86    /// The order liquidity side.
87    pub liquidity_side: Option<LiquiditySide>,
88    /// The order average fill price.
89    pub avg_px: Option<f64>,
90    /// The order total price slippage.
91    pub slippage: Option<f64>,
92    /// The commissions for the order.
93    pub commissions: Vec<Money>,
94    /// The order status.
95    pub status: OrderStatus,
96    /// If the order will only provide liquidity (make a market).
97    pub is_post_only: bool,
98    /// If the order carries the 'reduce-only' execution instruction.
99    pub is_reduce_only: bool,
100    /// If the order quantity is denominated in the quote currency.
101    pub is_quote_quantity: bool,
102    /// The quantity of the `LIMIT` order to display on the public book (iceberg).
103    pub display_qty: Option<Quantity>,
104    /// The order emulation trigger type.
105    pub emulation_trigger: Option<TriggerType>,
106    /// The order emulation trigger instrument ID (will be `instrument_id` if `None`).
107    pub trigger_instrument_id: Option<InstrumentId>,
108    /// The orders contingency type.
109    pub contingency_type: Option<ContingencyType>,
110    /// The order list ID associated with the order.
111    pub order_list_id: Option<OrderListId>,
112    /// The orders linked client order ID(s).
113    pub linked_order_ids: Option<Vec<ClientOrderId>>,
114    /// The parent client order ID.
115    pub parent_order_id: Option<ClientOrderId>,
116    /// The execution algorithm ID for the order.
117    pub exec_algorithm_id: Option<ExecAlgorithmId>,
118    /// The execution algorithm parameters for the order.
119    pub exec_algorithm_params: Option<IndexMap<Ustr, Ustr>>,
120    /// The execution algorithm spawning client order ID.
121    pub exec_spawn_id: Option<ClientOrderId>,
122    /// The order custom user tags.
123    pub tags: Option<Vec<Ustr>>,
124    /// The event ID of the `OrderInitialized` event.
125    pub init_id: UUID4,
126    /// UNIX timestamp (nanoseconds) when the object was initialized.
127    pub ts_init: UnixNanos,
128    /// UNIX timestamp (nanoseconds) when the last event occurred.
129    pub ts_last: UnixNanos,
130    /// The causation ID associated with the snapshot.
131    #[serde(default, skip_serializing_if = "Option::is_none")]
132    pub causation_id: Option<UUID4>,
133}
134
135impl From<OrderAny> for OrderSnapshot {
136    fn from(order: OrderAny) -> Self {
137        Self {
138            trader_id: order.trader_id(),
139            strategy_id: order.strategy_id(),
140            instrument_id: order.instrument_id(),
141            client_order_id: order.client_order_id(),
142            venue_order_id: order.venue_order_id(),
143            position_id: order.position_id(),
144            account_id: order.account_id(),
145            last_trade_id: order.last_trade_id(),
146            order_type: order.order_type(),
147            order_side: order.order_side(),
148            quantity: order.quantity(),
149            price: order.price(),
150            trigger_price: order.trigger_price(),
151            trigger_type: order.trigger_type(),
152            limit_offset: order.limit_offset(),
153            trailing_offset: order.trailing_offset(),
154            trailing_offset_type: order.trailing_offset_type(),
155            time_in_force: order.time_in_force(),
156            expire_time: order.expire_time(),
157            filled_qty: order.filled_qty(),
158            liquidity_side: order.liquidity_side(),
159            avg_px: order.avg_px(),
160            slippage: order.slippage(),
161            commissions: order.commissions().values().copied().collect(),
162            status: order.status(),
163            is_post_only: order.is_post_only(),
164            is_reduce_only: order.is_reduce_only(),
165            is_quote_quantity: order.is_quote_quantity(),
166            display_qty: order.display_qty(),
167            emulation_trigger: order.emulation_trigger(),
168            trigger_instrument_id: order.trigger_instrument_id(),
169            contingency_type: order.contingency_type(),
170            order_list_id: order.order_list_id(),
171            linked_order_ids: order.linked_order_ids().map(Vec::from),
172            parent_order_id: order.parent_order_id(),
173            exec_algorithm_id: order.exec_algorithm_id(),
174            exec_algorithm_params: order.exec_algorithm_params().cloned(),
175            exec_spawn_id: order.exec_spawn_id(),
176            tags: order.tags().map(Vec::from),
177            init_id: order.init_id(),
178            ts_init: order.ts_init(),
179            ts_last: order.ts_last(),
180            causation_id: None,
181        }
182    }
183}
184
185#[cfg(test)]
186mod tests {
187    use rstest::rstest;
188
189    use super::*;
190    use crate::orders::OrderTestBuilder;
191
192    #[rstest]
193    fn test_snapshot_from_market_order() {
194        let order = OrderTestBuilder::new(OrderType::Market)
195            .instrument_id(InstrumentId::from("EURUSD.SIM"))
196            .side(OrderSide::Buy)
197            .quantity(Quantity::from(100))
198            .build();
199
200        let snapshot = OrderSnapshot::from(order.clone());
201
202        assert_eq!(snapshot.trader_id, order.trader_id());
203        assert_eq!(snapshot.strategy_id, order.strategy_id());
204        assert_eq!(snapshot.instrument_id, order.instrument_id());
205        assert_eq!(snapshot.client_order_id, order.client_order_id());
206        assert_eq!(snapshot.venue_order_id, order.venue_order_id());
207        assert_eq!(snapshot.order_side, order.order_side());
208        assert_eq!(snapshot.order_type, order.order_type());
209        assert_eq!(snapshot.quantity, order.quantity());
210        assert_eq!(snapshot.status, order.status());
211        assert_eq!(snapshot.ts_init, order.ts_init());
212        assert_eq!(snapshot.ts_last, order.ts_last());
213        assert_eq!(snapshot.filled_qty, order.filled_qty());
214        assert!(!snapshot.is_post_only);
215        assert!(!snapshot.is_quote_quantity);
216    }
217
218    #[rstest]
219    fn test_snapshot_from_limit_order() {
220        let order = OrderTestBuilder::new(OrderType::Limit)
221            .instrument_id(InstrumentId::from("BTCUSDT.BINANCE"))
222            .side(OrderSide::Sell)
223            .quantity(Quantity::from("0.5"))
224            .price(Price::from("50000"))
225            .build();
226
227        let snapshot = OrderSnapshot::from(order);
228
229        assert_eq!(snapshot.order_type, OrderType::Limit);
230        assert_eq!(snapshot.order_side, OrderSide::Sell);
231        assert_eq!(snapshot.price, Some(Price::from("50000")));
232        assert_eq!(
233            snapshot.instrument_id,
234            InstrumentId::from("BTCUSDT.BINANCE")
235        );
236        assert_eq!(snapshot.quantity, Quantity::from("0.5"));
237    }
238}