nautilus_model/python/instruments/
equity.rs1use std::{
17 collections::hash_map::DefaultHasher,
18 hash::{Hash, Hasher},
19};
20
21use nautilus_core::{
22 from_pydict,
23 python::{IntoPyObjectNautilusExt, serialization::from_dict_pyo3, to_pyvalue_err},
24};
25use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
26use rust_decimal::Decimal;
27use ustr::Ustr;
28
29use crate::{
30 identifiers::{InstrumentId, Symbol},
31 instruments::Equity,
32 types::{Currency, Price, Quantity},
33};
34
35#[pymethods]
36#[pyo3_stub_gen::derive::gen_stub_pymethods]
37impl Equity {
38 #[expect(clippy::too_many_arguments)]
40 #[new]
41 #[pyo3(signature = (instrument_id, raw_symbol, currency, price_precision, price_increment, ts_event, ts_init, isin=None, lot_size=None, max_quantity=None, min_quantity=None, max_price=None, min_price=None, margin_init=None, margin_maint=None, maker_fee=None, taker_fee=None, info=None))]
42 fn py_new(
43 instrument_id: InstrumentId,
44 raw_symbol: Symbol,
45 currency: Currency,
46 price_precision: u8,
47 price_increment: Price,
48 ts_event: u64,
49 ts_init: u64,
50 isin: Option<String>,
51 lot_size: Option<Quantity>,
52 max_quantity: Option<Quantity>,
53 min_quantity: Option<Quantity>,
54 max_price: Option<Price>,
55 min_price: Option<Price>,
56 margin_init: Option<Decimal>,
57 margin_maint: Option<Decimal>,
58 maker_fee: Option<Decimal>,
59 taker_fee: Option<Decimal>,
60 info: Option<Py<PyDict>>,
61 ) -> PyResult<Self> {
62 let info_map = if let Some(info_dict) = info {
64 Python::attach(|py| from_pydict(py, info_dict))?
65 } else {
66 None
67 };
68
69 Self::new_checked(
70 instrument_id,
71 raw_symbol,
72 isin.map(|x| Ustr::from(&x)),
73 currency,
74 price_precision,
75 price_increment,
76 lot_size,
77 max_quantity,
78 min_quantity,
79 max_price,
80 min_price,
81 margin_init,
82 margin_maint,
83 maker_fee,
84 taker_fee,
85 info_map,
86 ts_event.into(),
87 ts_init.into(),
88 )
89 .map_err(to_pyvalue_err)
90 }
91
92 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
93 match op {
94 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
95 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
96 _ => py.NotImplemented(),
97 }
98 }
99
100 fn __hash__(&self) -> isize {
101 let mut hasher = DefaultHasher::new();
102 self.hash(&mut hasher);
103 hasher.finish() as isize
104 }
105
106 #[getter]
107 fn type_name(&self) -> &'static str {
108 stringify!(Equity)
109 }
110
111 #[getter]
112 #[pyo3(name = "id")]
113 fn py_id(&self) -> InstrumentId {
114 self.id
115 }
116
117 #[getter]
118 #[pyo3(name = "raw_symbol")]
119 fn py_raw_symbol(&self) -> Symbol {
120 self.raw_symbol
121 }
122
123 #[getter]
124 #[pyo3(name = "isin")]
125 fn py_isin(&self) -> Option<&str> {
126 match self.isin {
127 Some(isin) => Some(isin.as_str()),
128 None => None,
129 }
130 }
131
132 #[getter]
133 #[pyo3(name = "quote_currency")] fn py_quote_currency(&self) -> Currency {
135 self.currency
136 }
137
138 #[getter]
139 #[pyo3(name = "price_precision")]
140 fn py_price_precision(&self) -> u8 {
141 self.price_precision
142 }
143
144 #[getter]
145 #[pyo3(name = "size_precision")]
146 fn py_size_precision(&self) -> u8 {
147 0
148 }
149
150 #[getter]
151 #[pyo3(name = "price_increment")]
152 fn py_price_increment(&self) -> Price {
153 self.price_increment
154 }
155
156 #[getter]
157 #[pyo3(name = "size_increment")]
158 fn py_size_increment(&self) -> Quantity {
159 Quantity::from(1)
160 }
161
162 #[getter]
163 #[pyo3(name = "lot_size")]
164 fn py_lot_size(&self) -> Option<Quantity> {
165 self.lot_size
166 }
167
168 #[getter]
169 #[pyo3(name = "max_quantity")]
170 fn py_max_quantity(&self) -> Option<Quantity> {
171 self.max_quantity
172 }
173
174 #[getter]
175 #[pyo3(name = "min_quantity")]
176 fn py_min_quantity(&self) -> Option<Quantity> {
177 self.min_quantity
178 }
179
180 #[getter]
181 #[pyo3(name = "max_price")]
182 fn py_max_price(&self) -> Option<Price> {
183 self.max_price
184 }
185
186 #[getter]
187 #[pyo3(name = "min_price")]
188 fn py_min_price(&self) -> Option<Price> {
189 self.min_price
190 }
191
192 #[getter]
193 #[pyo3(name = "margin_init")]
194 fn py_margin_init(&self) -> Decimal {
195 self.margin_init
196 }
197
198 #[getter]
199 #[pyo3(name = "margin_maint")]
200 fn py_margin_maint(&self) -> Decimal {
201 self.margin_maint
202 }
203
204 #[getter]
205 #[pyo3(name = "maker_fee")]
206 fn py_maker_fee(&self) -> Decimal {
207 self.maker_fee
208 }
209
210 #[getter]
211 #[pyo3(name = "taker_fee")]
212 fn py_taker_fee(&self) -> Decimal {
213 self.taker_fee
214 }
215
216 #[getter]
217 #[pyo3(name = "ts_event")]
218 fn py_ts_event(&self) -> u64 {
219 self.ts_event.as_u64()
220 }
221
222 #[getter]
223 #[pyo3(name = "ts_init")]
224 fn py_ts_init(&self) -> u64 {
225 self.ts_init.as_u64()
226 }
227
228 #[getter]
229 #[pyo3(name = "info")]
230 fn py_info(&self, py: Python<'_>) -> PyResult<Py<PyDict>> {
231 if let Some(ref info_map) = self.info {
233 let py_dict = PyDict::new(py);
234
235 for (key, value) in info_map {
236 let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
238 let py_value =
239 PyModule::import(py, "json")?.call_method("loads", (json_str,), None)?;
240 py_dict.set_item(key, py_value)?;
241 }
242 Ok(py_dict.unbind())
243 } else {
244 Ok(PyDict::new(py).unbind())
245 }
246 }
247
248 #[staticmethod]
249 #[pyo3(name = "from_dict")]
250 fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
251 from_dict_pyo3(py, values)
252 }
253
254 #[pyo3(name = "to_dict")]
255 fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
256 let dict = PyDict::new(py);
257 dict.set_item("type", stringify!(Equity))?;
258 dict.set_item("id", self.id.to_string())?;
259 dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
260 dict.set_item("currency", self.currency.code.to_string())?;
261 dict.set_item("price_precision", self.price_precision)?;
262 dict.set_item("price_increment", self.price_increment.to_string())?;
263 dict.set_item("ts_event", self.ts_event.as_u64())?;
264 dict.set_item("ts_init", self.ts_init.as_u64())?;
265 if let Some(ref info_map) = self.info {
267 let info_dict = PyDict::new(py);
268
269 for (key, value) in info_map {
270 let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
271 let py_value =
272 PyModule::import(py, "json")?.call_method("loads", (json_str,), None)?;
273 info_dict.set_item(key, py_value)?;
274 }
275 dict.set_item("info", info_dict)?;
276 } else {
277 dict.set_item("info", PyDict::new(py))?;
278 }
279 dict.set_item("maker_fee", self.maker_fee.to_string())?;
280 dict.set_item("taker_fee", self.taker_fee.to_string())?;
281 dict.set_item("margin_init", self.margin_init.to_string())?;
282 dict.set_item("margin_maint", self.margin_maint.to_string())?;
283 match &self.isin {
284 Some(value) => dict.set_item("isin", value.to_string())?,
285 None => dict.set_item("isin", py.None())?,
286 }
287
288 match self.lot_size {
289 Some(value) => dict.set_item("lot_size", value.to_string())?,
290 None => dict.set_item("lot_size", py.None())?,
291 }
292
293 match self.max_quantity {
294 Some(value) => dict.set_item("max_quantity", value.to_string())?,
295 None => dict.set_item("max_quantity", py.None())?,
296 }
297
298 match self.min_quantity {
299 Some(value) => dict.set_item("min_quantity", value.to_string())?,
300 None => dict.set_item("min_quantity", py.None())?,
301 }
302
303 match self.max_price {
304 Some(value) => dict.set_item("max_price", value.to_string())?,
305 None => dict.set_item("max_price", py.None())?,
306 }
307
308 match self.min_price {
309 Some(value) => dict.set_item("min_price", value.to_string())?,
310 None => dict.set_item("min_price", py.None())?,
311 }
312 Ok(dict.into())
313 }
314}