nautilus_model/python/instruments/
crypto_future.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::{
17    collections::hash_map::DefaultHasher,
18    hash::{Hash, Hasher},
19};
20
21use nautilus_core::python::{
22    IntoPyObjectNautilusExt, serialization::from_dict_pyo3, to_pyvalue_err,
23};
24use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
25use rust_decimal::Decimal;
26
27use crate::{
28    identifiers::{InstrumentId, Symbol},
29    instruments::CryptoFuture,
30    types::{Currency, Money, Price, Quantity},
31};
32
33#[pymethods]
34impl CryptoFuture {
35    #[allow(clippy::too_many_arguments)]
36    #[new]
37    #[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))]
38    fn py_new(
39        id: InstrumentId,
40        raw_symbol: Symbol,
41        underlying: Currency,
42        quote_currency: Currency,
43        settlement_currency: Currency,
44        is_inverse: bool,
45        activation_ns: u64,
46        expiration_ns: u64,
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        multiplier: Option<Quantity>,
54        lot_size: Option<Quantity>,
55        max_quantity: Option<Quantity>,
56        min_quantity: Option<Quantity>,
57        max_notional: Option<Money>,
58        min_notional: Option<Money>,
59        max_price: Option<Price>,
60        min_price: Option<Price>,
61        margin_init: Option<Decimal>,
62        margin_maint: Option<Decimal>,
63        maker_fee: Option<Decimal>,
64        taker_fee: Option<Decimal>,
65    ) -> PyResult<Self> {
66        Self::new_checked(
67            id,
68            raw_symbol,
69            underlying,
70            quote_currency,
71            settlement_currency,
72            is_inverse,
73            activation_ns.into(),
74            expiration_ns.into(),
75            price_precision,
76            size_precision,
77            price_increment,
78            size_increment,
79            multiplier,
80            lot_size,
81            max_quantity,
82            min_quantity,
83            max_notional,
84            min_notional,
85            max_price,
86            min_price,
87            margin_init,
88            margin_maint,
89            maker_fee,
90            taker_fee,
91            ts_event.into(),
92            ts_init.into(),
93        )
94        .map_err(to_pyvalue_err)
95    }
96
97    fn __hash__(&self) -> isize {
98        let mut hasher = DefaultHasher::new();
99        self.hash(&mut hasher);
100        hasher.finish() as isize
101    }
102
103    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
104        match op {
105            CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
106            CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
107            _ => py.NotImplemented(),
108        }
109    }
110
111    #[getter]
112    fn type_str(&self) -> &str {
113        stringify!(CryptoFuture)
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 = "underlying")]
130    fn py_underlying(&self) -> Currency {
131        self.underlying
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 = "settlement_currency")]
142    fn py_settlement_currency(&self) -> Currency {
143        self.settlement_currency
144    }
145
146    #[getter]
147    #[pyo3(name = "is_inverse")]
148    fn py_is_inverse(&self) -> bool {
149        self.is_inverse
150    }
151
152    #[getter]
153    #[pyo3(name = "activation_ns")]
154    fn py_activation_ns(&self) -> u64 {
155        self.activation_ns.as_u64()
156    }
157
158    #[getter]
159    #[pyo3(name = "expiration_ns")]
160    fn py_expiration_ns(&self) -> u64 {
161        self.expiration_ns.as_u64()
162    }
163
164    #[getter]
165    #[pyo3(name = "price_precision")]
166    fn py_price_precision(&self) -> u8 {
167        self.price_precision
168    }
169
170    #[getter]
171    #[pyo3(name = "size_precision")]
172    fn py_size_precision(&self) -> u8 {
173        self.size_precision
174    }
175
176    #[getter]
177    #[pyo3(name = "price_increment")]
178    fn py_price_increment(&self) -> Price {
179        self.price_increment
180    }
181
182    #[getter]
183    #[pyo3(name = "size_increment")]
184    fn py_size_increment(&self) -> Quantity {
185        self.size_increment
186    }
187
188    #[getter]
189    #[pyo3(name = "multiplier")]
190    fn py_multiplier(&self) -> Quantity {
191        self.multiplier
192    }
193
194    #[getter]
195    #[pyo3(name = "lot_size")]
196    fn py_lot_size(&self) -> Option<Quantity> {
197        Some(self.lot_size)
198    }
199
200    #[getter]
201    #[pyo3(name = "max_quantity")]
202    fn py_max_quantity(&self) -> Option<Quantity> {
203        self.max_quantity
204    }
205
206    #[getter]
207    #[pyo3(name = "min_quantity")]
208    fn py_min_quantity(&self) -> Option<Quantity> {
209        self.min_quantity
210    }
211
212    #[getter]
213    #[pyo3(name = "max_notional")]
214    fn py_max_notional(&self) -> Option<Money> {
215        self.max_notional
216    }
217
218    #[getter]
219    #[pyo3(name = "min_notional")]
220    fn py_min_notional(&self) -> Option<Money> {
221        self.min_notional
222    }
223
224    #[getter]
225    #[pyo3(name = "max_price")]
226    fn py_max_price(&self) -> Option<Price> {
227        self.max_price
228    }
229
230    #[getter]
231    #[pyo3(name = "min_price")]
232    fn py_min_price(&self) -> Option<Price> {
233        self.min_price
234    }
235
236    #[getter]
237    #[pyo3(name = "margin_init")]
238    fn py_margin_init(&self) -> Decimal {
239        self.margin_init
240    }
241
242    #[getter]
243    #[pyo3(name = "margin_maint")]
244    fn py_margin_maint(&self) -> Decimal {
245        self.margin_maint
246    }
247
248    #[getter]
249    #[pyo3(name = "maker_fee")]
250    fn py_maker_fee(&self) -> Decimal {
251        self.maker_fee
252    }
253
254    #[getter]
255    #[pyo3(name = "taker_fee")]
256    fn py_taker_fee(&self) -> Decimal {
257        self.taker_fee
258    }
259
260    #[getter]
261    #[pyo3(name = "info")]
262    fn py_info(&self, py: Python<'_>) -> PyResult<PyObject> {
263        Ok(PyDict::new(py).into())
264    }
265
266    #[getter]
267    #[pyo3(name = "ts_event")]
268    fn py_ts_event(&self) -> u64 {
269        self.ts_event.as_u64()
270    }
271
272    #[getter]
273    #[pyo3(name = "ts_init")]
274    fn py_ts_init(&self) -> u64 {
275        self.ts_init.as_u64()
276    }
277
278    #[staticmethod]
279    #[pyo3(name = "from_dict")]
280    fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
281        from_dict_pyo3(py, values)
282    }
283
284    #[pyo3(name = "to_dict")]
285    fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
286        let dict = PyDict::new(py);
287        dict.set_item("type", stringify!(CryptoFuture))?;
288        dict.set_item("id", self.id.to_string())?;
289        dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
290        dict.set_item("underlying", self.underlying.code.to_string())?;
291        dict.set_item("quote_currency", self.quote_currency.code.to_string())?;
292        dict.set_item(
293            "settlement_currency",
294            self.settlement_currency.code.to_string(),
295        )?;
296        dict.set_item("is_inverse", self.is_inverse)?;
297        dict.set_item("activation_ns", self.activation_ns.as_u64())?;
298        dict.set_item("expiration_ns", self.expiration_ns.as_u64())?;
299        dict.set_item("price_precision", self.price_precision)?;
300        dict.set_item("size_precision", self.size_precision)?;
301        dict.set_item("price_increment", self.price_increment.to_string())?;
302        dict.set_item("size_increment", self.size_increment.to_string())?;
303        dict.set_item("margin_init", self.margin_init.to_string())?;
304        dict.set_item("margin_maint", self.margin_maint.to_string())?;
305        dict.set_item("multiplier", self.multiplier.to_string())?;
306        dict.set_item("lot_size", self.lot_size.to_string())?;
307        dict.set_item("info", PyDict::new(py))?;
308        dict.set_item("maker_fee", self.maker_fee.to_string())?;
309        dict.set_item("taker_fee", self.taker_fee.to_string())?;
310        dict.set_item("ts_event", self.ts_event.as_u64())?;
311        dict.set_item("ts_init", self.ts_init.as_u64())?;
312        match self.max_quantity {
313            Some(value) => dict.set_item("max_quantity", value.to_string())?,
314            None => dict.set_item("max_quantity", py.None())?,
315        }
316        match self.min_quantity {
317            Some(value) => dict.set_item("min_quantity", value.to_string())?,
318            None => dict.set_item("min_quantity", py.None())?,
319        }
320        match self.max_notional {
321            Some(value) => dict.set_item("max_notional", value.to_string())?,
322            None => dict.set_item("max_notional", py.None())?,
323        }
324        match self.min_notional {
325            Some(value) => dict.set_item("min_notional", value.to_string())?,
326            None => dict.set_item("min_notional", py.None())?,
327        }
328        match self.max_price {
329            Some(value) => dict.set_item("max_price", value.to_string())?,
330            None => dict.set_item("max_price", py.None())?,
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        Ok(dict.into())
337    }
338}
339
340////////////////////////////////////////////////////////////////////////////////
341// Tests
342////////////////////////////////////////////////////////////////////////////////
343#[cfg(test)]
344mod tests {
345    use pyo3::{prelude::*, prepare_freethreaded_python, types::PyDict};
346    use rstest::rstest;
347
348    use crate::instruments::{CryptoFuture, stubs::*};
349
350    #[rstest]
351    fn test_dict_round_trip(crypto_future_btcusdt: CryptoFuture) {
352        prepare_freethreaded_python();
353        Python::with_gil(|py| {
354            let crypto_future = crypto_future_btcusdt;
355            let values = crypto_future.py_to_dict(py).unwrap();
356            let values: Py<PyDict> = values.extract(py).unwrap();
357            let new_crypto_future = CryptoFuture::py_from_dict(py, values).unwrap();
358            assert_eq!(crypto_future, new_crypto_future);
359        })
360    }
361}