nautilus_okx/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//! OKX 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    OKXContractType, OKXInstrumentType, OKXMarginMode, OKXPositionMode, OKXTradeMode,
26};
27
28#[pymethods]
29impl OKXInstrumentType {
30    #[new]
31    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
32        let t = Self::type_object(py);
33        Self::py_from_str(&t, value)
34    }
35
36    fn __hash__(&self) -> isize {
37        *self as isize
38    }
39
40    fn __repr__(&self) -> String {
41        format!(
42            "<{}.{}: '{}'>",
43            stringify!(OKXInstrumentType),
44            self.name(),
45            self.value(),
46        )
47    }
48
49    fn __str__(&self) -> String {
50        self.to_string()
51    }
52
53    #[getter]
54    #[must_use]
55    pub fn name(&self) -> &str {
56        self.as_ref()
57    }
58
59    #[getter]
60    #[must_use]
61    pub fn value(&self) -> u8 {
62        *self as u8
63    }
64
65    #[staticmethod]
66    #[must_use]
67    fn variants() -> Vec<String> {
68        Self::iter().map(|x| x.to_string()).collect()
69    }
70
71    #[classmethod]
72    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
73        let data_str: String = data.str()?.extract()?;
74        Self::from_str(&data_str).map_err(to_pyvalue_err)
75    }
76
77    #[classattr]
78    #[pyo3(name = "ANY")]
79    fn py_any() -> Self {
80        Self::Any
81    }
82
83    #[classattr]
84    #[pyo3(name = "SPOT")]
85    fn py_spot() -> Self {
86        Self::Spot
87    }
88
89    #[classattr]
90    #[pyo3(name = "MARGIN")]
91    fn py_margin() -> Self {
92        Self::Margin
93    }
94
95    #[classattr]
96    #[pyo3(name = "SWAP")]
97    fn py_swap() -> Self {
98        Self::Swap
99    }
100
101    #[classattr]
102    #[pyo3(name = "FUTURES")]
103    fn futures() -> Self {
104        Self::Futures
105    }
106
107    #[classattr]
108    #[pyo3(name = "OPTION")]
109    fn option() -> Self {
110        Self::Option
111    }
112}
113
114#[pymethods]
115impl OKXContractType {
116    #[new]
117    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
118        let t = Self::type_object(py);
119        Self::py_from_str(&t, value)
120    }
121
122    fn __hash__(&self) -> isize {
123        *self as isize
124    }
125
126    fn __repr__(&self) -> String {
127        format!(
128            "<{}.{}: '{}'>",
129            stringify!(OKXContractType),
130            self.name(),
131            self.value(),
132        )
133    }
134
135    fn __str__(&self) -> String {
136        self.to_string()
137    }
138
139    #[getter]
140    #[must_use]
141    pub fn name(&self) -> &str {
142        self.as_ref()
143    }
144
145    #[getter]
146    #[must_use]
147    pub fn value(&self) -> u8 {
148        *self as u8
149    }
150
151    #[classmethod]
152    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
153        let data_str: String = data.str()?.extract()?;
154        Self::from_str(&data_str).map_err(to_pyvalue_err)
155    }
156
157    #[staticmethod]
158    #[must_use]
159    fn variants() -> Vec<String> {
160        Self::iter().map(|x| x.to_string()).collect()
161    }
162
163    #[classattr]
164    #[pyo3(name = "NONE")]
165    fn py_none() -> Self {
166        Self::None
167    }
168
169    #[classattr]
170    #[pyo3(name = "LINEAR")]
171    fn py_linear() -> Self {
172        Self::Linear
173    }
174
175    #[classattr]
176    #[pyo3(name = "INVERSE")]
177    fn py_inverse() -> Self {
178        Self::Inverse
179    }
180}
181
182#[pymethods]
183impl OKXMarginMode {
184    #[new]
185    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
186        let t = Self::type_object(py);
187        Self::py_from_str(&t, value)
188    }
189
190    fn __hash__(&self) -> isize {
191        *self as isize
192    }
193
194    fn __repr__(&self) -> String {
195        format!(
196            "<{}.{}: '{}'>",
197            stringify!(OKXMarginMode),
198            self.name(),
199            self.value(),
200        )
201    }
202
203    fn __str__(&self) -> String {
204        self.to_string()
205    }
206
207    #[getter]
208    #[must_use]
209    pub fn name(&self) -> &str {
210        self.as_ref()
211    }
212
213    #[getter]
214    #[must_use]
215    pub fn value(&self) -> u8 {
216        *self as u8
217    }
218
219    #[staticmethod]
220    #[must_use]
221    fn variants() -> Vec<String> {
222        Self::iter().map(|x| x.to_string()).collect()
223    }
224
225    #[classmethod]
226    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
227        let data_str: String = data.str()?.extract()?;
228        Self::from_str(&data_str).map_err(to_pyvalue_err)
229    }
230
231    #[classattr]
232    #[pyo3(name = "NONE")]
233    fn py_none() -> Self {
234        Self::None
235    }
236
237    #[classattr]
238    #[pyo3(name = "ISOLATED")]
239    fn py_isolated() -> Self {
240        Self::Isolated
241    }
242
243    #[classattr]
244    #[pyo3(name = "CROSS")]
245    fn py_cross() -> Self {
246        Self::Cross
247    }
248}
249
250#[pymethods]
251impl OKXTradeMode {
252    #[new]
253    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
254        let t = Self::type_object(py);
255        Self::py_from_str(&t, value)
256    }
257
258    fn __hash__(&self) -> isize {
259        *self as isize
260    }
261
262    fn __repr__(&self) -> String {
263        format!(
264            "<{}.{}: '{}'>",
265            stringify!(OKXTradeMode),
266            self.name(),
267            self.value(),
268        )
269    }
270
271    fn __str__(&self) -> String {
272        self.to_string()
273    }
274
275    #[getter]
276    #[must_use]
277    pub fn name(&self) -> &str {
278        self.as_ref()
279    }
280
281    #[getter]
282    #[must_use]
283    pub fn value(&self) -> u8 {
284        *self as u8
285    }
286
287    #[staticmethod]
288    #[must_use]
289    fn variants() -> Vec<String> {
290        Self::iter().map(|x| x.to_string()).collect()
291    }
292
293    #[classmethod]
294    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
295        let data_str: String = data.str()?.extract()?;
296        Self::from_str(&data_str).map_err(to_pyvalue_err)
297    }
298
299    #[classattr]
300    #[pyo3(name = "CASH")]
301    fn py_cash() -> Self {
302        Self::Cash
303    }
304
305    #[classattr]
306    #[pyo3(name = "ISOLATED")]
307    fn py_isolated() -> Self {
308        Self::Isolated
309    }
310
311    #[classattr]
312    #[pyo3(name = "CROSS")]
313    fn py_cross() -> Self {
314        Self::Cross
315    }
316
317    #[classattr]
318    #[pyo3(name = "SPOT_ISOLATED")]
319    fn py_spot_isolated() -> Self {
320        Self::SpotIsolated
321    }
322}
323
324#[pymethods]
325impl OKXPositionMode {
326    #[new]
327    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
328        let t = Self::type_object(py);
329        Self::py_from_str(&t, value)
330    }
331
332    fn __hash__(&self) -> isize {
333        *self as isize
334    }
335
336    fn __repr__(&self) -> String {
337        format!(
338            "<{}.{}: '{}'>",
339            stringify!(OKXPositionMode),
340            self.name(),
341            self.value(),
342        )
343    }
344
345    fn __str__(&self) -> String {
346        self.to_string()
347    }
348
349    #[getter]
350    #[must_use]
351    pub fn name(&self) -> &str {
352        self.as_ref()
353    }
354
355    #[getter]
356    #[must_use]
357    pub fn value(&self) -> u8 {
358        *self as u8
359    }
360
361    #[staticmethod]
362    #[must_use]
363    fn variants() -> Vec<String> {
364        Self::iter().map(|x| x.to_string()).collect()
365    }
366
367    #[classmethod]
368    fn py_from_str(_cls: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
369        let data_str: String = data.str()?.extract()?;
370        Self::from_str(&data_str).map_err(to_pyvalue_err)
371    }
372
373    #[classattr]
374    #[pyo3(name = "NET_MODE")]
375    fn py_net_mode() -> Self {
376        Self::NetMode
377    }
378
379    #[classattr]
380    #[pyo3(name = "LONG_SHORT_MODE")]
381    fn py_long_short_mode() -> Self {
382        Self::LongShortMode
383    }
384}