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