Skip to main content

nautilus_hyperliquid/python/
enums.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
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, HyperliquidProductType, HyperliquidTpSl,
26    HyperliquidTrailingOffsetType,
27};
28
29#[pymethods]
30#[pyo3_stub_gen::derive::gen_stub_pymethods]
31impl HyperliquidTpSl {
32    /// Represents the take profit / stop loss type.
33    #[new]
34    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
35        let t = Self::type_object(py);
36        Self::py_from_str(&t, value)
37    }
38
39    fn __hash__(&self) -> isize {
40        *self as isize
41    }
42
43    fn __repr__(&self) -> String {
44        format!(
45            "<{}.{}: '{}'>",
46            stringify!(HyperliquidTpSl),
47            self.name(),
48            self.value(),
49        )
50    }
51
52    fn __str__(&self) -> String {
53        self.to_string()
54    }
55
56    #[getter]
57    #[must_use]
58    pub fn name(&self) -> &str {
59        self.as_ref()
60    }
61
62    #[getter]
63    #[must_use]
64    pub fn value(&self) -> String {
65        self.to_string().to_lowercase()
66    }
67
68    #[staticmethod]
69    #[must_use]
70    fn variants() -> Vec<String> {
71        Self::iter().map(|x| x.to_string()).collect()
72    }
73
74    #[classmethod]
75    #[pyo3(name = "from_str")]
76    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
77        let data_str: String = data.str()?.extract()?;
78        Self::from_str(&data_str).map_err(to_pyvalue_err)
79    }
80}
81
82#[pymethods]
83#[pyo3_stub_gen::derive::gen_stub_pymethods]
84impl HyperliquidConditionalOrderType {
85    /// Represents conditional/trigger order types.
86    ///
87    /// Hyperliquid supports various conditional order types that trigger
88    /// based on market conditions. These map to Nautilus OrderType variants.
89    #[new]
90    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
91        let t = Self::type_object(py);
92        Self::py_from_str(&t, value)
93    }
94
95    fn __hash__(&self) -> isize {
96        *self as isize
97    }
98
99    fn __repr__(&self) -> String {
100        format!(
101            "<{}.{}: '{}'>",
102            stringify!(HyperliquidConditionalOrderType),
103            self.name(),
104            self.value(),
105        )
106    }
107
108    fn __str__(&self) -> String {
109        self.to_string()
110    }
111
112    #[getter]
113    #[must_use]
114    pub fn name(&self) -> &str {
115        self.as_ref()
116    }
117
118    #[getter]
119    #[must_use]
120    pub fn value(&self) -> String {
121        self.to_string().to_lowercase()
122    }
123
124    #[staticmethod]
125    #[must_use]
126    fn variants() -> Vec<String> {
127        Self::iter().map(|x| x.to_string()).collect()
128    }
129
130    #[classmethod]
131    #[pyo3(name = "from_str")]
132    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
133        let data_str: String = data.str()?.extract()?;
134        Self::from_str(&data_str).map_err(to_pyvalue_err)
135    }
136}
137
138#[pymethods]
139#[pyo3_stub_gen::derive::gen_stub_pymethods]
140impl HyperliquidTrailingOffsetType {
141    /// Represents trailing offset types for trailing stop orders.
142    ///
143    /// Trailing stops adjust dynamically based on market movement:
144    /// - Price: Fixed price offset (e.g., $100)
145    /// - Percentage: Percentage offset (e.g., 5%)
146    /// - BasisPoints: Basis points offset (e.g., 250 bps = 2.5%)
147    #[new]
148    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
149        let t = Self::type_object(py);
150        Self::py_from_str(&t, value)
151    }
152
153    fn __hash__(&self) -> isize {
154        *self as isize
155    }
156
157    fn __repr__(&self) -> String {
158        format!(
159            "<{}.{}: '{}'>",
160            stringify!(HyperliquidTrailingOffsetType),
161            self.name(),
162            self.value(),
163        )
164    }
165
166    fn __str__(&self) -> String {
167        self.to_string()
168    }
169
170    #[getter]
171    #[must_use]
172    pub fn name(&self) -> &str {
173        self.as_ref()
174    }
175
176    #[getter]
177    #[must_use]
178    pub fn value(&self) -> String {
179        self.to_string().to_lowercase()
180    }
181
182    #[staticmethod]
183    #[must_use]
184    fn variants() -> Vec<String> {
185        Self::iter().map(|x| x.to_string()).collect()
186    }
187
188    #[classmethod]
189    #[pyo3(name = "from_str")]
190    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
191        let data_str: String = data.str()?.extract()?;
192        Self::from_str(&data_str).map_err(to_pyvalue_err)
193    }
194}
195
196#[pymethods]
197#[pyo3_stub_gen::derive::gen_stub_pymethods]
198impl HyperliquidProductType {
199    /// Hyperliquid product type.
200    #[new]
201    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
202        let t = Self::type_object(py);
203        Self::py_from_str(&t, value)
204    }
205
206    fn __hash__(&self) -> isize {
207        *self as isize
208    }
209
210    fn __eq__(&self, other: &Self) -> bool {
211        self == other
212    }
213
214    fn __repr__(&self) -> String {
215        format!(
216            "<{}.{}: '{}'>",
217            stringify!(HyperliquidProductType),
218            self.name(),
219            self.value(),
220        )
221    }
222
223    fn __str__(&self) -> String {
224        self.to_string()
225    }
226
227    #[getter]
228    #[must_use]
229    pub fn name(&self) -> &str {
230        self.as_ref()
231    }
232
233    #[getter]
234    #[must_use]
235    pub fn value(&self) -> String {
236        self.to_string()
237    }
238
239    #[staticmethod]
240    #[must_use]
241    fn variants() -> Vec<String> {
242        Self::iter().map(|x| x.to_string()).collect()
243    }
244
245    #[classmethod]
246    #[pyo3(name = "from_str")]
247    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
248        let data_str: String = data.str()?.extract()?;
249        Self::from_str(&data_str).map_err(to_pyvalue_err)
250    }
251}