Type/to search
Built-in Functions
Structures
Built-in Variables and Constants
Conventions
Exchanges
Capability Matrix
Centralized Exchanges
AscendEx
Backpack
BigONE
Binance
Bitfinex
bitFlyer
Bitget
Bithumb
Bitstamp
Bybit
Coincheck
Coinone
CoinW
Crypto.com
DeepCoin
Deribit
DigiFinex
Gate
Gemini
HashKey
HitBTC
HTX
Korbit
Kraken
KuCoin
LBank
MEXC
OKX
Poloniex
Upbit
WEEX
WOO X
Zaif
Decentralized Exchanges (DEX)
Prediction Markets
Securities and Futures
On-chain & Custom

OKEX (Spot): OKX spot, plus spot margin trading (isolated and cross) on the same account. Uses the OKX V5 API and needs the API key's Access Key, Secret Key and Passphrase.

Futures_OKCoin (Futures): OKX perpetual contracts (USDT-, USDC-margined and coin-margined), delivery contracts (coin-margined, USDT-margined and USD-margined) and options. Uses the OKX V5 API and needs the API key's Access Key, Secret Key and Passphrase; order amounts are in contracts.

Names and Configuration
exchange.GetName()TypeAliases
OKEXSpot—
Futures_OKCoinFuturesFutures_OKX
FieldRequiredDefault
---
Access KeyYes—
Secret KeyYes—
PassphraseYes—
V5 protocolNoChecked
SimulateNoUnchecked

Trading Pairs and Contract Codes
OKEX (Spot)

Pair examples: BTC_USDT, ETH_BTC

Pairs are written as BTC_USDT (OKX BTC-USDT) without a contract code; a symbol with a contract suffix (such as BTC_USDT.swap) is rejected. Use a Futures_OKCoin exchange object for contracts.

Futures_OKCoin (Futures)

Pair examples: BTC_USDT, BTC_USDC, BTC_USD, BTC_USD_UM

The quote currency selects the contract family: BTC_USDT and BTC_USDC are USDT- and USDC-margined contracts, BTC_USD is coin-margined. OKX USD-margined delivery contracts (exchange contract family BTC-USD_UM) use the pair BTC_USD_UM, for example BTC_USD_UM.quarter.

Contract codeDescription
swapPerpetual contract (the default).
this_weekCurrent-week delivery contract.
next_weekNext-week delivery contract.
monthCurrent-month delivery contract.
next_monthNext-month delivery contract.
quarterCurrent-quarter delivery contract.
next_quarterNext-quarter delivery contract.
third_quarterThird-quarter delivery contract.
fourth_quarterFourth-quarter delivery contract.

Delivery contracts are looked up by their tier in the OKX instrument list; a tier the exchange has not listed is rejected (for example no quarter futures for BTC-USDT on okx).

Native contract code examples: BTC-USD-260925, HMSTR-USDT-250207

Delivery contracts can also be given by their OKX native code, including pre-market contracts without a fixed cycle: set the pair to HMSTR_USDT, then exchange.SetContractType("HMSTR-USDT-250207"); functions that take a symbol accept HMSTR_USDT.HMSTR-USDT-250207. OKX long-term delivery contracts (USD-margined, exchange contract family like BTC-USD_UM_XPERP) have no fixed tier; exchange.GetMarkets() lists them in native-code form such as BTC_USD_UM_XPERP.BTC-USD_UM_XPERP-310404, and they are used the same way.

Exchange-specific IO Commands
OKEX (Spot)
CommandArgumentsDescriptionReturns
simulateboolSwitch between demo (true) and live (false) trading; demo requests carry the x-simulated-trading: 1 header, the API address stays the same.The value set.
trade_margin—Switch to the margin account, isolated (tdMode=isolated).The command name itself.
trade_super_margin—Switch to the margin account, cross (tdMode=cross).The command name itself.
trade_normal—Switch back to normal spot (tdMode=cash, the default).The command name itself.
tdModestringSet the trade mode directly: cash (spot), cross, isolated (cross or isolated margin).The mode set.

For the common commands api, base and currency see exchange.IO.

Futures_OKCoin (Futures)
CommandArgumentsDescriptionReturns
simulateboolSwitch between demo (true) and live (false) trading; demo requests carry the x-simulated-trading: 1 header, the API address stays the same.The value set.
crossboolCross (true, the default) / isolated (false) margin. Kept locally and sent as tdMode/mgnMode with subsequent orders, conditional orders and exchange.SetMarginLevel(); no request is made and it applies to all contracts.The value set.
tdModestringSet the order tdMode directly: cross, isolated, cash.The mode set.
dualboolHedge (true, long_short_mode) / one-way (false, net_mode) position mode. OKX position mode is an account-level setting; the call sends a switch request to the exchange.The value set.

For the common commands api, base and currency see exchange.IO.

Common Usage
Switch between isolated margin, cross margin and plain spot with tdMode (OKEX only)
javascript
function main() { // Isolated margin, same as exchange.IO("trade_margin"); use cross for cross margin Log(exchange.IO("tdMode", "isolated")) // isolated // Orders now carry tdMode=isolated; open and history order queries read margin orders Log(exchange.GetOrders("BTC_USDT")) // Back to plain spot, same as exchange.IO("trade_normal") Log(exchange.IO("tdMode", "cash")) // cash }
Set leverage and place an order in isolated margin mode (Futures_OKCoin only)
javascript
function main() { exchange.SetContractType("swap") // Saved locally only; later orders, conditional orders and leverage settings use // isolated margin, same as exchange.IO("cross", false) Log(exchange.IO("tdMode", "isolated")) // isolated exchange.SetMarginLevel(5) var id = exchange.CreateOrder("BTC_USDT.swap", "buy", -1, 1) Log("Order Id:", id) }

Function Support

No unsupported standard functions.

Served through a fallback:

  • exchange.GetRecords: Native periods are 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 12h, 1d, 3d, 1w, 1M, 3M; other periods are synthesized by the docker.

Margin, Account and Simulation
All exchange objects

Simulation: Check the Simulate box or call exchange.IO("simulate", true) to use OKX demo trading; it needs an API key created on the OKX demo trading page.

OKEX (Spot)

Account: Spot and margin share the same OKX API; they differ in the order's tdMode and the instrument type used for order queries. After switching to margin, the connector queries the account mode (acctLv) on the first request: a spot-mode account cannot place margin orders (error acctLv is 1); in single-currency margin mode margin orders carry the borrowing currency (the quote currency for buys, the base currency for sells); in multi-currency and portfolio margin modes cross margin orders are recorded under the spot instrument type, which queries take into account. tdMode and ccy can be overridden explicitly in the order's extra parameters. After switching to margin, exchange.GetMarkets() lists OKX margin instruments (instType MARGIN), and exchange.GetTickers() also queries MARGIN, which OKX rejects (an error); switch back with trade_normal for market-wide tickers.

Futures_OKCoin (Futures)

Margin mode: Cross margin by default; switch to isolated with exchange.IO("cross", false) or exchange.IO("tdMode", "isolated"), or pass tdMode explicitly in the order's extra parameters. exchange.SetMarginLevel() sets leverage for the current cross/isolated mode (it fails when tdMode is cash); in hedge mode with isolated margin OKX keeps separate long and short leverage, and the side is taken from the direction set by exchange.SetDirection() (buy is long, sell is short, long when not set).

Position mode: The account's position mode is queried automatically before orders, and in hedge mode posSide is set from the open/close direction; it can also be switched with exchange.IO("dual", bool).

Options
Futures_OKCoin (Futures)

OKX options are supported and used like futures contracts.

  • exchange.GetMarkets() and exchange.GetTickers() do not list option contracts; query them with OKX's /api/v5/public/instruments?instType=OPTION&uly=BTC-USD endpoint.
  • When the current contract is an option, exchange.GetPositions(), exchange.GetOrders() and similar calls without a symbol query the USD.option range, and the connector queries each underlying in OKX's option underlying list.
  • For an option contract that has never traded, the Last of exchange.GetTicker() is the mark price.
  • Option orders carry no posSide.

Option code examples: BTC-USD-260925-60000-C

Option codes are OKX native codes: underlying-quote-expiry (YYMMDD)-strike-C/P. Set the pair to BTC_USD; the full symbol looks like BTC_USD.BTC-USD-260925-60000-C.

Notes
OKEX (Spot)
  • The amount of a spot market buy order is a quote-currency amount (the connector does not set tgtCcy, so OKX's default applies); to size a market buy in the base currency, pass {"tgtCcy": "base_ccy"} in the order's extra parameters.
  • exchange.ModifyOrder() amends in place through OKX's amend endpoint (amend-order), keeping the order ID.
  • Conditional orders support take-profit, stop-loss and OCO; exchange.ModifyConditionOrder() amends in place through OKX's amend-algo endpoint (amend-algos) without canceling and re-placing.
  • exchange.GetHistoryOrders() queries one pair at a time; an empty string fails. exchange.GetHistoryConditionOrders() does not support the since parameter; passing it fails.
Futures_OKCoin (Futures)
  • exchange.GetFundings() has no batch query; pass a specific perpetual contract, for example exchange.GetFundings("ETH_USDT.swap"); calling it without a symbol or with a range (such as USDT.swap) fails.
  • Conditional orders support take-profit, stop-loss and OCO; exchange.ModifyOrder() and exchange.ModifyConditionOrder() amend in place through OKX's amend endpoints, keeping the order ID.
  • exchange.GetHistoryConditionOrders() does not support the since parameter; passing it fails. exchange.GetHistoryOrders() does.