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, python::to_pyvalue_err};
23use pyo3::{prelude::*, pyclass::CompareOp};
24use ustr::Ustr;
25
26use crate::identifiers::{OptionSeriesId, Venue};
27
28#[pymethods]
29#[pyo3_stub_gen::derive::gen_stub_pymethods]
30impl OptionSeriesId {
31    /// Identifies a unique option series: a specific venue + underlying + settlement currency + expiration.
32    #[new]
33    fn py_new(
34        venue: &str,
35        underlying: &str,
36        settlement_currency: &str,
37        expiration_ns: u64,
38    ) -> Self {
39        Self {
40            venue: Venue::new(venue),
41            underlying: Ustr::from(underlying),
42            settlement_currency: Ustr::from(settlement_currency),
43            expiration_ns: UnixNanos::from(expiration_ns),
44        }
45    }
46
47    /// Creates an `OptionSeriesId` from venue name, underlying symbol, settlement currency, and date string.
48    ///
49    /// The `date_str` is parsed via `UnixNanos::FromStr`, which accepts `"YYYY-MM-DD"`,
50    /// RFC 3339 timestamps, integer nanoseconds, or floating-point seconds.
51    #[staticmethod]
52    #[pyo3(name = "from_expiry")]
53    fn py_from_expiry(
54        venue: &str,
55        underlying: &str,
56        settlement_currency: &str,
57        date_str: &str,
58    ) -> PyResult<Self> {
59        Self::from_expiry(venue, underlying, settlement_currency, date_str).map_err(to_pyvalue_err)
60    }
61
62    #[staticmethod]
63    #[pyo3(name = "from_str")]
64    fn py_from_str(value: &str) -> PyResult<Self> {
65        Self::from_str(value).map_err(to_pyvalue_err)
66    }
67
68    #[getter]
69    #[pyo3(name = "venue")]
70    fn py_venue(&self) -> Venue {
71        self.venue
72    }
73
74    #[getter]
75    #[pyo3(name = "underlying")]
76    fn py_underlying(&self) -> String {
77        self.underlying.to_string()
78    }
79
80    #[getter]
81    #[pyo3(name = "settlement_currency")]
82    fn py_settlement_currency(&self) -> String {
83        self.settlement_currency.to_string()
84    }
85
86    #[getter]
87    #[pyo3(name = "expiration_ns")]
88    fn py_expiration_ns(&self) -> u64 {
89        self.expiration_ns.as_u64()
90    }
91
92    #[getter]
93    #[pyo3(name = "value")]
94    fn py_value(&self) -> String {
95        self.to_string()
96    }
97
98    fn __richcmp__(&self, other: &Self, op: CompareOp) -> bool {
99        match op {
100            CompareOp::Eq => self == other,
101            CompareOp::Ne => self != other,
102            CompareOp::Ge => self >= other,
103            CompareOp::Gt => self > other,
104            CompareOp::Le => self <= other,
105            CompareOp::Lt => self < other,
106        }
107    }
108
109    fn __hash__(&self) -> isize {
110        let mut h = DefaultHasher::new();
111        self.hash(&mut h);
112        h.finish() as isize
113    }
114
115    fn __repr__(&self) -> String {
116        format!("OptionSeriesId('{self}')")
117    }
118
119    fn __str__(&self) -> String {
120        self.to_string()
121    }
122}