nautilus_bybit/python/
mod.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//! Python bindings from `pyo3`.
17
18pub mod enums;
19pub mod http;
20pub mod params;
21pub mod urls;
22pub mod websocket;
23
24use nautilus_core::python::to_pyvalue_err;
25use nautilus_model::enums::BarAggregation;
26use pyo3::prelude::*;
27
28use crate::common::{
29    consts::BYBIT_NAUTILUS_BROKER_ID,
30    parse::{bar_spec_to_bybit_interval, extract_raw_symbol},
31    symbol::BybitSymbol,
32};
33
34/// Extracts the raw symbol from a Bybit symbol by removing the product type suffix.
35///
36/// # Examples
37/// - `"ETHUSDT-LINEAR"` → `"ETHUSDT"`
38/// - `"BTCUSDT-SPOT"` → `"BTCUSDT"`
39/// - `"ETHUSDT"` → `"ETHUSDT"` (no suffix)
40#[pyfunction]
41#[pyo3(name = "bybit_extract_raw_symbol")]
42fn py_bybit_extract_raw_symbol(symbol: &str) -> &str {
43    extract_raw_symbol(symbol)
44}
45
46/// Converts a Nautilus bar aggregation and step to a Bybit kline interval string.
47///
48/// # Errors
49///
50/// Returns an error if the aggregation type or step is not supported by Bybit.
51#[pyfunction]
52#[pyo3(name = "bybit_bar_spec_to_interval")]
53fn py_bybit_bar_spec_to_interval(aggregation: u8, step: u64) -> PyResult<String> {
54    let aggregation = BarAggregation::from_repr(aggregation as usize).ok_or_else(|| {
55        pyo3::exceptions::PyValueError::new_err(format!(
56            "Invalid BarAggregation value: {aggregation}"
57        ))
58    })?;
59    let interval = bar_spec_to_bybit_interval(aggregation, step).map_err(to_pyvalue_err)?;
60    Ok(interval.to_string())
61}
62
63/// Extracts the product type from a Bybit symbol.
64///
65/// # Examples
66/// - `"ETHUSDT-LINEAR"` → `BybitProductType.LINEAR`
67/// - `"BTCUSDT-SPOT"` → `BybitProductType.SPOT`
68/// - `"BTCUSD-INVERSE"` → `BybitProductType.INVERSE`
69/// - `"ETH-26JUN26-16000-P-OPTION"` → `BybitProductType.OPTION`
70///
71/// # Errors
72///
73/// Returns an error if the symbol does not contain a valid Bybit product type suffix.
74#[pyfunction]
75#[pyo3(name = "bybit_product_type_from_symbol")]
76fn py_bybit_product_type_from_symbol(
77    symbol: &str,
78) -> PyResult<crate::common::enums::BybitProductType> {
79    let bybit_symbol = BybitSymbol::new(symbol).map_err(to_pyvalue_err)?;
80    Ok(bybit_symbol.product_type())
81}
82
83/// Loaded as `nautilus_pyo3.bybit`.
84///
85/// # Errors
86///
87/// Returns an error if any bindings fail to register with the Python module.
88#[pymodule]
89#[rustfmt::skip]
90pub fn bybit(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
91    m.add(stringify!(BYBIT_NAUTILUS_BROKER_ID), BYBIT_NAUTILUS_BROKER_ID)?;
92    m.add_class::<crate::common::enums::BybitAccountType>()?;
93    m.add_class::<crate::common::enums::BybitMarginMode>()?;
94    m.add_class::<crate::common::enums::BybitPositionMode>()?;
95    m.add_class::<crate::common::enums::BybitProductType>()?;
96    m.add_class::<crate::common::enums::BybitEnvironment>()?;
97    m.add_class::<crate::http::client::BybitHttpClient>()?;
98    m.add_class::<crate::http::models::BybitTickerData>()?;
99    m.add_class::<crate::websocket::client::BybitWebSocketClient>()?;
100    m.add_class::<crate::websocket::messages::BybitWebSocketError>()?;
101    m.add_class::<params::BybitWsPlaceOrderParams>()?;
102    m.add_class::<params::BybitWsAmendOrderParams>()?;
103    m.add_function(wrap_pyfunction!(urls::py_get_bybit_http_base_url, m)?)?;
104    m.add_function(wrap_pyfunction!(urls::py_get_bybit_ws_url_public, m)?)?;
105    m.add_function(wrap_pyfunction!(urls::py_get_bybit_ws_url_private, m)?)?;
106    m.add_function(wrap_pyfunction!(urls::py_get_bybit_ws_url_trade, m)?)?;
107    m.add_function(wrap_pyfunction!(py_bybit_extract_raw_symbol, m)?)?;
108    m.add_function(wrap_pyfunction!(py_bybit_bar_spec_to_interval, m)?)?;
109    m.add_function(wrap_pyfunction!(py_bybit_product_type_from_symbol, m)?)?;
110    Ok(())
111}