nautilus_okx/common/
consts.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
16use std::{collections::HashSet, sync::LazyLock};
17
18use nautilus_model::{
19    enums::{OrderType, TimeInForce},
20    identifiers::Venue,
21};
22use ustr::Ustr;
23
24pub const OKX: &str = "OKX";
25pub static OKX_VENUE: LazyLock<Venue> = LazyLock::new(|| Venue::new(Ustr::from(OKX)));
26
27/// See <https://www.okx.com/docs-v5/en/#overview-broker-program> for further details.
28pub const OKX_NAUTILUS_BROKER_ID: &str = "a535cbe8d0c8BCDE";
29
30pub const OKX_HTTP_URL: &str = "https://okx.com";
31pub const OKX_WS_PUBLIC_URL: &str = "wss://ws.okx.com:8443/ws/v5/public";
32pub const OKX_WS_PRIVATE_URL: &str = "wss://ws.okx.com:8443/ws/v5/private";
33pub const OKX_WS_BUSINESS_URL: &str = "wss://ws.okx.com:8443/ws/v5/business";
34pub const OKX_WS_DEMO_PUBLIC_URL: &str = "wss://wspap.okx.com:8443/ws/v5/public";
35pub const OKX_WS_DEMO_PRIVATE_URL: &str = "wss://wspap.okx.com:8443/ws/v5/private";
36pub const OKX_WS_DEMO_BUSINESS_URL: &str = "wss://wspap.okx.com:8443/ws/v5/business";
37
38/// OKX supported order time in force for market orders.
39///
40/// # Notes
41///
42/// - OKX implements IOC and FOK as order types rather than separate time-in-force parameters.
43/// - GTD is supported via expire_time parameter.
44pub const OKX_SUPPORTED_TIME_IN_FORCE: &[TimeInForce] = &[
45    TimeInForce::Gtc, // Good Till Cancel (default)
46    TimeInForce::Ioc, // Immediate or Cancel (mapped to OKXOrderType::Ioc)
47    TimeInForce::Fok, // Fill or Kill (mapped to OKXOrderType::Fok)
48];
49
50/// OKX supported order types.
51///
52/// # Notes
53///
54/// - PostOnly is supported as a flag on limit orders.
55pub const OKX_SUPPORTED_ORDER_TYPES: &[OrderType] = &[
56    OrderType::Market,
57    OrderType::Limit,
58    OrderType::MarketToLimit, // Mapped to IOC when no price is specified
59];
60
61/// OKX error codes that should trigger retries.
62/// Based on OKX API documentation: <https://www.okx.com/docs-v5/en/#error-codes>
63/// Only retry on temporary network/system issues.
64pub static OKX_RETRY_ERROR_CODES: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
65    let mut codes = HashSet::new();
66
67    // Temporary system errors
68    codes.insert("50001"); // Service temporarily unavailable
69    codes.insert("50004"); // API endpoint request timeout (does not mean that the request was successful or failed, please check the request result)
70    codes.insert("50005"); // API is offline or unavailable
71    codes.insert("50013"); // System busy, please try again later
72    codes.insert("50026"); // System error, please try again later
73
74    // Rate limit errors (temporary)
75    codes.insert("50011"); // Request too frequent
76    codes.insert("50113"); // API requests exceed the limit
77
78    // WebSocket connection issues (temporary)
79    codes.insert("60001"); // OK not received in time
80    codes.insert("60005"); // Connection closed as there was no data transmission in the last 30 seconds
81
82    codes
83});
84
85/// Determines if an OKX error code should trigger a retry.
86pub fn should_retry_error_code(error_code: &str) -> bool {
87    OKX_RETRY_ERROR_CODES.contains(error_code)
88}