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