资源加载中... loading...

exchange.IO

The exchange.IO() function is used for other interface calls related to the exchange object.

The exchange.IO() function calls other interfaces related to the exchange object, returning the requested response data on a successful call and it returns null on a failed call. string, number, bool, object, array, null, and any other type supported by the system

exchange.IO(k, …args)

The k parameter is used to set the call type, with optional values "api", "currency", "base", "trade_margin", "trade_normal", "public_base", "mbase", selfTradePreventionMode, simulate, cross, dual, unified and so on. k true string Extended parameters, passed according to the specific call scenario, arg parameters can be passed more than one. Due to the polymorphic mechanism of the exchange.IO() function, different parameter settings correspond to different functions. The number and type of parameters to the exchange.IO() function are indeterminate. arg true string, number, bool, object, array, null and any other types supported by the system

function main() {
    var arrOrders = [
        {"instId":"BTC-USDT-SWAP","tdMode":"cross","side":"buy","ordType":"limit","px":"16000","sz":"1","posSide":"long"},
        {"instId":"BTC-USDT-SWAP","tdMode":"cross","side":"buy","ordType":"limit","px":"16000","sz":"2","posSide":"long"}
    ]
    
    // Call exchange.IO to access the exchange's bulk order interface directly
    var ret = exchange.IO("api", "POST", "/api/v5/trade/batch-orders", "", JSON.stringify(arrOrders))
    Log(ret)
}
import json
def main():
    arrOrders = [
        {"instId":"BTC-USDT-SWAP","tdMode":"cross","side":"buy","ordType":"limit","px":"16000","sz":"1","posSide":"long"}, 
        {"instId":"BTC-USDT-SWAP","tdMode":"cross","side":"buy","ordType":"limit","px":"16000","sz":"2","posSide":"long"}
    ]
    ret = exchange.IO("api", "POST", "/api/v5/trade/batch-orders", "", json.dumps(arrOrders))
    Log(ret)
void main() {
    json arrOrders = R"([
        {"instId":"BTC-USDT-SWAP","tdMode":"cross","side":"buy","ordType":"limit","px":"16000","sz":"1","posSide":"long"},
        {"instId":"BTC-USDT-SWAP","tdMode":"cross","side":"buy","ordType":"limit","px":"16000","sz":"2","posSide":"long"}
    ])"_json;
    auto ret = exchange.IO("api", "POST", "/api/v5/trade/batch-orders", "", arrOrders.dump());
    Log(ret);
}

Using the exchange.IO("api", httpMethod, resource, params, raw) call form of the exchange.IO() function, it is necessary to understand the API interface of the exchange and check the relevant documentation first. This will allow you to extend the functionality that is not added to the FMZ platform. Submitting a POST request does not require you to worry about encrypting, signing, or verifying the parameters, which are already handled by FMZ at the bottom, as long as you fill in the corresponding parameters. You can refer to the batch order example of OKX Exchange futures contracts, and use the parameter raw to pass the order parameters:

var amount = 1
var price = 10
var basecurrency = "ltc"
function main () {
    // Note that both amount.toString() and price.toString() have a ' character on the left and right side
    var message = "symbol=" + basecurrency + "&amount='" + amount.toString() + "'&price='" + price.toString() + "'&side=buy" + "&type=limit"
    var id = exchange.IO("api", "POST", "/v1/order/new", message)
}
amount = 1
price = 10
basecurrency = "ltc"
def main():
    message = "symbol=" + basecurrency + "&amount='" + str(amount) + "'&price='" + str(price) + "'&side=buy" + "&type=limit"
    id = exchange.IO("api", "POST", "/v1/order/new", message)
void main() {
    auto amount = 1.0;
    auto price = 10.0;
    auto basecurrency = "ltc";
    string message = format("symbol=%s&amount=\"%.1f\"&price=\"%.1f\"&side=buy&type=limit", basecurrency, amount, price);
    auto id = exchange.IO("api", "POST", "/v1/order/new", message);
}

If the key value in the params parameter (i.e., Http request parameter) is a string, it needs to be written in single quotes (i.e., the symbol ') around the parameter value to wrap the parameter value.

function main() {
    var ret = exchange.IO("api", "GET", "https://www.okx.com/api/v5/account/max-withdrawal", "ccy=BTC")
    Log(ret)
}
def main():
    ret = exchange.IO("api", "GET", "https://www.okx.com/api/v5/account/max-withdrawal", "ccy=BTC")
    Log(ret)
void main() {
    auto ret = exchange.IO("api", "GET", "https://www.okx.com/api/v5/account/max-withdrawal", "ccy=BTC");
    Log(ret);
}

It supports passing in complete url parameters, which can omit the operation of switching the base address (calling the exchange.SetBase() function).

function main(){
    var ret = exchange.IO("api", "GET", "/api/v5/trade/orders-pending", "instType=SPOT")
    Log(ret)
}
def main():
    ret = exchange.IO("api", "GET", "/api/v5/trade/orders-pending", "instType=SPOT")
    Log(ret)
void main() {
    auto ret = exchange.IO("api", "GET", "/api/v5/trade/orders-pending", "instType=SPOT");
    Log(ret);
}

Example of a call without the parameter raw:

function main() {
    // For example, if you set the current trading pair of the exchange object to BTC_USDT at the beginning of the live trading, print the current trading pair tickers
    Log(exchange.GetTicker())
    // Switch the trading pair to LTC_BTC      
    exchange.IO("currency", "LTC_BTC")
    Log(exchange.GetTicker())
}
def main():
    Log(exchange.GetTicker())
    exchange.IO("currency", "LTC_BTC")
    Log(exchange.GetTicker())
void main() {
    Log(exchange.GetTicker());
    exchange.IO("currency", "LTC_BTC");
    Log(exchange.GetTicker());
}

Switch the trading pair of current exchange, so that it will switch the trading pair configured by code at live trading creation or at backtest.

function main () {
    // exchanges[0] is the first exchange object added when the live trading is created 
    exchanges[0].IO("base", "https://api.huobi.pro")
}
def main():
    exchanges[0].IO("base", "https://api.huobi.pro")
void main() {
    exchanges[0].IO("base", "https://api.huobi.pro");
}```
For example, the default base address when the exchange object is wrapped is ```https://api.huobipro.com```, and when you need to switch to ```https://api.huobi.pro```, use the following code to switch:
```javascript
function main() {
    exchange.SetBase("https://api.bitfinex.com")
    exchange.IO("mbase", "https://api-pub.bitfinex.com")
}
def main():
    exchange.SetBase("https://api.bitfinex.com")
    exchange.IO("mbase", "https://api-pub.bitfinex.com")
void main() {
    exchange.SetBase("https://api.bitfinex.com");
    exchange.IO("mbase", "https://api-pub.bitfinex.com");
}

For exchanges with different base addresses for the ticker interface and trading interface, for example, Bitfinex Futures have two addresses, one for the ticker interface and the other for the trading interface. Bitfinex futures switch private interface base address using exchange.SetBase("xxx"). Bitfinex futures switch public interface base address using exchange.IO("mbase", "xxx").

I. For cryptocurrency-centric exchanges other API interface calls that are not uniformly encapsulated, with parameter k set to "api":

exchange.IO("api", httpMethod, resource, params, raw)
  • httpMethod : The parameter is a string type, fill in the request type POST, GET, etc.
  • resource : The parameter is of string type and fills in the request path; It supports the use of complete request paths, see the reference example for details.
  • params : The parameter is a string type, filled with request parameters, encoded with URL.
  • raw : The parameter is the raw string parameter and can be left out.

The exchange.IO("api", httpMethod, resource, params, raw) function call will access the exchange interface and return null if the call fails and an error occurs. Only the real trading supports calling the exchange.IO("api", httpMethod, resource, params, raw) function.

II. For switching trading pairs, the parameter k is set to "currency":

exchange.IO("currency", currency)
  • currency : The parameter is a string type with a uniform upper case format, using an underscore to separate baseCurrency from quoteCurrency, such as BTC_USDT.

    1. Backtesting system now supports switching trading pairs (only for digital currency spot exchange objects), when backtesting, you need to pay attention that you can only switch to trading pairs with the same denominated currency, for example, the current trading pair is ETH_BTC can only switch to LTC_BTC, not to LTC_USDT.
    2. For cryptocurrency futures contracts the exchange object switches trading pairs and the contract code needs to be set again to determine which contract is to be traded.
    3. Using the {@fun/Account/exchange.SetCurrency exchange.SetCurrency} function to switch trading pairs is exactly the same as using exchange.IO("currency", currency) to switch trading pairs.

III. Used to switch the cryptocurrency spot exchange object leveraged account mode:

  • The parameter k is set to "trade_margin" to switch to the spot leverage account mode. Placing orders and obtaining account assets will access the exchange’s spot leveraged interface. If the exchange distinguishes between full margin and isolated margin in spot leverage, use: exchange.IO("trade_super_margin") to switch to full margin for leveraged account, and exchange.IO("trade_margin") to switch to isolated margin for leveraged account.
  • Parameter k is set to "trade_normal" to switch back to normal spot account mode.

Spot exchanges that support switching between leveraged account models:

Exchanges Special remarks
OKX Trading pairs in leveraged account mode are different from normal ones, some trading pairs may not have them. Use exchange.IO("trade_super_margin") to switch to full position for leveraged accounts and use exchange.IO("trade_margin") to switch to position by position. Use trade_normal to switch to normal spot mode. Use exchange.IO("tdMode", "cross") to directly specify the leverage mode.
Huobi Leveraged account mode trading pairs are different from normal ones, some trading pairs may not have them. There are full positions and position-by-position in Huobi leveraged accounts. Use trade_margin to switch to leverage account position by position, use trade_super_margin to switch to leverage account full position. Use trade_normal to switch to normal currency-currency mode.
Binance Leveraged account mode is divided into position by position and full position, use trade_margin to switch to position by position, use trade_super_margin to switch to full position, use trade_normal to switch to normal currency-currency mode.
GateIO Leveraged account mode is divided into position by position and full position, use trade_margin to switch to position by position, use trade_super_margin to switch to full position, use trade_normal to switch to normal currency-currency mode.
AscendEx Use exchange.IO("trade_margin") to switch to leverage account mode and exchange.IO("trade_normal") to switch back to normal account mode.
WOO Use exchange.IO("trade_margin") to switch to leverage account mode and exchange.IO("trade_normal") to switch back to normal account mode.
CoinEx Use exchange.IO("trade_margin") to switch to leveraged account mode and exchange.IO("trade_normal") to switch back to normal account mode.

IV. Other switching functions: Check out the exchange.IO() function for Other switching functions in the User Guide.

{@fun/NetSettings/exchange.SetBase exchange.SetBase}, {@fun/Account/exchange.SetCurrency exchange.SetCurrency}, {@var EXCHANGE_OP_IO_CONTROL}

exchange.SetRate exchange.Log