Skip to main content

nautilus_binance/python/
types.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
16use std::{
17    collections::hash_map::DefaultHasher,
18    hash::{Hash, Hasher},
19};
20
21use nautilus_core::python::{IntoPyObjectNautilusExt, serialization::from_dict_pyo3};
22use nautilus_model::{
23    data::bar::BarType,
24    types::{Price, Quantity},
25};
26use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
27use rust_decimal::Decimal;
28
29use crate::common::bar::BinanceBar;
30
31#[pymethods]
32#[pyo3_stub_gen::derive::gen_stub_pymethods]
33impl BinanceBar {
34    fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
35        match op {
36            CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
37            CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
38            _ => py.NotImplemented(),
39        }
40    }
41
42    fn __hash__(&self) -> isize {
43        let mut hasher = DefaultHasher::new();
44        self.bar_type.hash(&mut hasher);
45        self.ts_event.hash(&mut hasher);
46        hasher.finish() as isize
47    }
48
49    fn __repr__(&self) -> String {
50        format!(
51            "{}(bar_type={}, open={}, high={}, low={}, close={}, volume={}, quote_volume={}, count={}, taker_buy_base_volume={}, taker_buy_quote_volume={}, ts_event={}, ts_init={})",
52            stringify!(BinanceBar),
53            self.bar_type,
54            self.open,
55            self.high,
56            self.low,
57            self.close,
58            self.volume,
59            self.quote_volume,
60            self.count,
61            self.taker_buy_base_volume,
62            self.taker_buy_quote_volume,
63            self.ts_event,
64            self.ts_init,
65        )
66    }
67
68    fn __str__(&self) -> String {
69        self.__repr__()
70    }
71
72    #[getter]
73    #[pyo3(name = "bar_type")]
74    fn py_bar_type(&self) -> BarType {
75        self.bar_type
76    }
77
78    #[getter]
79    #[pyo3(name = "open")]
80    const fn py_open(&self) -> Price {
81        self.open
82    }
83
84    #[getter]
85    #[pyo3(name = "high")]
86    const fn py_high(&self) -> Price {
87        self.high
88    }
89
90    #[getter]
91    #[pyo3(name = "low")]
92    const fn py_low(&self) -> Price {
93        self.low
94    }
95
96    #[getter]
97    #[pyo3(name = "close")]
98    const fn py_close(&self) -> Price {
99        self.close
100    }
101
102    #[getter]
103    #[pyo3(name = "volume")]
104    const fn py_volume(&self) -> Quantity {
105        self.volume
106    }
107
108    #[getter]
109    #[pyo3(name = "quote_volume")]
110    fn py_quote_volume(&self) -> Decimal {
111        self.quote_volume
112    }
113
114    #[getter]
115    #[pyo3(name = "count")]
116    const fn py_count(&self) -> u64 {
117        self.count
118    }
119
120    #[getter]
121    #[pyo3(name = "taker_buy_base_volume")]
122    fn py_taker_buy_base_volume(&self) -> Decimal {
123        self.taker_buy_base_volume
124    }
125
126    #[getter]
127    #[pyo3(name = "taker_buy_quote_volume")]
128    fn py_taker_buy_quote_volume(&self) -> Decimal {
129        self.taker_buy_quote_volume
130    }
131
132    #[getter]
133    #[pyo3(name = "ts_event")]
134    const fn py_ts_event(&self) -> u64 {
135        self.ts_event.as_u64()
136    }
137
138    #[getter]
139    #[pyo3(name = "ts_init")]
140    const fn py_ts_init(&self) -> u64 {
141        self.ts_init.as_u64()
142    }
143
144    #[staticmethod]
145    #[pyo3(name = "from_dict")]
146    fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
147        from_dict_pyo3(py, values)
148    }
149
150    /// # Errors
151    ///
152    /// Returns a `PyErr` if generating the Python dictionary fails.
153    #[pyo3(name = "to_dict")]
154    pub fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
155        let dict = PyDict::new(py);
156        dict.set_item("type", stringify!(BinanceBar))?;
157        dict.set_item("bar_type", self.bar_type.to_string())?;
158        dict.set_item("open", self.open.to_string())?;
159        dict.set_item("high", self.high.to_string())?;
160        dict.set_item("low", self.low.to_string())?;
161        dict.set_item("close", self.close.to_string())?;
162        dict.set_item("volume", self.volume.to_string())?;
163        dict.set_item("quote_volume", self.quote_volume.to_string())?;
164        dict.set_item("count", self.count)?;
165        dict.set_item(
166            "taker_buy_base_volume",
167            self.taker_buy_base_volume.to_string(),
168        )?;
169        dict.set_item(
170            "taker_buy_quote_volume",
171            self.taker_buy_quote_volume.to_string(),
172        )?;
173        dict.set_item("ts_event", self.ts_event.as_u64())?;
174        dict.set_item("ts_init", self.ts_init.as_u64())?;
175        Ok(dict.into())
176    }
177}