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
16//! Core constants shared across the OKX adapter components.
17
18use std::sync::LazyLock;
19
20use ahash::AHashSet;
21use nautilus_model::{
22 enums::{OrderType, TimeInForce},
23 identifiers::Venue,
24};
25use ustr::Ustr;
26
27pub const OKX: &str = "OKX";
28pub static OKX_VENUE: LazyLock<Venue> = LazyLock::new(|| Venue::new(Ustr::from(OKX)));
29
30/// See <https://www.okx.com/docs-v5/en/#overview-broker-program> for further details.
31pub const OKX_NAUTILUS_BROKER_ID: &str = "a535cbe8d0c8BCDE";
32
33// Use the canonical host with www to avoid cross-domain redirects which may
34// strip authentication headers in some HTTP clients and middleboxes.
35pub const OKX_HTTP_URL: &str = "https://www.okx.com";
36pub const OKX_WS_PUBLIC_URL: &str = "wss://ws.okx.com:8443/ws/v5/public";
37pub const OKX_WS_PRIVATE_URL: &str = "wss://ws.okx.com:8443/ws/v5/private";
38pub const OKX_WS_BUSINESS_URL: &str = "wss://ws.okx.com:8443/ws/v5/business";
39pub const OKX_WS_DEMO_PUBLIC_URL: &str = "wss://wspap.okx.com:8443/ws/v5/public";
40pub const OKX_WS_DEMO_PRIVATE_URL: &str = "wss://wspap.okx.com:8443/ws/v5/private";
41pub const OKX_WS_DEMO_BUSINESS_URL: &str = "wss://wspap.okx.com:8443/ws/v5/business";
42
43/// OKX supported order time in force for market orders.
44///
45/// # Notes
46///
47/// - OKX implements IOC and FOK as order types rather than separate time-in-force parameters.
48/// - GTD is supported via expire_time parameter.
49pub const OKX_SUPPORTED_TIME_IN_FORCE: &[TimeInForce] = &[
50 TimeInForce::Gtc, // Good Till Cancel (default)
51 TimeInForce::Ioc, // Immediate or Cancel (mapped to OKXOrderType::Ioc)
52 TimeInForce::Fok, // Fill or Kill (mapped to OKXOrderType::Fok)
53];
54
55/// OKX supported order types.
56///
57/// # Notes
58///
59/// - PostOnly is supported as a flag on limit orders.
60/// - Conditional orders (stop/trigger) are supported via algo orders.
61pub const OKX_SUPPORTED_ORDER_TYPES: &[OrderType] = &[
62 OrderType::Market,
63 OrderType::Limit,
64 OrderType::MarketToLimit, // Mapped to IOC when no price is specified
65 OrderType::StopMarket, // Supported via algo order API
66 OrderType::StopLimit, // Supported via algo order API
67 OrderType::MarketIfTouched, // Supported via algo order API
68 OrderType::LimitIfTouched, // Supported via algo order API
69];
70
71/// Conditional order types that require the OKX algo order API.
72pub const OKX_CONDITIONAL_ORDER_TYPES: &[OrderType] = &[
73 OrderType::StopMarket,
74 OrderType::StopLimit,
75 OrderType::MarketIfTouched,
76 OrderType::LimitIfTouched,
77];
78
79/// OKX error codes that should trigger retries.
80///
81/// Only retry on temporary network/system issues.
82///
83/// # References
84///
85/// Based on OKX API documentation: <https://www.okx.com/docs-v5/en/#error-codes>
86pub static OKX_RETRY_ERROR_CODES: LazyLock<AHashSet<&'static str>> = LazyLock::new(|| {
87 let mut codes = AHashSet::new();
88
89 // Temporary system errors
90 codes.insert("50001"); // Service temporarily unavailable
91 codes.insert("50004"); // API endpoint request timeout (does not mean that the request was successful or failed, please check the request result)
92 codes.insert("50005"); // API is offline or unavailable
93 codes.insert("50013"); // System busy, please try again later
94 codes.insert("50026"); // System error, please try again later
95
96 // Rate limit errors (temporary)
97 codes.insert("50011"); // Request too frequent
98 codes.insert("50113"); // API requests exceed the limit
99
100 // WebSocket connection issues (temporary)
101 codes.insert("60001"); // OK not received in time
102 codes.insert("60005"); // Connection closed as there was no data transmission in the last 30 seconds
103
104 codes
105});
106
107/// Determines if an OKX error code should trigger a retry.
108pub fn should_retry_error_code(error_code: &str) -> bool {
109 OKX_RETRY_ERROR_CODES.contains(error_code)
110}
111
112/// OKX error code returned when a post-only order would immediately take liquidity.
113pub const OKX_POST_ONLY_ERROR_CODE: &str = "51019";
114
115/// OKX cancel source code used when a post-only order is auto-cancelled for taking liquidity.
116pub const OKX_POST_ONLY_CANCEL_SOURCE: &str = "31";
117
118/// Human-readable reason used when a post-only order is auto-cancelled for taking liquidity.
119pub const OKX_POST_ONLY_CANCEL_REASON: &str = "POST_ONLY would take liquidity";
120
121/// Target currency literal for base currency.
122pub const OKX_TARGET_CCY_BASE: &str = "base_ccy";
123
124/// Target currency literal for quote currency.
125pub const OKX_TARGET_CCY_QUOTE: &str = "quote_ccy";