码迷,mamicode.com
首页 > Windows程序 > 详细

Deribit交易所 websocket API 连接范例

时间:2019-06-06 12:27:39      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:com   while   地址   ror   param   申请   _id   运行   token   

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()
}

  

Deribit交易所 websocket API 连接范例

标签:com   while   地址   ror   param   申请   _id   运行   token   

原文地址:https://www.cnblogs.com/botvsing/p/10984090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!