nautilus_okx/config.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//! Configuration structures for the OKX adapter.
17
18use crate::common::{
19 enums::{OKXContractType, OKXInstrumentType, OKXMarginMode, OKXVipLevel},
20 urls::{
21 get_http_base_url, get_ws_base_url_business, get_ws_base_url_private,
22 get_ws_base_url_public,
23 },
24};
25
26/// Configuration for the OKX data client.
27#[derive(Clone, Debug)]
28pub struct OKXDataClientConfig {
29 /// Optional API key for authenticated endpoints.
30 pub api_key: Option<String>,
31 /// Optional API secret for authenticated endpoints.
32 pub api_secret: Option<String>,
33 /// Optional API passphrase for authenticated endpoints.
34 pub api_passphrase: Option<String>,
35 /// Instrument types to load and subscribe to.
36 pub instrument_types: Vec<OKXInstrumentType>,
37 /// Contract type filter applied to loaded instruments.
38 pub contract_types: Option<Vec<OKXContractType>>,
39 /// Instrument families to load (e.g., "BTC-USD", "ETH-USD").
40 /// Required for OPTIONS. Optional for FUTURES/SWAP. Not applicable for SPOT/MARGIN.
41 pub instrument_families: Option<Vec<String>>,
42 /// Optional override for the HTTP base URL.
43 pub base_url_http: Option<String>,
44 /// Optional override for the public WebSocket URL.
45 pub base_url_ws_public: Option<String>,
46 /// Optional override for the business WebSocket URL.
47 pub base_url_ws_business: Option<String>,
48 /// Optional HTTP proxy URL.
49 pub http_proxy_url: Option<String>,
50 /// Optional WebSocket proxy URL.
51 ///
52 /// Note: WebSocket proxy support is not yet implemented. This field is reserved
53 /// for future functionality. Use `http_proxy_url` for REST API proxy support.
54 pub ws_proxy_url: Option<String>,
55 /// When true the client will use OKX demo endpoints.
56 pub is_demo: bool,
57 /// Optional HTTP timeout in seconds.
58 pub http_timeout_secs: Option<u64>,
59 /// Optional maximum retry attempts for requests.
60 pub max_retries: Option<u32>,
61 /// Optional initial retry delay in milliseconds.
62 pub retry_delay_initial_ms: Option<u64>,
63 /// Optional maximum retry delay in milliseconds.
64 pub retry_delay_max_ms: Option<u64>,
65 /// Optional interval for refreshing instruments.
66 pub update_instruments_interval_mins: Option<u64>,
67 /// Optional VIP level that unlocks additional subscriptions.
68 pub vip_level: Option<OKXVipLevel>,
69}
70
71impl Default for OKXDataClientConfig {
72 fn default() -> Self {
73 Self {
74 api_key: None,
75 api_secret: None,
76 api_passphrase: None,
77 instrument_types: vec![OKXInstrumentType::Spot],
78 contract_types: None,
79 instrument_families: None,
80 base_url_http: None,
81 base_url_ws_public: None,
82 base_url_ws_business: None,
83 http_proxy_url: None,
84 ws_proxy_url: None,
85 is_demo: false,
86 http_timeout_secs: Some(60),
87 max_retries: Some(3),
88 retry_delay_initial_ms: Some(1_000),
89 retry_delay_max_ms: Some(10_000),
90 update_instruments_interval_mins: Some(60),
91 vip_level: None,
92 }
93 }
94}
95
96impl OKXDataClientConfig {
97 /// Creates a new configuration with default settings.
98 #[must_use]
99 pub fn new() -> Self {
100 Self::default()
101 }
102
103 /// Returns `true` when all API credential fields are populated.
104 #[must_use]
105 pub fn has_api_credentials(&self) -> bool {
106 self.api_key.is_some() && self.api_secret.is_some() && self.api_passphrase.is_some()
107 }
108
109 /// Returns the HTTP base URL, falling back to the default when unset.
110 #[must_use]
111 pub fn http_base_url(&self) -> String {
112 self.base_url_http.clone().unwrap_or_else(get_http_base_url)
113 }
114
115 /// Returns the public WebSocket URL, respecting the demo flag and overrides.
116 #[must_use]
117 pub fn ws_public_url(&self) -> String {
118 self.base_url_ws_public
119 .clone()
120 .unwrap_or_else(|| get_ws_base_url_public(self.is_demo))
121 }
122
123 /// Returns the business WebSocket URL, respecting the demo flag and overrides.
124 #[must_use]
125 pub fn ws_business_url(&self) -> String {
126 self.base_url_ws_business
127 .clone()
128 .unwrap_or_else(|| get_ws_base_url_business(self.is_demo))
129 }
130
131 /// Returns `true` when the business WebSocket should be instantiated.
132 #[must_use]
133 pub fn requires_business_ws(&self) -> bool {
134 self.has_api_credentials()
135 }
136}
137
138/// Configuration for the OKX execution client.
139#[derive(Clone, Debug)]
140pub struct OKXExecClientConfig {
141 /// Optional API key for authenticated endpoints.
142 pub api_key: Option<String>,
143 /// Optional API secret for authenticated endpoints.
144 pub api_secret: Option<String>,
145 /// Optional API passphrase for authenticated endpoints.
146 pub api_passphrase: Option<String>,
147 /// Instrument types the execution client should support.
148 pub instrument_types: Vec<OKXInstrumentType>,
149 /// Contract type filter applied to operations.
150 pub contract_types: Option<Vec<OKXContractType>>,
151 /// Instrument families to load (e.g., "BTC-USD", "ETH-USD").
152 /// Required for OPTIONS. Optional for FUTURES/SWAP. Not applicable for SPOT/MARGIN.
153 pub instrument_families: Option<Vec<String>>,
154 /// Optional override for the HTTP base URL.
155 pub base_url_http: Option<String>,
156 /// Optional override for the private WebSocket URL.
157 pub base_url_ws_private: Option<String>,
158 /// Optional override for the business WebSocket URL.
159 pub base_url_ws_business: Option<String>,
160 /// Optional HTTP proxy URL.
161 pub http_proxy_url: Option<String>,
162 /// Optional WebSocket proxy URL.
163 ///
164 /// Note: WebSocket proxy support is not yet implemented. This field is reserved
165 /// for future functionality. Use `http_proxy_url` for REST API proxy support.
166 pub ws_proxy_url: Option<String>,
167 /// When true the client will use OKX demo endpoints.
168 pub is_demo: bool,
169 /// Optional HTTP timeout in seconds.
170 pub http_timeout_secs: Option<u64>,
171 /// Enables consumption of the fills WebSocket channel when true.
172 pub use_fills_channel: bool,
173 /// Enables mass-cancel support when true.
174 pub use_mm_mass_cancel: bool,
175 /// Optional maximum retry attempts for requests.
176 pub max_retries: Option<u32>,
177 /// Optional initial retry delay in milliseconds.
178 pub retry_delay_initial_ms: Option<u64>,
179 /// Optional maximum retry delay in milliseconds.
180 pub retry_delay_max_ms: Option<u64>,
181 /// Optional margin mode (CROSS or ISOLATED) for margin/derivative accounts.
182 pub margin_mode: Option<OKXMarginMode>,
183 /// Enables margin/leverage for SPOT trading when true.
184 pub use_spot_margin: bool,
185}
186
187impl Default for OKXExecClientConfig {
188 fn default() -> Self {
189 Self {
190 api_key: None,
191 api_secret: None,
192 api_passphrase: None,
193 instrument_types: vec![OKXInstrumentType::Spot],
194 contract_types: None,
195 instrument_families: None,
196 base_url_http: None,
197 base_url_ws_private: None,
198 base_url_ws_business: None,
199 http_proxy_url: None,
200 ws_proxy_url: None,
201 is_demo: false,
202 http_timeout_secs: Some(60),
203 use_fills_channel: false,
204 use_mm_mass_cancel: false,
205 max_retries: Some(3),
206 retry_delay_initial_ms: Some(1_000),
207 retry_delay_max_ms: Some(10_000),
208 margin_mode: None,
209 use_spot_margin: false,
210 }
211 }
212}
213
214impl OKXExecClientConfig {
215 /// Creates a new configuration with default settings.
216 #[must_use]
217 pub fn new() -> Self {
218 Self::default()
219 }
220
221 /// Returns `true` when all API credential fields are populated.
222 #[must_use]
223 pub fn has_api_credentials(&self) -> bool {
224 self.api_key.is_some() && self.api_secret.is_some() && self.api_passphrase.is_some()
225 }
226
227 /// Returns the HTTP base URL, falling back to the default when unset.
228 #[must_use]
229 pub fn http_base_url(&self) -> String {
230 self.base_url_http.clone().unwrap_or_else(get_http_base_url)
231 }
232
233 /// Returns the private WebSocket URL, respecting the demo flag and overrides.
234 #[must_use]
235 pub fn ws_private_url(&self) -> String {
236 self.base_url_ws_private
237 .clone()
238 .unwrap_or_else(|| get_ws_base_url_private(self.is_demo))
239 }
240
241 /// Returns the business WebSocket URL, respecting the demo flag and overrides.
242 #[must_use]
243 pub fn ws_business_url(&self) -> String {
244 self.base_url_ws_business
245 .clone()
246 .unwrap_or_else(|| get_ws_base_url_business(self.is_demo))
247 }
248}