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