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