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