nautilus_bybit/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//! Bybit 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::{BybitAccountType, BybitEnvironment, BybitProductType};
25
26#[pymethods]
27impl BybitProductType {
28    #[new]
29    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
30        let t = Self::type_object(py);
31        Self::py_from_str(&t, value)
32    }
33
34    fn __hash__(&self) -> isize {
35        *self as isize
36    }
37
38    fn __repr__(&self) -> String {
39        format!(
40            "<{}.{}: '{}'>",
41            stringify!(BybitProductType),
42            self.name(),
43            self.value(),
44        )
45    }
46
47    fn __str__(&self) -> String {
48        self.to_string()
49    }
50
51    #[getter]
52    #[must_use]
53    pub fn name(&self) -> &str {
54        self.as_ref()
55    }
56
57    #[getter]
58    #[must_use]
59    pub fn value(&self) -> String {
60        self.to_string().to_lowercase()
61    }
62
63    #[staticmethod]
64    #[must_use]
65    fn variants() -> Vec<String> {
66        Self::iter().map(|x| x.to_string()).collect()
67    }
68
69    #[classmethod]
70    #[pyo3(name = "from_str")]
71    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
72        let data_str: String = data.str()?.extract()?;
73        Self::from_str(&data_str).map_err(to_pyvalue_err)
74    }
75
76    #[classattr]
77    #[pyo3(name = "SPOT")]
78    fn py_spot() -> Self {
79        Self::Spot
80    }
81
82    #[classattr]
83    #[pyo3(name = "LINEAR")]
84    fn py_linear() -> Self {
85        Self::Linear
86    }
87
88    #[classattr]
89    #[pyo3(name = "INVERSE")]
90    fn py_inverse() -> Self {
91        Self::Inverse
92    }
93
94    #[classattr]
95    #[pyo3(name = "OPTION")]
96    fn py_option() -> Self {
97        Self::Option
98    }
99}
100
101#[pymethods]
102impl BybitEnvironment {
103    #[new]
104    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
105        let t = Self::type_object(py);
106        Self::py_from_str(&t, value)
107    }
108
109    fn __hash__(&self) -> isize {
110        *self as isize
111    }
112
113    fn __repr__(&self) -> String {
114        format!(
115            "<{}.{}: {}>",
116            stringify!(BybitEnvironment),
117            self.name(),
118            *self as u8,
119        )
120    }
121
122    fn __str__(&self) -> String {
123        self.to_string()
124    }
125
126    #[getter]
127    #[must_use]
128    pub fn name(&self) -> &str {
129        self.as_ref()
130    }
131
132    #[getter]
133    #[must_use]
134    pub fn value(&self) -> String {
135        self.to_string().to_lowercase()
136    }
137
138    #[staticmethod]
139    #[must_use]
140    fn variants() -> Vec<String> {
141        Self::iter().map(|x| x.to_string()).collect()
142    }
143
144    #[classmethod]
145    #[pyo3(name = "from_str")]
146    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
147        let data_str: String = data.str()?.extract()?;
148        Self::from_str(&data_str).map_err(to_pyvalue_err)
149    }
150
151    #[classattr]
152    #[pyo3(name = "MAINNET")]
153    fn py_mainnet() -> Self {
154        Self::Mainnet
155    }
156
157    #[classattr]
158    #[pyo3(name = "DEMO")]
159    fn py_demo() -> Self {
160        Self::Demo
161    }
162
163    #[classattr]
164    #[pyo3(name = "TESTNET")]
165    fn py_testnet() -> Self {
166        Self::Testnet
167    }
168}
169
170#[pymethods]
171impl BybitAccountType {
172    #[new]
173    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
174        let t = Self::type_object(py);
175        Self::py_from_str(&t, value)
176    }
177
178    fn __hash__(&self) -> isize {
179        *self as isize
180    }
181
182    fn __repr__(&self) -> String {
183        format!(
184            "<{}.{}: {}>",
185            stringify!(BybitAccountType),
186            self.name(),
187            *self as u8,
188        )
189    }
190
191    fn __str__(&self) -> String {
192        self.to_string()
193    }
194
195    #[getter]
196    #[must_use]
197    pub fn name(&self) -> &str {
198        self.as_ref()
199    }
200
201    #[getter]
202    #[must_use]
203    pub fn value(&self) -> String {
204        self.to_string().to_uppercase()
205    }
206
207    #[staticmethod]
208    #[must_use]
209    fn variants() -> Vec<String> {
210        Self::iter().map(|x| x.to_string()).collect()
211    }
212
213    #[classmethod]
214    #[pyo3(name = "from_str")]
215    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
216        let data_str: String = data.str()?.extract()?;
217        Self::from_str(&data_str).map_err(to_pyvalue_err)
218    }
219
220    #[classattr]
221    #[pyo3(name = "UNIFIED")]
222    fn py_unified() -> Self {
223        Self::Unified
224    }
225}