nautilus_model/python/instruments/
futures_contract.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 enums::AssetClass,
31 identifiers::{InstrumentId, Symbol},
32 instruments::FuturesContract,
33 types::{Currency, Price, Quantity},
34};
35
36#[pymethods]
37#[pyo3_stub_gen::derive::gen_stub_pymethods]
38impl FuturesContract {
39 #[expect(clippy::too_many_arguments)]
41 #[new]
42 #[pyo3(signature = (instrument_id, raw_symbol, asset_class, underlying, activation_ns, expiration_ns, currency, price_precision, price_increment, multiplier, lot_size, ts_event, ts_init, max_quantity=None, min_quantity=None, max_price=None, min_price=None, margin_init=None, margin_maint=None, exchange=None, tick_scheme=None, info=None))]
43 fn py_new(
44 instrument_id: InstrumentId,
45 raw_symbol: Symbol,
46 asset_class: AssetClass,
47 underlying: String,
48 activation_ns: u64,
49 expiration_ns: u64,
50 currency: Currency,
51 price_precision: u8,
52 price_increment: Price,
53 multiplier: Quantity,
54 lot_size: Quantity,
55 ts_event: u64,
56 ts_init: u64,
57 max_quantity: Option<Quantity>,
58 min_quantity: Option<Quantity>,
59 max_price: Option<Price>,
60 min_price: Option<Price>,
61 margin_init: Option<Decimal>,
62 margin_maint: Option<Decimal>,
63 exchange: Option<String>,
64 tick_scheme: Option<String>,
65 info: Option<Py<PyDict>>,
66 ) -> PyResult<Self> {
67 let info_map = if let Some(info_dict) = info {
69 Python::attach(|py| from_pydict(py, &info_dict))?
70 } else {
71 None
72 };
73
74 Self::builder()
75 .instrument_id(instrument_id)
76 .raw_symbol(raw_symbol)
77 .asset_class(asset_class)
78 .maybe_exchange(exchange.map(|x| Ustr::from(&x)))
79 .underlying(underlying.into())
80 .activation_ns(activation_ns.into())
81 .expiration_ns(expiration_ns.into())
82 .currency(currency)
83 .price_precision(price_precision)
84 .price_increment(price_increment)
85 .multiplier(multiplier)
86 .lot_size(lot_size)
87 .maybe_max_quantity(max_quantity)
88 .maybe_min_quantity(min_quantity)
89 .maybe_max_price(max_price)
90 .maybe_min_price(min_price)
91 .maybe_margin_init(margin_init)
92 .maybe_margin_maint(margin_maint)
93 .maybe_tick_scheme(tick_scheme.map(|name| ustr::Ustr::from(name.as_str())))
94 .maybe_info(info_map)
95 .ts_event(ts_event.into())
96 .ts_init(ts_init.into())
97 .build()
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!(FuturesContract)
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 = "exchange")]
140 fn py_exchange(&self) -> Option<String> {
141 self.exchange.map(|e| e.to_string())
142 }
143
144 #[getter]
145 #[pyo3(name = "underlying")]
146 fn py_underlying(&self) -> &str {
147 self.underlying.as_str()
148 }
149
150 #[getter]
151 #[pyo3(name = "activation_ns")]
152 fn py_activation_ns(&self) -> u64 {
153 self.activation_ns.as_u64()
154 }
155
156 #[getter]
157 #[pyo3(name = "expiration_ns")]
158 fn py_expiration_ns(&self) -> u64 {
159 self.expiration_ns.as_u64()
160 }
161
162 #[getter]
163 #[pyo3(name = "currency")]
164 fn py_currency(&self) -> Currency {
165 self.currency
166 }
167
168 #[getter]
169 #[pyo3(name = "price_precision")]
170 fn py_price_precision(&self) -> u8 {
171 self.price_precision
172 }
173
174 #[getter]
175 #[pyo3(name = "price_increment")]
176 fn py_price_increment(&self) -> Price {
177 self.price_increment
178 }
179
180 #[getter]
181 #[pyo3(name = "multiplier")]
182 fn py_multiplier(&self) -> Quantity {
183 self.multiplier
184 }
185
186 #[getter]
187 #[pyo3(name = "lot_size")]
188 fn py_lot_size(&self) -> Quantity {
189 self.lot_size
190 }
191
192 #[getter]
193 #[pyo3(name = "max_quantity")]
194 fn py_max_quantity(&self) -> Option<Quantity> {
195 self.max_quantity
196 }
197
198 #[getter]
199 #[pyo3(name = "min_quantity")]
200 fn py_min_quantity(&self) -> Option<Quantity> {
201 self.min_quantity
202 }
203
204 #[getter]
205 #[pyo3(name = "max_price")]
206 fn py_max_price(&self) -> Option<Price> {
207 self.max_price
208 }
209
210 #[getter]
211 #[pyo3(name = "min_price")]
212 fn py_min_price(&self) -> Option<Price> {
213 self.min_price
214 }
215
216 #[getter]
217 #[pyo3(name = "size_increment")]
218 fn py_size_increment(&self) -> Quantity {
219 self.size_increment
220 }
221
222 #[getter]
223 #[pyo3(name = "size_precision")]
224 fn py_size_precision(&self) -> u8 {
225 self.size_precision
226 }
227
228 #[getter]
229 #[pyo3(name = "ts_event")]
230 fn py_ts_event(&self) -> u64 {
231 self.ts_event.as_u64()
232 }
233
234 #[getter]
235 #[pyo3(name = "ts_init")]
236 fn py_ts_init(&self) -> u64 {
237 self.ts_init.as_u64()
238 }
239
240 #[getter]
241 #[pyo3(name = "margin_init")]
242 fn py_margin_init(&self) -> Decimal {
243 self.margin_init
244 }
245
246 #[getter]
247 #[pyo3(name = "margin_maint")]
248 fn py_margin_maint(&self) -> Decimal {
249 self.margin_maint
250 }
251
252 #[getter]
253 #[pyo3(name = "info")]
254 fn py_info(&self, py: Python<'_>) -> PyResult<Py<PyDict>> {
255 if let Some(ref info_map) = self.info {
257 let py_dict = PyDict::new(py);
258
259 for (key, value) in info_map {
260 let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
262 let py_value =
263 PyModule::import(py, "json")?.call_method("loads", (json_str,), None)?;
264 py_dict.set_item(key, py_value)?;
265 }
266 Ok(py_dict.unbind())
267 } else {
268 Ok(PyDict::new(py).unbind())
269 }
270 }
271
272 #[staticmethod]
273 #[pyo3(name = "from_dict")]
274 fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
275 crate::python::instruments::from_dict_instrument_pyo3(py, values)
276 }
277
278 #[pyo3(name = "to_dict")]
279 fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
280 let dict = PyDict::new(py);
281 dict.set_item("type", stringify!(FuturesContract))?;
282 dict.set_item("id", self.id.to_string())?;
283 dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
284 dict.set_item("asset_class", self.asset_class.to_string())?;
285 dict.set_item("underlying", self.underlying.to_string())?;
286 dict.set_item("activation_ns", self.activation_ns.as_u64())?;
287 dict.set_item("expiration_ns", self.expiration_ns.as_u64())?;
288 dict.set_item("currency", self.currency.code.to_string())?;
289 dict.set_item("price_precision", self.price_precision)?;
290 dict.set_item("price_increment", self.price_increment.to_string())?;
291 dict.set_item("size_increment", self.size_increment.to_string())?;
292 dict.set_item("size_precision", self.size_precision)?;
293 dict.set_item("multiplier", self.multiplier.to_string())?;
294 dict.set_item("lot_size", self.lot_size.to_string())?;
295 dict.set_item("margin_init", self.margin_init.to_string())?;
296 dict.set_item("margin_maint", self.margin_maint.to_string())?;
297 if let Some(ref info_map) = self.info {
299 let info_dict = PyDict::new(py);
300
301 for (key, value) in info_map {
302 let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
303 let py_value =
304 PyModule::import(py, "json")?.call_method("loads", (json_str,), None)?;
305 info_dict.set_item(key, py_value)?;
306 }
307 dict.set_item("info", info_dict)?;
308 } else {
309 dict.set_item("info", PyDict::new(py))?;
310 }
311 dict.set_item("ts_event", self.ts_event.as_u64())?;
312 dict.set_item("ts_init", self.ts_init.as_u64())?;
313 match self.max_quantity {
314 Some(value) => dict.set_item("max_quantity", value.to_string())?,
315 None => dict.set_item("max_quantity", py.None())?,
316 }
317
318 match self.min_quantity {
319 Some(value) => dict.set_item("min_quantity", value.to_string())?,
320 None => dict.set_item("min_quantity", py.None())?,
321 }
322
323 match self.max_price {
324 Some(value) => dict.set_item("max_price", value.to_string())?,
325 None => dict.set_item("max_price", py.None())?,
326 }
327
328 match self.min_price {
329 Some(value) => dict.set_item("min_price", value.to_string())?,
330 None => dict.set_item("min_price", py.None())?,
331 }
332
333 match self.exchange {
334 Some(value) => dict.set_item("exchange", value.to_string())?,
335 None => dict.set_item("exchange", py.None())?,
336 }
337 dict.set_item(
338 "tick_scheme",
339 crate::python::instruments::tick_scheme_to_py(self),
340 )?;
341 Ok(dict.into())
342 }
343}