nautilus_model/python/instruments/
crypto_perpetual.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::{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::CryptoPerpetual,
28    types::{Currency, Money, Price, Quantity},
29};
30
31#[pymethods]
32impl CryptoPerpetual {
33    #[allow(clippy::too_many_arguments)]
34    #[new]
35    #[pyo3(signature = (id, raw_symbol, base_currency, quote_currency, settlement_currency, is_inverse, 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        base_currency: Currency,
40        quote_currency: Currency,
41        settlement_currency: Currency,
42        is_inverse: bool,
43        price_precision: u8,
44        size_precision: u8,
45        price_increment: Price,
46        size_increment: Quantity,
47        ts_event: u64,
48        ts_init: u64,
49        multiplier: Option<Quantity>,
50        lot_size: Option<Quantity>,
51        max_quantity: Option<Quantity>,
52        min_quantity: Option<Quantity>,
53        max_notional: Option<Money>,
54        min_notional: Option<Money>,
55        max_price: Option<Price>,
56        min_price: Option<Price>,
57        margin_init: Option<Decimal>,
58        margin_maint: Option<Decimal>,
59        maker_fee: Option<Decimal>,
60        taker_fee: Option<Decimal>,
61    ) -> PyResult<Self> {
62        Self::new_checked(
63            id,
64            raw_symbol,
65            base_currency,
66            quote_currency,
67            settlement_currency,
68            is_inverse,
69            price_precision,
70            size_precision,
71            price_increment,
72            size_increment,
73            multiplier,
74            lot_size,
75            max_quantity,
76            min_quantity,
77            max_notional,
78            min_notional,
79            max_price,
80            min_price,
81            margin_init,
82            margin_maint,
83            maker_fee,
84            taker_fee,
85            ts_event.into(),
86            ts_init.into(),
87        )
88        .map_err(to_pyvalue_err)
89    }
90
91    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
92        match op {
93            CompareOp::Eq => self.eq(other).into_py(py),
94            _ => panic!("Not implemented"),
95        }
96    }
97
98    fn __hash__(&self) -> isize {
99        let mut hasher = DefaultHasher::new();
100        self.hash(&mut hasher);
101        hasher.finish() as isize
102    }
103
104    #[getter]
105    fn type_str(&self) -> &str {
106        stringify!(CryptoPerpetual)
107    }
108
109    #[getter]
110    #[pyo3(name = "id")]
111    fn py_id(&self) -> InstrumentId {
112        self.id
113    }
114
115    #[getter]
116    #[pyo3(name = "raw_symbol")]
117    fn py_raw_symbol(&self) -> Symbol {
118        self.raw_symbol
119    }
120
121    #[getter]
122    #[pyo3(name = "base_currency")]
123    fn py_base_currency(&self) -> Currency {
124        self.base_currency
125    }
126
127    #[getter]
128    #[pyo3(name = "quote_currency")]
129    fn py_quote_currency(&self) -> Currency {
130        self.quote_currency
131    }
132
133    #[getter]
134    #[pyo3(name = "settlement_currency")]
135    fn py_settlement_currency(&self) -> Currency {
136        self.settlement_currency
137    }
138
139    #[getter]
140    #[pyo3(name = "is_inverse")]
141    fn py_is_inverse(&self) -> bool {
142        self.is_inverse
143    }
144
145    #[getter]
146    #[pyo3(name = "price_precision")]
147    fn py_price_precision(&self) -> u8 {
148        self.price_precision
149    }
150
151    #[getter]
152    #[pyo3(name = "size_precision")]
153    fn py_size_precision(&self) -> u8 {
154        self.size_precision
155    }
156
157    #[getter]
158    #[pyo3(name = "price_increment")]
159    fn py_price_increment(&self) -> Price {
160        self.price_increment
161    }
162
163    #[getter]
164    #[pyo3(name = "size_increment")]
165    fn py_size_increment(&self) -> Quantity {
166        self.size_increment
167    }
168
169    #[getter]
170    #[pyo3(name = "multiplier")]
171    fn py_multiplier(&self) -> Quantity {
172        self.multiplier
173    }
174
175    #[getter]
176    #[pyo3(name = "lot_size")]
177    fn py_lot_size(&self) -> Quantity {
178        self.lot_size
179    }
180
181    #[getter]
182    #[pyo3(name = "max_quantity")]
183    fn py_max_quantity(&self) -> Option<Quantity> {
184        self.max_quantity
185    }
186
187    #[getter]
188    #[pyo3(name = "min_quantity")]
189    fn py_min_quantity(&self) -> Option<Quantity> {
190        self.min_quantity
191    }
192
193    #[getter]
194    #[pyo3(name = "max_notional")]
195    fn py_max_notional(&self) -> Option<Money> {
196        self.max_notional
197    }
198
199    #[getter]
200    #[pyo3(name = "min_notional")]
201    fn py_min_notional(&self) -> Option<Money> {
202        self.min_notional
203    }
204
205    #[getter]
206    #[pyo3(name = "max_price")]
207    fn py_max_price(&self) -> Option<Price> {
208        self.max_price
209    }
210
211    #[getter]
212    #[pyo3(name = "min_price")]
213    fn py_min_price(&self) -> Option<Price> {
214        self.min_price
215    }
216
217    #[getter]
218    #[pyo3(name = "ts_event")]
219    fn py_ts_event(&self) -> u64 {
220        self.ts_event.as_u64()
221    }
222
223    #[getter]
224    #[pyo3(name = "ts_init")]
225    fn py_ts_init(&self) -> u64 {
226        self.ts_init.as_u64()
227    }
228
229    #[getter]
230    #[pyo3(name = "margin_init")]
231    fn py_margin_init(&self) -> Decimal {
232        self.margin_init
233    }
234
235    #[getter]
236    #[pyo3(name = "margin_maint")]
237    fn py_margin_maint(&self) -> Decimal {
238        self.margin_maint
239    }
240
241    #[getter]
242    #[pyo3(name = "maker_fee")]
243    fn py_maker_fee(&self) -> Decimal {
244        self.maker_fee
245    }
246
247    #[getter]
248    #[pyo3(name = "taker_fee")]
249    fn py_taker_fee(&self) -> Decimal {
250        self.taker_fee
251    }
252
253    #[getter]
254    #[pyo3(name = "info")]
255    fn py_info(&self, py: Python<'_>) -> PyResult<PyObject> {
256        Ok(PyDict::new(py).into())
257    }
258
259    #[staticmethod]
260    #[pyo3(name = "from_dict")]
261    fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
262        from_dict_pyo3(py, values)
263    }
264
265    #[pyo3(name = "to_dict")]
266    fn py_to_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
267        let dict = PyDict::new(py);
268        dict.set_item("type", stringify!(CryptoPerpetual))?;
269        dict.set_item("id", self.id.to_string())?;
270        dict.set_item("raw_symbol", self.raw_symbol.to_string())?;
271        dict.set_item("base_currency", self.base_currency.code.to_string())?;
272        dict.set_item("quote_currency", self.quote_currency.code.to_string())?;
273        dict.set_item(
274            "settlement_currency",
275            self.settlement_currency.code.to_string(),
276        )?;
277        dict.set_item("is_inverse", self.is_inverse)?;
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("maker_fee", self.maker_fee.to_string())?;
283        dict.set_item("taker_fee", self.taker_fee.to_string())?;
284        dict.set_item("margin_init", self.margin_init.to_string())?;
285        dict.set_item("margin_maint", self.margin_maint.to_string())?;
286        dict.set_item("info", PyDict::new(py))?;
287        dict.set_item("ts_event", self.ts_event.as_u64())?;
288        dict.set_item("ts_init", self.ts_init.as_u64())?;
289        dict.set_item("multiplier", self.multiplier.to_string())?;
290        dict.set_item("lot_size", self.lot_size.to_string())?;
291        match self.max_quantity {
292            Some(value) => dict.set_item("max_quantity", value.to_string())?,
293            None => dict.set_item("max_quantity", py.None())?,
294        }
295        match self.min_quantity {
296            Some(value) => dict.set_item("min_quantity", value.to_string())?,
297            None => dict.set_item("min_quantity", py.None())?,
298        }
299        match self.max_notional {
300            Some(value) => dict.set_item("max_notional", value.to_string())?,
301            None => dict.set_item("max_notional", py.None())?,
302        }
303        match self.min_notional {
304            Some(value) => dict.set_item("min_notional", value.to_string())?,
305            None => dict.set_item("min_notional", py.None())?,
306        }
307        match self.max_price {
308            Some(value) => dict.set_item("max_price", value.to_string())?,
309            None => dict.set_item("max_price", py.None())?,
310        }
311        match self.min_price {
312            Some(value) => dict.set_item("min_price", value.to_string())?,
313            None => dict.set_item("min_price", py.None())?,
314        }
315        Ok(dict.into())
316    }
317}