Skip to main content

nautilus_bybit/python/
config.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//! Python bindings for Bybit configuration.
17
18use nautilus_model::identifiers::AccountId;
19use pyo3::pymethods;
20
21use crate::{
22    common::enums::{BybitEnvironment, BybitMarginMode, BybitProductType},
23    config::{BybitDataClientConfig, BybitExecClientConfig},
24};
25
26#[pymethods]
27#[pyo3_stub_gen::derive::gen_stub_pymethods]
28impl BybitDataClientConfig {
29    /// Configuration for the Bybit live data client.
30    #[new]
31    #[pyo3(signature = (
32        product_types = None,
33        environment = None,
34        api_key = None,
35        api_secret = None,
36        base_url_http = None,
37        base_url_ws_public = None,
38        base_url_ws_private = None,
39        proxy_url = None,
40        http_timeout_secs = None,
41        max_retries = None,
42        retry_delay_initial_ms = None,
43        retry_delay_max_ms = None,
44        heartbeat_interval_secs = None,
45        recv_window_ms = None,
46        update_instruments_interval_mins = None,
47        instrument_status_poll_secs = None,
48    ))]
49    #[expect(clippy::too_many_arguments)]
50    fn py_new(
51        product_types: Option<Vec<BybitProductType>>,
52        environment: Option<BybitEnvironment>,
53        api_key: Option<String>,
54        api_secret: Option<String>,
55        base_url_http: Option<String>,
56        base_url_ws_public: Option<String>,
57        base_url_ws_private: Option<String>,
58        proxy_url: Option<String>,
59        http_timeout_secs: Option<u64>,
60        max_retries: Option<u32>,
61        retry_delay_initial_ms: Option<u64>,
62        retry_delay_max_ms: Option<u64>,
63        heartbeat_interval_secs: Option<u64>,
64        recv_window_ms: Option<u64>,
65        update_instruments_interval_mins: Option<u64>,
66        instrument_status_poll_secs: Option<u64>,
67    ) -> Self {
68        let defaults = Self::default();
69        Self {
70            api_key,
71            api_secret,
72            product_types: product_types.unwrap_or(defaults.product_types),
73            environment: environment.unwrap_or(defaults.environment),
74            base_url_http,
75            base_url_ws_public,
76            base_url_ws_private,
77            proxy_url,
78            http_timeout_secs: http_timeout_secs.unwrap_or(defaults.http_timeout_secs),
79            max_retries: max_retries.unwrap_or(defaults.max_retries),
80            retry_delay_initial_ms: retry_delay_initial_ms
81                .unwrap_or(defaults.retry_delay_initial_ms),
82            retry_delay_max_ms: retry_delay_max_ms.unwrap_or(defaults.retry_delay_max_ms),
83            heartbeat_interval_secs: heartbeat_interval_secs
84                .unwrap_or(defaults.heartbeat_interval_secs),
85            recv_window_ms: recv_window_ms.unwrap_or(defaults.recv_window_ms),
86            update_instruments_interval_mins: update_instruments_interval_mins
87                .or(defaults.update_instruments_interval_mins),
88            instrument_status_poll_secs: instrument_status_poll_secs
89                .or(defaults.instrument_status_poll_secs),
90            transport_backend: defaults.transport_backend,
91        }
92    }
93
94    fn __repr__(&self) -> String {
95        format!("{self:?}")
96    }
97}
98
99#[pymethods]
100#[pyo3_stub_gen::derive::gen_stub_pymethods]
101impl BybitExecClientConfig {
102    /// Configuration for the Bybit live execution client.
103    #[new]
104    #[pyo3(signature = (
105        product_types = None,
106        environment = None,
107        api_key = None,
108        api_secret = None,
109        base_url_http = None,
110        base_url_ws_private = None,
111        base_url_ws_trade = None,
112        proxy_url = None,
113        http_timeout_secs = None,
114        max_retries = None,
115        retry_delay_initial_ms = None,
116        retry_delay_max_ms = None,
117        heartbeat_interval_secs = None,
118        recv_window_ms = None,
119        account_id = None,
120        use_spot_position_reports = None,
121        margin_mode = None,
122    ))]
123    #[expect(clippy::too_many_arguments)]
124    fn py_new(
125        product_types: Option<Vec<BybitProductType>>,
126        environment: Option<BybitEnvironment>,
127        api_key: Option<String>,
128        api_secret: Option<String>,
129        base_url_http: Option<String>,
130        base_url_ws_private: Option<String>,
131        base_url_ws_trade: Option<String>,
132        proxy_url: Option<String>,
133        http_timeout_secs: Option<u64>,
134        max_retries: Option<u32>,
135        retry_delay_initial_ms: Option<u64>,
136        retry_delay_max_ms: Option<u64>,
137        heartbeat_interval_secs: Option<u64>,
138        recv_window_ms: Option<u64>,
139        account_id: Option<AccountId>,
140        use_spot_position_reports: Option<bool>,
141        margin_mode: Option<BybitMarginMode>,
142    ) -> Self {
143        let defaults = Self::default();
144        Self {
145            api_key,
146            api_secret,
147            product_types: product_types.unwrap_or(defaults.product_types),
148            environment: environment.unwrap_or(defaults.environment),
149            base_url_http,
150            base_url_ws_private,
151            base_url_ws_trade,
152            proxy_url,
153            http_timeout_secs: http_timeout_secs.unwrap_or(defaults.http_timeout_secs),
154            max_retries: max_retries.unwrap_or(defaults.max_retries),
155            retry_delay_initial_ms: retry_delay_initial_ms
156                .unwrap_or(defaults.retry_delay_initial_ms),
157            retry_delay_max_ms: retry_delay_max_ms.unwrap_or(defaults.retry_delay_max_ms),
158            heartbeat_interval_secs: heartbeat_interval_secs
159                .unwrap_or(defaults.heartbeat_interval_secs),
160            recv_window_ms: recv_window_ms.unwrap_or(defaults.recv_window_ms),
161            account_id,
162            use_spot_position_reports: use_spot_position_reports
163                .unwrap_or(defaults.use_spot_position_reports),
164            futures_leverages: None,
165            position_mode: None,
166            margin_mode,
167            transport_backend: defaults.transport_backend,
168        }
169    }
170
171    fn __repr__(&self) -> String {
172        format!("{self:?}")
173    }
174}