Skip to main content

nautilus_model/events/position/
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 nautilus_core::UnixNanos;
17use serde::{Deserialize, Serialize};
18
19use crate::{
20    enums::{OrderSide, PositionSide},
21    identifiers::{AccountId, ClientOrderId, InstrumentId, PositionId, StrategyId, TraderId},
22    position::Position,
23    types::{Currency, Money, Quantity},
24};
25
26/// Represents a position state snapshot as a certain instant.
27#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
28#[cfg_attr(
29    feature = "python",
30    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
31)]
32#[cfg_attr(
33    feature = "python",
34    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
35)]
36pub struct PositionSnapshot {
37    /// The trader ID associated with the snapshot.
38    pub trader_id: TraderId,
39    /// The strategy ID associated with the snapshot.
40    pub strategy_id: StrategyId,
41    /// The instrument ID associated with the snapshot.
42    pub instrument_id: InstrumentId,
43    /// The position ID associated with the snapshot.
44    pub position_id: PositionId,
45    /// The account ID associated with the position.
46    pub account_id: AccountId,
47    /// The client order ID for the order which opened the position.
48    pub opening_order_id: ClientOrderId,
49    /// The client order ID for the order which closed the position.
50    pub closing_order_id: Option<ClientOrderId>,
51    /// The entry direction from open.
52    pub entry: OrderSide,
53    /// The position side.
54    pub side: PositionSide,
55    /// The position signed quantity (positive for LONG, negative for SHOT).
56    pub signed_qty: f64,
57    /// The position open quantity.
58    pub quantity: Quantity,
59    /// The peak directional quantity reached by the position.
60    pub peak_qty: Quantity,
61    /// The position quote currency.
62    pub quote_currency: Currency,
63    /// The position base currency.
64    pub base_currency: Option<Currency>,
65    /// The position settlement currency.
66    pub settlement_currency: Currency,
67    /// The average open price.
68    pub avg_px_open: f64,
69    /// The average closing price.
70    pub avg_px_close: Option<f64>,
71    /// The realized return for the position.
72    pub realized_return: Option<f64>,
73    /// The realized PnL for the position (including commissions).
74    pub realized_pnl: Option<Money>,
75    /// The unrealized PnL for the position (including commissions).
76    pub unrealized_pnl: Option<Money>,
77    /// The commissions for the position.
78    pub commissions: Vec<Money>,
79    /// The open duration for the position (nanoseconds).
80    pub duration_ns: Option<u64>,
81    /// UNIX timestamp (nanoseconds) when the position opened.
82    pub ts_opened: UnixNanos,
83    /// UNIX timestamp (nanoseconds) when the position closed.
84    pub ts_closed: Option<UnixNanos>,
85    /// UNIX timestamp (nanoseconds) when the snapshot was initialized.
86    pub ts_init: UnixNanos,
87    /// UNIX timestamp (nanoseconds) when the last position event occurred.
88    pub ts_last: UnixNanos,
89}
90
91impl PositionSnapshot {
92    #[must_use]
93    pub fn from(position: &Position, unrealized_pnl: Option<Money>) -> Self {
94        Self {
95            trader_id: position.trader_id,
96            strategy_id: position.strategy_id,
97            instrument_id: position.instrument_id,
98            position_id: position.id,
99            account_id: position.account_id,
100            opening_order_id: position.opening_order_id,
101            closing_order_id: position.closing_order_id,
102            entry: position.entry,
103            side: position.side,
104            signed_qty: position.signed_qty,
105            quantity: position.quantity,
106            peak_qty: position.peak_qty,
107            quote_currency: position.quote_currency,
108            base_currency: position.base_currency,
109            settlement_currency: position.settlement_currency,
110            avg_px_open: position.avg_px_open,
111            avg_px_close: position.avg_px_close,
112            realized_return: Some(position.realized_return), // TODO: Standardize
113            realized_pnl: position.realized_pnl,
114            unrealized_pnl,
115            commissions: position.commissions.values().copied().collect(), // TODO: Optimize
116            duration_ns: Some(position.duration_ns),                       // TODO: Standardize
117            ts_opened: position.ts_opened,
118            ts_closed: position.ts_closed,
119            ts_init: position.ts_init,
120            ts_last: position.ts_last,
121        }
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use nautilus_core::{UUID4, UnixNanos};
128    use rstest::*;
129
130    use super::*;
131    use crate::{
132        enums::{LiquiditySide, OrderSide, OrderType, PositionSide},
133        events::OrderFilled,
134        identifiers::{
135            AccountId, ClientOrderId, InstrumentId, PositionId, StrategyId, TradeId, TraderId,
136            VenueOrderId,
137        },
138        instruments::{InstrumentAny, stubs::audusd_sim},
139        position::Position,
140        types::{Currency, Money, Price, Quantity},
141    };
142
143    fn create_test_position_snapshot() -> PositionSnapshot {
144        PositionSnapshot {
145            trader_id: TraderId::from("TRADER-001"),
146            strategy_id: StrategyId::from("EMA-CROSS"),
147            instrument_id: InstrumentId::from("EURUSD.SIM"),
148            position_id: PositionId::from("P-001"),
149            account_id: AccountId::from("SIM-001"),
150            opening_order_id: ClientOrderId::from("O-19700101-000000-001-001-1"),
151            closing_order_id: Some(ClientOrderId::from("O-19700101-000000-001-001-2")),
152            entry: OrderSide::Buy,
153            side: PositionSide::Long,
154            signed_qty: 100.0,
155            quantity: Quantity::from("100"),
156            peak_qty: Quantity::from("100"),
157            quote_currency: Currency::USD(),
158            base_currency: Some(Currency::EUR()),
159            settlement_currency: Currency::USD(),
160            avg_px_open: 1.0500,
161            avg_px_close: Some(1.0600),
162            realized_return: Some(0.0095),
163            realized_pnl: Some(Money::new(100.0, Currency::USD())),
164            unrealized_pnl: Some(Money::new(50.0, Currency::USD())),
165            commissions: vec![Money::new(2.0, Currency::USD())],
166            duration_ns: Some(3_600_000_000_000), // 1 hour in nanoseconds
167            ts_opened: UnixNanos::from(1_000_000_000),
168            ts_closed: Some(UnixNanos::from(4_600_000_000)),
169            ts_init: UnixNanos::from(2_000_000_000),
170            ts_last: UnixNanos::from(4_600_000_000),
171        }
172    }
173
174    fn create_test_order_filled() -> OrderFilled {
175        OrderFilled::new(
176            TraderId::from("TRADER-001"),
177            StrategyId::from("EMA-CROSS"),
178            InstrumentId::from("AUD/USD.SIM"),
179            ClientOrderId::from("O-19700101-000000-001-001-1"),
180            VenueOrderId::from("1"),
181            AccountId::from("SIM-001"),
182            TradeId::from("T-001"),
183            OrderSide::Buy,
184            OrderType::Market,
185            Quantity::from("100"),
186            Price::from("0.8000"),
187            Currency::USD(),
188            LiquiditySide::Taker,
189            UUID4::default(),
190            UnixNanos::from(1_000_000_000),
191            UnixNanos::from(2_000_000_000),
192            false,
193            Some(PositionId::from("P-001")),
194            Some(Money::new(2.0, Currency::USD())),
195        )
196    }
197
198    #[rstest]
199    fn test_position_snapshot_from() {
200        let instrument = audusd_sim();
201        let fill = create_test_order_filled();
202        let position = Position::new(&InstrumentAny::CurrencyPair(instrument), fill);
203        let unrealized_pnl = Some(Money::new(75.0, Currency::USD()));
204
205        let snapshot = PositionSnapshot::from(&position, unrealized_pnl);
206
207        assert_eq!(snapshot.trader_id, position.trader_id);
208        assert_eq!(snapshot.strategy_id, position.strategy_id);
209        assert_eq!(snapshot.instrument_id, position.instrument_id);
210        assert_eq!(snapshot.position_id, position.id);
211        assert_eq!(snapshot.account_id, position.account_id);
212        assert_eq!(snapshot.opening_order_id, position.opening_order_id);
213        assert_eq!(snapshot.closing_order_id, position.closing_order_id);
214        assert_eq!(snapshot.entry, position.entry);
215        assert_eq!(snapshot.side, position.side);
216        assert_eq!(snapshot.signed_qty, position.signed_qty);
217        assert_eq!(snapshot.quantity, position.quantity);
218        assert_eq!(snapshot.peak_qty, position.peak_qty);
219        assert_eq!(snapshot.quote_currency, position.quote_currency);
220        assert_eq!(snapshot.base_currency, position.base_currency);
221        assert_eq!(snapshot.settlement_currency, position.settlement_currency);
222        assert_eq!(snapshot.avg_px_open, position.avg_px_open);
223        assert_eq!(snapshot.avg_px_close, position.avg_px_close);
224        assert_eq!(snapshot.realized_return, Some(position.realized_return));
225        assert_eq!(snapshot.realized_pnl, position.realized_pnl);
226        assert_eq!(snapshot.unrealized_pnl, unrealized_pnl);
227        assert_eq!(snapshot.duration_ns, Some(position.duration_ns));
228        assert_eq!(snapshot.ts_opened, position.ts_opened);
229        assert_eq!(snapshot.ts_closed, position.ts_closed);
230        assert_eq!(snapshot.ts_init, position.ts_init);
231        assert_eq!(snapshot.ts_last, position.ts_last);
232    }
233
234    #[rstest]
235    fn test_position_snapshot_from_with_no_unrealized_pnl() {
236        let instrument = audusd_sim();
237        let fill = create_test_order_filled();
238        let position = Position::new(&InstrumentAny::CurrencyPair(instrument), fill);
239
240        let snapshot = PositionSnapshot::from(&position, None);
241
242        assert_eq!(snapshot.unrealized_pnl, None);
243    }
244
245    #[rstest]
246    fn test_position_snapshot_serialization() {
247        let original = create_test_position_snapshot();
248
249        // Test JSON serialization
250        let json = serde_json::to_string(&original).unwrap();
251        let deserialized: PositionSnapshot = serde_json::from_str(&json).unwrap();
252
253        assert_eq!(original, deserialized);
254    }
255}