0bps

Strategy vault / Other

OKEx V5 WS Interface Account Position Push Example

OKEX V5 WSInterface private interface usage examples, policy parameters , , . Among them, is the secret key password filled in when creating APIKEY on the exchange website. The strategy example first logs in to…

JavaScriptOther
Author
发明者量化-小小梦
Complexity
Starter · 54 lines
Parameters
3

OKEX V5 WSExample of account position push via interface

OKEX V5 WSInterface private interface usage examples, policy parameters accessKey, secretKey, passphrase. Among them, passphrase is the secret key password filled in when creating APIKEY on the exchange website. The strategy example first logs in to verify, and then subscribes to all position information. Send ping regularly and the exchange server will respondpong.

Parameters

NameDefaultWhat it controls
accessKey$$$__enc__$$$accessKey
secretKey$$$__enc__$$$secretKey
passphrase$$$__enc__$$$passphrase

Source JavaScript

function getLogin(pAccessKey, pSecretKey, pPassphrase) {
    // Signature function, used for login
    var ts = (new Date().getTime() / 1000) + ""
    var login = {
        "op": "login",
        "args":[{
            "apiKey"    : pAccessKey,
            "passphrase" : pPassphrase,
            "timestamp" : ts,
            "sign" : exchange.HMAC("sha256", "base64", ts + "GET" + "/users/self/verify", pSecretKey)
        }]
    }    
    return login
}

var client_private = null 
function main() {
    // BecausereadThe function uses a timeout setting to filter out timeout errors, otherwise there will be redundant error output.
    SetErrorFilter("timeout")
    
    // Subscription information for position channel
    var posSubscribe = {
        "op": "subscribe",
        "args": [{
            "channel": "positions",
            "instType": "ANY"
        }]
    }

    var payload = JSON.stringify(getLogin(accessKey, secretKey, passphrase))
    client_private = Dial("wss://ws.okex.com:8443/ws/v5/private|reconnect=true&payload=" + payload)
    Sleep(3000)  // When logging in, you cannot immediately subscribe to private channels and must wait for the server's response
    if (client_private) {        
        client_private.write(JSON.stringify(posSubscribe))
        var lastPingTS = new Date().getTime()
        while (true) {
            var buf = client_private.read(2000)
            if (buf) {
                Log(buf)    
            }
            // Send heartbeat packet
            var nowPingTS = new Date().getTime()
            if (nowPingTS - lastPingTS > 10 * 1000) {
                client_private.write("ping")
                lastPingTS = nowPingTS
            }            
        }        
    }
}

function onexit() {    
    var ret = client_private.close()
    Log("Close connection!", ret)
}

Published by 发明者量化-小小梦 on the FMZ strategy square · last modified 2021-09-03. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/OKEx-V5-WS-Interface-Account-Position-Push-Example.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open