nautilus_model/python/instruments/
commodity.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;
27
28use crate::{
29 enums::AssetClass,
30 identifiers::{InstrumentId, Symbol},
31 instruments::Commodity,
32 types::{Currency, Money, Price, Quantity},
33};
34
35#[pymethods]
36#[pyo3_stub_gen::derive::gen_stub_pymethods]
37impl Commodity {
38 #[expect(clippy::too_many_arguments)]
40 #[new]
41 #[pyo3(signature = (instrument_id, raw_symbol, asset_class, quote_currency, price_precision, size_precision, price_increment, size_increment, ts_event, ts_init, lot_size=None, max_quantity=None, min_quantity=None, max_notional=None, min_notional=None, max_price=None, min_price=None, margin_init=None, margin_maint=None, tick_scheme=None, info=None))]
42 fn py_new(
43 instrument_id: InstrumentId,
44 raw_symbol: Symbol,
45 asset_class: AssetClass,
46 quote_currency: Currency,
47 price_precision: u8,
48 size_precision: u8,
49 price_increment: Price,
50 size_increment: Quantity,
51 ts_event: u64,
52 ts_init: u64,
53 lot_size: Option<Quantity>,
54 max_quantity: Option<Quantity>,
55 min_quantity: Option<Quantity>,
56 max_notional: Option<Money>,
57 min_notional: Option<Money>,
58 max_price: Option<Price>,
59 min_price: Option<Price>,
60 margin_init: Option<Decimal>,
61 margin_maint: Option<Decimal>,
62 tick_scheme: Option<String>,
63 info: Option<Py<PyDict>>,
64 ) -> PyResult<Self> {
65 let info_map = if let Some(info_dict) = info {
66 Python::attach(|py| from_pydict(py, &info_dict))?
67 } else {
68 None
69 };
70
71 Self::builder()
72 .instrument_id(instrument_id)
73 .raw_symbol(raw_symbol)
74 .asset_class(asset_class)
75 .quote_currency(quote_currency)
76 .price_precision(price_precision)
77 .size_precision(size_precision)
78 .price_increment(price_increment)
79 .size_increment(size_increment)
80 .maybe_lot_size(lot_size)
81 .maybe_max_quantity(max_quantity)
82 .maybe_min_quantity(min_quantity)
83 .maybe_max_notional(max_notional)
84 .maybe_min_notional(min_notional)
85 .maybe_max_price(max_price)
86 .maybe_min_price(min_price)
87 .maybe_margin_init(margin_init)
88 .maybe_margin_maint(margin_maint)
89 .maybe_tick_scheme(tick_scheme.map(|name| ustr::Ustr::from(name.as_str())))
90 .maybe_info(info_map)
91 .ts_event(ts_event.into())
92 .ts_init(ts_init.into())
93 .build()
94 .map_err(to_pyvalue_err)
95 }
96
97 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
98 match op {
99 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
100 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
101 _ => py.NotImplemented(),
102 }
103 }
104
105 fn __hash__(&self) -> isize {
106 let mut hasher = DefaultHasher::new();
107 self.hash(&mut hasher);
108 hasher.finish() as isize
109 }
110
111 #[getter]
112 fn type_name(&self) -> &'static str {
113 stringify!(Commodity)
114 }
115
116 #[getter]
117 #[pyo3(name = "id")]
118 fn py_id(&self) -> InstrumentId {
119 self.id
120 }
121
122 #[getter]
123 #[pyo3(name = "raw_symbol")]
124 fn py_raw_symbol(&self) -> Symbol {
125 self.raw_symbol
126 }
127
128 #[getter]
129 #[pyo3(name = "asset_class")]
130 fn py_asset_class(&self) -> AssetClass {
131 self.asset_class
132 }
133
134 #[getter]
135 #[pyo3(name = "quote_currency")]
136 fn py_quote_currency(&self) -> Currency {
137 self.quote_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 self.size_precision
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 self.size_increment
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_notional")]
184 fn py_max_notional(&self) -> Option<Money> {
185 self.max_notional
186 }
187
188 #[getter]
189 #[pyo3(name = "min_notional")]
190 fn py_min_notional(&self) -> Option<Money> {
191 self.min_notional
192 }
193
194 #[getter]
195 #[pyo3(name = "max_price")]
196 fn py_max_price(&self) -> Option<Price> {
197 self.max_price
198 }
199
200 #[getter]
201 #[pyo3(name = "min_price")]
202 fn py_min_price(&self) -> Option<Price> {
203 self.min_price
204 }
205
206 #[getter]
207 #[pyo3(name = "margin_init")]
208 fn py_margin_init(&self) -> Decimal {
209 self.margin_init
210 }
211
212 #[getter]
213 #[pyo3(name = "margin_maint")]
214 fn py_margin_maint(&self) -> Decimal {
215 self.margin_maint
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 {
234 let py_dict = PyDict::new(py);
235
236 for (key, value) in info_map {
237 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 crate::python::instruments::from_dict_instrument_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!(Commodity))?;
258 dict.set_item("id", self.id.to_string())?;
259 dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
260 dict.set_item("asset_class", self.asset_class.to_string())?;
261 dict.set_item("quote_currency", self.quote_currency.code.to_string())?;
262 dict.set_item("price_precision", self.price_precision)?;
263 dict.set_item("size_precision", self.size_precision)?;
264 dict.set_item("price_increment", self.price_increment.to_string())?;
265 dict.set_item("size_increment", self.size_increment.to_string())?;
266 dict.set_item("ts_event", self.ts_event.as_u64())?;
267 dict.set_item("ts_init", self.ts_init.as_u64())?;
268
269 if let Some(ref info_map) = self.info {
270 let info_dict = PyDict::new(py);
271
272 for (key, value) in info_map {
273 let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
274 let py_value =
275 PyModule::import(py, "json")?.call_method("loads", (json_str,), None)?;
276 info_dict.set_item(key, py_value)?;
277 }
278 dict.set_item("info", info_dict)?;
279 } else {
280 dict.set_item("info", PyDict::new(py))?;
281 }
282 dict.set_item("margin_init", self.margin_init.to_string())?;
283 dict.set_item("margin_maint", self.margin_maint.to_string())?;
284 match self.lot_size {
285 Some(value) => dict.set_item("lot_size", value.to_string())?,
286 None => dict.set_item("lot_size", py.None())?,
287 }
288
289 match self.max_quantity {
290 Some(value) => dict.set_item("max_quantity", value.to_string())?,
291 None => dict.set_item("max_quantity", py.None())?,
292 }
293
294 match self.min_quantity {
295 Some(value) => dict.set_item("min_quantity", value.to_string())?,
296 None => dict.set_item("min_quantity", py.None())?,
297 }
298
299 match self.max_notional {
300 Some(value) => dict.set_item("max_notional", value.to_string())?,
301 None => dict.set_item("max_notional", py.None())?,
302 }
303
304 match self.min_notional {
305 Some(value) => dict.set_item("min_notional", value.to_string())?,
306 None => dict.set_item("min_notional", py.None())?,
307 }
308
309 match self.max_price {
310 Some(value) => dict.set_item("max_price", value.to_string())?,
311 None => dict.set_item("max_price", py.None())?,
312 }
313
314 match self.min_price {
315 Some(value) => dict.set_item("min_price", value.to_string())?,
316 None => dict.set_item("min_price", py.None())?,
317 }
318 dict.set_item(
319 "tick_scheme",
320 crate::python::instruments::tick_scheme_to_py(self),
321 )?;
322 Ok(dict.into())
323 }
324}