roq-binance#

Purpose#

  • Maintain network connectivity with the Binance 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 Japan (to be confirmed)

  • No test environment

Conda#

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

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

Config#

Flags#

$ roq-binance --help
--exchange (binance)

Exchange identifier

--ws_api (true)

Use the websocket api?

--request_limit_interval (5s)

Request limit monitor interval

--request_limit (8)

Request limit per interval

--encode_buffer_size (1048576)

Encode buffer size

--decode_buffer_size (10485760)

Decode buffer size

--mbp_allow_price_inversion (false)

Allow price inversion?

--mbp_max_depth

Maximum depth levels (count)

--mbp_sequencer_timeout (120s)

MbP sequencer timeout

--cancel_replace_stop_on_failure (false)

If the cancel request fails, the replacement will not be attempted

--download_symbols

List of symbols to download

--download_trades_lookback (300s)

Download trades? (lookback period)

--download_trades_lookback_on_restart

Download trades? (lookback period)

--download_trades_limit (1000)

Exchange limitation

--rest_network_interfaces

List of REST network interfaces

--rest_uri (https://testnet.binance.com)

REST end-point (URI)

--rest_host

Host (HTTP header) to use when the URI configured with IP address

--rest_proxy

REST proxy end-point (URI)

--rest_ping_freq (5s)

Ping frequency

--rest_ping_path (/api/v3/time)

URI path used for REST connection keep-alive messages

--rest_request_timeout (30s)

Request timeout

--rest_terminate_on_403 (false)

Terminate after getting a 403 response code

--rest_back_off_delay (300s)

Time to wait before retrying a rejected request

--rest_listen_key_refresh (1800s)

Listen key refresh period

--rest_order_recv_window (5000ms)

Receive window (please refer to Binance documentation)

--rest_cancel_on_disconnect (false)

Cancel orders on disconnect?

--ws_uri (wss://testnet.binance.com/realtime)

WebSocket end-point (URI)

--ws_host

Host (HTTP header) to use when the URI configured with IP address

--ws_ping_freq (5s)

Ping frequency

--ws_max_subscriptions_per_stream (128)

Maximum number of symbols per connection

--ws_subscribe_depth_levels (1000)

Pepth levels (count)

--ws_subscribe_depth_freq (100ms)

Depth update frequency

--ws_mbp_request_delay (10s)

Request delay

--ws_mbp_request_max_retries (0)

Request max retries

--ws_subscribe_trade_details (false)

Report individual matches for trade summary? (bool)

--ws_enable_secondary (false)

Create secondary market data connection? (bool)

--ws_api_network_interfaces

List of WS API network interfaces

--ws_api_uri (wss://ws-api.binance.com/ws-api/v3)

WS API end-point (URI)

--ws_api_host

Host (HTTP header) in case uri uses an IP address

--ws_api_ping_freq (5s)

Ping frequency

Environments#

$ $CONDA_PREFIX/share/roq-binance/flags
# flagfile for the roq-binance gateway

--rest_uri=https://api.binance.com
--ws_uri=wss://stream.binance.com:9443/stream
--ws_api_uri=wss://ws-api.binance.com/ws-api/v3
# flagfile for the roq-binance gateway

--rest_uri=https://api.binance.us
--ws_uri=wss://stream.binance.us:9443/stream
# flagfile for the roq-binance gateway

--rest_uri=https://testnet.binance.vision
--ws_uri=wss://testnet.binance.vision/stream
--ws_api_uri=wss://testnet.binance.vision/ws-api/v3

Market Data#

Event

Stream

Messages

Comments

roq::ReferenceData

Unavailable

roq::MarketStatus

Unavailable

roq::TopOfBook

MarketData

<symbol>@bookTicker

roq::MarketByPriceUpdate

MarketData

<symbol>@depth@<freq>

See Flags

roq::MarketByOrderUpdate

Unavailable

roq::TradeSummary

MarketData

<symbol>@trade, <symbol>@aggTrade

See Flags

roq::StatisticsUpdate

MarketData

<symbol>@miniTicker

Event

Stream

Messages

Comments

roq::ReferenceData

OrderEntry

GET /api/v3/exchangeInfo

roq::MarketStatus

OrderEntry

GET /api/v3/exchangeInfo

roq::TopOfBook

roq::MarketByPriceUpdate

OrderEntry

GET /api/v3/depth

See Flags

roq::MarketByOrderUpdate

roq::TradeSummary

roq::StatisticsUpdate

Statistics#

Type

Comments

HIGHEST_TRADED_PRICE

(miniTicker) high_price

LOWEST_TRADED_PRICE

(miniTicker) low_price

OPEN_PRICE

(miniTicker) open_price

CLOSE_PRICE

(miniTicker) close_price

Order Management#

Event

Stream

Messages

Comments

roq::OrderUpdate

DropCopy

executionReport

roq::TradeUpdate

DropCopy

executionReport

Event

Stream

Messages

Comments

roq::OrderUpdate

OrderEntry

GET /api/v3/openOrders

It is only possible to download open orders

roq::TradeUpdate

Event

Stream

Messages

Comments

roq::CreateOrder

OrderEntry

POST /api/v3/order

roq::ModifyOrder

Unavailable

roq::CancelOrder

OrderEntry

DELETE /api/v3/order

roq::CancelAllOrders

OrderEntry

DELETE /api/v3/openOrders

This request is per-symbol! Only executed for those symbols where the gateway has seen order actions or download.

Event

Stream

Messages

Comments

roq::OrderAck

OrderEntry

/api/v3/order

Order Types#

Type

Comments

MARKET

Mapped to 'MARKET' (JSON)

LIMIT

Mapped to 'LIMIT' (JSON)

Time in Force#

Type

Comments

GTC

Mapped to 'GTC' (JSON)

IOC

Mapped to 'IOC' (JSON)

FOK

Mapped to 'FOK' (JSON)

Position Effect#

Note

Not supported

Execution Instructions#

Type

Comments

PARTICIPATE_DO_NOT_INITIATE

Maps OrderType to 'LIMIT_MAKER' (JSON)

Templates#

create_order#

Field

Values

Comments

self_trade_prevention_mode

EXPIRE_TAKER, EXPIRE_MAKER, EXPIRE_BOTH

Exchange field is selfTradePreventionMode

cancel_order#

Field

Values

Comments

cancel_restrictions

ONLY_NEW, ONLY_PARTIALLY_FILLED

Exchange field is cancelRestrictions

cancel_replace_mode

STOP_ON_FAILURE, ALLOW_FAILURE

Exchange field is cancelReplaceMode

Account Management#

Event

Stream

Messages

Comments

roq::PositionUpdate

Unavailable

roq::FundsUpdate

DropCopy

outboundAccountInfo, outboundAccountPosition

Event

Stream

Messages

Comments

roq::PositionUpdate

Unavailable

roq::FundsUpdate

OrderEntry

GET /api/v3/account

Streams#

Type

Comments

REST

Primary purpose

  • support order management

Each connection

  • supports a single account

  • maintains a listen key (used by the DropCopy stream)

Type

Comments

WebSocket

Primary purpose

  • live account updates, including orders and fills

Each connection

  • supports a single account

Type

Comments

WebSocket

Primary purpose

  • live market data

Each connection

  • supports a slice of the symbols

Type

Comments

REST

Primary purpose

  • download reference data

One connection

Constraints#

  • It is only possible to download current order status for open orders. The implication is that backup procedures must be implemented to reoncile positions in the scenario where orders are completely filled during a disconnect.

  • The newClientOrderId field (used by CreateOrder) must conform to the ^[.A-Z:/a-z0-9_-]{1,36}$ regular expression (ECMAScript). This restricts length and character used when supplying the routing_id field.

  • The exchange will monitor rate-limit usage per IP address.

  • Rate-limit usage is quite strict when downloading full order books. Due to this constraint, it may take a very long time to initialize all symbols. It is therefore STRONGLY recommended to reduce the configured number of symbols, e.g. symbols=".*BTC.*", or even more specific by using lists.

Comments#

  • It is possible to see gateway warnings about dropped messages caused by order status reversal, e.g. seeing WORKING after COMPLETED. This is partly due to REST vs WebSocket and partly due to exchange publishing both NEW and FILLED order update messages for taker orders.

  • The online documentation has a note about rejected messages never ping pushed into the user data stream. This effectively means that there’s the risk that you will have to wait for the gateway to detect timeout if the REST connection is disconnected right after an order request has been sent.

  • The exchange API supports a cancel-replace order request. This is supported by first placing a cancel order with is_last=false followed immediately by a create order with is_last=true.

    Note

    The two orders must share the same symbol.

  • Multiple order entry connections (REST or WS) may be supported by specifying a list of local network interfaces to bind to. The implementation will then round-robin requests between connections already in the ready-state.