nautilus_model/python/instruments/
crypto_future.rs1use std::{
17 collections::hash_map::DefaultHasher,
18 hash::{Hash, Hasher},
19};
20
21use nautilus_core::python::{serialization::from_dict_pyo3, to_pyvalue_err};
22use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
23use rust_decimal::Decimal;
24
25use crate::{
26 identifiers::{InstrumentId, Symbol},
27 instruments::CryptoFuture,
28 types::{Currency, Money, Price, Quantity},
29};
30
31#[pymethods]
32impl CryptoFuture {
33 #[allow(clippy::too_many_arguments)]
34 #[new]
35 #[pyo3(signature = (id, raw_symbol, underlying, quote_currency, settlement_currency, is_inverse, 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, maker_fee=None, taker_fee=None))]
36 fn py_new(
37 id: InstrumentId,
38 raw_symbol: Symbol,
39 underlying: Currency,
40 quote_currency: Currency,
41 settlement_currency: Currency,
42 is_inverse: bool,
43 activation_ns: u64,
44 expiration_ns: u64,
45 price_precision: u8,
46 size_precision: u8,
47 price_increment: Price,
48 size_increment: Quantity,
49 ts_event: u64,
50 ts_init: u64,
51 multiplier: Option<Quantity>,
52 lot_size: Option<Quantity>,
53 max_quantity: Option<Quantity>,
54 min_quantity: Option<Quantity>,
55 max_notional: Option<Money>,
56 min_notional: Option<Money>,
57 max_price: Option<Price>,
58 min_price: Option<Price>,
59 margin_init: Option<Decimal>,
60 margin_maint: Option<Decimal>,
61 maker_fee: Option<Decimal>,
62 taker_fee: Option<Decimal>,
63 ) -> PyResult<Self> {
64 Self::new_checked(
65 id,
66 raw_symbol,
67 underlying,
68 quote_currency,
69 settlement_currency,
70 is_inverse,
71 activation_ns.into(),
72 expiration_ns.into(),
73 price_precision,
74 size_precision,
75 price_increment,
76 size_increment,
77 multiplier,
78 lot_size,
79 max_quantity,
80 min_quantity,
81 max_notional,
82 min_notional,
83 max_price,
84 min_price,
85 margin_init,
86 margin_maint,
87 maker_fee,
88 taker_fee,
89 ts_event.into(),
90 ts_init.into(),
91 )
92 .map_err(to_pyvalue_err)
93 }
94
95 fn __hash__(&self) -> isize {
96 let mut hasher = DefaultHasher::new();
97 self.hash(&mut hasher);
98 hasher.finish() as isize
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(py),
104 _ => panic!("Not implemented"),
105 }
106 }
107
108 #[getter]
109 fn type_str(&self) -> &str {
110 stringify!(CryptoFuture)
111 }
112
113 #[getter]
114 #[pyo3(name = "id")]
115 fn py_id(&self) -> InstrumentId {
116 self.id
117 }
118
119 #[getter]
120 #[pyo3(name = "raw_symbol")]
121 fn py_raw_symbol(&self) -> Symbol {
122 self.raw_symbol
123 }
124
125 #[getter]
126 #[pyo3(name = "underlying")]
127 fn py_underlying(&self) -> Currency {
128 self.underlying
129 }
130
131 #[getter]
132 #[pyo3(name = "quote_currency")]
133 fn py_quote_currency(&self) -> Currency {
134 self.quote_currency
135 }
136
137 #[getter]
138 #[pyo3(name = "settlement_currency")]
139 fn py_settlement_currency(&self) -> Currency {
140 self.settlement_currency
141 }
142
143 #[getter]
144 #[pyo3(name = "is_inverse")]
145 fn py_is_inverse(&self) -> bool {
146 self.is_inverse
147 }
148
149 #[getter]
150 #[pyo3(name = "activation_ns")]
151 fn py_activation_ns(&self) -> u64 {
152 self.activation_ns.as_u64()
153 }
154
155 #[getter]
156 #[pyo3(name = "expiration_ns")]
157 fn py_expiration_ns(&self) -> u64 {
158 self.expiration_ns.as_u64()
159 }
160
161 #[getter]
162 #[pyo3(name = "price_precision")]
163 fn py_price_precision(&self) -> u8 {
164 self.price_precision
165 }
166
167 #[getter]
168 #[pyo3(name = "size_precision")]
169 fn py_size_precision(&self) -> u8 {
170 self.size_precision
171 }
172
173 #[getter]
174 #[pyo3(name = "price_increment")]
175 fn py_price_increment(&self) -> Price {
176 self.price_increment
177 }
178
179 #[getter]
180 #[pyo3(name = "size_increment")]
181 fn py_size_increment(&self) -> Quantity {
182 self.size_increment
183 }
184
185 #[getter]
186 #[pyo3(name = "multiplier")]
187 fn py_multiplier(&self) -> Quantity {
188 self.multiplier
189 }
190
191 #[getter]
192 #[pyo3(name = "lot_size")]
193 fn py_lot_size(&self) -> Option<Quantity> {
194 Some(self.lot_size)
195 }
196
197 #[getter]
198 #[pyo3(name = "max_quantity")]
199 fn py_max_quantity(&self) -> Option<Quantity> {
200 self.max_quantity
201 }
202
203 #[getter]
204 #[pyo3(name = "min_quantity")]
205 fn py_min_quantity(&self) -> Option<Quantity> {
206 self.min_quantity
207 }
208
209 #[getter]
210 #[pyo3(name = "max_notional")]
211 fn py_max_notional(&self) -> Option<Money> {
212 self.max_notional
213 }
214
215 #[getter]
216 #[pyo3(name = "min_notional")]
217 fn py_min_notional(&self) -> Option<Money> {
218 self.min_notional
219 }
220
221 #[getter]
222 #[pyo3(name = "max_price")]
223 fn py_max_price(&self) -> Option<Price> {
224 self.max_price
225 }
226
227 #[getter]
228 #[pyo3(name = "min_price")]
229 fn py_min_price(&self) -> Option<Price> {
230 self.min_price
231 }
232
233 #[getter]
234 #[pyo3(name = "margin_init")]
235 fn py_margin_init(&self) -> Decimal {
236 self.margin_init
237 }
238
239 #[getter]
240 #[pyo3(name = "margin_maint")]
241 fn py_margin_maint(&self) -> Decimal {
242 self.margin_maint
243 }
244
245 #[getter]
246 #[pyo3(name = "maker_fee")]
247 fn py_maker_fee(&self) -> Decimal {
248 self.maker_fee
249 }
250
251 #[getter]
252 #[pyo3(name = "taker_fee")]
253 fn py_taker_fee(&self) -> Decimal {
254 self.taker_fee
255 }
256
257 #[getter]
258 #[pyo3(name = "info")]
259 fn py_info(&self, py: Python<'_>) -> PyResult<PyObject> {
260 Ok(PyDict::new(py).into())
261 }
262
263 #[getter]
264 #[pyo3(name = "ts_event")]
265 fn py_ts_event(&self) -> u64 {
266 self.ts_event.as_u64()
267 }
268
269 #[getter]
270 #[pyo3(name = "ts_init")]
271 fn py_ts_init(&self) -> u64 {
272 self.ts_init.as_u64()
273 }
274
275 #[staticmethod]
276 #[pyo3(name = "from_dict")]
277 fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
278 from_dict_pyo3(py, values)
279 }
280
281 #[pyo3(name = "to_dict")]
282 fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
283 let dict = PyDict::new(py);
284 dict.set_item("type", stringify!(CryptoFuture))?;
285 dict.set_item("id", self.id.to_string())?;
286 dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
287 dict.set_item("underlying", self.underlying.code.to_string())?;
288 dict.set_item("quote_currency", self.quote_currency.code.to_string())?;
289 dict.set_item(
290 "settlement_currency",
291 self.settlement_currency.code.to_string(),
292 )?;
293 dict.set_item("is_inverse", self.is_inverse)?;
294 dict.set_item("activation_ns", self.activation_ns.as_u64())?;
295 dict.set_item("expiration_ns", self.expiration_ns.as_u64())?;
296 dict.set_item("price_precision", self.price_precision)?;
297 dict.set_item("size_precision", self.size_precision)?;
298 dict.set_item("price_increment", self.price_increment.to_string())?;
299 dict.set_item("size_increment", self.size_increment.to_string())?;
300 dict.set_item("margin_init", self.margin_init.to_string())?;
301 dict.set_item("margin_maint", self.margin_maint.to_string())?;
302 dict.set_item("multiplier", self.multiplier.to_string())?;
303 dict.set_item("lot_size", self.lot_size.to_string())?;
304 dict.set_item("info", PyDict::new(py))?;
305 dict.set_item("maker_fee", self.maker_fee.to_string())?;
306 dict.set_item("taker_fee", self.taker_fee.to_string())?;
307 dict.set_item("ts_event", self.ts_event.as_u64())?;
308 dict.set_item("ts_init", self.ts_init.as_u64())?;
309 match self.max_quantity {
310 Some(value) => dict.set_item("max_quantity", value.to_string())?,
311 None => dict.set_item("max_quantity", py.None())?,
312 }
313 match self.min_quantity {
314 Some(value) => dict.set_item("min_quantity", value.to_string())?,
315 None => dict.set_item("min_quantity", py.None())?,
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 match self.min_notional {
322 Some(value) => dict.set_item("min_notional", value.to_string())?,
323 None => dict.set_item("min_notional", py.None())?,
324 }
325 match self.max_price {
326 Some(value) => dict.set_item("max_price", value.to_string())?,
327 None => dict.set_item("max_price", py.None())?,
328 }
329 match self.min_price {
330 Some(value) => dict.set_item("min_price", value.to_string())?,
331 None => dict.set_item("min_price", py.None())?,
332 }
333 Ok(dict.into())
334 }
335}
336
337#[cfg(test)]
341mod tests {
342 use pyo3::{prelude::*, prepare_freethreaded_python, types::PyDict};
343 use rstest::rstest;
344
345 use crate::instruments::{stubs::*, CryptoFuture};
346
347 #[rstest]
348 fn test_dict_round_trip(crypto_future_btcusdt: CryptoFuture) {
349 prepare_freethreaded_python();
350 Python::with_gil(|py| {
351 let crypto_future = crypto_future_btcusdt;
352 let values = crypto_future.py_to_dict(py).unwrap();
353 let values: Py<PyDict> = values.extract(py).unwrap();
354 let new_crypto_future = CryptoFuture::py_from_dict(py, values).unwrap();
355 assert_eq!(crypto_future, new_crypto_future);
356 })
357 }
358}