Skip to main content

nautilus_model/python/reports/
position.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    python::{IntoPyObjectNautilusExt, serialization::from_dict_pyo3},
19};
20use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
21use rust_decimal::Decimal;
22
23use crate::{
24    enums::PositionSide,
25    identifiers::{AccountId, InstrumentId, PositionId},
26    reports::position::PositionStatusReport,
27    types::Quantity,
28};
29
30#[pymethods]
31#[pyo3_stub_gen::derive::gen_stub_pymethods]
32impl PositionStatusReport {
33    /// Represents a position status at a point in time.
34    #[new]
35    #[pyo3(signature = (account_id, instrument_id, position_side, quantity, ts_last, ts_init, report_id=None, venue_position_id=None, avg_px_open=None))]
36    #[expect(clippy::too_many_arguments)]
37    fn py_new(
38        account_id: AccountId,
39        instrument_id: InstrumentId,
40        position_side: PositionSide,
41        quantity: Quantity,
42        ts_last: u64,
43        ts_init: u64,
44        report_id: Option<UUID4>,
45        venue_position_id: Option<PositionId>,
46        avg_px_open: Option<Decimal>,
47    ) -> Self {
48        Self::new(
49            account_id,
50            instrument_id,
51            position_side.as_specified(),
52            quantity,
53            ts_last.into(),
54            ts_init.into(),
55            report_id,
56            venue_position_id,
57            avg_px_open,
58        )
59    }
60
61    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
62        match op {
63            CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
64            CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
65            _ => py.NotImplemented(),
66        }
67    }
68
69    fn __repr__(&self) -> String {
70        self.to_string()
71    }
72
73    fn __str__(&self) -> String {
74        self.to_string()
75    }
76
77    #[getter]
78    #[pyo3(name = "account_id")]
79    const fn py_account_id(&self) -> AccountId {
80        self.account_id
81    }
82
83    #[getter]
84    #[pyo3(name = "instrument_id")]
85    const fn py_instrument_id(&self) -> InstrumentId {
86        self.instrument_id
87    }
88
89    #[getter]
90    #[pyo3(name = "strategy_id")]
91    const fn py_strategy_id(&self) -> InstrumentId {
92        self.instrument_id
93    }
94
95    #[getter]
96    #[pyo3(name = "venue_position_id")]
97    const fn py_venue_position_id(&self) -> Option<PositionId> {
98        self.venue_position_id
99    }
100
101    #[getter]
102    #[pyo3(name = "position_side")]
103    fn py_position_side(&self) -> PositionSide {
104        self.position_side.as_position_side()
105    }
106
107    #[getter]
108    #[pyo3(name = "quantity")]
109    const fn py_quantity(&self) -> Quantity {
110        self.quantity
111    }
112
113    #[getter]
114    #[pyo3(name = "avg_px_open")]
115    const fn py_avg_px_open(&self) -> Option<Decimal> {
116        self.avg_px_open
117    }
118
119    #[getter]
120    #[pyo3(name = "report_id")]
121    const fn py_report_id(&self) -> UUID4 {
122        self.report_id
123    }
124
125    #[getter]
126    #[pyo3(name = "ts_last")]
127    const fn py_ts_last(&self) -> u64 {
128        self.ts_last.as_u64()
129    }
130
131    #[getter]
132    #[pyo3(name = "ts_init")]
133    const fn py_ts_init(&self) -> u64 {
134        self.ts_init.as_u64()
135    }
136
137    /// Checks if this is a flat position (quantity is zero).
138    #[getter]
139    #[pyo3(name = "is_flat")]
140    fn py_is_flat(&self) -> bool {
141        self.is_flat()
142    }
143
144    /// Checks if this is a long position.
145    #[getter]
146    #[pyo3(name = "is_long")]
147    fn py_is_long(&self) -> bool {
148        self.is_long()
149    }
150
151    /// Checks if this is a short position.
152    #[getter]
153    #[pyo3(name = "is_short")]
154    fn py_is_short(&self) -> bool {
155        self.is_short()
156    }
157
158    /// Creates a `PositionStatusReport` from a Python dictionary.
159    ///
160    /// # Errors
161    ///
162    /// Returns a Python exception if conversion from dict fails.
163    #[staticmethod]
164    #[pyo3(name = "from_dict")]
165    pub fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
166        from_dict_pyo3(py, values)
167    }
168
169    /// Converts the `PositionStatusReport` to a Python dictionary.
170    ///
171    /// # Errors
172    ///
173    /// Returns a Python exception if conversion to dict fails.
174    #[pyo3(name = "to_dict")]
175    pub fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
176        let dict = PyDict::new(py);
177        dict.set_item("type", stringify!(PositionStatusReport))?;
178        dict.set_item("account_id", self.account_id.to_string())?;
179        dict.set_item("instrument_id", self.instrument_id.to_string())?;
180
181        match self.venue_position_id {
182            Some(venue_position_id) => {
183                dict.set_item("venue_position_id", venue_position_id.to_string())?;
184            }
185            None => dict.set_item("venue_position_id", py.None())?,
186        }
187        dict.set_item("position_side", self.position_side.to_string())?;
188        dict.set_item("quantity", self.quantity.to_string())?;
189        dict.set_item("signed_decimal_qty", self.signed_decimal_qty.to_string())?;
190        dict.set_item("report_id", self.report_id.to_string())?;
191        dict.set_item("ts_last", self.ts_last.as_u64())?;
192        dict.set_item("ts_init", self.ts_init.as_u64())?;
193        Ok(dict.into())
194    }
195}