nautilus_model/events/position/
mod.rs

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