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}
131
132impl From<OrderAny> for OrderSnapshot {
133    fn from(order: OrderAny) -> Self {
134        Self {
135            trader_id: order.trader_id(),
136            strategy_id: order.strategy_id(),
137            instrument_id: order.instrument_id(),
138            client_order_id: order.client_order_id(),
139            venue_order_id: order.venue_order_id(),
140            position_id: order.position_id(),
141            account_id: order.account_id(),
142            last_trade_id: order.last_trade_id(),
143            order_type: order.order_type(),
144            order_side: order.order_side(),
145            quantity: order.quantity(),
146            price: order.price(),
147            trigger_price: order.trigger_price(),
148            trigger_type: order.trigger_type(),
149            limit_offset: order.limit_offset(),
150            trailing_offset: order.trailing_offset(),
151            trailing_offset_type: order.trailing_offset_type(),
152            time_in_force: order.time_in_force(),
153            expire_time: order.expire_time(),
154            filled_qty: order.filled_qty(),
155            liquidity_side: order.liquidity_side(),
156            avg_px: order.avg_px(),
157            slippage: order.slippage(),
158            commissions: order.commissions().values().copied().collect(),
159            status: order.status(),
160            is_post_only: order.is_post_only(),
161            is_reduce_only: order.is_reduce_only(),
162            is_quote_quantity: order.is_quote_quantity(),
163            display_qty: order.display_qty(),
164            emulation_trigger: order.emulation_trigger(),
165            trigger_instrument_id: order.trigger_instrument_id(),
166            contingency_type: order.contingency_type(),
167            order_list_id: order.order_list_id(),
168            linked_order_ids: order.linked_order_ids().map(Vec::from),
169            parent_order_id: order.parent_order_id(),
170            exec_algorithm_id: order.exec_algorithm_id(),
171            exec_algorithm_params: order.exec_algorithm_params().cloned(),
172            exec_spawn_id: order.exec_spawn_id(),
173            tags: order.tags().map(Vec::from),
174            init_id: order.init_id(),
175            ts_init: order.ts_init(),
176            ts_last: order.ts_last(),
177        }
178    }
179}
180
181#[cfg(test)]
182mod tests {
183    use rstest::rstest;
184
185    use super::*;
186    use crate::orders::OrderTestBuilder;
187
188    #[rstest]
189    fn test_snapshot_from_market_order() {
190        let order = OrderTestBuilder::new(OrderType::Market)
191            .instrument_id(InstrumentId::from("EURUSD.SIM"))
192            .side(OrderSide::Buy)
193            .quantity(Quantity::from(100))
194            .build();
195
196        let snapshot = OrderSnapshot::from(order.clone());
197
198        assert_eq!(snapshot.trader_id, order.trader_id());
199        assert_eq!(snapshot.strategy_id, order.strategy_id());
200        assert_eq!(snapshot.instrument_id, order.instrument_id());
201        assert_eq!(snapshot.client_order_id, order.client_order_id());
202        assert_eq!(snapshot.venue_order_id, order.venue_order_id());
203        assert_eq!(snapshot.order_side, order.order_side());
204        assert_eq!(snapshot.order_type, order.order_type());
205        assert_eq!(snapshot.quantity, order.quantity());
206        assert_eq!(snapshot.status, order.status());
207        assert_eq!(snapshot.ts_init, order.ts_init());
208        assert_eq!(snapshot.ts_last, order.ts_last());
209        assert_eq!(snapshot.filled_qty, order.filled_qty());
210        assert!(!snapshot.is_post_only);
211        assert!(!snapshot.is_quote_quantity);
212    }
213
214    #[rstest]
215    fn test_snapshot_from_limit_order() {
216        let order = OrderTestBuilder::new(OrderType::Limit)
217            .instrument_id(InstrumentId::from("BTCUSDT.BINANCE"))
218            .side(OrderSide::Sell)
219            .quantity(Quantity::from("0.5"))
220            .price(Price::from("50000"))
221            .build();
222
223        let snapshot = OrderSnapshot::from(order);
224
225        assert_eq!(snapshot.order_type, OrderType::Limit);
226        assert_eq!(snapshot.order_side, OrderSide::Sell);
227        assert_eq!(snapshot.price, Some(Price::from("50000")));
228        assert_eq!(
229            snapshot.instrument_id,
230            InstrumentId::from("BTCUSDT.BINANCE")
231        );
232        assert_eq!(snapshot.quantity, Quantity::from("0.5"));
233    }
234}