Type/to search
Built-in Functions
Structures
Built-in Variables and Constants
Conventions
Exchanges
Capability Matrix
Centralized Exchanges
Decentralized Exchanges (DEX)
Prediction Markets
Securities and Futures
On-chain & Custom
General Protocol
Web3

Connects exchanges the platform has not integrated (exchange.GetName() returns Exchange): the standard functions do not access any exchange directly but forward requests to a protocol plugin program (gateway) you run yourself, which talks to the exchange's REST, FIX or other interface. For writing the plugin program, see the REST protocol reference documentation and the FIX protocol reference project.

Every call is one HTTP POST to the configured server address with a JSON body {"access_key", "secret_key", "nonce", "method", "params"} (nonce is a nanosecond timestamp), the request header Http-Method set to POST (to the caller's method for exchange.IO("api", ...)) and Content-Type set to application/x-www-form-urlencoded. A reply with an error key at the top level is a failure (null or an empty string count too, reported as gateway returned an error without a message), and the payload is in data.

FunctionGateway methodparams
exchange.GetTicker()tickersymbol
exchange.GetDepth()depthsymbol, limit
exchange.GetTrades()tradessymbol
exchange.GetRecords()recordssymbol, limit (default 200), period (minutes)
exchange.GetAccount()accounts (assets when the gateway returns an error)none
exchange.GetAssets()assetsnone
exchange.CreateOrder() / exchange.Buy() / exchange.Sell()tradesymbol, type, price, amount
exchange.GetOrders()orderssymbol (the current pair when no symbol is given)
exchange.GetHistoryOrders()historyorderssymbol, since, limit
exchange.GetOrder()orderid, symbol
exchange.CancelOrder()cancelid, symbol

Names and Configuration

exchange.GetName(): Exchange (Custom access); aliases: Custom Exchange.

FieldRequiredDescription
Server AddressYesThe full address of the protocol plugin program (gateway); only http:// and https:// are supported, and requests are POSTed to this address as is.
Access KeyYesPut into the request body's access_key as is.
Secret KeyYesPut into the request body's secret_key as is.

Trading Pairs and Contract Codes

Pair examples: BTC_USDT

Pairs are written as BTC_USDT and sent to the gateway in lowercase (btc_usdt).

Exchange-specific IO Commands
CommandArgumentsDescriptionReturns
apihttpMethod, name[, params]Call the gateway's custom method __api_<name> with the request header Http-Method set to httpMethod; params is an object or a string like "a=1&b=2" (values are typed as integer, float, JSON or string, in that order).The gateway's whole reply (not just data).
iocode, ...argsForward the numeric command code and its arguments to the gateway's io method (params {code, args}); exchange.IO(code, ...args) works as well. With code 4 nothing is forwarded, and the first argument is run as a local IO command.The data of the gateway's reply.

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

Common Usage
Call a custom gateway method (__api_<name>)
javascript
function main() { // The gateway receives method __api_getAccountInfo with header Http-Method: GET // and params {"accountType": "spot", "limit": 10} (numeric strings are sent as numbers) var r = exchange.IO("api", "GET", "getAccountInfo", "accountType=spot&limit=10") Log(r) // the whole gateway reply, e.g. {"data": {...}} // params can also be an object; nested objects and arrays are forwarded as is var ret = exchange.IO("api", "POST", "batchOrders", {orders: [{symbol: "BTC_USDT", amount: 1}]}) Log(ret.data) }
Forward a numeric command to the gateway's io method
javascript
function main() { // The gateway receives method io with params {"code": 2, "args": ["foo", 1]}; // the reply's data is returned var d1 = exchange.IO("io", 2, "foo", 1) var d2 = exchange.IO(2, "foo", 1) // same as the line above Log(d1, d2) // Code 4 is not forwarded: the first argument runs as a local IO command, // here querying the gateway's server address Log(exchange.IO(4, "base")) }

Function Support

Not supported (the call fails with an error and returns null): exchange.GetTickers, exchange.GetMarkets, exchange.ModifyOrder, exchange.CreateConditionOrder, exchange.ModifyConditionOrder, exchange.CancelConditionOrder, exchange.GetConditionOrder, exchange.GetConditionOrders, exchange.GetHistoryConditionOrders.

Served through a fallback:

  • exchange.GetRecords: The gateway's records takes only the 1m, 3m, 5m, 15m, 30m, 1h and 1d periods; other periods are synthesized by the docker, and a start time cannot be specified.

Notes
  • Orders (gateway method trade): type is buy or sell, and closebuy or closesell for closing orders, which can also be set directly with direction in the order extra parameters; a market order has price "-1"; other extra parameters are merged into params as is. Post-only orders and time-in-force other than GTC are not supported.
  • exchange.GetAccount() calls the gateway's accounts method first and switches to the assets method when the gateway replies with an error (for example, the method is not supported), so gateways implementing only assets also work; exchange.GetAssets() calls only the assets method. Both methods reply with data [{currency, free, frozen}].
  • The order ID is the value of data.id in the gateway's trade reply as is: a number for a positive integer, a string otherwise, which may contain commas (e.g. BTC-USDT,123456); the platform neither adds to nor splits it. exchange.GetOrder and exchange.CancelOrder send the order ID to the gateway as id unchanged, with symbol set to the current pair (call exchange.SetCurrency first to query or cancel an order of another pair). An order reply must carry data.id, and the data.id of the exchange.GetOrder() reply must match the requested order ID.
  • exchange.GetOrders without a symbol queries the current pair (the gateway's orders always takes a symbol). The Symbol of the order structures is the requested pair (e.g. BTC_USDT); the gateway's own symbol stays in Info.
  • The order status is read from the state field, then from the status field when that is not recognized (case-insensitive): open, pending, pre-submitted, submitting, submitted, partial and partial-filled are pending; filled, closed and finished are closed; canceled, cancelled and partial-canceled are canceled. When neither is recognized (numeric status codes included), exchange.GetOrders() treats the order as pending, exchange.GetHistoryOrders() as closed and exchange.GetOrder() as unknown.
  • Reply fields may be omitted: missing prices, amounts, times and other numbers are 0, numbers may be strings (thousands separators allowed), and times may be integers, floats or numeric strings; the direction type is buy for buy or bid and sell for anything else. The type of data must be right (an object for the ticker and a single order, an array for trades, K-lines, balances and order lists, [price, amount] arrays for depth levels), otherwise an error is reported.
  • Requests time out after 30 seconds by default; the timeout of the standard functions' requests can be changed with exchange.SetTimeout, while requests forwarded by exchange.IO() always use 30 seconds.