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