Skip to main content

nautilus_model/python/identifiers/
option_series_id.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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    str::FromStr,
20};
21
22use nautilus_core::UnixNanos;
23use pyo3::{prelude::*, pyclass::CompareOp};
24
25use crate::{
26    identifiers::{InstrumentId, OptionSeriesId, Venue},
27    python::option_series_id_error_to_pyvalue_err,
28};
29
30#[pymethods]
31#[pyo3_stub_gen::derive::gen_stub_pymethods]
32impl OptionSeriesId {
33    /// Identifies an option series and the instrument supplying its reference price.
34    ///
35    /// A reference matching `<UNDERLYING>.<VENUE>` uses the four-part representation. Others use
36    /// `VENUE:UNDERLYING:UNDERLYING_INSTRUMENT_ID:SETTLEMENT:EXPIRY`.
37    /// The reference instrument participates in equality, hashing, and ordering.
38    #[new]
39    #[pyo3(signature = (venue, underlying, settlement_currency, expiration_ns, underlying_instrument_id=None))]
40    fn py_new(
41        venue: &str,
42        underlying: &str,
43        settlement_currency: &str,
44        expiration_ns: u64,
45        underlying_instrument_id: Option<InstrumentId>,
46    ) -> PyResult<Self> {
47        Self::from_expiry_ns(
48            venue,
49            underlying,
50            settlement_currency,
51            UnixNanos::from(expiration_ns),
52            underlying_instrument_id,
53        )
54        .map_err(option_series_id_error_to_pyvalue_err)
55    }
56
57    /// Creates a series from a date string and an optional typed reference instrument.
58    ///
59    /// The date accepts `YYYY-MM-DD`, RFC 3339, integer nanoseconds, or floating-point seconds.
60    /// An absent reference derives `<UNDERLYING>.<VENUE>`.
61    ///
62    /// # Errors
63    ///
64    /// Returns an error if the venue, derived underlying instrument, or expiration is invalid.
65    #[staticmethod]
66    #[pyo3(name = "from_expiry")]
67    #[pyo3(signature = (venue, underlying, settlement_currency, date_str, underlying_instrument_id=None))]
68    fn py_from_expiry(
69        venue: &str,
70        underlying: &str,
71        settlement_currency: &str,
72        date_str: &str,
73        underlying_instrument_id: Option<InstrumentId>,
74    ) -> PyResult<Self> {
75        Self::from_expiry(
76            venue,
77            underlying,
78            settlement_currency,
79            date_str,
80            underlying_instrument_id,
81        )
82        .map_err(option_series_id_error_to_pyvalue_err)
83    }
84
85    #[staticmethod]
86    #[pyo3(name = "from_str")]
87    fn py_from_str(value: &str) -> PyResult<Self> {
88        Self::from_str(value).map_err(option_series_id_error_to_pyvalue_err)
89    }
90
91    #[getter]
92    #[pyo3(name = "venue")]
93    fn py_venue(&self) -> Venue {
94        self.venue
95    }
96
97    #[getter]
98    #[pyo3(name = "underlying")]
99    fn py_underlying(&self) -> String {
100        self.underlying.to_string()
101    }
102
103    #[getter]
104    #[pyo3(name = "underlying_instrument_id")]
105    fn py_underlying_instrument_id(&self) -> InstrumentId {
106        self.underlying_instrument_id
107    }
108
109    #[getter]
110    #[pyo3(name = "settlement_currency")]
111    fn py_settlement_currency(&self) -> String {
112        self.settlement_currency.to_string()
113    }
114
115    #[getter]
116    #[pyo3(name = "expiration_ns")]
117    fn py_expiration_ns(&self) -> u64 {
118        self.expiration_ns.as_u64()
119    }
120
121    #[getter]
122    #[pyo3(name = "value")]
123    fn py_value(&self) -> String {
124        self.to_string()
125    }
126
127    fn __richcmp__(&self, other: &Self, op: CompareOp) -> bool {
128        match op {
129            CompareOp::Eq => self == other,
130            CompareOp::Ne => self != other,
131            CompareOp::Ge => self >= other,
132            CompareOp::Gt => self > other,
133            CompareOp::Le => self <= other,
134            CompareOp::Lt => self < other,
135        }
136    }
137
138    fn __hash__(&self) -> isize {
139        let mut h = DefaultHasher::new();
140        self.hash(&mut h);
141        h.finish() as isize
142    }
143
144    fn __repr__(&self) -> String {
145        format!("OptionSeriesId('{self}')")
146    }
147
148    fn __str__(&self) -> String {
149        self.to_string()
150    }
151}