Skip to main content

nautilus_okx/common/
models.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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//! Data models representing OKX API payloads consumed by the adapter.
17
18use serde::{Deserialize, Serialize};
19use ustr::Ustr;
20
21use super::enums::{OKXOptionType, OKXTriggerType};
22use crate::common::{
23    enums::{OKXContractType, OKXInstrumentCategory, OKXInstrumentStatus, OKXInstrumentType},
24    parse::{deserialize_empty_ustr_as_none, deserialize_optional_string_to_u64},
25};
26
27/// Attached TP/SL child order metadata returned by OKX on parent orders.
28#[derive(Clone, Debug, Default, Serialize, Deserialize)]
29#[serde(rename_all = "camelCase")]
30pub struct OKXAttachedAlgoOrd {
31    /// Attached algo order ID, if assigned by OKX.
32    #[serde(default)]
33    pub attach_algo_id: String,
34    /// Attached child client order ID.
35    #[serde(default)]
36    pub attach_algo_cl_ord_id: String,
37    /// Stop-loss trigger price.
38    #[serde(default)]
39    pub sl_trigger_px: String,
40    /// Stop-loss order price.
41    #[serde(default)]
42    pub sl_ord_px: String,
43    /// Stop-loss trigger price type.
44    #[serde(default)]
45    pub sl_trigger_px_type: Option<OKXTriggerType>,
46    /// Take-profit trigger price.
47    #[serde(default)]
48    pub tp_trigger_px: String,
49    /// Take-profit order price.
50    #[serde(default)]
51    pub tp_ord_px: String,
52    /// Take-profit trigger price type.
53    #[serde(default)]
54    pub tp_trigger_px_type: Option<OKXTriggerType>,
55    /// Callback ratio for attached trailing stop orders.
56    #[serde(default)]
57    pub callback_ratio: String,
58    /// Callback spread for attached trailing stop orders.
59    #[serde(default)]
60    pub callback_spread: String,
61    /// Activation price for attached trailing stop orders.
62    #[serde(default)]
63    pub active_px: String,
64}
65
66/// Represents an instrument on the OKX exchange.
67#[derive(Clone, Debug, Serialize, Deserialize)]
68#[serde(rename_all = "camelCase")]
69pub struct OKXInstrument {
70    /// Product type (SPOT, MARGIN, SWAP, FUTURES, OPTION).
71    pub inst_type: OKXInstrumentType,
72    /// Instrument ID, e.g. "BTC-USD-SWAP".
73    pub inst_id: Ustr,
74    /// Instrument ID code (numeric). Required for WebSocket order operations.
75    /// E.g., 10458 for BTC-USD-SWAP.
76    #[serde(default)]
77    pub inst_id_code: Option<u64>,
78    /// Underlying of the instrument, e.g. "BTC-USD". Only applicable to FUTURES/SWAP/OPTION.
79    pub uly: Ustr,
80    /// Instrument family, e.g. "BTC-USD". Only applicable to FUTURES/SWAP/OPTION.
81    pub inst_family: Ustr,
82    /// Event contract series ID. Only applicable to EVENTS.
83    #[serde(default, deserialize_with = "deserialize_empty_ustr_as_none")]
84    pub series_id: Option<Ustr>,
85    /// Instrument category (the OKX `instCategory` field; the deprecated, distinct
86    /// `category` field is intentionally ignored).
87    #[serde(default)]
88    pub inst_category: Option<OKXInstrumentCategory>,
89    /// Base currency, e.g. "BTC" in BTC-USDT. Applicable to SPOT/MARGIN.
90    pub base_ccy: Ustr,
91    /// Quote currency, e.g. "USDT" in BTC-USDT.
92    pub quote_ccy: Ustr,
93    /// Settlement currency, e.g. "BTC" for BTC-USD-SWAP.
94    pub settle_ccy: Ustr,
95    /// Contract value. Only applicable to FUTURES/SWAP/OPTION.
96    pub ct_val: String,
97    /// Contract multiplier. Only applicable to FUTURES/SWAP/OPTION.
98    pub ct_mult: String,
99    /// Contract value currency. Only applicable to FUTURES/SWAP/OPTION.
100    pub ct_val_ccy: String,
101    /// Option type, "C" for call options, "P" for put options. Only applicable to OPTION.
102    pub opt_type: OKXOptionType,
103    /// Strike price. Only applicable to OPTION.
104    pub stk: String,
105    /// Listing time, Unix timestamp format in milliseconds, e.g. "1597026383085".
106    #[serde(deserialize_with = "deserialize_optional_string_to_u64")]
107    pub list_time: Option<u64>,
108    /// Expiry time, Unix timestamp format in milliseconds, e.g. "1597026383085".
109    #[serde(deserialize_with = "deserialize_optional_string_to_u64")]
110    pub exp_time: Option<u64>,
111    /// Leverage. Not applicable to SPOT.
112    pub lever: String,
113    /// Tick size, e.g. "0.1".
114    pub tick_sz: String,
115    /// Lot size, e.g. "1".
116    pub lot_sz: String,
117    /// Minimum order size.
118    pub min_sz: String,
119    /// Contract type. linear: "linear", inverse: "inverse". Only applicable to FUTURES/SWAP.
120    pub ct_type: OKXContractType,
121    /// Instrument status.
122    pub state: OKXInstrumentStatus,
123    /// Rule type, e.g. "DynamicPL", "CT", etc.
124    pub rule_type: String,
125    /// Maximum limit order size.
126    #[serde(default)]
127    pub max_lmt_sz: String,
128    /// Maximum market order size.
129    #[serde(default)]
130    pub max_mkt_sz: String,
131    /// Maximum limit order amount.
132    #[serde(default)]
133    pub max_lmt_amt: String,
134    /// Maximum market order amount.
135    #[serde(default)]
136    pub max_mkt_amt: String,
137    /// Maximum TWAP order size.
138    #[serde(default)]
139    pub max_twap_sz: String,
140    /// Maximum iceberg order size.
141    #[serde(default)]
142    pub max_iceberg_sz: String,
143    /// Maximum trigger order size.
144    #[serde(default)]
145    pub max_trigger_sz: String,
146    /// Maximum stop order size.
147    #[serde(default)]
148    pub max_stop_sz: String,
149}