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