Skip to main content

nautilus_model/python/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::{UUID4, nanos::DurationNanos, python::IntoPyObjectNautilusExt};
17use pyo3::{basic::CompareOp, prelude::*};
18
19use crate::{
20    enums::{OrderSide, PositionSide},
21    events::{OrderFilled, PositionClosed},
22    identifiers::{AccountId, ClientOrderId, InstrumentId, PositionId, StrategyId, TraderId},
23    position::Position,
24    types::{Currency, Money, Price, Quantity},
25};
26
27#[pymethods]
28#[pyo3_stub_gen::derive::gen_stub_pymethods]
29impl PositionClosed {
30    #[staticmethod]
31    #[pyo3(name = "create")]
32    fn py_create(position: &Position, fill: &OrderFilled, event_id: UUID4, ts_init: u64) -> Self {
33        Self::create(position, fill, event_id, ts_init.into())
34    }
35
36    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
37        match op {
38            CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
39            CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
40            _ => py.NotImplemented(),
41        }
42    }
43
44    fn __repr__(&self) -> String {
45        format!("{self:?}")
46    }
47
48    #[getter]
49    #[pyo3(name = "trader_id")]
50    fn py_trader_id(&self) -> TraderId {
51        self.trader_id
52    }
53
54    #[getter]
55    #[pyo3(name = "strategy_id")]
56    fn py_strategy_id(&self) -> StrategyId {
57        self.strategy_id
58    }
59
60    #[getter]
61    #[pyo3(name = "instrument_id")]
62    fn py_instrument_id(&self) -> InstrumentId {
63        self.instrument_id
64    }
65
66    #[getter]
67    #[pyo3(name = "position_id")]
68    fn py_position_id(&self) -> PositionId {
69        self.position_id
70    }
71
72    #[getter]
73    #[pyo3(name = "account_id")]
74    fn py_account_id(&self) -> AccountId {
75        self.account_id
76    }
77
78    #[getter]
79    #[pyo3(name = "opening_order_id")]
80    fn py_opening_order_id(&self) -> ClientOrderId {
81        self.opening_order_id
82    }
83
84    #[getter]
85    #[pyo3(name = "closing_order_id")]
86    fn py_closing_order_id(&self) -> Option<ClientOrderId> {
87        self.closing_order_id
88    }
89
90    #[getter]
91    #[pyo3(name = "entry")]
92    fn py_entry(&self) -> OrderSide {
93        self.entry
94    }
95
96    #[getter]
97    #[pyo3(name = "side")]
98    fn py_side(&self) -> PositionSide {
99        self.side
100    }
101
102    #[getter]
103    #[pyo3(name = "signed_qty")]
104    fn py_signed_qty(&self) -> f64 {
105        self.signed_qty
106    }
107
108    #[getter]
109    #[pyo3(name = "quantity")]
110    fn py_quantity(&self) -> Quantity {
111        self.quantity
112    }
113
114    #[getter]
115    #[pyo3(name = "peak_quantity")]
116    fn py_peak_quantity(&self) -> Quantity {
117        self.peak_quantity
118    }
119
120    #[getter]
121    #[pyo3(name = "last_qty")]
122    fn py_last_qty(&self) -> Quantity {
123        self.last_qty
124    }
125
126    #[getter]
127    #[pyo3(name = "last_px")]
128    fn py_last_px(&self) -> Price {
129        self.last_px
130    }
131
132    #[getter]
133    #[pyo3(name = "currency")]
134    fn py_currency(&self) -> Currency {
135        self.currency
136    }
137
138    #[getter]
139    #[pyo3(name = "avg_px_open")]
140    fn py_avg_px_open(&self) -> f64 {
141        self.avg_px_open
142    }
143
144    #[getter]
145    #[pyo3(name = "avg_px_close")]
146    fn py_avg_px_close(&self) -> Option<f64> {
147        self.avg_px_close
148    }
149
150    #[getter]
151    #[pyo3(name = "realized_return")]
152    fn py_realized_return(&self) -> f64 {
153        self.realized_return
154    }
155
156    #[getter]
157    #[pyo3(name = "realized_pnl")]
158    fn py_realized_pnl(&self) -> Option<Money> {
159        self.realized_pnl
160    }
161
162    #[getter]
163    #[pyo3(name = "unrealized_pnl")]
164    fn py_unrealized_pnl(&self) -> Money {
165        self.unrealized_pnl
166    }
167
168    #[getter]
169    #[pyo3(name = "duration")]
170    fn py_duration(&self) -> DurationNanos {
171        self.duration
172    }
173
174    #[getter]
175    #[pyo3(name = "event_id")]
176    fn py_event_id(&self) -> UUID4 {
177        self.event_id
178    }
179
180    #[getter]
181    #[pyo3(name = "ts_opened")]
182    fn py_ts_opened(&self) -> u64 {
183        self.ts_opened.as_u64()
184    }
185
186    #[getter]
187    #[pyo3(name = "ts_closed")]
188    fn py_ts_closed(&self) -> Option<u64> {
189        self.ts_closed.map(|ts| ts.as_u64())
190    }
191
192    #[getter]
193    #[pyo3(name = "ts_event")]
194    fn py_ts_event(&self) -> u64 {
195        self.ts_event.as_u64()
196    }
197
198    #[getter]
199    #[pyo3(name = "ts_init")]
200    fn py_ts_init(&self) -> u64 {
201        self.ts_init.as_u64()
202    }
203}