General Protocol
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.
| Function | Gateway method | params |
|---|---|---|
| exchange.GetTicker() | ticker | symbol |
| exchange.GetDepth() | depth | symbol, limit |
| exchange.GetTrades() | trades | symbol |
| exchange.GetRecords() | records | symbol, limit (default 200), period (minutes) |
| exchange.GetAccount() | accounts (assets when the gateway returns an error) | none |
| exchange.GetAssets() | assets | none |
| exchange.CreateOrder() / exchange.Buy() / exchange.Sell() | trade | symbol, type, price, amount |
| exchange.GetOrders() | orders | symbol (the current pair when no symbol is given) |
| exchange.GetHistoryOrders() | historyorders | symbol, since, limit |
| exchange.GetOrder() | order | id, symbol |
| exchange.CancelOrder() | cancel | id, symbol |
Names and Configuration
exchange.GetName(): Exchange (Custom access); aliases: Custom Exchange.
| Field | Required | Description |
|---|---|---|
| Server Address | Yes | The full address of the protocol plugin program (gateway); only http:// and https:// are supported, and requests are POSTed to this address as is. |
| Access Key | Yes | Put into the request body's access_key as is. |
| Secret Key | Yes | Put 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
| Command | Arguments | Description | Returns |
|---|---|---|---|
api | httpMethod, 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). |
io | code, ...args | Forward 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'srecordstakes 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):typeisbuyorsell, andclosebuyorclosesellfor closing orders, which can also be set directly withdirectionin the order extra parameters; a market order hasprice"-1"; other extra parameters are merged intoparamsas is. Post-only orders and time-in-force other than GTC are not supported. exchange.GetAccount()calls the gateway'saccountsmethod first and switches to theassetsmethod when the gateway replies with an error (for example, the method is not supported), so gateways implementing onlyassetsalso work;exchange.GetAssets()calls only theassetsmethod. Both methods reply withdata[{currency, free, frozen}].- The order ID is the value of
data.idin the gateway'stradereply 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.GetOrderandexchange.CancelOrdersend the order ID to the gateway asidunchanged, withsymbolset to the current pair (callexchange.SetCurrencyfirst to query or cancel an order of another pair). An order reply must carrydata.id, and thedata.idof theexchange.GetOrder()reply must match the requested order ID. exchange.GetOrderswithout a symbol queries the current pair (the gateway'sordersalways takes asymbol). TheSymbolof the order structures is the requested pair (e.g.BTC_USDT); the gateway's ownsymbolstays inInfo.- The order status is read from the
statefield, then from thestatusfield when that is not recognized (case-insensitive):open,pending,pre-submitted,submitting,submitted,partialandpartial-filledare pending;filled,closedandfinishedare closed;canceled,cancelledandpartial-canceledare canceled. When neither is recognized (numeric status codes included),exchange.GetOrders()treats the order as pending,exchange.GetHistoryOrders()as closed andexchange.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
typeis buy forbuyorbidand sell for anything else. The type ofdatamust 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 byexchange.IO()always use 30 seconds.