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