nautilus_hyperliquid/python/
enums.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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
16//! Hyperliquid enumerations Python bindings.
17
18use std::str::FromStr;
19
20use nautilus_core::python::to_pyvalue_err;
21use pyo3::{PyTypeInfo, prelude::*, types::PyType};
22use strum::IntoEnumIterator;
23
24use crate::common::enums::{
25    HyperliquidConditionalOrderType, HyperliquidTpSl, HyperliquidTrailingOffsetType,
26    HyperliquidTriggerPriceType,
27};
28
29#[pymethods]
30impl HyperliquidTriggerPriceType {
31    #[new]
32    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
33        let t = Self::type_object(py);
34        Self::py_from_str(&t, value)
35    }
36
37    fn __hash__(&self) -> isize {
38        *self as isize
39    }
40
41    fn __repr__(&self) -> String {
42        format!(
43            "<{}.{}: '{}'>",
44            stringify!(HyperliquidTriggerPriceType),
45            self.name(),
46            self.value(),
47        )
48    }
49
50    fn __str__(&self) -> String {
51        self.to_string()
52    }
53
54    #[getter]
55    #[must_use]
56    pub fn name(&self) -> &str {
57        self.as_ref()
58    }
59
60    #[getter]
61    #[must_use]
62    pub fn value(&self) -> String {
63        self.to_string().to_lowercase()
64    }
65
66    #[staticmethod]
67    #[must_use]
68    fn variants() -> Vec<String> {
69        Self::iter().map(|x| x.to_string()).collect()
70    }
71
72    #[classmethod]
73    #[pyo3(name = "from_str")]
74    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
75        let data_str: String = data.str()?.extract()?;
76        Self::from_str(&data_str).map_err(to_pyvalue_err)
77    }
78
79    #[classattr]
80    #[pyo3(name = "LAST")]
81    fn py_last() -> Self {
82        Self::Last
83    }
84
85    #[classattr]
86    #[pyo3(name = "MARK")]
87    fn py_mark() -> Self {
88        Self::Mark
89    }
90
91    #[classattr]
92    #[pyo3(name = "ORACLE")]
93    fn py_oracle() -> Self {
94        Self::Oracle
95    }
96}
97
98#[pymethods]
99impl HyperliquidTpSl {
100    #[new]
101    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
102        let t = Self::type_object(py);
103        Self::py_from_str(&t, value)
104    }
105
106    fn __hash__(&self) -> isize {
107        *self as isize
108    }
109
110    fn __repr__(&self) -> String {
111        format!(
112            "<{}.{}: '{}'>",
113            stringify!(HyperliquidTpSl),
114            self.name(),
115            self.value(),
116        )
117    }
118
119    fn __str__(&self) -> String {
120        self.to_string()
121    }
122
123    #[getter]
124    #[must_use]
125    pub fn name(&self) -> &str {
126        self.as_ref()
127    }
128
129    #[getter]
130    #[must_use]
131    pub fn value(&self) -> String {
132        self.to_string().to_lowercase()
133    }
134
135    #[staticmethod]
136    #[must_use]
137    fn variants() -> Vec<String> {
138        Self::iter().map(|x| x.to_string()).collect()
139    }
140
141    #[classmethod]
142    #[pyo3(name = "from_str")]
143    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
144        let data_str: String = data.str()?.extract()?;
145        Self::from_str(&data_str).map_err(to_pyvalue_err)
146    }
147
148    #[classattr]
149    #[pyo3(name = "TP")]
150    fn py_tp() -> Self {
151        Self::Tp
152    }
153
154    #[classattr]
155    #[pyo3(name = "SL")]
156    fn py_sl() -> Self {
157        Self::Sl
158    }
159}
160
161#[pymethods]
162impl HyperliquidConditionalOrderType {
163    #[new]
164    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
165        let t = Self::type_object(py);
166        Self::py_from_str(&t, value)
167    }
168
169    fn __hash__(&self) -> isize {
170        *self as isize
171    }
172
173    fn __repr__(&self) -> String {
174        format!(
175            "<{}.{}: '{}'>",
176            stringify!(HyperliquidConditionalOrderType),
177            self.name(),
178            self.value(),
179        )
180    }
181
182    fn __str__(&self) -> String {
183        self.to_string()
184    }
185
186    #[getter]
187    #[must_use]
188    pub fn name(&self) -> &str {
189        self.as_ref()
190    }
191
192    #[getter]
193    #[must_use]
194    pub fn value(&self) -> String {
195        self.to_string().to_lowercase()
196    }
197
198    #[staticmethod]
199    #[must_use]
200    fn variants() -> Vec<String> {
201        Self::iter().map(|x| x.to_string()).collect()
202    }
203
204    #[classmethod]
205    #[pyo3(name = "from_str")]
206    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
207        let data_str: String = data.str()?.extract()?;
208        Self::from_str(&data_str).map_err(to_pyvalue_err)
209    }
210
211    #[classattr]
212    #[pyo3(name = "STOP_MARKET")]
213    fn py_stop_market() -> Self {
214        Self::StopMarket
215    }
216
217    #[classattr]
218    #[pyo3(name = "STOP_LIMIT")]
219    fn py_stop_limit() -> Self {
220        Self::StopLimit
221    }
222
223    #[classattr]
224    #[pyo3(name = "TAKE_PROFIT_MARKET")]
225    fn py_take_profit_market() -> Self {
226        Self::TakeProfitMarket
227    }
228
229    #[classattr]
230    #[pyo3(name = "TAKE_PROFIT_LIMIT")]
231    fn py_take_profit_limit() -> Self {
232        Self::TakeProfitLimit
233    }
234
235    #[classattr]
236    #[pyo3(name = "TRAILING_STOP_MARKET")]
237    fn py_trailing_stop_market() -> Self {
238        Self::TrailingStopMarket
239    }
240
241    #[classattr]
242    #[pyo3(name = "TRAILING_STOP_LIMIT")]
243    fn py_trailing_stop_limit() -> Self {
244        Self::TrailingStopLimit
245    }
246}
247
248#[pymethods]
249impl HyperliquidTrailingOffsetType {
250    #[new]
251    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
252        let t = Self::type_object(py);
253        Self::py_from_str(&t, value)
254    }
255
256    fn __hash__(&self) -> isize {
257        *self as isize
258    }
259
260    fn __repr__(&self) -> String {
261        format!(
262            "<{}.{}: '{}'>",
263            stringify!(HyperliquidTrailingOffsetType),
264            self.name(),
265            self.value(),
266        )
267    }
268
269    fn __str__(&self) -> String {
270        self.to_string()
271    }
272
273    #[getter]
274    #[must_use]
275    pub fn name(&self) -> &str {
276        self.as_ref()
277    }
278
279    #[getter]
280    #[must_use]
281    pub fn value(&self) -> String {
282        self.to_string().to_lowercase()
283    }
284
285    #[staticmethod]
286    #[must_use]
287    fn variants() -> Vec<String> {
288        Self::iter().map(|x| x.to_string()).collect()
289    }
290
291    #[classmethod]
292    #[pyo3(name = "from_str")]
293    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
294        let data_str: String = data.str()?.extract()?;
295        Self::from_str(&data_str).map_err(to_pyvalue_err)
296    }
297
298    #[classattr]
299    #[pyo3(name = "PRICE")]
300    fn py_price() -> Self {
301        Self::Price
302    }
303
304    #[classattr]
305    #[pyo3(name = "PERCENTAGE")]
306    fn py_percentage() -> Self {
307        Self::Percentage
308    }
309
310    #[classattr]
311    #[pyo3(name = "BASIS_POINTS")]
312    fn py_basis_points() -> Self {
313        Self::BasisPoints
314    }
315}