nautilus_execution/python/
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::{python::serialization::from_dict_pyo3, UUID4};
17use nautilus_model::{
18    enums::PositionSide,
19    identifiers::{AccountId, InstrumentId, PositionId},
20    types::Quantity,
21};
22use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
23
24use crate::reports::position::PositionStatusReport;
25
26#[pymethods]
27impl PositionStatusReport {
28    #[new]
29    #[pyo3(signature = (account_id, instrument_id, position_side, quantity, ts_last, ts_init, venue_position_id=None, report_id=None))]
30    #[allow(clippy::too_many_arguments)]
31    fn py_new(
32        account_id: AccountId,
33        instrument_id: InstrumentId,
34        position_side: PositionSide,
35        quantity: Quantity,
36        ts_last: u64,
37        ts_init: u64,
38        venue_position_id: Option<PositionId>,
39        report_id: Option<UUID4>,
40    ) -> PyResult<Self> {
41        Ok(Self::new(
42            account_id,
43            instrument_id,
44            position_side,
45            quantity,
46            venue_position_id,
47            ts_last.into(),
48            ts_init.into(),
49            report_id,
50        ))
51    }
52
53    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
54        match op {
55            CompareOp::Eq => self.eq(other).into_py(py),
56            CompareOp::Ne => self.ne(other).into_py(py),
57            _ => py.NotImplemented(),
58        }
59    }
60
61    fn __repr__(&self) -> String {
62        self.to_string()
63    }
64
65    fn __str__(&self) -> String {
66        self.to_string()
67    }
68
69    #[getter]
70    #[pyo3(name = "account_id")]
71    const fn py_account_id(&self) -> AccountId {
72        self.account_id
73    }
74
75    #[getter]
76    #[pyo3(name = "instrument_id")]
77    const fn py_instrument_id(&self) -> InstrumentId {
78        self.instrument_id
79    }
80
81    #[getter]
82    #[pyo3(name = "strategy_id")]
83    const fn py_strategy_id(&self) -> InstrumentId {
84        self.instrument_id
85    }
86
87    #[getter]
88    #[pyo3(name = "venue_position_id")]
89    const fn py_venue_position_id(&self) -> Option<PositionId> {
90        self.venue_position_id
91    }
92
93    #[getter]
94    #[pyo3(name = "position_side")]
95    const fn py_position_side(&self) -> PositionSide {
96        self.position_side
97    }
98
99    #[getter]
100    #[pyo3(name = "quantity")]
101    const fn py_quantity(&self) -> Quantity {
102        self.quantity
103    }
104
105    #[getter]
106    #[pyo3(name = "report_id")]
107    const fn py_report_id(&self) -> UUID4 {
108        self.report_id
109    }
110
111    #[getter]
112    #[pyo3(name = "ts_last")]
113    const fn py_ts_last(&self) -> u64 {
114        self.ts_last.as_u64()
115    }
116
117    #[getter]
118    #[pyo3(name = "ts_init")]
119    const fn py_ts_init(&self) -> u64 {
120        self.ts_init.as_u64()
121    }
122
123    #[staticmethod]
124    #[pyo3(name = "from_dict")]
125    pub fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
126        from_dict_pyo3(py, values)
127    }
128
129    #[pyo3(name = "to_dict")]
130    pub fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
131        let dict = PyDict::new(py);
132        dict.set_item("type", stringify!(PositionStatusReport))?;
133        dict.set_item("account_id", self.account_id.to_string())?;
134        dict.set_item("instrument_id", self.instrument_id.to_string())?;
135        match self.venue_position_id {
136            Some(venue_position_id) => {
137                dict.set_item("venue_position_id", venue_position_id.to_string())?;
138            }
139            None => dict.set_item("venue_position_id", py.None())?,
140        }
141        dict.set_item("position_side", self.position_side.to_string())?;
142        dict.set_item("quantity", self.quantity.to_string())?;
143        dict.set_item("signed_decimal_qty", self.signed_decimal_qty.to_string())?;
144        dict.set_item("report_id", self.report_id.to_string())?;
145        dict.set_item("ts_last", self.ts_last.as_u64())?;
146        dict.set_item("ts_init", self.ts_init.as_u64())?;
147        Ok(dict.into())
148    }
149}