Skip to main content

nautilus_okx/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 OKX configuration.
17
18use nautilus_model::identifiers::{AccountId, TraderId};
19use pyo3::prelude::*;
20
21use crate::{
22    common::enums::{OKXEnvironment, OKXInstrumentType, OKXMarginMode, OKXVipLevel},
23    config::{OKXDataClientConfig, OKXExecClientConfig},
24};
25
26#[pymethods]
27#[pyo3_stub_gen::derive::gen_stub_pymethods]
28impl OKXDataClientConfig {
29    /// Configuration for the OKX data client.
30    #[new]
31    #[pyo3(signature = (
32        instrument_types = None,
33        environment = None,
34        api_key = None,
35        api_secret = None,
36        api_passphrase = None,
37        base_url_http = None,
38        base_url_ws_public = None,
39        base_url_ws_business = None,
40        proxy_url = None,
41        http_timeout_secs = None,
42        max_retries = None,
43        retry_delay_initial_ms = None,
44        retry_delay_max_ms = None,
45        update_instruments_interval_mins = None,
46        vip_level = None,
47    ))]
48    #[expect(clippy::too_many_arguments)]
49    fn py_new(
50        instrument_types: Option<Vec<OKXInstrumentType>>,
51        environment: Option<OKXEnvironment>,
52        api_key: Option<String>,
53        api_secret: Option<String>,
54        api_passphrase: Option<String>,
55        base_url_http: Option<String>,
56        base_url_ws_public: Option<String>,
57        base_url_ws_business: 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        update_instruments_interval_mins: Option<u64>,
64        vip_level: Option<OKXVipLevel>,
65    ) -> Self {
66        let defaults = Self::default();
67        Self {
68            api_key,
69            api_secret,
70            api_passphrase,
71            instrument_types: instrument_types.unwrap_or(defaults.instrument_types),
72            contract_types: None,
73            instrument_families: None,
74            base_url_http,
75            base_url_ws_public,
76            base_url_ws_business,
77            proxy_url,
78            environment: environment.unwrap_or(defaults.environment),
79            http_timeout_secs: http_timeout_secs.unwrap_or(defaults.http_timeout_secs),
80            max_retries: max_retries.unwrap_or(defaults.max_retries),
81            retry_delay_initial_ms: retry_delay_initial_ms
82                .unwrap_or(defaults.retry_delay_initial_ms),
83            retry_delay_max_ms: retry_delay_max_ms.unwrap_or(defaults.retry_delay_max_ms),
84            update_instruments_interval_mins: update_instruments_interval_mins
85                .unwrap_or(defaults.update_instruments_interval_mins),
86            vip_level,
87            transport_backend: defaults.transport_backend,
88        }
89    }
90
91    fn __repr__(&self) -> String {
92        format!("{self:?}")
93    }
94}
95
96#[pymethods]
97#[pyo3_stub_gen::derive::gen_stub_pymethods]
98impl OKXExecClientConfig {
99    /// Configuration for the OKX execution client.
100    #[new]
101    #[pyo3(signature = (
102        trader_id,
103        account_id,
104        instrument_types = None,
105        environment = None,
106        api_key = None,
107        api_secret = None,
108        api_passphrase = None,
109        base_url_http = None,
110        base_url_ws_private = None,
111        base_url_ws_business = 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        margin_mode = None,
118    ))]
119    #[expect(clippy::too_many_arguments)]
120    fn py_new(
121        trader_id: TraderId,
122        account_id: AccountId,
123        instrument_types: Option<Vec<OKXInstrumentType>>,
124        environment: Option<OKXEnvironment>,
125        api_key: Option<String>,
126        api_secret: Option<String>,
127        api_passphrase: Option<String>,
128        base_url_http: Option<String>,
129        base_url_ws_private: Option<String>,
130        base_url_ws_business: Option<String>,
131        proxy_url: Option<String>,
132        http_timeout_secs: Option<u64>,
133        max_retries: Option<u32>,
134        retry_delay_initial_ms: Option<u64>,
135        retry_delay_max_ms: Option<u64>,
136        margin_mode: Option<OKXMarginMode>,
137    ) -> Self {
138        let defaults = Self::default();
139        Self {
140            trader_id,
141            account_id,
142            api_key,
143            api_secret,
144            api_passphrase,
145            instrument_types: instrument_types.unwrap_or(defaults.instrument_types),
146            contract_types: None,
147            instrument_families: None,
148            base_url_http,
149            base_url_ws_private,
150            base_url_ws_business,
151            proxy_url,
152            environment: environment.unwrap_or(defaults.environment),
153            http_timeout_secs: http_timeout_secs.unwrap_or(defaults.http_timeout_secs),
154            use_fills_channel: defaults.use_fills_channel,
155            use_mm_mass_cancel: defaults.use_mm_mass_cancel,
156            max_retries: max_retries.unwrap_or(defaults.max_retries),
157            retry_delay_initial_ms: retry_delay_initial_ms
158                .unwrap_or(defaults.retry_delay_initial_ms),
159            retry_delay_max_ms: retry_delay_max_ms.unwrap_or(defaults.retry_delay_max_ms),
160            margin_mode,
161            use_spot_margin: defaults.use_spot_margin,
162            transport_backend: defaults.transport_backend,
163        }
164    }
165
166    fn __repr__(&self) -> String {
167        format!("{self:?}")
168    }
169}