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