roq-okx#

Important

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

$ mamba install \
      --channel https://roq-trading.com/conda/stable \
      roq-okx
$ mamba install \
      --channel https://roq-trading.com/conda/unstable \
      roq-okx

roq-okx#

$ roq-okx [FLAGS]

Description#

roq-okx is a gateway

Supports#

Products

Spot

Futures

Options

Combos

Market Data

Reference Data

Market Status

Top of Book

Market by Price

Market by Order

Trade Summary

Statistics

Order Management

Create

Modify

Cancel

Cancel All

Auto-Cancel

Account Management

Positions

Funds

Flags#

$ roq-okx --help
--exchange (okx)

Exchange identifier

--trade_mode (cross)

Trade mode (cross, isolated or cash)

--test_margin_currency

TEST Margin currency used when placing new orders

--rest_uri (https://www.okx.com)

Exchange end-point

--rest_host

Host (when URI is an IP address)

--rest_proxy

Proxy end-point

--rest_ping_freq (10s)

Ping frequency

--rest_ping_path (/api/v5/public/time)

Ping path

--rest_request_timeout (30s)

Request timeout

--ws_public_uri (wss://ws.okx.com:8443/ws/v5/public)

Exchange end-point

--ws_public_host

Host (when URI is an IP address)

--ws_private_uri (wss://ws.okx.com:8443/ws/v5/private)

Exchange end-point

--ws_private_host

Host (when URI is an IP address)

--ws_ping_freq (5s)

Ping frequency

--ws_max_subscriptions_per_stream (32)

Maximum number of symbols per connection

--ws_books_depth (400)

Depth defines what ‘books’ channel to subscribe to (some require authentication)

--ws_books_use_public (false)

Only use publicly available data sources (not requiring VIP membership)?

--ws_disconnect_timeout (10s)

Disconnect if connection has been silent for this period

--download_trades_lookback (300s)

Lookback period when downloading trades (following a disconnect)

--download_trades_lookback_on_restart

Lookback period when downloading trades (initial download)

--download_trades_limit (100)

Maximum number of trades per response (exchange limit)

--mbp_allow_price_inversion (false)

Allow price inversion?

--request_limit (80)

Maximum number of requests per interval

--request_limit_interval (10s)

Monitor interval

--encode_buffer_size (1048576)

Encode buffer size

--decode_buffer_size (10485760)

Decode buffer size

--include_bad_subscriptions (false)

TEST

Environments#

$ $CONDA_PREFIX/share/roq-okx/flags/prod/flags.cfg
# flagfile for the roq-okx gateway
# exchange servers are located in Alibaba, Hong Kong, China

--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
$ $CONDA_PREFIX/share/roq-okx/flags/test/flags.cfg
# flagfile for the roq-okx gateway
# exchange servers are located in Alibaba, Hong Kong, China

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

Configuration#

$ $CONDA_PREFIX/share/roq-okx/config.toml

Important

The template will be replaced when the software is upgraded. Make a copy and modify to your needs.

symbols = [
  "^BTC-USD[A-Z]?(-[0-9]{6})?$",  # spot + futures
  "^ETH-USD[A-Z]?(-[0-9]{6})?$",  # spot + futures
  "^PERP-USD[A-Z]?(-SWAP)?$"      # swaps
]

[accounts]

  [accounts.A1]
  master = true
  login = "YOUR_OKX_API_KEY_GOES_HERE"
  password = "YOUR_OKX_PASSPHRASE_GOES_HERE"
  secret = "YOUR_OKX_SECRET_GOES_HERE"
  symbols = [ ".*" ]

[users]

  [users.test]
  password = "1234"
  symbols = ".*"

  [users.trader]
  password = "secret"
  accounts = [ "A1" ]
  symbols = [ ".*BTC.*", ".*ETH.*" ]

[rate_limits]

  [rate_limits.global]
    type = "ORDER_ACTION"
    aggregate = true
    request_limit = 100
    monitor_period = "10s"
    ban_period = "5m"

Market Data#

Event

Stream

Messages

Comments

roq::ReferenceData

MarketData

instruments

roq::MarketStatus

MarketData

instruments

roq::TopOfBook

MarketData

bbo-tbt

roq::MarketByPriceUpdate

MarketData

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

      • 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.

References#

Common#

OKX#