roq-okx#

Important

The account must be configured for the “net” position mode (futures and swaps).

Purpose#

  • Maintain network connectivity with the OKX exchange

  • Route exchange updates to connected clients

  • Route client requests to the relevant exchange accounts

  • Stream all messages to an event-log

Overview#

Products

Spot

Futures

Options

Market Data

Reference Data

Market Status

Top of Book

Market by Price (L2)

Market by Order (L3)

Trade Summary

Statistics

Order Management

Create

Modify

Cancel

Cancel All

Auto Cancellation

Account Management

Positions

Funds

  • Data center located in AWS, Hong Kong

  • No test environment

Conda#

$ conda install \
  --channel https://roq-trading.com/conda/stable \
  roq-okx
$ cp $CONDA_PREFIX/share/roq-okx/config.toml $CONFIG_FILE_PATH

# Then modify $CONFIG_FILE_PATH to match your specific configuration
$ roq-okx \
      --name "okx" \
      --config_file "$CONFIG_FILE_PATH" \
      --client_listen_address "$UNIX_SOCKET_PATH" \
      --metrics_listen_address "$TCP_LISTEN_PORT" \
      --flagfile "$FLAG_FILE"

Flags#

$ roq-okx --help

--ws_order_book_checksum_every

Number of updates between validating order-book checksum (0 means never validate).

--ws_books_depth

Depth used for L2 subscription. Currently supported values: 1, 5, 50, 400. More details in the comments section.

--ws_books_use_public

Required to opt-out from using the real-time feeds when an account has been configured. More details in the comments section.

--trade_mode

Trade mode, e.g. “cross”, “isolated” or “cash”

Environments#

# flagfile

--rest_uri=https://www.okx.com/
--ws_public_uri=wss://ws.okx.com:8443/ws/v5/public
--ws_private_uri=wss://ws.okx.com:8443/ws/v5/private

Market Data#

Event

Stream

Messages

Comments

roq::ReferenceData

MarketData

instruments

roq::MarketStatus

MarketData

instruments

roq::TopOfBook

MarketData

tickers

roq::MarketByPriceUpdate

MarketData

bbo-tbt, books5, books, books50-l2-tbt, or books-l2-tbt

roq::MarketByOrderUpdate

roq::TradeSummary

MarketData

trades

roq::StatisticsUpdate

MarketData

tickers, index-tickers, funding-rate

Statistics#

Type

Comments

OPEN_PRICE

(tickers) open24h

HIGHEST_TRADED_PRICE

(tickers) high24h

LOWEST_TRADED_PRICE

(tickers) low24h

TRADE_VOLUME

(tickers) vol24h

FUNDING_RATE

(funding-rate) fundingRate

FUNDING_RATE_PREDICTION

(funding-rate) nextFundingRate

INDEX_VALUE

(index-tickers) idxPx

Order Management#

Event

Stream

Messages

Comments

roq::OrderUpdate

OrderEntry

orders

roq::TradeUpdate

Event

Stream

Messages

Comments

roq::OrderUpdate

DropCopy

/api/v5/trade/orders-pending

roq::TradeUpdate

Event

Stream

Messages

Comments

roq::CreateOrder

OrderEntry

batch-orders

roq::ModifyOrder

OrderEntry

batch-amend-orders

roq::CancelOrder

OrderEntry

batch-cancel-orders

roq::CancelAllOrders

OrderEntry

batch-cancel-orders

The exchange API does not support this atomically

Event

Stream

Messages

Comments

roq::OrderAck

OrderEntry

order, amend-order, cancel-order

Mapping#

OrderType

TimeInForce

ExecutionInstruction

ordType

reduceOnly

MARKET

market

false

LIMIT

GTC

limit

false

LIMIT

GTC

DO_NOT_INCREASE

limit

true

LIMIT

GTC

PARTICIPATE_DO_NOT_INITIATE

post_only

false

LIMIT

FOK

fok

false

LIMIT

IOC

ioc

false

Account Management#

Event

Stream

Messages

Comments

roq::PositionUpdate

roq::FundsUpdate

Event

Stream

Messages

Comments

roq::PositionUpdate

roq::FundsUpdate

OrderEntry

/api/spot/v3/accounts

Streams#

Type

Comments

REST

Primary purpose

  • download orders

Each connection

  • supports a single account

Type

Comments

WebSocket

Primary purpose

  • support order management

Each connection

  • supports a single account

Type

Comments

WebSocket

Primary purpose

  • live market data

  • reference data and markets (from the instruments channel)

Each connection

  • supports a slice of the symbols

The first stream is used to

  • subscribe the instruments channel

Constraints#

  • Only VIP members can access the L2 tick-by-tick market data feed.

Comments#

  • TopOfBook is throttled at 100ms (by exchange)

  • The index-tickers and funding-rate channels are subscribed for all swaps and futures. This may generate some false subscriptions caused by non-existing channels. Unfortunately, there doesn’t seem to be any way to detect if a symbol has these channels available. The error messages are therefore only logged as warnings.

  • Using the “batch-” version of the order operations due to 5x higher rate limit allowance.

  • There is no exchange API to support CancelAllOrders. This is therefore implemented by sending batch cancel requests for all known non-finished orders.

  • There are a number of different “books” channels which can be used as source for MarketByPrice. Some of these require authentication and VIP membership. The gateway will by default choose the most detailed lowest latency feed. This is however not always correct. In particular, the gateway has no knowledge of the VIP membership status. These are the flags you can use to override default behaviour

    • --ws_books_depth

      • 1 will use the bbo-tbt realtime feed having only “best”

      • 5 will use the books5 throttled snapshot feed

      • 50 will use the books50-l2-tbt realtime incremental feed

      • 400 will use either the books-l2-tbt realtime incremental feed or the books throttled incremental feed.

    • --ws_books_use_public is an opt-out flag because the gateway will by default use a realtime feed if an account has been configured.