• Deribit交易所 websocket API 连接范例


    Deribit websocket API 连接范例,使用JavaScript语言,策略运行在FMZ发明者量化平台。

    源码地址:https://www.fmz.com/strategy/147765

    var client = null
    var deribitAcc = {}                               // 可以设置个全局对象保存 token 
    
    function WS_GetAccount() {                        // 获取账户 某个币种 的资产信息
        var msg = {
            "jsonrpc": "2.0",
            "id": 2515,
            "method": "private/get_account_summary",
            "params": {
                "currency": "ETH",
                "extended": true
            }
        }
    
        client.write(JSON.stringify(msg))
        var ret = client.read()
        Log(ret, "#FF0000")
    }
    
    function WS_GetToken() {                          // 认证 ,并且获取 token 
        var msg = {
            "jsonrpc": "2.0",
            "id": 9929,
            "method": "public/auth",
            "params": {
                "grant_type": "client_credentials",
                "client_id": "XXXXXXX",                             // 申请 API KEY 时获取
                "client_secret": "XXXXXXXXXXXXXXXXXXXXXXXXXX"       // 申请 API KEY 时获取
            }
        }
        while (1) {
            client.write(JSON.stringify(msg))
            var ret = client.read()
            try {
                var jsonObj = JSON.parse(ret)
                if (jsonObj) {
                    deribitAcc.accessToken = jsonObj.result.access_token
                    deribitAcc.refToken = jsonObj.result.refresh_token
                    break
                }
            } catch (e) {
                Log("error:", e)
            }
        }
        Log("更新 deribitAcc accessToken , refToken:", deribitAcc)
    }
    
    function WS_Depth() {                                     // 访问 get_order_book 公共 频道,获取 订单薄深度数据
        var msg = {
            "jsonrpc": "2.0",
            "id": 8772,
            "method": "public/get_order_book",
            "params": {
                "instrument_name": "BTC-PERPETUAL",           // 指定,获取 BTC 永续合约的深度数据
                "depth": 5
            }
        }
    
        client.write(JSON.stringify(msg))
        var ret = client.read()
        Log("depth : ", ret)
    }
    
    function main() {
        client = Dial("wss://www.deribit.com/ws/api/v2")
        WS_GetToken()
    
        WS_GetAccount()
        WS_Depth()
    
    }
    
    function onexit() {
        Log("关闭 ws 连接")
        client.close()
    }
    

      

  • 相关阅读:
    html5的离线缓存
    html5的本地存储
    html5的地理位置定位
    html5新添加的表单类型和属性
    html5的鼠标拖拽
    win下svn常用操作笔记
    git常用命令笔记
    centos7下NFS使用与配置
    centos7下mysql5.6的主从复制
    centos7下创建mysql5.6多实例
  • 原文地址:https://www.cnblogs.com/botvsing/p/10984090.html
Copyright © 2020-2023  润新知