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};
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 #[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 #[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}