nautilus_model/python/events/account/
state.rs1use std::str::FromStr;
17
18use nautilus_core::{
19 UUID4,
20 python::{IntoPyObjectPoseiExt, to_pyvalue_err},
21};
22use pyo3::{
23 basic::CompareOp,
24 prelude::*,
25 types::{PyDict, PyList},
26};
27
28use crate::{
29 enums::AccountType,
30 events::AccountState,
31 identifiers::AccountId,
32 types::{AccountBalance, Currency, MarginBalance},
33};
34
35#[pymethods]
36impl AccountState {
37 #[allow(clippy::too_many_arguments)]
38 #[new]
39 #[pyo3(signature = (account_id, account_type, balances, margins, is_reported, event_id, ts_event, ts_init, base_currency=None))]
40 fn py_new(
41 account_id: AccountId,
42 account_type: AccountType,
43 balances: Vec<AccountBalance>,
44 margins: Vec<MarginBalance>,
45 is_reported: bool,
46 event_id: UUID4,
47 ts_event: u64,
48 ts_init: u64,
49 base_currency: Option<Currency>,
50 ) -> Self {
51 Self::new(
52 account_id,
53 account_type,
54 balances,
55 margins,
56 is_reported,
57 event_id,
58 ts_event.into(),
59 ts_init.into(),
60 base_currency,
61 )
62 }
63
64 #[getter]
65 fn account_id(&self) -> AccountId {
66 self.account_id
67 }
68
69 #[getter]
70 fn account_type(&self) -> AccountType {
71 self.account_type
72 }
73
74 #[getter]
75 fn base_currency(&self) -> Option<Currency> {
76 self.base_currency
77 }
78
79 #[getter]
80 fn balances(&self) -> Vec<AccountBalance> {
81 self.balances.clone()
82 }
83
84 #[getter]
85 fn margins(&self) -> Vec<MarginBalance> {
86 self.margins.clone()
87 }
88
89 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
90 match op {
91 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
92 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
93 _ => py.NotImplemented(),
94 }
95 }
96
97 fn __repr__(&self) -> String {
98 format!("{self:?}")
99 }
100
101 fn __str__(&self) -> String {
102 self.to_string()
103 }
104
105 #[staticmethod]
106 #[pyo3(name = "from_dict")]
116 pub fn py_from_dict(values: &Bound<'_, PyDict>) -> PyResult<Self> {
117 let dict = values.as_ref();
118 let account_id: String = dict.get_item("account_id")?.extract()?;
119 let account_type: String = dict.get_item("account_type")?.extract()?;
120 let base_currency: String = dict.get_item("base_currency")?.extract()?;
121 let balances_list: Bound<'_, PyList> = dict.get_item("balances")?.extract()?;
122 let balances: Vec<AccountBalance> = balances_list
123 .iter()
124 .map(|b| {
125 let balance_dict = b.extract::<Bound<'_, PyDict>>()?;
126 AccountBalance::py_from_dict(&balance_dict)
127 })
128 .collect::<PyResult<Vec<AccountBalance>>>()?;
129 let margins_list: Bound<'_, PyList> = dict.get_item("margins")?.extract()?;
130 let margins: Vec<MarginBalance> = margins_list
131 .iter()
132 .map(|m| {
133 let margin_dict = m.extract::<Bound<'_, PyDict>>()?;
134 MarginBalance::py_from_dict(&margin_dict)
135 })
136 .collect::<PyResult<Vec<MarginBalance>>>()?;
137 let reported: bool = dict.get_item("reported")?.extract()?;
138 let event_id: String = dict.get_item("event_id")?.extract()?;
139 let ts_event: u64 = dict.get_item("ts_event")?.extract()?;
140 let ts_init: u64 = dict.get_item("ts_init")?.extract()?;
141 let account = Self::new(
142 AccountId::from(account_id.as_str()),
143 AccountType::from_str(account_type.as_str()).unwrap(),
144 balances,
145 margins,
146 reported,
147 UUID4::from_str(event_id.as_str()).unwrap(),
148 ts_event.into(),
149 ts_init.into(),
150 Some(Currency::from_str(base_currency.as_str()).map_err(to_pyvalue_err)?),
151 );
152 Ok(account)
153 }
154
155 #[pyo3(name = "to_dict")]
161 pub fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
162 let dict = PyDict::new(py);
163 dict.set_item("type", stringify!(AccountState))?;
164 dict.set_item("account_id", self.account_id.to_string())?;
165 dict.set_item("account_type", self.account_type.to_string())?;
166 let balances_dict: PyResult<Vec<_>> =
168 self.balances.iter().map(|b| b.py_to_dict(py)).collect();
169 let margins_dict: PyResult<Vec<_>> =
170 self.margins.iter().map(|m| m.py_to_dict(py)).collect();
171 dict.set_item("balances", balances_dict?)?;
172 dict.set_item("margins", margins_dict?)?;
173 dict.set_item("reported", self.is_reported)?;
174 dict.set_item("event_id", self.event_id.to_string())?;
175 dict.set_item("info", PyDict::new(py))?;
176 dict.set_item("ts_event", self.ts_event.as_u64())?;
177 dict.set_item("ts_init", self.ts_init.as_u64())?;
178 match self.base_currency {
179 Some(base_currency) => {
180 dict.set_item("base_currency", base_currency.code.to_string())?;
181 }
182 None => dict.set_item("base_currency", "None")?,
183 }
184 Ok(dict.into())
185 }
186}