nautilus_execution/python/
position.rs
1use 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 #[getter]
127 #[pyo3(name = "is_flat")]
128 const fn py_is_flat(&self) -> bool {
129 self.is_flat()
130 }
131
132 #[getter]
133 #[pyo3(name = "is_long")]
134 fn py_is_long(&self) -> bool {
135 self.is_long()
136 }
137
138 #[getter]
139 #[pyo3(name = "is_short")]
140 fn py_is_short(&self) -> bool {
141 self.is_short()
142 }
143
144 #[staticmethod]
145 #[pyo3(name = "from_dict")]
146 pub fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
147 from_dict_pyo3(py, values)
148 }
149
150 #[pyo3(name = "to_dict")]
151 pub fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
152 let dict = PyDict::new(py);
153 dict.set_item("type", stringify!(PositionStatusReport))?;
154 dict.set_item("account_id", self.account_id.to_string())?;
155 dict.set_item("instrument_id", self.instrument_id.to_string())?;
156 match self.venue_position_id {
157 Some(venue_position_id) => {
158 dict.set_item("venue_position_id", venue_position_id.to_string())?;
159 }
160 None => dict.set_item("venue_position_id", py.None())?,
161 }
162 dict.set_item("position_side", self.position_side.to_string())?;
163 dict.set_item("quantity", self.quantity.to_string())?;
164 dict.set_item("signed_decimal_qty", self.signed_decimal_qty.to_string())?;
165 dict.set_item("report_id", self.report_id.to_string())?;
166 dict.set_item("ts_last", self.ts_last.as_u64())?;
167 dict.set_item("ts_init", self.ts_init.as_u64())?;
168 Ok(dict.into())
169 }
170}