nautilus_model/python/instruments/
crypto_futures_spread.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 identifiers::{InstrumentId, Symbol},
31 instruments::CryptoFuturesSpread,
32 python::instruments::register_crypto_currencies_from_dict,
33 types::{Currency, Money, Price, Quantity},
34};
35
36#[pymethods]
37#[pyo3_stub_gen::derive::gen_stub_pymethods]
38impl CryptoFuturesSpread {
39 #[expect(clippy::too_many_arguments)]
42 #[new]
43 #[pyo3(signature = (instrument_id, raw_symbol, underlying, quote_currency, settlement_currency, is_inverse, strategy_type, activation_ns, expiration_ns, price_precision, size_precision, price_increment, size_increment,ts_event, ts_init, multiplier=None, 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))]
44 fn py_new(
45 instrument_id: InstrumentId,
46 raw_symbol: Symbol,
47 underlying: Currency,
48 quote_currency: Currency,
49 settlement_currency: Currency,
50 is_inverse: bool,
51 strategy_type: &str,
52 activation_ns: u64,
53 expiration_ns: u64,
54 price_precision: u8,
55 size_precision: u8,
56 price_increment: Price,
57 size_increment: Quantity,
58 ts_event: u64,
59 ts_init: u64,
60 multiplier: Option<Quantity>,
61 lot_size: Option<Quantity>,
62 max_quantity: Option<Quantity>,
63 min_quantity: Option<Quantity>,
64 max_notional: Option<Money>,
65 min_notional: Option<Money>,
66 max_price: Option<Price>,
67 min_price: Option<Price>,
68 margin_init: Option<Decimal>,
69 margin_maint: Option<Decimal>,
70 tick_scheme: Option<String>,
71 info: Option<Py<PyDict>>,
72 ) -> PyResult<Self> {
73 let info_map = if let Some(info_dict) = info {
74 Python::attach(|py| from_pydict(py, &info_dict))?
75 } else {
76 None
77 };
78
79 Self::builder()
80 .instrument_id(instrument_id)
81 .raw_symbol(raw_symbol)
82 .underlying(underlying)
83 .quote_currency(quote_currency)
84 .settlement_currency(settlement_currency)
85 .is_inverse(is_inverse)
86 .strategy_type(Ustr::from(strategy_type))
87 .activation_ns(activation_ns.into())
88 .expiration_ns(expiration_ns.into())
89 .price_precision(price_precision)
90 .size_precision(size_precision)
91 .price_increment(price_increment)
92 .size_increment(size_increment)
93 .maybe_multiplier(multiplier)
94 .maybe_lot_size(lot_size)
95 .maybe_max_quantity(max_quantity)
96 .maybe_min_quantity(min_quantity)
97 .maybe_max_notional(max_notional)
98 .maybe_min_notional(min_notional)
99 .maybe_max_price(max_price)
100 .maybe_min_price(min_price)
101 .maybe_margin_init(margin_init)
102 .maybe_margin_maint(margin_maint)
103 .maybe_tick_scheme(tick_scheme.map(|name| ustr::Ustr::from(name.as_str())))
104 .maybe_info(info_map)
105 .ts_event(ts_event.into())
106 .ts_init(ts_init.into())
107 .build()
108 .map_err(to_pyvalue_err)
109 }
110
111 fn __hash__(&self) -> isize {
112 let mut hasher = DefaultHasher::new();
113 self.hash(&mut hasher);
114 hasher.finish() as isize
115 }
116
117 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
118 match op {
119 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
120 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
121 _ => py.NotImplemented(),
122 }
123 }
124
125 #[getter]
126 fn type_name(&self) -> &'static str {
127 stringify!(CryptoFuturesSpread)
128 }
129
130 #[getter]
131 #[pyo3(name = "id")]
132 fn py_id(&self) -> InstrumentId {
133 self.id
134 }
135
136 #[getter]
137 #[pyo3(name = "raw_symbol")]
138 fn py_raw_symbol(&self) -> Symbol {
139 self.raw_symbol
140 }
141
142 #[getter]
143 #[pyo3(name = "underlying")]
144 fn py_underlying(&self) -> Currency {
145 self.underlying
146 }
147
148 #[getter]
149 #[pyo3(name = "quote_currency")]
150 fn py_quote_currency(&self) -> Currency {
151 self.quote_currency
152 }
153
154 #[getter]
155 #[pyo3(name = "settlement_currency")]
156 fn py_settlement_currency(&self) -> Currency {
157 self.settlement_currency
158 }
159
160 #[getter]
161 #[pyo3(name = "is_inverse")]
162 fn py_is_inverse(&self) -> bool {
163 self.is_inverse
164 }
165
166 #[getter]
167 #[pyo3(name = "strategy_type")]
168 fn py_strategy_type(&self) -> String {
169 self.strategy_type.to_string()
170 }
171
172 #[getter]
173 #[pyo3(name = "activation_ns")]
174 fn py_activation_ns(&self) -> u64 {
175 self.activation_ns.as_u64()
176 }
177
178 #[getter]
179 #[pyo3(name = "expiration_ns")]
180 fn py_expiration_ns(&self) -> u64 {
181 self.expiration_ns.as_u64()
182 }
183
184 #[getter]
185 #[pyo3(name = "price_precision")]
186 fn py_price_precision(&self) -> u8 {
187 self.price_precision
188 }
189
190 #[getter]
191 #[pyo3(name = "size_precision")]
192 fn py_size_precision(&self) -> u8 {
193 self.size_precision
194 }
195
196 #[getter]
197 #[pyo3(name = "price_increment")]
198 fn py_price_increment(&self) -> Price {
199 self.price_increment
200 }
201
202 #[getter]
203 #[pyo3(name = "size_increment")]
204 fn py_size_increment(&self) -> Quantity {
205 self.size_increment
206 }
207
208 #[getter]
209 #[pyo3(name = "multiplier")]
210 fn py_multiplier(&self) -> Quantity {
211 self.multiplier
212 }
213
214 #[getter]
215 #[pyo3(name = "lot_size")]
216 fn py_lot_size(&self) -> Quantity {
217 self.lot_size
218 }
219
220 #[getter]
221 #[pyo3(name = "max_quantity")]
222 fn py_max_quantity(&self) -> Option<Quantity> {
223 self.max_quantity
224 }
225
226 #[getter]
227 #[pyo3(name = "min_quantity")]
228 fn py_min_quantity(&self) -> Option<Quantity> {
229 self.min_quantity
230 }
231
232 #[getter]
233 #[pyo3(name = "max_notional")]
234 fn py_max_notional(&self) -> Option<Money> {
235 self.max_notional
236 }
237
238 #[getter]
239 #[pyo3(name = "min_notional")]
240 fn py_min_notional(&self) -> Option<Money> {
241 self.min_notional
242 }
243
244 #[getter]
245 #[pyo3(name = "max_price")]
246 fn py_max_price(&self) -> Option<Price> {
247 self.max_price
248 }
249
250 #[getter]
251 #[pyo3(name = "min_price")]
252 fn py_min_price(&self) -> Option<Price> {
253 self.min_price
254 }
255
256 #[getter]
257 #[pyo3(name = "margin_init")]
258 fn py_margin_init(&self) -> Decimal {
259 self.margin_init
260 }
261
262 #[getter]
263 #[pyo3(name = "margin_maint")]
264 fn py_margin_maint(&self) -> Decimal {
265 self.margin_maint
266 }
267
268 #[getter]
269 #[pyo3(name = "info")]
270 fn py_info(&self, py: Python<'_>) -> PyResult<Py<PyDict>> {
271 if let Some(ref info_map) = self.info {
272 let py_dict = PyDict::new(py);
273
274 for (key, value) in info_map {
275 let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
276 let py_value =
277 PyModule::import(py, "json")?.call_method("loads", (json_str,), None)?;
278 py_dict.set_item(key, py_value)?;
279 }
280 Ok(py_dict.unbind())
281 } else {
282 Ok(PyDict::new(py).unbind())
283 }
284 }
285
286 #[getter]
287 #[pyo3(name = "ts_event")]
288 fn py_ts_event(&self) -> u64 {
289 self.ts_event.as_u64()
290 }
291
292 #[getter]
293 #[pyo3(name = "ts_init")]
294 fn py_ts_init(&self) -> u64 {
295 self.ts_init.as_u64()
296 }
297
298 #[staticmethod]
299 #[pyo3(name = "from_dict")]
300 fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
301 register_crypto_currencies_from_dict(py, &values, &["underlying"]);
302 crate::python::instruments::from_dict_instrument_pyo3(py, values)
303 }
304
305 #[pyo3(name = "to_dict")]
306 fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
307 let dict = PyDict::new(py);
308 dict.set_item("type", stringify!(CryptoFuturesSpread))?;
309 dict.set_item("id", self.id.to_string())?;
310 dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
311 dict.set_item("underlying", self.underlying.code.to_string())?;
312 dict.set_item("quote_currency", self.quote_currency.code.to_string())?;
313 dict.set_item(
314 "settlement_currency",
315 self.settlement_currency.code.to_string(),
316 )?;
317 dict.set_item("is_inverse", self.is_inverse)?;
318 dict.set_item("strategy_type", self.strategy_type.to_string())?;
319 dict.set_item("activation_ns", self.activation_ns.as_u64())?;
320 dict.set_item("expiration_ns", self.expiration_ns.as_u64())?;
321 dict.set_item("price_precision", self.price_precision)?;
322 dict.set_item("size_precision", self.size_precision)?;
323 dict.set_item("price_increment", self.price_increment.to_string())?;
324 dict.set_item("size_increment", self.size_increment.to_string())?;
325 dict.set_item("margin_init", self.margin_init.to_string())?;
326 dict.set_item("margin_maint", self.margin_maint.to_string())?;
327 dict.set_item("multiplier", self.multiplier.to_string())?;
328 dict.set_item("lot_size", self.lot_size.to_string())?;
329
330 if let Some(ref info_map) = self.info {
331 let info_dict = PyDict::new(py);
332
333 for (key, value) in info_map {
334 let json_str = serde_json::to_string(value).map_err(to_pyvalue_err)?;
335 let py_value =
336 PyModule::import(py, "json")?.call_method("loads", (json_str,), None)?;
337 info_dict.set_item(key, py_value)?;
338 }
339 dict.set_item("info", info_dict)?;
340 } else {
341 dict.set_item("info", PyDict::new(py))?;
342 }
343 dict.set_item("ts_event", self.ts_event.as_u64())?;
344 dict.set_item("ts_init", self.ts_init.as_u64())?;
345
346 match self.max_quantity {
347 Some(value) => dict.set_item("max_quantity", value.to_string())?,
348 None => dict.set_item("max_quantity", py.None())?,
349 }
350
351 match self.min_quantity {
352 Some(value) => dict.set_item("min_quantity", value.to_string())?,
353 None => dict.set_item("min_quantity", py.None())?,
354 }
355
356 match self.max_notional {
357 Some(value) => dict.set_item("max_notional", value.to_string())?,
358 None => dict.set_item("max_notional", py.None())?,
359 }
360
361 match self.min_notional {
362 Some(value) => dict.set_item("min_notional", value.to_string())?,
363 None => dict.set_item("min_notional", py.None())?,
364 }
365
366 match self.max_price {
367 Some(value) => dict.set_item("max_price", value.to_string())?,
368 None => dict.set_item("max_price", py.None())?,
369 }
370
371 match self.min_price {
372 Some(value) => dict.set_item("min_price", value.to_string())?,
373 None => dict.set_item("min_price", py.None())?,
374 }
375 dict.set_item(
376 "tick_scheme",
377 crate::python::instruments::tick_scheme_to_py(self),
378 )?;
379 Ok(dict.into())
380 }
381}
382
383#[cfg(test)]
384mod tests {
385 use pyo3::{prelude::*, types::PyDict};
386 use rstest::rstest;
387
388 use crate::instruments::{CryptoFuturesSpread, stubs::*};
389
390 #[rstest]
391 fn test_dict_round_trip(crypto_futures_spread_btc_deribit: CryptoFuturesSpread) {
392 Python::initialize();
393 Python::attach(|py| {
394 let original = crypto_futures_spread_btc_deribit;
395 let values = original.py_to_dict(py).unwrap();
396 let values: Py<PyDict> = values.extract(py).unwrap();
397 let restored = CryptoFuturesSpread::py_from_dict(py, values).unwrap();
398 assert_eq!(original, restored);
399 });
400 }
401}