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