The resource loading... loading...

exchange.Encode

The exchange.Encode() function is used for signature encryption calculations.

The exchange.Encode() function returns the calculated hash value encoding. string

exchange.Encode(algo, inputFormat, outputFormat, data) exchange.Encode(algo, inputFormat, outputFormat, data, keyFormat, key)

The parameter algo is the algorithm used for encoding calculation. Supported settings are: “raw” (no algorithm used), “sign”, “signTx”, “md4”, “md5”, “sha256”, “sha512”, “sha1”, “keccak256”, “sha3.224”, “sha3.256”, “sha3.384”, “sha3.512”, “sha3.keccak256”, “sha3.keccak512”, “sha512.384”, “sha512.256”, “sha512.224”, “ripemd160”, “blake2b.256”, “blake2b.512”, “blake2s.128”, “blake2s.256”. The parameter algo also supports: “text.encoder.utf8”, “text.decoder.utf8”, “text.encoder.gbk”, “text.decoder.gbk”, for encoding and decoding strings. The parameter algo also supports: “ed25519” algorithm. It supports the use of different hash algorithms, for example, the parameter algo can be written as “ed25519.md5”, “ed25519.sha512”, etc. Supports ed25519.seed calculation. algo true string Used to specify the data format of the data parameter. The inputFormat parameter can be set to one of: “raw”, “hex”, “base64”, and “string”. “raw” means the data is raw data, “hex” means the data is hex encoded, “base64” means the data is base64 encoded, and “string” means the data is a string. inputFormat true string Used to specify the output data format. The outputFormat parameter supports the following settings: “raw”, “hex”, “base64”, “string”. “raw” means the data is raw data, “hex” means the data is hex encoded, “base64” means the data is base64 encoded, and “string” means the data is a string. outputFormat true string The parameter data is the data to be processed. data true string Used to specify the data format of the key parameter. The key parameter can be set to one of: “raw”, “hex”, “base64”, and “string”. “raw” means the data is raw data, “hex” means the data is hex encoded, “base64” means the data is base64 encoded, and “string” means the data is a string. keyFormat false string The key parameter is used to specify the key used in the signature calculation, and it can be used as a plaintext string. You can also use "{{accesskey}}", "{{secretkey}}" to refer to the accessKey and secretKey configured in the {@var/EXCHANGE exchange} exchange object. key false string

function main() {
    var APIKEY = "your Access Key(Bitmex API ID)"
    var expires = parseInt(Date.now() / 1000) + 10
    var signature = exchange.Encode("sha256", "string", "hex", "GET/realtime" + expires, "hex", "{{secretkey}}")
    var client = Dial("wss://www.bitmex.com/realtime", 60)
    var auth = JSON.stringify({args: [APIKEY, expires, signature], op: "authKeyExpires"})
    var pos = 0
    client.write(auth)
    client.write('{"op": "subscribe", "args": "position"}')
    while (true) {
        bitmexData = client.read()
        if(bitmexData.table == 'position' && pos != parseInt(bitmexData.data[0].currentQty)){
            Log('position change', pos, parseInt(bitmexData.data[0].currentQty), '@')
            pos = parseInt(bitmexData.data[0].currentQty)
        }
    }
}
import time
def main():
    APIKEY = "your Access Key(Bitmex API ID)"
    expires = int(time.time() + 10)
    signature = exchange.Encode("sha256", "string", "hex", "GET/realtime" + expires, "hex", "{{secretkey}}")
    client = Dial("wss://www.bitmex.com/realtime", 60)
    auth = json.dumps({"args": [APIKEY, expires, signature], "op": "authKeyExpires"})
    pos = 0
    client.write(auth)
    client.write('{"op": "subscribe", "args": "position"}')
    while True:
        bitmexData = json.loads(client.read())
        if "table" in bitmexData and bitmexData["table"] == "position" and len(bitmexData["data"]) != 0 and pos != bitmexData["data"][0]["currentQty"]:   
            Log("position change", pos, bitmexData["data"][0]["currentQty"], "@")
            pos = bitmexData["data"][0]["currentQty"]
void main() {
    auto APIKEY = "your Access Key(Bitmex API ID)";
    auto expires = Unix() + 10;
    auto signature = exchange.Encode("sha256", "string", "hex", format("GET/realtime%d", expires), "hex", "{{secretkey}}");
    
    auto client = Dial("wss://www.bitmex.com/realtime", 60);
    json auth = R"({"args": [], "op": "authKeyExpires"})"_json;            

    auth["args"].push_back(APIKEY);
    auth["args"].push_back(expires);
    auth["args"].push_back(signature);
    auto pos = 0;
    client.write(auth.dump());
    client.write("{\"op\": \"subscribe\", \"args\": \"position\"}");
    while(true) {
        auto bitmexData = json::parse(client.read());
        if(bitmexData["table"] == "position" && bitmexData["data"][0].find("currentQty") != bitmexData["data"][0].end() && pos != bitmexData["data"][0]["currentQty"]) {
            Log("test");
            Log("position change", pos, bitmexData["data"][0]["currentQty"], "@");
            pos = bitmexData["data"][0]["currentQty"];
        }
    }
}

Example of BitMEX position change push (wss protocol):

Only the real trading supports calling the exchange.Encode() function. The "{{accesskey}}", "{{secretkey}}" references are only valid when the exchange.Encode() function is used.

{@var/EXCHANGE exchange}, {@fun/Global/Encode Encode}

exchange.Log exchange.Go