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