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