nautilus_model/events/position/
mod.rs1use serde::{Deserialize, Serialize};
17
18use crate::{
19 events::{PositionAdjusted, PositionChanged, PositionClosed, PositionOpened},
20 identifiers::{AccountId, InstrumentId},
21};
22pub mod adjusted;
23pub mod changed;
24pub mod closed;
25pub mod opened;
26pub mod snapshot;
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
29pub enum PositionEvent {
30 PositionOpened(PositionOpened),
31 PositionChanged(PositionChanged),
32 PositionClosed(PositionClosed),
33 PositionAdjusted(PositionAdjusted),
34}
35
36impl PositionEvent {
37 #[must_use]
38 pub fn instrument_id(&self) -> InstrumentId {
39 match self {
40 Self::PositionOpened(position) => position.instrument_id,
41 Self::PositionChanged(position) => position.instrument_id,
42 Self::PositionClosed(position) => position.instrument_id,
43 Self::PositionAdjusted(adjustment) => adjustment.instrument_id,
44 }
45 }
46
47 #[must_use]
48 pub fn account_id(&self) -> AccountId {
49 match self {
50 Self::PositionOpened(position) => position.account_id,
51 Self::PositionChanged(position) => position.account_id,
52 Self::PositionClosed(position) => position.account_id,
53 Self::PositionAdjusted(adjustment) => adjustment.account_id,
54 }
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use nautilus_core::{UUID4, UnixNanos};
61 use rstest::*;
62
63 use super::*;
64 use crate::{
65 enums::{OrderSide, PositionSide},
66 events::{PositionChanged, PositionClosed, PositionOpened},
67 identifiers::{AccountId, ClientOrderId, InstrumentId, PositionId, StrategyId, TraderId},
68 types::{Currency, Money, Price, Quantity},
69 };
70
71 fn create_test_position_opened() -> PositionOpened {
72 PositionOpened {
73 trader_id: TraderId::from("TRADER-001"),
74 strategy_id: StrategyId::from("EMA-CROSS"),
75 instrument_id: InstrumentId::from("EURUSD.SIM"),
76 position_id: PositionId::from("P-001"),
77 account_id: AccountId::from("SIM-001"),
78 opening_order_id: ClientOrderId::from("O-19700101-000000-001-001-1"),
79 entry: OrderSide::Buy,
80 side: PositionSide::Long,
81 signed_qty: 100.0,
82 quantity: Quantity::from("100"),
83 last_qty: Quantity::from("100"),
84 last_px: Price::from("1.0500"),
85 currency: Currency::USD(),
86 avg_px_open: 1.0500,
87 realized_pnl: Some(Money::new(-2.0, Currency::USD())),
88 event_id: UUID4::default(),
89 ts_event: UnixNanos::from(1_000_000_000),
90 ts_init: UnixNanos::from(2_000_000_000),
91 }
92 }
93
94 fn create_test_position_changed() -> PositionChanged {
95 PositionChanged {
96 trader_id: TraderId::from("TRADER-001"),
97 strategy_id: StrategyId::from("EMA-CROSS"),
98 instrument_id: InstrumentId::from("EURUSD.SIM"),
99 position_id: PositionId::from("P-001"),
100 account_id: AccountId::from("SIM-001"),
101 opening_order_id: ClientOrderId::from("O-19700101-000000-001-001-1"),
102 entry: OrderSide::Buy,
103 side: PositionSide::Long,
104 signed_qty: 150.0,
105 quantity: Quantity::from("150"),
106 peak_quantity: Quantity::from("150"),
107 last_qty: Quantity::from("50"),
108 last_px: Price::from("1.0550"),
109 currency: Currency::USD(),
110 avg_px_open: 1.0525,
111 avg_px_close: None,
112 realized_return: 0.0,
113 realized_pnl: None,
114 unrealized_pnl: Money::new(75.0, Currency::USD()),
115 event_id: UUID4::default(),
116 ts_opened: UnixNanos::from(1_000_000_000),
117 ts_event: UnixNanos::from(1_500_000_000),
118 ts_init: UnixNanos::from(2_500_000_000),
119 }
120 }
121
122 fn create_test_position_closed() -> PositionClosed {
123 PositionClosed {
124 trader_id: TraderId::from("TRADER-001"),
125 strategy_id: StrategyId::from("EMA-CROSS"),
126 instrument_id: InstrumentId::from("EURUSD.SIM"),
127 position_id: PositionId::from("P-001"),
128 account_id: AccountId::from("SIM-001"),
129 opening_order_id: ClientOrderId::from("O-19700101-000000-001-001-1"),
130 closing_order_id: Some(ClientOrderId::from("O-19700101-000000-001-001-2")),
131 entry: OrderSide::Buy,
132 side: PositionSide::Flat,
133 signed_qty: 0.0,
134 quantity: Quantity::from("0"),
135 peak_quantity: Quantity::from("150"),
136 last_qty: Quantity::from("150"),
137 last_px: Price::from("1.0600"),
138 currency: Currency::USD(),
139 avg_px_open: 1.0525,
140 avg_px_close: Some(1.0600),
141 realized_return: 0.0071,
142 realized_pnl: Some(Money::new(112.50, Currency::USD())),
143 unrealized_pnl: Money::new(0.0, Currency::USD()),
144 duration: 3_600_000_000_000, event_id: UUID4::default(),
146 ts_opened: UnixNanos::from(1_000_000_000),
147 ts_closed: Some(UnixNanos::from(4_600_000_000)),
148 ts_event: UnixNanos::from(4_600_000_000),
149 ts_init: UnixNanos::from(5_000_000_000),
150 }
151 }
152
153 #[rstest]
154 fn test_position_event_opened_instrument_id() {
155 let opened = create_test_position_opened();
156 let event = PositionEvent::PositionOpened(opened);
157
158 assert_eq!(event.instrument_id(), InstrumentId::from("EURUSD.SIM"));
159 }
160
161 #[rstest]
162 fn test_position_event_changed_instrument_id() {
163 let changed = create_test_position_changed();
164 let event = PositionEvent::PositionChanged(changed);
165
166 assert_eq!(event.instrument_id(), InstrumentId::from("EURUSD.SIM"));
167 }
168
169 #[rstest]
170 fn test_position_event_closed_instrument_id() {
171 let closed = create_test_position_closed();
172 let event = PositionEvent::PositionClosed(closed);
173
174 assert_eq!(event.instrument_id(), InstrumentId::from("EURUSD.SIM"));
175 }
176
177 #[rstest]
178 fn test_position_event_opened_account_id() {
179 let opened = create_test_position_opened();
180 let event = PositionEvent::PositionOpened(opened);
181
182 assert_eq!(event.account_id(), AccountId::from("SIM-001"));
183 }
184
185 #[rstest]
186 fn test_position_event_changed_account_id() {
187 let changed = create_test_position_changed();
188 let event = PositionEvent::PositionChanged(changed);
189
190 assert_eq!(event.account_id(), AccountId::from("SIM-001"));
191 }
192
193 #[rstest]
194 fn test_position_event_closed_account_id() {
195 let closed = create_test_position_closed();
196 let event = PositionEvent::PositionClosed(closed);
197
198 assert_eq!(event.account_id(), AccountId::from("SIM-001"));
199 }
200
201 #[rstest]
202 fn test_position_event_enum_variants() {
203 let opened = create_test_position_opened();
204 let changed = create_test_position_changed();
205 let closed = create_test_position_closed();
206
207 let event_opened = PositionEvent::PositionOpened(opened);
208 let event_changed = PositionEvent::PositionChanged(changed);
209 let event_closed = PositionEvent::PositionClosed(closed);
210
211 match event_opened {
212 PositionEvent::PositionOpened(_) => {}
213 _ => panic!("Expected PositionOpened variant"),
214 }
215
216 match event_changed {
217 PositionEvent::PositionChanged(_) => {}
218 _ => panic!("Expected PositionChanged variant"),
219 }
220
221 match event_closed {
222 PositionEvent::PositionClosed(_) => {}
223 _ => panic!("Expected PositionClosed variant"),
224 }
225 }
226}