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