Skip to main content

nautilus_model/events/position/
closed.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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 nautilus_core::{
17    UUID4,
18    nanos::{DurationNanos, UnixNanos},
19};
20use serde::{Deserialize, Serialize};
21
22use crate::{
23    enums::{OrderSide, PositionSide},
24    events::OrderFilled,
25    identifiers::{AccountId, ClientOrderId, InstrumentId, PositionId, StrategyId, TraderId},
26    position::Position,
27    types::{Currency, Money, Price, Quantity},
28};
29
30/// Represents an event where a position has been closed.
31#[repr(C)]
32#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
33#[cfg_attr(
34    feature = "python",
35    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
36)]
37#[cfg_attr(
38    feature = "python",
39    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
40)]
41pub struct PositionClosed {
42    /// The trader ID associated with the event.
43    pub trader_id: TraderId,
44    /// The strategy ID associated with the event.
45    pub strategy_id: StrategyId,
46    /// The instrument ID associated with the event.
47    pub instrument_id: InstrumentId,
48    /// The position ID associated with the event.
49    pub position_id: PositionId,
50    /// The account ID associated with the position.
51    pub account_id: AccountId,
52    /// The client order ID for the order which opened the position.
53    pub opening_order_id: ClientOrderId,
54    /// The client order ID for the order which closed the position.
55    pub closing_order_id: Option<ClientOrderId>,
56    /// The position entry order side.
57    pub entry: OrderSide,
58    /// The position side.
59    pub side: PositionSide,
60    /// The current signed quantity (positive for position side `LONG`, negative for `SHORT`).
61    pub signed_qty: f64,
62    /// The current open quantity.
63    pub quantity: Quantity,
64    /// The peak directional quantity reached by the position.
65    pub peak_quantity: Quantity,
66    /// The last fill quantity for the position.
67    pub last_qty: Quantity,
68    /// The last fill price for the position.
69    pub last_px: Price,
70    /// The position quote currency.
71    pub currency: Currency,
72    /// The average open price.
73    pub avg_px_open: f64,
74    /// The average closing price.
75    pub avg_px_close: Option<f64>,
76    /// The realized return for the position.
77    pub realized_return: f64,
78    /// The realized PnL for the position (including commissions).
79    pub realized_pnl: Option<Money>,
80    /// The unrealized PnL for the position (including commissions).
81    pub unrealized_pnl: Money,
82    /// The total open duration (nanoseconds).
83    pub duration: DurationNanos,
84    /// The unique identifier for the event.
85    pub event_id: UUID4,
86    /// UNIX timestamp (nanoseconds) when the position was opened.
87    pub ts_opened: UnixNanos,
88    /// UNIX timestamp (nanoseconds) when the position was closed.
89    pub ts_closed: Option<UnixNanos>,
90    /// UNIX timestamp (nanoseconds) when the event occurred.
91    pub ts_event: UnixNanos,
92    /// UNIX timestamp (nanoseconds) when the event was initialized.
93    pub ts_init: UnixNanos,
94}
95
96impl PositionClosed {
97    #[must_use]
98    pub fn create(
99        position: &Position,
100        fill: &OrderFilled,
101        event_id: UUID4,
102        ts_init: UnixNanos,
103    ) -> Self {
104        Self {
105            trader_id: position.trader_id,
106            strategy_id: position.strategy_id,
107            instrument_id: position.instrument_id,
108            position_id: position.id,
109            account_id: position.account_id,
110            opening_order_id: position.opening_order_id,
111            closing_order_id: position.closing_order_id,
112            entry: position.entry,
113            side: position.side,
114            signed_qty: position.signed_qty,
115            quantity: position.quantity,
116            peak_quantity: position.peak_qty,
117            last_qty: fill.last_qty,
118            last_px: fill.last_px,
119            currency: position.quote_currency,
120            avg_px_open: position.avg_px_open,
121            avg_px_close: position.avg_px_close,
122            realized_return: position.realized_return,
123            realized_pnl: position.realized_pnl,
124            unrealized_pnl: Money::zero(position.quote_currency),
125            duration: position.duration_ns,
126            event_id,
127            ts_opened: position.ts_opened,
128            ts_closed: position.ts_closed,
129            ts_event: fill.ts_event,
130            ts_init,
131        }
132    }
133}
134
135#[cfg(test)]
136mod tests {
137    use nautilus_core::UnixNanos;
138    use rstest::*;
139
140    use super::*;
141    use crate::{
142        enums::{OrderSide, PositionSide},
143        events::{OrderFilled, order::spec::OrderFilledSpec},
144        identifiers::{
145            AccountId, ClientOrderId, InstrumentId, PositionId, StrategyId, TradeId, TraderId,
146            VenueOrderId,
147        },
148        instruments::{InstrumentAny, stubs::audusd_sim},
149        position::Position,
150        types::{Currency, Money, Price, Quantity},
151    };
152
153    fn create_test_position_closed() -> PositionClosed {
154        PositionClosed {
155            trader_id: TraderId::from("TRADER-001"),
156            strategy_id: StrategyId::from("EMA-CROSS"),
157            instrument_id: InstrumentId::from("EURUSD.SIM"),
158            position_id: PositionId::from("P-001"),
159            account_id: AccountId::from("SIM-001"),
160            opening_order_id: ClientOrderId::from("O-19700101-000000-001-001-1"),
161            closing_order_id: Some(ClientOrderId::from("O-19700101-000000-001-001-2")),
162            entry: OrderSide::Buy,
163            side: PositionSide::Flat,
164            signed_qty: 0.0,
165            quantity: Quantity::from("0"),
166            peak_quantity: Quantity::from("150"),
167            last_qty: Quantity::from("150"),
168            last_px: Price::from("1.0600"),
169            currency: Currency::USD(),
170            avg_px_open: 1.0525,
171            avg_px_close: Some(1.0600),
172            realized_return: 0.0071,
173            realized_pnl: Some(Money::new(112.50, Currency::USD())),
174            unrealized_pnl: Money::new(0.0, Currency::USD()),
175            duration: 3_600_000_000_000, // 1 hour in nanoseconds
176            event_id: UUID4::default(),
177            ts_opened: UnixNanos::from(1_000_000_000),
178            ts_closed: Some(UnixNanos::from(4_600_000_000)),
179            ts_event: UnixNanos::from(4_600_000_000),
180            ts_init: UnixNanos::from(5_000_000_000),
181        }
182    }
183
184    fn create_test_order_filled() -> OrderFilled {
185        OrderFilledSpec::builder()
186            .strategy_id(StrategyId::from("EMA-CROSS"))
187            .instrument_id(InstrumentId::from("EURUSD.SIM"))
188            .client_order_id(ClientOrderId::from("O-19700101-000000-001-001-2"))
189            .venue_order_id(VenueOrderId::from("2"))
190            .trade_id(TradeId::from("T-002"))
191            .order_side(OrderSide::Sell)
192            .last_qty(Quantity::from("150"))
193            .last_px(Price::from("1.0600"))
194            .ts_event(UnixNanos::from(4_600_000_000))
195            .ts_init(UnixNanos::from(5_000_000_000))
196            .position_id(PositionId::from("P-001"))
197            .commission(Money::new(2.5, Currency::USD()))
198            .build()
199    }
200
201    #[rstest]
202    fn test_position_closed_create() {
203        let instrument = audusd_sim();
204        let initial_fill = OrderFilledSpec::builder()
205            .strategy_id(StrategyId::from("EMA-CROSS"))
206            .instrument_id(InstrumentId::from("AUD/USD.SIM"))
207            .client_order_id(ClientOrderId::from("O-19700101-000000-001-001-1"))
208            .venue_order_id(VenueOrderId::from("1"))
209            .trade_id(TradeId::from("T-001"))
210            .last_qty(Quantity::from("100"))
211            .last_px(Price::from("0.8000"))
212            .ts_event(UnixNanos::from(1_000_000_000))
213            .ts_init(UnixNanos::from(2_000_000_000))
214            .position_id(PositionId::from("P-001"))
215            .commission(Money::new(2.0, Currency::USD()))
216            .build();
217
218        let position = Position::new(&InstrumentAny::CurrencyPair(instrument), initial_fill);
219        let closing_fill = create_test_order_filled();
220        let event_id = UUID4::default();
221        let ts_init = UnixNanos::from(6_000_000_000);
222
223        let position_closed = PositionClosed::create(&position, &closing_fill, event_id, ts_init);
224
225        assert_eq!(position_closed.trader_id, position.trader_id);
226        assert_eq!(position_closed.strategy_id, position.strategy_id);
227        assert_eq!(position_closed.instrument_id, position.instrument_id);
228        assert_eq!(position_closed.position_id, position.id);
229        assert_eq!(position_closed.account_id, position.account_id);
230        assert_eq!(position_closed.opening_order_id, position.opening_order_id);
231        assert_eq!(position_closed.closing_order_id, position.closing_order_id);
232        assert_eq!(position_closed.entry, position.entry);
233        assert_eq!(position_closed.side, position.side);
234        assert_eq!(position_closed.signed_qty, position.signed_qty);
235        assert_eq!(position_closed.quantity, position.quantity);
236        assert_eq!(position_closed.peak_quantity, position.peak_qty);
237        assert_eq!(position_closed.last_qty, closing_fill.last_qty);
238        assert_eq!(position_closed.last_px, closing_fill.last_px);
239        assert_eq!(position_closed.currency, position.quote_currency);
240        assert_eq!(position_closed.avg_px_open, position.avg_px_open);
241        assert_eq!(position_closed.avg_px_close, position.avg_px_close);
242        assert_eq!(position_closed.realized_return, position.realized_return);
243        assert_eq!(position_closed.realized_pnl, position.realized_pnl);
244        assert_eq!(
245            position_closed.unrealized_pnl,
246            Money::new(0.0, position.quote_currency)
247        );
248        assert_eq!(position_closed.duration, position.duration_ns);
249        assert_eq!(position_closed.event_id, event_id);
250        assert_eq!(position_closed.ts_opened, position.ts_opened);
251        assert_eq!(position_closed.ts_closed, position.ts_closed);
252        assert_eq!(position_closed.ts_event, closing_fill.ts_event);
253        assert_eq!(position_closed.ts_init, ts_init);
254    }
255
256    #[rstest]
257    fn test_position_closed_flat_position() {
258        let position_closed = create_test_position_closed();
259
260        assert_eq!(position_closed.side, PositionSide::Flat);
261        assert_eq!(position_closed.signed_qty, 0.0);
262        assert_eq!(position_closed.quantity, Quantity::from("0"));
263        assert_eq!(
264            position_closed.unrealized_pnl,
265            Money::new(0.0, Currency::USD())
266        );
267    }
268
269    #[rstest]
270    fn test_position_closed_loss_scenario() {
271        let mut position_closed = create_test_position_closed();
272        position_closed.avg_px_close = Some(1.0400); // Sold below open price
273        position_closed.realized_return = -0.0119;
274        position_closed.realized_pnl = Some(Money::new(-187.50, Currency::USD()));
275
276        assert_eq!(position_closed.avg_px_close, Some(1.0400));
277        assert!(position_closed.realized_return < 0.0);
278        assert_eq!(
279            position_closed.realized_pnl,
280            Some(Money::new(-187.50, Currency::USD()))
281        );
282    }
283}