nautilus_model/python/instruments/
equity.rs1use std::{
17 collections::hash_map::DefaultHasher,
18 hash::{Hash, Hasher},
19};
20
21use nautilus_core::python::{
22 IntoPyObjectNautilusExt, serialization::from_dict_pyo3, to_pyvalue_err,
23};
24use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
25use rust_decimal::Decimal;
26use ustr::Ustr;
27
28use crate::{
29 identifiers::{InstrumentId, Symbol},
30 instruments::Equity,
31 types::{Currency, Price, Quantity},
32};
33
34#[pymethods]
35impl Equity {
36 #[allow(clippy::too_many_arguments)]
37 #[new]
38 #[pyo3(signature = (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))]
39 fn py_new(
40 id: InstrumentId,
41 raw_symbol: Symbol,
42 currency: Currency,
43 price_precision: u8,
44 price_increment: Price,
45 ts_event: u64,
46 ts_init: u64,
47 isin: Option<String>,
48 lot_size: Option<Quantity>,
49 max_quantity: Option<Quantity>,
50 min_quantity: Option<Quantity>,
51 max_price: Option<Price>,
52 min_price: Option<Price>,
53 margin_init: Option<Decimal>,
54 margin_maint: Option<Decimal>,
55 maker_fee: Option<Decimal>,
56 taker_fee: Option<Decimal>,
57 ) -> PyResult<Self> {
58 Self::new_checked(
59 id,
60 raw_symbol,
61 isin.map(|x| Ustr::from(&x)),
62 currency,
63 price_precision,
64 price_increment,
65 lot_size,
66 max_quantity,
67 min_quantity,
68 max_price,
69 min_price,
70 margin_init,
71 margin_maint,
72 maker_fee,
73 taker_fee,
74 ts_event.into(),
75 ts_init.into(),
76 )
77 .map_err(to_pyvalue_err)
78 }
79
80 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
81 match op {
82 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
83 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
84 _ => py.NotImplemented(),
85 }
86 }
87
88 fn __hash__(&self) -> isize {
89 let mut hasher = DefaultHasher::new();
90 self.hash(&mut hasher);
91 hasher.finish() as isize
92 }
93
94 #[getter]
95 fn type_str(&self) -> &str {
96 stringify!(Equity)
97 }
98
99 #[getter]
100 #[pyo3(name = "id")]
101 fn py_id(&self) -> InstrumentId {
102 self.id
103 }
104
105 #[getter]
106 #[pyo3(name = "raw_symbol")]
107 fn py_raw_symbol(&self) -> Symbol {
108 self.raw_symbol
109 }
110
111 #[getter]
112 #[pyo3(name = "isin")]
113 fn py_isin(&self) -> Option<&str> {
114 match self.isin {
115 Some(isin) => Some(isin.as_str()),
116 None => None,
117 }
118 }
119
120 #[getter]
121 #[pyo3(name = "quote_currency")] fn py_quote_currency(&self) -> Currency {
123 self.currency
124 }
125
126 #[getter]
127 #[pyo3(name = "price_precision")]
128 fn py_price_precision(&self) -> u8 {
129 self.price_precision
130 }
131
132 #[getter]
133 #[pyo3(name = "size_precision")]
134 fn py_size_precision(&self) -> u8 {
135 0
136 }
137
138 #[getter]
139 #[pyo3(name = "price_increment")]
140 fn py_price_increment(&self) -> Price {
141 self.price_increment
142 }
143
144 #[getter]
145 #[pyo3(name = "size_increment")]
146 fn py_size_increment(&self) -> Quantity {
147 Quantity::from(1)
148 }
149
150 #[getter]
151 #[pyo3(name = "lot_size")]
152 fn py_lot_size(&self) -> Option<Quantity> {
153 self.lot_size
154 }
155
156 #[getter]
157 #[pyo3(name = "max_quantity")]
158 fn py_max_quantity(&self) -> Option<Quantity> {
159 self.max_quantity
160 }
161
162 #[getter]
163 #[pyo3(name = "min_quantity")]
164 fn py_min_quantity(&self) -> Option<Quantity> {
165 self.min_quantity
166 }
167
168 #[getter]
169 #[pyo3(name = "max_price")]
170 fn py_max_price(&self) -> Option<Price> {
171 self.max_price
172 }
173
174 #[getter]
175 #[pyo3(name = "min_price")]
176 fn py_min_price(&self) -> Option<Price> {
177 self.min_price
178 }
179
180 #[getter]
181 #[pyo3(name = "ts_event")]
182 fn py_ts_event(&self) -> u64 {
183 self.ts_event.as_u64()
184 }
185
186 #[getter]
187 #[pyo3(name = "ts_init")]
188 fn py_ts_init(&self) -> u64 {
189 self.ts_init.as_u64()
190 }
191
192 #[getter]
193 #[pyo3(name = "info")]
194 fn py_info(&self, py: Python<'_>) -> PyResult<PyObject> {
195 Ok(PyDict::new(py).into())
196 }
197
198 #[staticmethod]
199 #[pyo3(name = "from_dict")]
200 fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
201 from_dict_pyo3(py, values)
202 }
203
204 #[pyo3(name = "to_dict")]
205 fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
206 let dict = PyDict::new(py);
207 dict.set_item("type", stringify!(Equity))?;
208 dict.set_item("id", self.id.to_string())?;
209 dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
210 dict.set_item("currency", self.currency.code.to_string())?;
211 dict.set_item("price_precision", self.price_precision)?;
212 dict.set_item("price_increment", self.price_increment.to_string())?;
213 dict.set_item("ts_event", self.ts_event.as_u64())?;
214 dict.set_item("ts_init", self.ts_init.as_u64())?;
215 dict.set_item("info", PyDict::new(py))?;
216 dict.set_item("maker_fee", self.maker_fee.to_string())?;
217 dict.set_item("taker_fee", self.taker_fee.to_string())?;
218 dict.set_item("margin_init", self.margin_init.to_string())?;
219 dict.set_item("margin_maint", self.margin_maint.to_string())?;
220 match &self.isin {
221 Some(value) => dict.set_item("isin", value.to_string())?,
222 None => dict.set_item("isin", py.None())?,
223 }
224 match self.lot_size {
225 Some(value) => dict.set_item("lot_size", value.to_string())?,
226 None => dict.set_item("lot_size", py.None())?,
227 }
228 match self.max_quantity {
229 Some(value) => dict.set_item("max_quantity", value.to_string())?,
230 None => dict.set_item("max_quantity", py.None())?,
231 }
232 match self.min_quantity {
233 Some(value) => dict.set_item("min_quantity", value.to_string())?,
234 None => dict.set_item("min_quantity", py.None())?,
235 }
236 match self.max_price {
237 Some(value) => dict.set_item("max_price", value.to_string())?,
238 None => dict.set_item("max_price", py.None())?,
239 }
240 match self.min_price {
241 Some(value) => dict.set_item("min_price", value.to_string())?,
242 None => dict.set_item("min_price", py.None())?,
243 }
244 Ok(dict.into())
245 }
246}