nautilus_model/python/events/position/
adjusted.rs1use 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::PositionAdjustmentType,
25 events::PositionAdjusted,
26 identifiers::{AccountId, InstrumentId, PositionId, StrategyId, TraderId},
27 types::Money,
28};
29
30#[pymethods]
31#[pyo3_stub_gen::derive::gen_stub_pymethods]
32impl PositionAdjusted {
33 #[expect(clippy::too_many_arguments)]
40 #[new]
41 #[pyo3(signature = (trader_id, strategy_id, instrument_id, position_id, account_id, adjustment_type, quantity_change, pnl_change, reason, event_id, ts_event, ts_init))]
42 fn py_new(
43 trader_id: TraderId,
44 strategy_id: StrategyId,
45 instrument_id: InstrumentId,
46 position_id: PositionId,
47 account_id: AccountId,
48 adjustment_type: PositionAdjustmentType,
49 quantity_change: Option<Decimal>,
50 pnl_change: Option<Money>,
51 reason: Option<String>,
52 event_id: UUID4,
53 ts_event: u64,
54 ts_init: u64,
55 ) -> Self {
56 Self::new(
57 trader_id,
58 strategy_id,
59 instrument_id,
60 position_id,
61 account_id,
62 adjustment_type,
63 quantity_change,
64 pnl_change,
65 reason.map(|s| s.as_str().into()),
66 event_id,
67 ts_event.into(),
68 ts_init.into(),
69 )
70 }
71
72 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
73 match op {
74 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
75 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
76 _ => py.NotImplemented(),
77 }
78 }
79
80 fn __repr__(&self) -> String {
81 format!("{self:?}")
82 }
83
84 #[getter]
85 #[pyo3(name = "trader_id")]
86 fn py_trader_id(&self) -> TraderId {
87 self.trader_id
88 }
89
90 #[getter]
91 #[pyo3(name = "strategy_id")]
92 fn py_strategy_id(&self) -> StrategyId {
93 self.strategy_id
94 }
95
96 #[getter]
97 #[pyo3(name = "instrument_id")]
98 fn py_instrument_id(&self) -> InstrumentId {
99 self.instrument_id
100 }
101
102 #[getter]
103 #[pyo3(name = "position_id")]
104 fn py_position_id(&self) -> PositionId {
105 self.position_id
106 }
107
108 #[getter]
109 #[pyo3(name = "account_id")]
110 fn py_account_id(&self) -> AccountId {
111 self.account_id
112 }
113
114 #[getter]
115 #[pyo3(name = "adjustment_type")]
116 fn py_adjustment_type(&self) -> PositionAdjustmentType {
117 self.adjustment_type
118 }
119
120 #[getter]
121 #[pyo3(name = "quantity_change")]
122 fn py_quantity_change(&self) -> Option<Decimal> {
123 self.quantity_change
124 }
125
126 #[getter]
127 #[pyo3(name = "pnl_change")]
128 fn py_pnl_change(&self) -> Option<Money> {
129 self.pnl_change
130 }
131
132 #[getter]
133 #[pyo3(name = "reason")]
134 fn py_reason(&self) -> Option<String> {
135 self.reason.map(|r| r.to_string())
136 }
137
138 #[getter]
139 #[pyo3(name = "event_id")]
140 fn py_event_id(&self) -> UUID4 {
141 self.event_id
142 }
143
144 #[getter]
145 #[pyo3(name = "ts_event")]
146 fn py_ts_event(&self) -> u64 {
147 self.ts_event.as_u64()
148 }
149
150 #[getter]
151 #[pyo3(name = "ts_init")]
152 fn py_ts_init(&self) -> u64 {
153 self.ts_init.as_u64()
154 }
155
156 #[staticmethod]
162 #[pyo3(name = "from_dict")]
163 fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
164 from_dict_pyo3(py, values)
165 }
166
167 #[pyo3(name = "to_dict")]
173 pub fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
174 let dict = PyDict::new(py);
175 dict.set_item("type", stringify!(PositionAdjusted))?;
176 dict.set_item("trader_id", self.trader_id.to_string())?;
177 dict.set_item("strategy_id", self.strategy_id.to_string())?;
178 dict.set_item("instrument_id", self.instrument_id.to_string())?;
179 dict.set_item("position_id", self.position_id.to_string())?;
180 dict.set_item("account_id", self.account_id.to_string())?;
181 dict.set_item("adjustment_type", self.adjustment_type.to_string())?;
182
183 match self.quantity_change {
184 Some(quantity_change) => {
185 dict.set_item("quantity_change", quantity_change.to_string())?;
186 }
187 None => dict.set_item("quantity_change", py.None())?,
188 }
189
190 match self.pnl_change {
191 Some(pnl_change) => dict.set_item("pnl_change", pnl_change.to_string())?,
192 None => dict.set_item("pnl_change", py.None())?,
193 }
194
195 match self.reason {
196 Some(reason) => dict.set_item("reason", reason.to_string())?,
197 None => dict.set_item("reason", py.None())?,
198 }
199 dict.set_item("event_id", self.event_id.to_string())?;
200 dict.set_item("ts_event", self.ts_event.as_u64())?;
201 dict.set_item("ts_init", self.ts_init.as_u64())?;
202 Ok(dict.into())
203 }
204}