1use 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#[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 pub trader_id: TraderId,
39 pub strategy_id: StrategyId,
41 pub instrument_id: InstrumentId,
43 pub position_id: PositionId,
45 pub account_id: AccountId,
47 pub opening_order_id: ClientOrderId,
49 pub closing_order_id: Option<ClientOrderId>,
51 pub entry: OrderSide,
53 pub side: PositionSide,
55 pub signed_qty: f64,
57 pub quantity: Quantity,
59 pub peak_qty: Quantity,
61 pub quote_currency: Currency,
63 pub base_currency: Option<Currency>,
65 pub settlement_currency: Currency,
67 pub avg_px_open: f64,
69 pub avg_px_close: Option<f64>,
71 pub realized_return: Option<f64>,
73 pub realized_pnl: Option<Money>,
75 pub unrealized_pnl: Option<Money>,
77 pub commissions: Vec<Money>,
79 pub duration_ns: Option<u64>,
81 pub ts_opened: UnixNanos,
83 pub ts_closed: Option<UnixNanos>,
85 pub ts_init: UnixNanos,
87 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), realized_pnl: position.realized_pnl,
114 unrealized_pnl,
115 commissions: position.commissions.values().copied().collect(), duration_ns: Some(position.duration_ns), 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), 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 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}