nautilus_model/python/identifiers/
option_series_id.rs1use 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 #[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 #[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}