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